tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Arieh Markel <Arieh.Mar...@Central.Sun.COM>
Subject [proposed PATCH] DefaultServlet.java - lookup for localized version
Date Tue, 20 Jun 2000 22:53:10 GMT
I am attaching the proposed patch for the DefaultServlet as specified
in the discussion that took place this past week.

The gist of it is that the decision for which file to serve is as follows:

	assume:
	
		reqCtry = request.getLocale().getCountry()
		reqLang = request.getLocale().getLanguage()

		defCtry = Locale.getDefault().getCountry()
		defLang = Locale.getDefault().getLanguage()
		
		
	a. try to serve from
	
	   <docBase>/<reqLang>_<reqCtry>/path
	   
	b. try serve from
	
	   <docBase>/<reqLang>/path
	   
	c. try to serve from
	
	   <docBase>/<defLang>_<defCtry>/path
	   
	d. try serve from
	
	   <docBase>/<defLang>/path
	   
	e. try serve from
	
	   <docBase>/path
	   


Follow the context diffs for the 3.1 code and for the 3.2 code:

3.1 code:

===================================================================
RCS file: RCS/DefaultServlet.java,v
retrieving revision 1.1
diff -u -r1.1 DefaultServlet.java
--- DefaultServlet.java	2000/06/15 19:44:56	1.1
+++ DefaultServlet.java	2000/06/20 22:42:04
@@ -130,12 +130,10 @@
 	    requestURI = request.getRequestURI();
 	}
 
-	// Clean up pathInfo
-	File file = null;
-	if ((pathInfo == null) || (pathInfo.length() < 1))
-	    file = new File(docBase);
-	else		// Avoid double slashes because docBase ends with one
-	    file = new File(docBase + pathInfo.substring(1));
+	// Get the file matching the Locale
+	//
+	File file = getLocalizedFile (docBase, pathInfo, request.getLocale());
+
 	String absPath = file.getAbsolutePath();
 
 	if( debug > 0 ) contextF.log( "DefaultServlet: "  + absPath);
@@ -221,6 +219,69 @@
                     "File Not Found<br>" + requestURI);
 	    }
 	}
+    }
+
+    static Locale defLoc = Locale.getDefault ();
+    static String dLang = defLoc.getLanguage ();
+    static String dCoun = defLoc.getCountry ();
+
+    private static File checkFile (String base, String info)
+    {
+	File file = null;
+
+	if ((info == null) || (info.length() < 1))
+	    file = new File(base);
+	else		// Avoid double slashes because docBase ends with one
+	    file = new File(base + info.substring(1));
+
+	return file;
+    }
+
+    private static File getLocalizedFile (String dBase, String pInfo,
+					  Locale loc)
+    {
+	String rLang = loc.getLanguage();
+	String rCoun = loc.getCountry();
+
+	File file = null;
+
+	//  first, try with the provided language+country
+	//
+	String base = null;
+
+	if (null != rCoun && ! ("".equals (rCoun)) )
+	{
+	    base = dBase + rLang + "_" + rCoun + "/";
+	    if ((file = checkFile(base, pInfo)).exists())
+		return file;
+	}
+
+	//  now, try with the provided language only
+	base = dBase + rLang + "/";
+
+	if ((file = checkFile(base, pInfo)).exists())
+	    return file;
+
+	//  now, try with the default language+country
+	//
+	if (null != dCoun && ! ("".equals (dCoun)) )
+	{
+	    base = dBase + dLang + "_" + dCoun + "/";
+
+	    if ((file = checkFile(base, pInfo)).exists())
+		return file;
+	}
+
+	//  now, try with the default language
+	//
+	base = dBase + dLang + "/";
+
+	if ((file = checkFile(base, pInfo)).exists())
+	    return file;
+
+	//  now, try with nothing
+
+	return checkFile(dBase, pInfo);
     }
 
     private String getWelcomeFile(File file) {




3.2 Code:

===================================================================
RCS file: RCS/DefaultServlet.java,v
retrieving revision 1.1
diff -u -r1.1 DefaultServlet.java
--- DefaultServlet.java	2000/06/20 22:40:23	1.1
+++ DefaultServlet.java	2000/06/20 22:45:39
@@ -130,12 +130,10 @@
 	    requestURI = request.getRequestURI();
 	}
 
-	// Clean up pathInfo
-	File file = null;
-	if (pathInfo == null) // pathInfo can't be "" ( spec )
-	    file = new File(docBase);
-	else		// Avoid double slashes because docBase ends with one
-	    file = new File(docBase + pathInfo.substring(1));
+        // Get the file matching the Locale
+	//
+	File file = getLocalizedFile (docBase, pathInfo, request.getLocale());
+
 	String absPath = file.getAbsolutePath();
 
 	if( debug > 0 ) contextF.log( "DefaultServlet: "  + absPath);
@@ -221,6 +219,69 @@
                     "File Not Found<br>" + requestURI);
 	    }
 	}
+    }
+
+    static Locale defLoc = Locale.getDefault ();
+    static String dLang = defLoc.getLanguage ();
+    static String dCoun = defLoc.getCountry ();
+
+    private static File checkFile (String base, String info)
+    {
+	File file = null;
+
+	if ((info == null) || (info.length() < 1))
+	    file = new File(base);
+	else		// Avoid double slashes because docBase ends with one
+	    file = new File(base + info.substring(1));
+
+	return file;
+    }
+
+    private static File getLocalizedFile (String dBase, String pInfo,
+					  Locale loc)
+    {
+	String rLang = loc.getLanguage();
+	String rCoun = loc.getCountry();
+
+	File file = null;
+
+	//  first, try with the provided language+country
+	//
+	String base = null;
+
+	if (null != rCoun && ! ("".equals (rCoun)) )
+	{
+	    base = dBase + rLang + "_" + rCoun + "/";
+	    if ((file = checkFile(base, pInfo)).exists())
+		return file;
+	}
+
+	//  now, try with the provided language only
+	base = dBase + rLang + "/";
+
+	if ((file = checkFile(base, pInfo)).exists())
+	    return file;
+
+	//  now, try with the default language+country
+	//
+	if (null != dCoun && ! ("".equals (dCoun)) )
+	{
+	    base = dBase + dLang + "_" + dCoun + "/";
+
+	    if ((file = checkFile(base, pInfo)).exists())
+		return file;
+	}
+
+	//  now, try with the default language
+	//
+	base = dBase + dLang + "/";
+
+	if ((file = checkFile(base, pInfo)).exists())
+	    return file;
+
+	//  now, try with nothing
+
+	return checkFile(dBase, pInfo);
     }
 
     private String getWelcomeFile(File file) {

	   	  	
--
 Arieh Markel		                Sun Microsystems Inc.
 Network Storage                        500 Eldorado Blvd. MS UBRM11-194
 e-mail: arieh.markel@sun.COM           Broomfield, CO 80021
 Let's go Panthers !!!!                 Phone: (303) 272-8547 x78547
 (e-mail me with subject SEND PUBLIC KEY to get public key)


Mime
View raw message