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 2010/03/01 03:06:22 UTC
svn commit: r917356 - in /incubator/wink/trunk:
wink-common/src/main/java/org/apache/wink/common/internal/ResponseImpl.java
wink-server/src/test/java/org/apache/wink/server/internal/ResponseTest.java
Author: bluk
Date: Mon Mar 1 02:06:22 2010
New Revision: 917356
URL: http://svn.apache.org/viewvc?rev=917356&view=rev
Log:
Response.location(URI) should check relative URI
If a relative URI is passed into location, then
it should be resolved against UriInfo.getBaseUri()
according to the JavaDoc.
Thanks to John Chang for IMing me the issue.
Added:
incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/ResponseTest.java
Modified:
incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/ResponseImpl.java
Modified: incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/ResponseImpl.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/ResponseImpl.java?rev=917356&r1=917355&r2=917356&view=diff
==============================================================================
--- incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/ResponseImpl.java (original)
+++ incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/ResponseImpl.java Mon Mar 1 02:06:22 2010
@@ -31,8 +31,12 @@
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.NewCookie;
import javax.ws.rs.core.Response;
+import javax.ws.rs.core.UriBuilder;
+import javax.ws.rs.core.UriInfo;
import javax.ws.rs.core.Variant;
+import org.apache.wink.common.RuntimeContext;
+import org.apache.wink.common.internal.runtime.RuntimeContextTLS;
import org.apache.wink.common.internal.utils.HeaderUtils;
public class ResponseImpl extends Response {
@@ -157,6 +161,19 @@
@Override
public ResponseBuilder location(URI location) {
+ if (location != null && !location.isAbsolute()) {
+ RuntimeContext rtContext = RuntimeContextTLS.getRuntimeContext();
+ if (rtContext != null) {
+ UriInfo info =
+ RuntimeContextTLS.getRuntimeContext().getAttribute(UriInfo.class);
+ if (info != null) {
+ location =
+ UriBuilder.fromUri(info.getBaseUri()).path(location.getPath())
+ .fragment(location.getFragment()).build();
+ }
+ }
+ }
+
return singleHeader(HttpHeaders.LOCATION, location);
}
Added: incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/ResponseTest.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/ResponseTest.java?rev=917356&view=auto
==============================================================================
--- incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/ResponseTest.java (added)
+++ incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/ResponseTest.java Mon Mar 1 02:06:22 2010
@@ -0,0 +1,115 @@
+/*
+ * 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.wink.server.internal;
+
+import java.net.URI;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.core.Response;
+
+import org.apache.wink.server.internal.servlet.MockServletInvocationTest;
+import org.apache.wink.test.mock.MockRequestConstructor;
+import org.springframework.mock.web.MockHttpServletRequest;
+import org.springframework.mock.web.MockHttpServletResponse;
+
+public class ResponseTest extends MockServletInvocationTest {
+
+ @Override
+ protected Class<?>[] getClasses() {
+ return new Class[] {LocationUriResource.class};
+ }
+
+ @Path("/baseUri")
+ public static class LocationUriResource {
+
+ @GET
+ public Response getBaseUri() {
+ // add this here to make sure null doesn't blow up
+ return Response.created(URI.create("abc")).build();
+ }
+
+ @GET
+ @Path("null")
+ public Response getNullUri() {
+ // add this here to make sure null doesn't blow up
+ return Response.created(URI.create("value")).location(null).build();
+ }
+
+ @GET
+ @Path("fragment")
+ public Response getBaseUriWithFragment() {
+ return Response.created(URI.create("def#frag")).build();
+ }
+
+ @GET
+ @Path("absolute")
+ public Response getAbsolute() {
+ return Response.created(URI.create("http://example.com/xyz#frag")).build();
+ }
+
+ @GET
+ @Path("location")
+ public Response getLocation() {
+ return Response.status(499).location(URI.create("abcd")).build();
+ }
+
+ @GET
+ @Path("contentLocation")
+ public Response getContentLocation() {
+ // note that content-location is different and is relative as
+ // defined in the spec
+ return Response.status(499).contentLocation(URI.create("abcd")).build();
+ }
+
+ } // class GetBaseUriResource
+
+ public void testDetection() throws Exception {
+ MockHttpServletRequest request =
+ MockRequestConstructor.constructMockRequest("GET", "/baseUri", "*/*");
+ MockHttpServletResponse response = invoke(request);
+ assertEquals("http://localhost:80/abc", response.getHeader("Location").toString());
+
+ request = MockRequestConstructor.constructMockRequest("GET", "/baseUri/fragment", "*/*");
+ response = invoke(request);
+ assertEquals("http://localhost:80/def#frag", response.getHeader("Location").toString());
+
+ request = MockRequestConstructor.constructMockRequest("GET", "/baseUri/absolute", "*/*");
+ response = invoke(request);
+ assertEquals("http://example.com/xyz#frag", response.getHeader("Location").toString());
+
+ request = MockRequestConstructor.constructMockRequest("GET", "/baseUri/null", "*/*");
+ response = invoke(request);
+ assertEquals(201, response.getStatus());
+ assertNull(response.getHeader("Location"));
+
+ request = MockRequestConstructor.constructMockRequest("GET", "/baseUri/location", "*/*");
+ response = invoke(request);
+ assertEquals(499, response.getStatus());
+ assertEquals("http://localhost:80/abcd", response.getHeader("Location").toString());
+
+ request =
+ MockRequestConstructor.constructMockRequest("GET", "/baseUri/contentLocation", "*/*");
+ response = invoke(request);
+ assertEquals(499, response.getStatus());
+ assertEquals("abcd", response.getHeader("Content-Location").toString());
+
+ }
+}