1 17 package org.alfresco.web.app.servlet; 18 19 import java.util.ArrayList ; 20 import java.util.List ; 21 import java.util.Locale ; 22 import java.util.StringTokenizer ; 23 24 import javax.servlet.http.HttpServletRequest ; 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 46 protected static final Locale parseAcceptLanguageHeader(HttpServletRequest req, List <String > m_languages) 47 { 48 50 Locale locale = Locale.getDefault(); 51 52 54 String acceptHeader = req.getHeader("Accept-Language"); 55 if ( acceptHeader != null) 56 { 57 59 StringTokenizer tokens = new StringTokenizer (acceptHeader, ","); 60 List <AcceptLanguage> langList = new ArrayList <AcceptLanguage>(); 61 62 while ( tokens.hasMoreTokens()) 63 { 64 66 String lang = tokens.nextToken(); 67 float quality = 1.0f; 68 69 71 int qpos = lang.indexOf(";"); 72 if ( qpos != -1) 73 { 74 76 try 77 { 78 quality = Float.parseFloat(lang.substring(qpos+3)); 79 } 80 catch (NumberFormatException ex) 81 { 82 logger.error("Error parsing Accept-Language value " + lang); 83 } 84 85 87 lang = lang.substring(0,qpos); 88 } 89 90 92 langList.add(new AcceptLanguage(lang, quality)); 93 } 94 95 97 if ( logger.isDebugEnabled()) 98 logger.debug("Accept-Language list : " + langList); 99 100 102 if ( langList.size() > 0) 103 { 104 106 AcceptLanguage useLang = null; 107 String useName = null; 108 boolean match = false; 109 110 for ( AcceptLanguage curLang : langList) 111 { 112 114 for(String availLang : m_languages) 115 { 116 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 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 146 if ( logger.isDebugEnabled()) 147 logger.debug("Accept-Language using " + (useLang != null ? useLang.toString() : "<none>")); 148 149 151 if ( useLang != null) 152 { 153 Locale useLocale = AcceptLanguage.createLocale(useName); 154 if ( useLocale != null) 155 { 156 locale = useLocale; 157 158 160 if ( logger.isDebugEnabled()) 161 logger.debug("Using language " + useLang + ", locale " + locale); 162 } 163 } 164 } 165 } 166 167 169 return locale; 170 } 171 172 } 173 | Popular Tags |