cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1207334 - in /cxf/trunk/rt/frontend/jaxrs/src: main/java/org/apache/cxf/jaxrs/utils/HttpUtils.java test/java/org/apache/cxf/jaxrs/utils/HttpUtilsTest.java
Date Mon, 28 Nov 2011 15:55:24 GMT
Author: sergeyb
Date: Mon Nov 28 15:55:23 2011
New Revision: 1207334

URL: http://svn.apache.org/viewvc?rev=1207334&view=rev
Log:
[CXF-3937] Minor fixes for UriInfo to return correct base URI

Modified:
    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/utils/HttpUtilsTest.java

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=1207334&r1=1207333&r2=1207334&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 Mon
Nov 28 15:55:23 2011
@@ -56,8 +56,9 @@ public final class HttpUtils {
     private static final ResourceBundle BUNDLE = BundleUtils.getBundle(HttpUtils.class);
     private static final Logger LOG = LogUtils.getL7dLogger(HttpUtils.class);
     
-    private static final String LOCAL_IP_ADDRESS = "127.0.0.1";
-    private static final String LOCAL_HOST = "localhost";
+    private static final String ANY_IP_ADDRESS = "0.0.0.0";
+    private static final int DEFAULT_HTTP_PORT = 80;
+    
     private static final Pattern ENCODE_PATTERN = Pattern.compile("%[0-9a-fA-F][0-9a-fA-F]");
     private static final String CHARSET_PARAMETER = "charset";
     
@@ -181,18 +182,21 @@ public final class HttpUtils {
                || HttpHeaders.LAST_MODIFIED.equalsIgnoreCase(headerName); 
     }
     
-    public static URI toAbsoluteUri(URI u, Message message) { 
-        if (!u.isAbsolute()) {
-            HttpServletRequest httpRequest = 
-                (HttpServletRequest)message.get(AbstractHTTPDestination.HTTP_REQUEST);
-            if (httpRequest != null) {
-                String scheme = httpRequest.isSecure() ? "https" : "http";
-                String host = httpRequest.getLocalName();
-                if (LOCAL_IP_ADDRESS.equals(host)) {
-                    host = LOCAL_HOST;
-                }
-                int port = httpRequest.getLocalPort();
-                return URI.create(scheme + "://" + host + ':' + port + u.toString());
+    public static URI toAbsoluteUri(URI u, Message message) {
+        HttpServletRequest request = 
+            (HttpServletRequest)message.get(AbstractHTTPDestination.HTTP_REQUEST);
+        boolean absolute = u.isAbsolute();
+        if (request != null && (!absolute || u.getHost().equals(ANY_IP_ADDRESS)))
{
+            String serverAndPort = request.getServerName();
+            int port = request.getLocalPort();
+            if (port != DEFAULT_HTTP_PORT) {
+                serverAndPort += ":" + port;
+            }
+            String base = request.getScheme() + "://" + serverAndPort;
+            if (!absolute) {
+                u = URI.create(base + u.toString());
+            } else {
+                u = URI.create(u.toString().replace(ANY_IP_ADDRESS, serverAndPort));
             }
         }
         return u;
@@ -239,7 +243,9 @@ public final class HttpUtils {
         } else {
             address = (String)m.get(Message.ENDPOINT_ADDRESS);
         }
-        
+        if (address.startsWith("http") && address.endsWith("//")) {
+            address = address.substring(0, address.length() - 1);
+        }
         return address;
     }
     

Modified: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/utils/HttpUtilsTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/utils/HttpUtilsTest.java?rev=1207334&r1=1207333&r2=1207334&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/utils/HttpUtilsTest.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/utils/HttpUtilsTest.java
Mon Nov 28 15:55:23 2011
@@ -19,6 +19,9 @@
 
 package org.apache.cxf.jaxrs.utils;
 
+import java.net.URI;
+
+import javax.servlet.http.HttpServletRequest;
 import javax.ws.rs.core.HttpHeaders;
 import javax.ws.rs.core.Response;
 
@@ -28,6 +31,7 @@ import org.apache.cxf.message.ExchangeIm
 import org.apache.cxf.message.Message;
 import org.apache.cxf.message.MessageImpl;
 import org.apache.cxf.transport.Destination;
+import org.apache.cxf.transport.http.AbstractHTTPDestination;
 import org.easymock.EasyMock;
 
 import org.junit.Assert;
@@ -142,6 +146,22 @@ public class HttpUtilsTest extends Asser
         doTestGetBaseAddress("/s", "/s");
     }
     
+    @Test
+    public void testReplaceAnyIPAddress() {
+        Message m = new MessageImpl();
+        HttpServletRequest req = EasyMock.createMock(HttpServletRequest.class);
+        m.put(AbstractHTTPDestination.HTTP_REQUEST, req);
+        req.getScheme();
+        EasyMock.expectLastCall().andReturn("http");
+        req.getServerName();
+        EasyMock.expectLastCall().andReturn("localhost");
+        req.getLocalPort();
+        EasyMock.expectLastCall().andReturn(8080);
+        EasyMock.replay(req);
+        URI u = HttpUtils.toAbsoluteUri(URI.create("http://0.0.0.0/bar/foo"), m);
+        assertEquals("http://localhost:8080/bar/foo", u.toString());
+    }
+    
     private void doTestGetBaseAddress(String baseURI, String expected) {
         Message m = new MessageImpl();
         Exchange exchange = new ExchangeImpl();



Mime
View raw message