KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > web > app > servlet > AbstractAuthenticationFilter


1 /*
2  * Copyright (C) 2006 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.web.app.servlet;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Locale JavaDoc;
22 import java.util.StringTokenizer JavaDoc;
23
24 import javax.servlet.http.HttpServletRequest JavaDoc;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28
29 public class AbstractAuthenticationFilter
30 {
31
32     private static Log logger = LogFactory.getLog(AbstractAuthenticationFilter.class);
33     
34     public AbstractAuthenticationFilter()
35     {
36         super();
37     }
38
39
40     /**
41      * Parse the Accept-Lanaguage HTTP header value
42      *
43      * @param req HttpServletRequest
44      * @return Locale
45      */

46     protected static final Locale JavaDoc parseAcceptLanguageHeader(HttpServletRequest JavaDoc req, List JavaDoc<String JavaDoc> m_languages)
47     {
48         // Default the locale
49

50         Locale JavaDoc locale = Locale.getDefault();
51         
52         // Get the accept language header value
53

54         String JavaDoc acceptHeader = req.getHeader("Accept-Language");
55         if ( acceptHeader != null)
56         {
57             // Parse the accepted language list
58

59             StringTokenizer JavaDoc tokens = new StringTokenizer JavaDoc(acceptHeader, ",");
60             List JavaDoc<AcceptLanguage> langList = new ArrayList JavaDoc<AcceptLanguage>();
61             
62             while ( tokens.hasMoreTokens())
63             {
64                 // Get the current language token
65

66                 String JavaDoc lang = tokens.nextToken();
67                 float quality = 1.0f;
68                 
69                 // Check if the optional quality has been specified
70

71                 int qpos = lang.indexOf(";");
72                 if ( qpos != -1)
73                 {
74                     // Parse the quality value
75

76                     try
77                     {
78                         quality = Float.parseFloat(lang.substring(qpos+3));
79                     }
80                     catch (NumberFormatException JavaDoc ex)
81                     {
82                         logger.error("Error parsing Accept-Language value " + lang);
83                     }
84                     
85                     // Strip the quality value from the language token
86

87                     lang = lang.substring(0,qpos);
88                 }
89                 
90                 // Add the language to the list
91

92                 langList.add(new AcceptLanguage(lang, quality));
93             }
94             
95             // Debug
96

97             if ( logger.isDebugEnabled())
98                 logger.debug("Accept-Language list : " + langList);
99             
100             // Match the client languages to the available locales
101

102             if ( langList.size() > 0)
103             {
104                 // Search for the best match locale to use for this client
105

106                 AcceptLanguage useLang = null;
107                 String JavaDoc useName = null;
108                 boolean match = false;
109     
110                 for ( AcceptLanguage curLang : langList)
111                 {
112                     // Match against the available languages
113

114                     for(String JavaDoc availLang : m_languages)
115                     {
116                         // The accept language may be in 'cc' or 'cc_cc' format
117

118                         match = false;
119                         
120                         if ( curLang.getLanguage().length() == 2)
121                         {
122                             if ( availLang.startsWith(curLang.getLanguage()))
123                                 match = true;
124                         }
125                         else if ( availLang.equalsIgnoreCase(curLang.getLanguage()))
126                             match = true;
127                         
128                         // If we found a match check if it is a higher quality than the current match.
129
// If the quality is the same we stick with the existing match as it was nearer the
130
// start of the list.
131

132                         if ( match == true)
133                         {
134                             if ( useLang == null ||
135                                     ( curLang.getQuality() > useLang.getQuality()))
136                             {
137                                 useLang = curLang;
138                                 useName = availLang;
139                             }
140                         }
141                     }
142                 }
143                 
144                 // Debug
145

146                 if ( logger.isDebugEnabled())
147                     logger.debug("Accept-Language using " + (useLang != null ? useLang.toString() : "<none>"));
148                 
149                 // Create the required user locale
150

151                 if ( useLang != null)
152                 {
153                     Locale JavaDoc useLocale = AcceptLanguage.createLocale(useName);
154                     if ( useLocale != null)
155                     {
156                         locale = useLocale;
157                         
158                         // Debug
159

160                         if ( logger.isDebugEnabled())
161                             logger.debug("Using language " + useLang + ", locale " + locale);
162                     }
163                 }
164             }
165         }
166     
167         // Return the selected locale
168

169         return locale;
170     }
171
172 }
173
Popular Tags