fix FilterInvocation so it doesn't lose the tail end (past the servlet path) of the request url

This commit is contained in:
Colin Sampaleanu 2004-04-23 02:29:18 +00:00
parent 0537900357
commit 6eb0a47632
3 changed files with 25 additions and 17 deletions

View File

@ -101,10 +101,11 @@ public class FilterInvocation {
} }
public String getRequestUrl() { public String getRequestUrl() {
return getHttpRequest().getServletPath() String pathInfo = getHttpRequest().getPathInfo();
+ ((getHttpRequest().getQueryString() == null) ? "" String queryString = getHttpRequest().getQueryString();
: ("?"
+ getHttpRequest().getQueryString())); return getHttpRequest().getServletPath() + (pathInfo == null ? "" : pathInfo)
+ (queryString == null ? "" : ("?" + queryString));
} }
public ServletResponse getResponse() { public ServletResponse getResponse() {

View File

@ -59,6 +59,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
private String serverName; private String serverName;
private String servletPath; private String servletPath;
private int serverPort; private int serverPort;
private String pathInfo; // null for no extra path
//~ Constructors =========================================================== //~ Constructors ===========================================================
@ -196,8 +197,13 @@ public class MockHttpServletRequest implements HttpServletRequest {
throw new UnsupportedOperationException("mock method not implemented"); throw new UnsupportedOperationException("mock method not implemented");
} }
public void setPathInfo(String pathInfo) {
this.pathInfo = pathInfo;
}
public String getPathInfo() { public String getPathInfo() {
throw new UnsupportedOperationException("mock method not implemented"); return pathInfo;
} }
public String getPathTranslated() { public String getPathTranslated() {
@ -240,8 +246,8 @@ public class MockHttpServletRequest implements HttpServletRequest {
throw new UnsupportedOperationException("mock method not implemented"); throw new UnsupportedOperationException("mock method not implemented");
} }
public void setRequestURL(String newRequestURL) { public void setRequestURL(String requestURL) {
requestURL = newRequestURL; this.requestURL = requestURL;
} }
public StringBuffer getRequestURL() { public StringBuffer getRequestURL() {
@ -268,8 +274,8 @@ public class MockHttpServletRequest implements HttpServletRequest {
throw new UnsupportedOperationException("mock method not implemented"); throw new UnsupportedOperationException("mock method not implemented");
} }
public void setScheme(String newScheme) { public void setScheme(String scheme) {
scheme = newScheme; this.scheme = scheme;
} }
public String getScheme() { public String getScheme() {
@ -280,16 +286,16 @@ public class MockHttpServletRequest implements HttpServletRequest {
throw new UnsupportedOperationException("mock method not implemented"); throw new UnsupportedOperationException("mock method not implemented");
} }
public void setServerName(String newServerName) { public void setServerName(String serverName) {
serverName = newServerName; this.serverName = serverName;
} }
public String getServerName() { public String getServerName() {
return serverName; return serverName;
} }
public void setServerPort(int newPort) { public void setServerPort(int serverPort) {
serverPort = newPort; this.serverPort = serverPort;
} }
public int getServerPort() { public int getServerPort() {

View File

@ -68,7 +68,8 @@ public class FilterInvocationTests extends TestCase {
public void testGettersAndStringMethods() { public void testGettersAndStringMethods() {
MockHttpServletRequest request = new MockHttpServletRequest(null, null); MockHttpServletRequest request = new MockHttpServletRequest(null, null);
request.setServletPath("/HelloWorld"); request.setServletPath("/HelloWorld");
request.setRequestURL("http://www.example.com/mycontext/HelloWorld"); request.setPathInfo("/some/more/segments.html");
request.setRequestURL("http://www.example.com/mycontext/HelloWorld/some/more/segments.html");
MockHttpServletResponse response = new MockHttpServletResponse(); MockHttpServletResponse response = new MockHttpServletResponse();
MockFilterChain chain = new MockFilterChain(); MockFilterChain chain = new MockFilterChain();
@ -78,9 +79,9 @@ public class FilterInvocationTests extends TestCase {
assertEquals(response, fi.getResponse()); assertEquals(response, fi.getResponse());
assertEquals(response, fi.getHttpResponse()); assertEquals(response, fi.getHttpResponse());
assertEquals(chain, fi.getChain()); assertEquals(chain, fi.getChain());
assertEquals("/HelloWorld", fi.getRequestUrl()); assertEquals("/HelloWorld/some/more/segments.html", fi.getRequestUrl());
assertEquals("FilterInvocation: URL: /HelloWorld", fi.toString()); assertEquals("FilterInvocation: URL: /HelloWorld/some/more/segments.html", fi.toString());
assertEquals("http://www.example.com/mycontext/HelloWorld", assertEquals("http://www.example.com/mycontext/HelloWorld/some/more/segments.html",
fi.getFullRequestUrl()); fi.getFullRequestUrl());
} }