Author: sergeyb
Date: Tue Mar 15 16:02:03 2011
New Revision: 1081825
URL: http://svn.apache.org/viewvc?rev=1081825&view=rev
Log:
[CXF-3403] Using context and servlet path for the baseURL calculation
Modified:
cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/AbstractServletController.java
cxf/trunk/rt/transports/http/src/test/java/org/apache/cxf/transport/servlet/ServletControllerTest.java
Modified:
cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/AbstractServletController.java
URL:
http://svn.apache.org/viewvc/cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/AbstractServletController.java?rev=1081825&r1=1081824&r2=1081825&view=diff
==============================================================================
---
cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/AbstractServletController.java
(original)
+++
cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/AbstractServletController.java
Tue Mar 15 16:02:03 2011
@@ -33,7 +33,6 @@ import javax.servlet.http.HttpServletRes
import org.apache.cxf.common.logging.LogUtils;
import org.apache.cxf.common.util.StringUtils;
-import org.apache.cxf.common.util.UrlUtils;
import org.apache.cxf.service.model.EndpointInfo;
import org.apache.cxf.transport.http.AbstractHTTPDestination;
import org.apache.cxf.transport.http.DestinationRegistry;
@@ -165,34 +164,19 @@ public abstract class AbstractServletCon
String pathInfo = request.getPathInfo() == null ? "" :
request.getPathInfo();
//fix for CXF-898
if (!"/".equals(pathInfo) || reqPrefix.endsWith("/")) {
- // needs to be done given that pathInfo is decoded
- // TODO : it's unlikely servlet path will contain encoded values
so we're most
- // likely safe however we need to ensure if it happens then this
code works properly too
- reqPrefix = UrlUtils.pathDecode(reqPrefix);
- // pathInfo drops matrix parameters attached to a last path segment
- int offset = 0;
- int index = getMatrixParameterIndex(reqPrefix, pathInfo);
- if (index >= pathInfo.length()) {
- offset = reqPrefix.length() - index;
+ String basePath = request.getContextPath() +
request.getServletPath();
+ int index;
+ if (basePath.length() == 0) {
+ index = reqPrefix.indexOf(request.getRequestURI());
+ } else {
+ index = reqPrefix.indexOf(basePath);
}
- reqPrefix = reqPrefix.substring(0, reqPrefix.length() -
pathInfo.length() - offset);
+ reqPrefix = reqPrefix.substring(0, index + basePath.length());
}
return reqPrefix;
}
- private int getMatrixParameterIndex(String reqPrefix, String pathInfo) {
- int index = reqPrefix.lastIndexOf(';');
- int lastIndex = -1;
- while (index >= pathInfo.length()) {
- lastIndex = index;
- reqPrefix = reqPrefix.substring(0, index);
- if (reqPrefix.endsWith(pathInfo)) {
- break;
- }
- index = reqPrefix.lastIndexOf(';');
- }
- return lastIndex;
- }
+
public void invokeDestination(final HttpServletRequest request,
HttpServletResponse response,
AbstractHTTPDestination d) throws
ServletException {
Modified:
cxf/trunk/rt/transports/http/src/test/java/org/apache/cxf/transport/servlet/ServletControllerTest.java
URL:
http://svn.apache.org/viewvc/cxf/trunk/rt/transports/http/src/test/java/org/apache/cxf/transport/servlet/ServletControllerTest.java?rev=1081825&r1=1081824&r2=1081825&view=diff
==============================================================================
---
cxf/trunk/rt/transports/http/src/test/java/org/apache/cxf/transport/servlet/ServletControllerTest.java
(original)
+++
cxf/trunk/rt/transports/http/src/test/java/org/apache/cxf/transport/servlet/ServletControllerTest.java
Tue Mar 15 16:02:03 2011
@@ -52,8 +52,13 @@ public class ServletControllerTest exten
private void setReq(String pathInfo, String requestUri, String styleSheet,
String formatted) {
req.getPathInfo();
EasyMock.expectLastCall().andReturn(pathInfo).anyTimes();
+ req.getContextPath();
+ EasyMock.expectLastCall().andReturn("");
+ req.getServletPath();
+ EasyMock.expectLastCall().andReturn("");
+
req.getRequestURI();
- EasyMock.expectLastCall().andReturn(requestUri);
+ EasyMock.expectLastCall().andReturn(requestUri).times(2);
req.getParameter("stylesheet");
EasyMock.expectLastCall().andReturn(styleSheet);
req.getParameter("formatted");
@@ -89,8 +94,13 @@ public class ServletControllerTest exten
public void testGenerateUnformattedServiceListing() throws Exception {
req.getPathInfo();
EasyMock.expectLastCall().andReturn(null).anyTimes();
+ req.getContextPath();
+ EasyMock.expectLastCall().andReturn("");
+ req.getServletPath();
+ EasyMock.expectLastCall().andReturn("");
req.getRequestURI();
- EasyMock.expectLastCall().andReturn("/services");
+ EasyMock.expectLastCall().andReturn("/services").times(2);
+
req.getParameter("stylesheet");
EasyMock.expectLastCall().andReturn(null);
req.getParameter("formatted");
@@ -141,52 +151,85 @@ public class ServletControllerTest exten
assertFalse(sc.invokeDestinationCalled());
}
- private String testGetRequestUrl(String requestUrl, String pathInfo) {
+ private String testGetBaseURL(String requestUrl, String contextPath,
+ String servletPath, String pathInfo) {
req.getRequestURL();
- EasyMock.expectLastCall().andReturn(
- new StringBuffer(requestUrl)).times(2);
+ EasyMock.expectLastCall().andReturn(new StringBuffer(requestUrl));
+
+ req.getContextPath();
+ EasyMock.expectLastCall().andReturn(contextPath);
+ req.getServletPath();
+ EasyMock.expectLastCall().andReturn(servletPath);
+
req.getPathInfo();
- EasyMock.expectLastCall().andReturn(pathInfo).anyTimes();
+ EasyMock.expectLastCall().andReturn(pathInfo).times(2);
+
+ String basePath = contextPath + servletPath;
+ if (basePath.length() == 0) {
+ req.getRequestURI();
+ EasyMock.expectLastCall().andReturn(pathInfo);
+ }
+
EasyMock.replay(req);
return new ServletController(null, null, null).getBaseURL(req);
}
@Test
public void testGetRequestURL() throws Exception {
- String url = testGetRequestUrl("http://localhost:8080/services/bar",
"/bar");
+ String url = testGetBaseURL("http://localhost:8080/services/bar",
+ "", "/services", "/bar");
assertEquals("http://localhost:8080/services", url);
}
@Test
+ public void testGetRequestURL2() throws Exception {
+ String url = testGetBaseURL("http://localhost:8080/services/bar",
+ "/services", "", "/bar");
+ assertEquals("http://localhost:8080/services", url);
+ }
+
+ @Test
+ public void testGetRequestURL3() throws Exception {
+ String url = testGetBaseURL("http://localhost:8080/services/bar",
+ "", "", "/services/bar");
+ assertEquals("http://localhost:8080", url);
+ }
+
+ @Test
public void testGetRequestURLSingleMatrixParam() throws Exception {
- String url =
testGetRequestUrl("http://localhost:8080/services/bar;a=b", "/bar");
+ String url = testGetBaseURL("http://localhost:8080/services/bar;a=b",
+ "", "/services", "/bar");
assertEquals("http://localhost:8080/services", url);
}
@Test
public void testGetRequestURLMultipleMatrixParam() throws Exception {
- String url =
testGetRequestUrl("http://localhost:8080/services/bar;a=b;c=d;e=f", "/bar");
+ String url =
testGetBaseURL("http://localhost:8080/services/bar;a=b;c=d;e=f",
+ "", "/services", "/bar");
assertEquals("http://localhost:8080/services", url);
}
@Test
public void testGetRequestURLMultipleMatrixParam2() throws Exception {
- String url =
testGetRequestUrl("http://localhost:8080/services/bar;a=b;c=d;e=f",
"/bar;a=b;c=d");
+ String url =
testGetBaseURL("http://localhost:8080/services/bar;a=b;c=d;e=f",
+ "", "/services", "/bar;a=b;c=d");
assertEquals("http://localhost:8080/services", url);
}
@Test
public void testGetRequestURLMultipleMatrixParam3() throws Exception {
- String url =
testGetRequestUrl("http://localhost:8080/services/bar;a=b;c=d;e=f", "/bar;a=b");
+ String url =
testGetBaseURL("http://localhost:8080/services/bar;a=b;c=d;e=f",
+ "", "/services", "/bar;a=b");
assertEquals("http://localhost:8080/services", url);
}
@Test
public void testGetRequestURLMultipleMatrixParam4() throws Exception {
- String url =
testGetRequestUrl("http://localhost:8080/services/bar;a=b;c=d;e=f;",
"/bar;a=b");
+ String url =
testGetBaseURL("http://localhost:8080/services/bar;a=b;c=d;e=f;",
+ "", "/services", "/bar;a=b");
assertEquals("http://localhost:8080/services", url);
}