You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wink.apache.org by bl...@apache.org on 2009/10/12 23:39:42 UTC
svn commit: r824508 - in /incubator/wink/trunk/wink-server/src:
main/java/org/apache/wink/server/internal/handlers/
test/java/org/apache/wink/server/internal/
Author: bluk
Date: Mon Oct 12 21:39:42 2009
New Revision: 824508
URL: http://svn.apache.org/viewvc?rev=824508&view=rev
Log:
Fix empty path annotation issue
See [WINK-217]
Thanks Mike Rheinheimer for the fix
and Jaroslav Libak for reporting the
issue.
Modified:
incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/handlers/FindResourceMethodHandler.java
incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/handlers/SearchResult.java
incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/EmptyPathParamTest.java
Modified: incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/handlers/FindResourceMethodHandler.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/handlers/FindResourceMethodHandler.java?rev=824508&r1=824507&r2=824508&view=diff
==============================================================================
--- incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/handlers/FindResourceMethodHandler.java (original)
+++ incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/handlers/FindResourceMethodHandler.java Mon Oct 12 21:39:42 2009
@@ -60,7 +60,9 @@
// resource method
if (resource.isExactMatch()) {
- logger.debug("Root resource @Path matches exactly so finding root resource method in {}", resource.getResourceClass().getName());
+ logger
+ .debug("Root resource @Path matches exactly so finding root resource method in {}",
+ resource.getResourceClass().getName());
handleResourceMethod(context, chain);
return;
}
@@ -272,7 +274,8 @@
// 1. get the number of segments that were matched up until the current
// match. this will be used as
// the offset into the full path segments list
- int offset = result.getData().getMatchedURIs().getFirst().size();
+ int offset = result.getData().calculateUriOffset();
+
// 2. save the current matched uri - it is added as the first uri in the
// list of matched uri's
int headSegmentsCount = result.getData().addMatchedURI(matcher.getHead(false));
Modified: incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/handlers/SearchResult.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/handlers/SearchResult.java?rev=824508&r1=824507&r2=824508&view=diff
==============================================================================
--- incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/handlers/SearchResult.java (original)
+++ incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/handlers/SearchResult.java Mon Oct 12 21:39:42 2009
@@ -121,7 +121,7 @@
* Used for providing the info for the {@link UriInfo#getMatchedURIs()}
* method.
*
- * @param uri the uri that was used for the matching is is stripped from
+ * @param uri the uri that was used for the matching is stripped from
* any matrix parameters
* @return the number of segments of the input uri
*/
@@ -129,11 +129,25 @@
// get all the segments of the original request (which include the
// matrix parameters)
List<PathSegment> segments = uriInfo.getPathSegments(false);
-
+
// count the number of segments in input uri
int count = uri.equals("") ? 0 : UriHelper.parsePath(uri).size();
// get the offset of the provided uri from the complete request path
+ int offset = calculateUriOffset();
+
+ // add the uri segments (including any matrix parameters) by
+ // obtaining a sub list from the the complete request segments
+ addMatchedURI(segments, offset, count);
+ return count;
+ }
+
+ /**
+ * Used to calculate the number of URI segments already matched.
+ *
+ * @return the offset past the URI segments already matched
+ */
+ public int calculateUriOffset() {
int offset = 0;
if (getMatchedURIs().size() > 0) {
// the first uri in the "matched uri's" list always reflects all
@@ -142,8 +156,10 @@
// complete request uri
List<PathSegment> firstMatchedUri = getMatchedURIs().getFirst();
offset = firstMatchedUri.size();
- // we need to skip all empty string as path segments that were added
- // because of matches to @Path("") and @Path("/"), so decrease the
+ // we need to skip all empty string as path segments that were
+ // added
+ // because of matches to @Path("") and @Path("/"), so decrease
+ // the
// offset by the number of empty segments
for (PathSegment segment : firstMatchedUri) {
if (segment.getPath().equals("")) {
@@ -151,10 +167,7 @@
}
}
}
- // add the uri segments (including any matrix parameters) by
- // obtaining a sub list from the the complete request segments
- addMatchedURI(segments, offset, count);
- return count;
+ return offset;
}
private void addMatchedURI(List<PathSegment> segments, int offset, int count) {
@@ -165,10 +178,11 @@
if (subListSegments.isEmpty()) {
// the sublist may be empty if the count is 0. this can happen
// if the given uri was an empty string (which itself can happen
- // if the resource/sub-resource are annotated with @Path("") or @Path("/").
+ // if the resource/sub-resource are annotated with @Path("") or
+ // @Path("/").
subListSegments = UriHelper.parsePath("");
}
-
+
LinkedList<List<PathSegment>> matchedURIs = getMatchedURIs();
if (matchedURIs.size() == 0) {
// if it's the first uri, simply add it
Modified: incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/EmptyPathParamTest.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/EmptyPathParamTest.java?rev=824508&r1=824507&r2=824508&view=diff
==============================================================================
--- incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/EmptyPathParamTest.java (original)
+++ incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/EmptyPathParamTest.java Mon Oct 12 21:39:42 2009
@@ -39,7 +39,7 @@
@Override
protected Class<?>[] getClasses() {
- return new Class[] {PathResource.class};
+ return new Class[] {PathResource.class, PathResource2.class, PathResource3.class};
}
@Path("/p")
@@ -48,7 +48,8 @@
@GET
@Path("/first{firstParams:.*}/second{secondParams:.*}")
@Produces(MediaType.TEXT_PLAIN)
- public String findTestRuns(@PathParam("firstParams") final PathSegment firstParams, @PathParam("secondParams") final PathSegment secondParams) {
+ public String findTestRuns(@PathParam("firstParams") final PathSegment firstParams,
+ @PathParam("secondParams") final PathSegment secondParams) {
assertEquals("first", firstParams.getPath());
assertEquals("bob", firstParams.getMatrixParameters().get("name").get(0));
@@ -59,14 +60,74 @@
}
}
-
- public void testMatrixParamsOnly() throws Exception {
+
+ @Path("/p2")
+ public static class PathResource2 {
+
+ @GET
+ @Path("/first/{firstParams:.*}/second/{secondParams:.*}")
+ @Produces(MediaType.TEXT_PLAIN)
+ public String findTestRuns(@PathParam("firstParams") final PathSegment firstParams,
+ @PathParam("secondParams") final PathSegment secondParams) {
+
+ assertEquals("first", firstParams.getPath());
+ assertEquals("bob", firstParams.getMatrixParameters().get("name").get(0));
+ assertEquals("second", secondParams.getPath());
+ assertEquals("blue", secondParams.getMatrixParameters().get("eyes").get(0));
+ return "hi2";
+
+ }
+
+ }
+
+ @Path("")
+ // "" is the essence of this test
+ public static class PathResource3 {
+ @GET
+ @Path("/first{firstParams:.*}/second{secondParams:.*}")
+ @Produces(MediaType.TEXT_PLAIN)
+ public String findTestRuns(@PathParam("firstParams") final PathSegment firstParams,
+ @PathParam("secondParams") final PathSegment secondParams) {
+
+ assertEquals("first", firstParams.getPath());
+ assertEquals("bob", firstParams.getMatrixParameters().get("name").get(0));
+ assertEquals("second", secondParams.getPath());
+ assertEquals("blue", secondParams.getMatrixParameters().get("eyes").get(0));
+ return "hi3";
+
+ }
+ }
+
+ public void testEmptyPathParam() throws Exception {
MockHttpServletRequest getRequest =
- MockRequestConstructor.constructMockRequest("GET", "/p/first;name=bob/second;eyes=blue", "*/*");
+ MockRequestConstructor.constructMockRequest("GET",
+ "/p/first;name=bob/second;eyes=blue",
+ MediaType.TEXT_PLAIN);
MockHttpServletResponse getResponse = invoke(getRequest);
- assertEquals(getResponse.getContentAsString(), "hi");
+ assertEquals(200, getResponse.getStatus());
+ assertEquals("hi", getResponse.getContentAsString());
}
+ public void _testEmptyPathParam2() throws Exception { // TODO: see WINK-216
+ // - need to enable
+ // this test AND fix
+ // production code
+ MockHttpServletRequest getRequest =
+ MockRequestConstructor.constructMockRequest("GET",
+ "/p2/first/;name=bob/second/;eyes=blue",
+ MediaType.TEXT_PLAIN);
+ MockHttpServletResponse getResponse = invoke(getRequest);
+ assertEquals(200, getResponse.getStatus());
+ assertEquals("hi2", getResponse.getContentAsString());
+ }
-
+ public void testEmptyPathParam3() throws Exception {
+ MockHttpServletRequest getRequest =
+ MockRequestConstructor.constructMockRequest("GET",
+ "/first;name=bob/second;eyes=blue",
+ MediaType.TEXT_PLAIN);
+ MockHttpServletResponse getResponse = invoke(getRequest);
+ assertEquals(200, getResponse.getStatus());
+ assertEquals("hi3", getResponse.getContentAsString());
+ }
}