cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1521947 - in /cxf/trunk/rt/frontend/jaxrs/src: main/java/org/apache/cxf/jaxrs/impl/ main/java/org/apache/cxf/jaxrs/utils/ test/java/org/apache/cxf/jaxrs/impl/
Date Wed, 11 Sep 2013 17:31:16 GMT
Author: sergeyb
Date: Wed Sep 11 17:31:16 2013
New Revision: 1521947

URL: http://svn.apache.org/r1521947
Log:
[CXF-5135] ResponseImpl should resolve relative Links against client request URI

Modified:
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/LinkBuilderImpl.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ResponseImpl.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/HttpUtils.java
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/LinkBuilderImplTest.java
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/UriInfoImplTest.java

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/LinkBuilderImpl.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/LinkBuilderImpl.java?rev=1521947&r1=1521946&r2=1521947&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/LinkBuilderImpl.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/LinkBuilderImpl.java
Wed Sep 11 17:31:16 2013
@@ -43,20 +43,24 @@ public class LinkBuilderImpl implements 
     
     @Override
     public Link build(Object... values) {
-        URI uri = ub.build(values);
-        return new LinkImpl(uri, new HashMap<String, String>(params));
+        URI resolvedLinkUri = getResolvedUri(values);
+        return new LinkImpl(resolvedLinkUri, new HashMap<String, String>(params));
     }
 
     @Override
     public Link buildRelativized(URI requestUri, Object... values) {
-        URI uri = ub.build(values);
-        
-        URI resolvedLinkUri = baseUri != null 
-            ? HttpUtils.resolve(UriBuilder.fromUri(baseUri), uri) : uri;
+        URI resolvedLinkUri = getResolvedUri(values);
         URI relativized = HttpUtils.relativize(requestUri, resolvedLinkUri);
         return new LinkImpl(relativized, new HashMap<String, String>(params));
     }
 
+    private URI getResolvedUri(Object... values) {
+        URI uri = ub.build(values);
+                
+        return baseUri != null 
+            ? HttpUtils.resolve(UriBuilder.fromUri(baseUri), uri) : uri;
+    }
+    
     @Override
     public Builder link(Link link) {
         ub = UriBuilder.fromLink(link);

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ResponseImpl.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ResponseImpl.java?rev=1521947&r1=1521946&r2=1521947&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ResponseImpl.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ResponseImpl.java
Wed Sep 11 17:31:16 2013
@@ -256,7 +256,16 @@ public final class ResponseImpl extends 
     }
     
     public boolean hasLink(String relation) {
-        return getLink(relation) != null;
+        List<Object> linkValues = metadata.get(HttpHeaders.LINK);
+        if (linkValues != null) {
+            for (Object o : linkValues) {
+                Link link = o instanceof Link ? (Link)o : Link.valueOf(o.toString());
+                if (relation.equals(link.getRel())) {
+                    return true;
+                }
+            }
+        }
+        return false;
     }
     
     public Link getLink(String relation) {
@@ -275,15 +284,7 @@ public final class ResponseImpl extends 
     }
 
     public Set<Link> getLinks() {
-        List<Object> linkValues = metadata.get(HttpHeaders.LINK);
-        Set<Link> links = new HashSet<Link>();
-        if (linkValues != null) {
-            for (Object o : linkValues) {
-                Link link = o instanceof Link ? (Link)o : Link.valueOf(o.toString());
-                links.add(link);
-            }
-        }
-        return links;
+        return new HashSet<Link>(getAllLinks().values());
     }
 
     private Map<String, Link> getAllLinks() {
@@ -294,6 +295,11 @@ public final class ResponseImpl extends 
             Map<String, Link> links = new LinkedHashMap<String, Link>();
             for (Object o : linkValues) {
                 Link link = o instanceof Link ? (Link)o : Link.valueOf(o.toString());
+                if (!link.getUri().isAbsolute()) {
+                    URI requestURI = URI.create((String)responseMessage.getExchange().getOutMessage()
+                        .get(Message.REQUEST_URI));
+                    link = Link.fromLink(link).baseUri(requestURI).build();
+                }
                 links.put(link.getRel(), link);
             }
             return links;

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/HttpUtils.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/HttpUtils.java?rev=1521947&r1=1521946&r2=1521947&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/HttpUtils.java (original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/HttpUtils.java Wed
Sep 11 17:31:16 2013
@@ -26,7 +26,6 @@ import java.net.URLEncoder;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.Date;
-import java.util.LinkedList;
 import java.util.List;
 import java.util.Locale;
 import java.util.Map;
@@ -80,10 +79,6 @@ public final class HttpUtils {
     private static final String PATH_RESERVED_CHARACTERS = "=@/:!$&\'(),;~";
     private static final String QUERY_RESERVED_CHARACTERS = "?/,";
     
-    private static final String CURRENT_PATH_SEGMENT = ".";
-    private static final String PARENT_PATH_SEGMENT = "..";
-    
-    
     private HttpUtils() {
     }
     
@@ -509,26 +504,7 @@ public final class HttpUtils {
     
     public static URI resolve(UriBuilder baseBuilder, URI uri) {
         if (!uri.isAbsolute()) {
-            String uriValue = uri.toString();
-            boolean parentPathSegmentAvail = uriValue.contains(PARENT_PATH_SEGMENT)
-                || uriValue.contains(CURRENT_PATH_SEGMENT); 
-            uri = baseBuilder.path(uriValue).build();
-            if (parentPathSegmentAvail) {
-                List<PathSegment> segments = JAXRSUtils.getPathSegments(uri.getRawPath(),
false);
-                List<PathSegment> actualSegments = new LinkedList<PathSegment>();
-                UriBuilder ub = UriBuilder.fromUri(uri).replacePath(null);
-                for (PathSegment ps : segments) {
-                    if (PARENT_PATH_SEGMENT.equals(ps.getPath()) && !actualSegments.isEmpty())
{
-                        actualSegments.remove(actualSegments.size() - 1);
-                    } else if (!CURRENT_PATH_SEGMENT.equals(ps.getPath())) {
-                        actualSegments.add(ps);
-                    }
-                }
-                for (PathSegment ps : actualSegments) {
-                    ub.segment(ps.toString());
-                }
-                uri = ub.build();
-            }
+            return baseBuilder.build().resolve(uri);
         }
         return uri;
     }

Modified: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/LinkBuilderImplTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/LinkBuilderImplTest.java?rev=1521947&r1=1521946&r2=1521947&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/LinkBuilderImplTest.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/LinkBuilderImplTest.java
Wed Sep 11 17:31:16 2013
@@ -66,13 +66,30 @@ public class LinkBuilderImplTest extends
     }
 
     @Test
-    public void testRelativeBuild() throws Exception {
+    public void testBuildRelativized() throws Exception {
+        
         Link.Builder linkBuilder = new LinkBuilderImpl();
         URI base = URI.create("http://example.com/page2");
         Link prevLink = linkBuilder.uri("http://example.com/page1").rel("previous").buildRelativized(base);
         assertEquals("<page1>;rel=\"previous\"", prevLink.toString());
     }
-
+    
+    @Test
+    public void testRelativeLink() throws Exception {
+        Link.Builder linkBuilder = Link.fromUri("relative");
+        linkBuilder.baseUri("http://localhost:8080/base/path");
+        Link link = linkBuilder.rel("next").build();
+        assertEquals("<http://localhost:8080/base/relative>;rel=\"next\"", link.toString());
+    }
+    
+    @Test
+    public void testRelativeLink2() throws Exception {
+        Link.Builder linkBuilder = Link.fromUri("/relative");
+        linkBuilder.baseUri("http://localhost:8080/base/path");
+        Link link = linkBuilder.rel("next").build();
+        assertEquals("<http://localhost:8080/relative>;rel=\"next\"", link.toString());
+    }
+    
     @Test
     public void testSeveralAttributes() throws Exception {
         Link.Builder linkBuilder = new LinkBuilderImpl();

Modified: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/UriInfoImplTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/UriInfoImplTest.java?rev=1521947&r1=1521946&r2=1521947&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/UriInfoImplTest.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/UriInfoImplTest.java
Wed Sep 11 17:31:16 2013
@@ -52,8 +52,8 @@ public class UriInfoImplTest extends Ass
     
     @Test
     public void testResolve() {
-        UriInfoImpl u = new UriInfoImpl(mockMessage("http://localhost:8080/baz", null), null);
-        assertEquals("Wrong base path", "http://localhost:8080/baz", 
+        UriInfoImpl u = new UriInfoImpl(mockMessage("http://localhost:8080/baz/", null),
null);
+        assertEquals("Wrong base path", "http://localhost:8080/baz/", 
                      u.getBaseUri().toString());
         URI resolved = u.resolve(URI.create("a"));
         assertEquals("http://localhost:8080/baz/a", resolved.toString());
@@ -65,7 +65,7 @@ public class UriInfoImplTest extends Ass
         assertEquals("Wrong base path", "http://localhost:8080/baz", 
                      u.getBaseUri().toString());
         URI resolved = u.resolve(URI.create("./a"));
-        assertEquals("http://localhost:8080/baz/a", resolved.toString());
+        assertEquals("http://localhost:8080/a", resolved.toString());
     }
     
     @Test



Mime
View raw message