1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.localization.impl;
18
19 import java.util.Enumeration;
20 import java.util.Locale;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.apache.jetspeed.PortalReservedParameters;
25 import org.apache.jetspeed.i18n.CurrentLocale;
26 import org.apache.jetspeed.pipeline.PipelineException;
27 import org.apache.jetspeed.pipeline.valve.AbstractValve;
28 import org.apache.jetspeed.pipeline.valve.LocalizationValve;
29 import org.apache.jetspeed.pipeline.valve.ValveContext;
30 import org.apache.jetspeed.request.RequestContext;
31
32 /***
33 * LocalizationValveImpl
34 *
35 * @author <a href="mailto:taylor@apache.org">David Sean Taylor </a>
36 * @version $Id: LocalizationValveImpl.java 378091 2006-02-15 21:12:28Z taylor $
37 */
38 public class SimplifiedLocalizationValveImpl extends AbstractValve implements LocalizationValve
39 {
40 private static final Log log = LogFactory.getLog(LocalizationValveImpl.class);
41 private Locale defaultLocale = null;
42
43 public SimplifiedLocalizationValveImpl() {}
44
45 public SimplifiedLocalizationValveImpl(String defaultLanguage)
46 {
47 String language = defaultLanguage != null ? defaultLanguage.trim() : "";
48 if (language.length()>0)
49 {
50
51 String country = "";
52 String variant = "";
53 int countryIndex = language.indexOf('_');
54 if (countryIndex > -1)
55 {
56 country = language.substring(countryIndex + 1).trim();
57 language = language.substring(0, countryIndex).trim();
58 int vDash = country.indexOf("_");
59 if (vDash > 0)
60 {
61 String cTemp = country.substring(0, vDash);
62 variant = country.substring(vDash + 1);
63 country = cTemp;
64 }
65 }
66
67 defaultLocale = new Locale(language, country, variant);
68 if ( defaultLocale.getLanguage().length() == 0 )
69 {
70
71 defaultLocale = null;
72 log.warn("Invalid or unrecognized default language: "+language);
73 }
74 else
75 {
76 log.info("Default language set: "+defaultLocale);
77 }
78
79 }
80 }
81
82
83
84
85
86
87
88 public void invoke( RequestContext request, ValveContext context ) throws PipelineException
89 {
90 Locale locale = (Locale)request.getRequest().getSession().getAttribute(PortalReservedParameters.PREFERED_LOCALE_ATTRIBUTE);
91
92 if ( locale == null && defaultLocale != null )
93 {
94 locale = defaultLocale;
95 }
96
97 if (locale == null)
98 {
99 locale = request.getRequest().getLocale();
100 }
101
102 if (locale == null)
103 {
104 Enumeration preferedLocales = request.getRequest().getLocales();
105 while (preferedLocales.hasMoreElements() && locale == null)
106 {
107 locale = (Locale) preferedLocales.nextElement();
108 }
109 }
110
111 if (locale == null)
112 {
113 locale = Locale.getDefault();
114 }
115
116 request.setLocale(locale);
117 request.getRequest().setAttribute(PortalReservedParameters.PREFERED_LOCALE_ATTRIBUTE, locale);
118 request.getRequest().getSession().setAttribute(PortalReservedParameters.PREFERED_LOCALE_ATTRIBUTE, locale);
119 CurrentLocale.set(locale);
120
121
122 context.invokeNext(request);
123
124 }
125
126 public String toString()
127 {
128 return "LocalizationValve";
129 }
130
131 }