You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by sr...@apache.org on 2009/10/06 23:15:55 UTC

svn commit: r822505 - in /felix/trunk/http/base/src: main/java/org/apache/felix/http/base/internal/handler/ test/java/org/apache/felix/http/base/internal/handler/

Author: srs
Date: Tue Oct  6 21:15:52 2009
New Revision: 822505

URL: http://svn.apache.org/viewvc?rev=822505&view=rev
Log:
FELIX-1713: Fixed getPathInfo() bug

Added:
    felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/handler/ServletHandlerRequest.java
    felix/trunk/http/base/src/test/java/org/apache/felix/http/base/internal/handler/ServletHandlerRequestTest.java
Modified:
    felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/handler/ServletHandler.java

Modified: felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/handler/ServletHandler.java
URL: http://svn.apache.org/viewvc/felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/handler/ServletHandler.java?rev=822505&r1=822504&r2=822505&view=diff
==============================================================================
--- felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/handler/ServletHandler.java (original)
+++ felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/handler/ServletHandler.java Tue Oct  6 21:15:52 2009
@@ -21,7 +21,6 @@
 import javax.servlet.ServletConfig;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
-import javax.servlet.http.HttpServletRequestWrapper;
 import org.apache.felix.http.base.internal.context.ExtServletContext;
 import java.io.IOException;
 
@@ -89,7 +88,7 @@
                 res.sendError(HttpServletResponse.SC_FORBIDDEN);
             }
         } else {
-            this.servlet.service(new RequestWrapper(req), res);
+            this.servlet.service(new ServletHandlerRequest(req, this.alias), res);
         }
     }
 
@@ -97,47 +96,4 @@
     {
         return other.alias.length() - this.alias.length();
     }    
-
-    private final class RequestWrapper
-        extends HttpServletRequestWrapper
-    {
-        private String pathInfo;
-        private boolean pathInfoComputed = false;
-
-        public RequestWrapper(HttpServletRequest req)
-        {
-            super(req);
-        }
-
-        @Override
-        public String getPathInfo()
-        {
-            if (!this.pathInfoComputed) {
-                final int servletPathLength = getServletPath().length();
-                this.pathInfo = getRequestURI().substring(getContextPath().length()).replaceAll("[/]{2,}", "/")
-                    .substring(servletPathLength);
-
-                if ("".equals(this.pathInfo) && servletPathLength != 0) {
-                    this.pathInfo = null;
-                }
-
-                this.pathInfoComputed = true;
-            }
-
-            return this.pathInfo;
-        }
-
-        @Override
-        public String getPathTranslated()
-        {
-            final String info = getPathInfo();
-            return (null == info) ? null : getRealPath(info);
-        }
-
-        @Override
-        public String getServletPath()
-        {
-            return alias;
-        }
-    }
 }

Added: felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/handler/ServletHandlerRequest.java
URL: http://svn.apache.org/viewvc/felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/handler/ServletHandlerRequest.java?rev=822505&view=auto
==============================================================================
--- felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/handler/ServletHandlerRequest.java (added)
+++ felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/handler/ServletHandlerRequest.java Tue Oct  6 21:15:52 2009
@@ -0,0 +1,84 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.felix.http.base.internal.handler;
+
+import javax.servlet.http.HttpServletRequestWrapper;
+import javax.servlet.http.HttpServletRequest;
+
+final class ServletHandlerRequest
+    extends HttpServletRequestWrapper
+{
+    private final String alias;
+    private String pathInfo;
+    private boolean pathInfoCalculated = false;
+
+    public ServletHandlerRequest(HttpServletRequest req, String alias)
+    {
+        super(req);
+        this.alias = alias;
+    }
+
+    @Override
+    public String getPathInfo()
+    {
+        if (!this.pathInfoCalculated) {
+            this.pathInfo = calculatePathInfo();
+            this.pathInfoCalculated = true;
+        }
+        
+        return this.pathInfo;
+    }
+
+    @Override
+    public String getPathTranslated()
+    {
+        final String info = getPathInfo();
+        return (null == info) ? null : getRealPath(info);
+    }
+
+    @Override
+    public String getServletPath()
+    {
+        return this.alias;
+    }
+
+    private String calculatePathInfo()
+    {
+        final int servletPathLength = getServletPath().length();
+        final String contextPath = getContextPath();
+        
+        String pathInfo = getRequestURI();
+        pathInfo = pathInfo.substring(contextPath.length());
+        pathInfo = pathInfo.replaceAll("[/]{2,}", "/");
+        pathInfo = pathInfo.substring(servletPathLength);
+
+        int qmarkPos = pathInfo.indexOf('?');
+        if (qmarkPos > 0) {
+            pathInfo = pathInfo.substring(0, qmarkPos);
+        }
+
+        if ("".equals(pathInfo) && servletPathLength != 0) {
+            pathInfo = null;
+        }
+
+        if (pathInfo != null && !pathInfo.startsWith("/")) {
+            pathInfo = "/" + pathInfo;
+        }
+
+        return pathInfo;
+    }
+}

Added: felix/trunk/http/base/src/test/java/org/apache/felix/http/base/internal/handler/ServletHandlerRequestTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/http/base/src/test/java/org/apache/felix/http/base/internal/handler/ServletHandlerRequestTest.java?rev=822505&view=auto
==============================================================================
--- felix/trunk/http/base/src/test/java/org/apache/felix/http/base/internal/handler/ServletHandlerRequestTest.java (added)
+++ felix/trunk/http/base/src/test/java/org/apache/felix/http/base/internal/handler/ServletHandlerRequestTest.java Tue Oct  6 21:15:52 2009
@@ -0,0 +1,50 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.felix.http.base.internal.handler;
+
+import org.mockito.Mockito;
+import org.junit.Test;
+import org.junit.Assert;
+import org.junit.Before;
+import javax.servlet.http.HttpServletRequest;
+
+public class ServletHandlerRequestTest
+{
+    private HttpServletRequest req1;
+    private HttpServletRequest req2;
+
+    @Before
+    public void setUp()
+    {
+        HttpServletRequest superReq = Mockito.mock(HttpServletRequest.class);
+        Mockito.when(superReq.getContextPath()).thenReturn("/mycontext");
+        Mockito.when(superReq.getRequestURI()).thenReturn("/mycontext/request/to/resource");
+        this.req1 = new ServletHandlerRequest(superReq, "/");
+
+        superReq = Mockito.mock(HttpServletRequest.class);
+        Mockito.when(superReq.getContextPath()).thenReturn("/mycontext");
+        Mockito.when(superReq.getRequestURI()).thenReturn("/mycontext/myservlet/request/to/resource?param=value");
+        this.req2 = new ServletHandlerRequest(superReq, "/myservlet");
+    }
+
+    @Test
+    public void testPathInfo()
+    {
+        Assert.assertEquals("/request/to/resource", this.req1.getPathInfo());
+        Assert.assertEquals("/request/to/resource", this.req2.getPathInfo());
+    }
+}