tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From na...@locus.apache.org
Subject cvs commit: jakarta-tomcat/src/share/org/apache/tomcat/util RequestUtil.java
Date Sun, 09 Jul 2000 02:32:58 GMT
nacho       00/07/08 19:32:58

  Modified:    src/share/org/apache/tomcat/util Tag: tomcat_32
                        RequestUtil.java
  Log:
  BUGFIX:
  
  Hashtable has no guarantees about the order in which elements
  are enumerated, and the Locale processing was reliying on that
  order from jdk 1.3.0 this order is not the same. In fact when the
  browser send  2 language codes in the "Accept-Language"
  Header jdk_1.3.0 inverts the order of the locale.
  
  Revision  Changes    Path
  No                   revision
  
  
  No                   revision
  
  
  1.14.2.1  +60 -59    jakarta-tomcat/src/share/org/apache/tomcat/util/RequestUtil.java
  
  Index: RequestUtil.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/util/RequestUtil.java,v
  retrieving revision 1.14
  retrieving revision 1.14.2.1
  diff -u -r1.14 -r1.14.2.1
  --- RequestUtil.java	2000/06/23 02:16:30	1.14
  +++ RequestUtil.java	2000/07/09 02:32:57	1.14.2.1
  @@ -389,43 +389,46 @@
       }
   
       public static Locale getLocale(Request req) {
  -	String acceptLanguage = req.getHeader("Accept-Language");
  -	if( acceptLanguage == null ) return Locale.getDefault();
  +    	String acceptLanguage = req.getHeader("Accept-Language");
  +	    if( acceptLanguage == null ) return Locale.getDefault();
   
           Hashtable languages = new Hashtable();
  -	processAcceptLanguage(acceptLanguage, languages);
  -	if (languages.size() == 0)
  -	    return Locale.getDefault();
  -	Vector l = new Vector();
  -	extractLocales( languages, l);
  -	return (Locale)l.elementAt(0);
  +        Vector quality=new Vector();
  +        processAcceptLanguage(acceptLanguage, languages,quality);
  +
  +        if (languages.size() == 0) return Locale.getDefault();
  +
  +        Vector l = new Vector();
  +        extractLocales( languages,quality, l);
  +
  +        return (Locale)l.elementAt(0);
       }
  -    
  +
       public static Enumeration getLocales(HttpServletRequest req) {
  -	String acceptLanguage = req.getHeader("Accept-Language");
  -	// Short circuit with an empty enumeration if null header
  +	    String acceptLanguage = req.getHeader("Accept-Language");
  +    	// Short circuit with an empty enumeration if null header
           if (acceptLanguage == null) {
  -            Vector def = new Vector();
  -            def.addElement(Locale.getDefault());
  -            return def.elements();
  +            Vector v = new Vector();
  +            v.addElement(Locale.getDefault());
  +            return v.elements();
           }
   
           Hashtable languages = new Hashtable();
  -	processAcceptLanguage(acceptLanguage, languages );
  +        Vector quality=new Vector();
  +    	processAcceptLanguage(acceptLanguage, languages , quality);
   
           if (languages.size() == 0) {
               Vector v = new Vector();
  -
  -            v.addElement(org.apache.tomcat.core.Constants.LOCALE_DEFAULT);
  -            languages.put("1.0", v);
  +            v.addElement(Locale.getDefault());
  +            return v.elements();
           }
  -	Vector l = new Vector();
  -	extractLocales( languages, l);
  -	return l.elements();
  +    	Vector l = new Vector();
  +    	extractLocales( languages, quality , l);
  +    	return l.elements();
       }
   
       public static void processAcceptLanguage( String acceptLanguage,
  -					      Hashtable languages )
  +					      Hashtable languages, Vector q)
       {
           StringTokenizer languageTokenizer =
               new StringTokenizer(acceptLanguage, ",");
  @@ -438,59 +441,57 @@
               Double qValue = new Double(1);
   
               if (qValueIndex > -1 &&
  -                qValueIndex < qIndex &&
  -                qIndex < equalIndex) {
  -	        String qValueStr = language.substring(qValueIndex + 1);
  -
  +                    qValueIndex < qIndex &&
  +                    qIndex < equalIndex) {
  +    	        String qValueStr = language.substring(qValueIndex + 1);
                   language = language.substring(0, qValueIndex);
                   qValueStr = qValueStr.trim().toLowerCase();
                   qValueIndex = qValueStr.indexOf('=');
                   qValue = new Double(0);
  -
                   if (qValueStr.startsWith("q") &&
                       qValueIndex > -1) {
                       qValueStr = qValueStr.substring(qValueIndex + 1);
  -
                       try {
                           qValue = new Double(qValueStr.trim());
                       } catch (NumberFormatException nfe) {
                       }
                   }
               }
  -	    
  -	    // XXX
  -	    // may need to handle "*" at some point in time
  -
  -	    if (! language.equals("*")) {
  -	        String key = qValue.toString();
  -		Vector v = (Vector)((languages.containsKey(key)) ?
  -		    languages.get(key) : new Vector());
  -
  -		v.addElement(language);
  -		languages.put(key, v);
  -	    }
  +
  +            // XXX
  +            // may need to handle "*" at some point in time
  +
  +            if (! language.equals("*")) {
  +                String key = qValue.toString();
  +                Vector v;
  +                if (languages.containsKey(key)) {
  +                    v = (Vector)languages.get(key) ;
  +                } else {
  +                    v= new Vector();
  +                    q.addElement(qValue);
  +                }
  +                v.addElement(language);
  +                languages.put(key, v);
  +            }
           }
       }
   
  -    public static void extractLocales(Hashtable languages, Vector l)
  +    public static void extractLocales(Hashtable languages, Vector q,Vector l)
       {
  -        Enumeration e = languages.keys();
  -
  +        // XXX We will need to order by q value Vector in the Future ?
  +        Enumeration e = q.elements();
           while (e.hasMoreElements()) {
  -            String key = (String)e.nextElement();
  -            Vector v = (Vector)languages.get(key);
  +            Vector v =
  +                (Vector)languages.get(((Double)e.nextElement()).toString());
               Enumeration le = v.elements();
  -
               while (le.hasMoreElements()) {
  -	        String language = (String)le.nextElement();
  -		String country = "";
  -		int countryIndex = language.indexOf("-");
  -
  -		if (countryIndex > -1) {
  -		    country = language.substring(countryIndex + 1).trim();
  -		    language = language.substring(0, countryIndex).trim();
  -		}
  -
  +    	        String language = (String)le.nextElement();
  +	        	String country = "";
  +        		int countryIndex = language.indexOf("-");
  +                if (countryIndex > -1) {
  +                    country = language.substring(countryIndex + 1).trim();
  +                    language = language.substring(0, countryIndex).trim();
  +                }
                   l.addElement(new Locale(language, country));
               }
           }
  @@ -505,7 +506,7 @@
       // which was created for each HeaderField ).
       // This also avoid passing HttpHeaders - which was required to access
       // HttpHeaderFiled to access HttpDate to access the parsing code.
  -    
  +
       // we force our locale here as all http dates are in english
       private final static Locale loc = Locale.US;
   
  @@ -520,13 +521,13 @@
   
       // format for C asctime() date string -- "Sun Nov  6 08:49:37 1994"
       private final static String asctimePattern ="EEE MMM d HH:mm:ss yyyyy";
  -    
  +
       private final static SimpleDateFormat rfc1123Format =
   	new SimpleDateFormat(rfc1123Pattern, loc);
  -    
  +
       private final static SimpleDateFormat rfc1036Format =
   	new SimpleDateFormat(rfc1036Pattern, loc);
  -    
  +
       private final static SimpleDateFormat asctimeFormat =
   	new SimpleDateFormat(asctimePattern, loc);
   
  
  
  

Mime
View raw message