Author: scottbw
Date: Mon Jun 13 12:41:45 2011
New Revision: 1135109

URL: http://svn.apache.org/viewvc?rev=1135109&view=rev
Log:
Implemented two changes to the folder localization algorithm: (1) when 
receiving a request for a widget start page (including id key) don't skip the 
localization algorithm as the start file may be incorrectly localized, and (2) 
check for defaultLocale before checking non-localized resources, as per the W3C 
Widgets P&C spec

Modified:
    
incubator/wookie/trunk/src/org/apache/wookie/server/LocalizedResourceFilter.java

Modified: 
incubator/wookie/trunk/src/org/apache/wookie/server/LocalizedResourceFilter.java
URL: 
http://svn.apache.org/viewvc/incubator/wookie/trunk/src/org/apache/wookie/server/LocalizedResourceFilter.java?rev=1135109&r1=1135108&r2=1135109&view=diff
==============================================================================
--- 
incubator/wookie/trunk/src/org/apache/wookie/server/LocalizedResourceFilter.java
 (original)
+++ 
incubator/wookie/trunk/src/org/apache/wookie/server/LocalizedResourceFilter.java
 Mon Jun 13 12:41:45 2011
@@ -57,53 +57,49 @@ public class LocalizedResourceFilter imp
        public void doFilter(ServletRequest request, ServletResponse response,
                        FilterChain chain) throws IOException, ServletException 
{
 
-               // If I have an instance key in the query, set it in the session
-               // This will only happen if the resource is the Start File for 
the Widget
-               String idkey = 
((HttpServletRequest)request).getParameter("idkey");
-               // We use the "localized" querystring parameter to indicate a 
resource
-               // has already been processed by this algorithm once, to 
prevent hunting and
-               // infinite recursion
-               String localized = 
((HttpServletRequest)request).getParameter("localized");
-               
-               if (idkey!=null){
-                       filterConfig.getServletContext().setAttribute("id_key", 
idkey);
-               } else {
-                       // Skip if already localized
-                       if (localized == null){
-                               // Find the instance key in the current session
-                               String key = 
(String)filterConfig.getServletContext().getAttribute("id_key");
-                               if (key != null){
-                                   IPersistenceManager persistenceManager = 
PersistenceManagerFactory.getPersistenceManager();
-                                       IWidgetInstance instance = 
persistenceManager.findWidgetInstanceByIdKey(key);
-                                       if (instance != null){
-                                               // Only if we have a valid 
instance and a resource which has no localization
-                                               // parameter do we start the 
locale algorithm
-
-                                               // Get the original request URL
-                                               String uri = 
((HttpServletRequest)request).getRequestURL().toString();
-                                               URL url = new URL(uri);
-                                               String path = url.getPath();
-
-                                               // Process the resource with 
the localization algorithm
-                                               String localizedPath = 
getLocalizedResource(path, instance);
-
-                                               // Redirect to localized 
resource URL only if different from the original resource URL
-                                               if 
(!path.equals(localizedPath)){
-                                                       uri = uri.replace(path, 
localizedPath);
-                                                       if (uri.contains("?")){
-                                                               uri += 
"&localized=1";
-                                                       } else {
-                                                               uri += 
"?localized=1";
-                                                       }
-                                                       URL newUrl = new 
URL(uri);
-                                                       
((HttpServletResponse)response).sendRedirect(newUrl.toString());
-                                                       return;                 
                                
-                                               }
-                                       }
-                               }
-                       }
-               }
-               chain.doFilter(request, response); 
+         // If I have an instance key in the query, set it in the session
+         // This will only happen if the resource is the Start File for the 
Widget
+         String idkey = ((HttpServletRequest)request).getParameter("idkey");
+         // We use the "localized" querystring parameter to indicate a resource
+         // has already been processed by this algorithm once, to prevent 
hunting and
+         // infinite recursion
+         String localized = 
((HttpServletRequest)request).getParameter("localized");
+         if (idkey!=null) 
filterConfig.getServletContext().setAttribute("id_key", idkey);
+         // Skip if already localized
+         if (localized == null){
+           // Find the instance key in the current session
+           String key = 
(String)filterConfig.getServletContext().getAttribute("id_key");
+           if (key != null){
+             IPersistenceManager persistenceManager = 
PersistenceManagerFactory.getPersistenceManager();
+             IWidgetInstance instance = 
persistenceManager.findWidgetInstanceByIdKey(key);
+             if (instance != null){
+               // Only if we have a valid instance and a resource which has no 
localization
+               // parameter do we start the locale algorithm
+
+               // Get the original request URL
+               String uri = 
((HttpServletRequest)request).getRequestURL().toString();
+               URL url = new URL(uri);
+               String path = url.getPath();
+
+               // Process the resource with the localization algorithm
+               String localizedPath = getLocalizedResource(path, instance);
+
+               // Redirect to localized resource URL only if different from 
the original resource URL
+               if (!path.equals(localizedPath)){
+                 uri = uri.replace(path, localizedPath);
+                 if (uri.contains("?")){
+                   uri += "&localized=1";
+                 } else {
+                   uri += "?localized=1";
+                 }
+                 URL newUrl = new URL(uri);
+                 
((HttpServletResponse)response).sendRedirect(newUrl.toString());
+                 return;                                                       
+               }
+             }
+           }
+         }
+         chain.doFilter(request, response); 
        }
 
        public void init(FilterConfig filterConfig) throws ServletException {
@@ -167,15 +163,15 @@ public class LocalizedResourceFilter imp
                        if (new File(filePath).exists()) return context+path;
                }
 
+        // As the next resort, we'll try defautLocale
+        if (instance.getWidget().getDefaultLocale() != null){
+          String path = basePath.replace(resource, 
"locales/"+instance.getWidget().getDefaultLocale().toLowerCase()+"/"+resource);
+          String filePath = 
filterConfig.getServletContext().getRealPath(path);   
+          if (new File(filePath).exists()) return context+path;
+        }
+    
                // All attempts to locate a localized copy have failed, so we 
must try to find a non-localized version instead
                if (new 
File(filterConfig.getServletContext().getRealPath(basePath)).exists()) return 
context+basePath;
-               
-               // As a last resort, we'll try defautLocale
-               if (instance.getWidget().getDefaultLocale() != null){
-                 String path = basePath.replace(resource, 
"locales/"+instance.getWidget().getDefaultLocale().toLowerCase()+"/"+resource);
-                 String filePath = 
filterConfig.getServletContext().getRealPath(path);         
-      if (new File(filePath).exists()) return context+path;
-               }
 
                // No localized or even non-localized file exists, so just 
return the original. This situation shouldn't arise except
                // where, e.g., the original request was for a non-existing 
resource


Reply via email to