You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by vg...@apache.org on 2007/11/24 04:56:01 UTC

svn commit: r597795 - in /cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon: servlet/RequestProcessor.java servlet/RequestUtil.java sitemap/SitemapServlet.java

Author: vgritsenko
Date: Fri Nov 23 19:56:00 2007
New Revision: 597795

URL: http://svn.apache.org/viewvc?rev=597795&view=rev
Log:
Fix processing of '/mountpath' URI in the sitemap servlet: issue a redirect to '/mountpath/'
since sitemap must always be mounted at path ending with '/'.

Modified:
    cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/servlet/RequestProcessor.java
    cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/servlet/RequestUtil.java
    cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/sitemap/SitemapServlet.java

Modified: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/servlet/RequestProcessor.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/servlet/RequestProcessor.java?rev=597795&r1=597794&r2=597795&view=diff
==============================================================================
--- cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/servlet/RequestProcessor.java (original)
+++ cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/servlet/RequestProcessor.java Fri Nov 23 19:56:00 2007
@@ -169,10 +169,10 @@
         }
 
         try {
-            if (this.process(env)) {
+            if (process(env)) {
                 contentType = env.getContentType();
             } else {
-                // We reach this when there is nothing in the processing change that matches
+                // We reach this when there is nothing in the processing chain that matches
                 // the request. For example, no matcher matches.
                 getLogger().fatal("The Cocoon engine failed to process the request.");
 

Modified: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/servlet/RequestUtil.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/servlet/RequestUtil.java?rev=597795&r1=597794&r2=597795&view=diff
==============================================================================
--- cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/servlet/RequestUtil.java (original)
+++ cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/servlet/RequestUtil.java Fri Nov 23 19:56:00 2007
@@ -40,12 +40,13 @@
     public static String getCompleteUri(HttpServletRequest request,
                                         HttpServletResponse response)
     throws IOException {
-        // We got it... Process the request
+        // Start with servlet path
         String uri = request.getServletPath();
         // uri should never be null, but we check it anyway
         if (uri == null) {
             uri = "";
         }
+
         String pathInfo = request.getPathInfo();
         if (pathInfo != null) {
             // VG: WebLogic fix: Both uri and pathInfo starts with '/'
@@ -60,12 +61,11 @@
         }
 
         if (uri.length() == 0) {
-            /* empty relative URI
-                 -> HTTP-redirect from /cocoon to /cocoon/ to avoid
-                    StringIndexOutOfBoundsException when calling
-                    "".charAt(0)
-               else process URI normally
-            */
+            /*
+             * Empty relative URI. Issue HTTP redirect from '/block' to '/block/' to
+             * avoid breaking relative URIs in the response generated by the sitemap
+             * which expects to be mounted at '.../'.
+             */
             String serverAbsoluteUri = request.getRequestURI();
             if (serverAbsoluteUri == null) {
                 serverAbsoluteUri = "/";
@@ -80,6 +80,53 @@
         if (uri.charAt(0) == '/') {
             uri = uri.substring(1);
         }
+        return uri;
+    }
+
+    /**
+     * Same as {@link #getCompleteUri(HttpServletRequest, HttpServletResponse)} but
+     * disregards {@link HttpServletRequest#getServletPath()}.
+     *
+     * <br>Used by {@link SitemapServlet}.
+     */
+    public static String getCompleteBlockUri(HttpServletRequest request,
+                                             HttpServletResponse response)
+    throws IOException {
+        // The original implementation prepend the servlet context path which doesn't work
+        // in the tree processor if there actually is a servlet context path
+        // VG: ...but still need to know if servlet path had a '/' at the end
+        //     (in case if path info is empty, need to know whether to issue a redirect or not)
+        String servletPath = request.getServletPath();
+        if (servletPath == null) {
+            servletPath = "";
+        }
+
+        String uri = request.getPathInfo();
+        if (uri == null) {
+            uri = "";
+        }
+
+        if (uri.length() == 0 && !servletPath.endsWith("/")) {
+            /*
+             * Empty relative URI. Issue HTTP redirect from '/block' to '/block/' to
+             * avoid breaking relative URIs in the response generated by the sitemap
+             * which expects to be mounted at '.../'.
+             */
+            String serverAbsoluteUri = request.getRequestURI();
+            if (serverAbsoluteUri == null) {
+                serverAbsoluteUri = "/";
+            } else {
+                serverAbsoluteUri += "/";
+            }
+
+            response.sendRedirect(response.encodeRedirectURL(serverAbsoluteUri));
+            return null;
+        }
+
+        if (uri.length() > 0 && uri.charAt(0) == '/') {
+            return uri.substring(1);
+        }
+
         return uri;
     }
 

Modified: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/sitemap/SitemapServlet.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/sitemap/SitemapServlet.java?rev=597795&r1=597794&r2=597795&view=diff
==============================================================================
--- cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/sitemap/SitemapServlet.java (original)
+++ cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/sitemap/SitemapServlet.java Fri Nov 23 19:56:00 2007
@@ -18,7 +18,6 @@
 
 import java.io.IOException;
 import java.net.URL;
-
 import javax.servlet.ServletContext;
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServlet;
@@ -29,11 +28,13 @@
 import org.apache.avalon.framework.configuration.DefaultConfiguration;
 import org.apache.avalon.framework.container.ContainerUtil;
 import org.apache.avalon.framework.service.ServiceManager;
+import org.springframework.beans.factory.BeanCreationException;
+
 import org.apache.cocoon.Processor;
 import org.apache.cocoon.components.LifecycleHelper;
 import org.apache.cocoon.components.treeprocessor.TreeProcessor;
 import org.apache.cocoon.core.container.spring.avalon.AvalonUtils;
-import org.springframework.beans.factory.BeanCreationException;
+import org.apache.cocoon.servlet.RequestUtil;
 
 /**
  * Use this servlet as entry point to Cocoon. It wraps the {@link TreeProcessor}
@@ -58,7 +59,7 @@
      * Process the incoming request using the Cocoon tree processor.
      */
     protected void service(HttpServletRequest request, HttpServletResponse response)
-    	throws ServletException, IOException {
+    throws ServletException, IOException {
 
         this.processor.service(request, response);
     }
@@ -120,22 +121,11 @@
             }
         }
 
-        /* (non-Javadoc)
-         * @see org.apache.cocoon.servlet.RequestProcessor#getURI(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
+        /**
+         * @see org.apache.cocoon.servlet.RequestProcessor#getURI(HttpServletRequest, HttpServletResponse)
          */
-        // The original implementation prepend the servlet context path which doesn't work
-        // in the tree processor if there actually is a servlet context path
-        protected String getURI(HttpServletRequest request, HttpServletResponse res) throws IOException {
-            String uri = request.getPathInfo();
-            if (uri == null) {
-                return "";
-            }
-
-            if (uri.length() > 0 && uri.charAt(0) == '/') {
-                return uri.substring(1);
-            }
-
-            return uri;
+        protected String getURI(HttpServletRequest request, HttpServletResponse response) throws IOException {
+            return RequestUtil.getCompleteBlockUri(request, response);
         }
 
         /**