cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1207335 - in /cxf/branches/2.4.x-fixes: ./ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/HttpUtils.java rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/utils/HttpUtilsTest.java
Date Mon, 28 Nov 2011 15:58:26 GMT
Author: sergeyb
Date: Mon Nov 28 15:58:26 2011
New Revision: 1207335

URL: http://svn.apache.org/viewvc?rev=1207335&view=rev
Log:
Merged revisions 1207334 via svnmerge from 
https://svn.apache.org/repos/asf/cxf/trunk

........
  r1207334 | sergeyb | 2011-11-28 15:55:23 +0000 (Mon, 28 Nov 2011) | 1 line
  
  [CXF-3937] Minor fixes for UriInfo to return correct base URI
........

Modified:
    cxf/branches/2.4.x-fixes/   (props changed)
    cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/HttpUtils.java
    cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/utils/HttpUtilsTest.java

Propchange: cxf/branches/2.4.x-fixes/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Mon Nov 28 15:58:26 2011
@@ -1 +1 @@
-/cxf/trunk:1205786,1205830,1205996-1205997,1206800
+/cxf/trunk:1205786,1205830,1205996-1205997,1206800,1207334

Propchange: cxf/branches/2.4.x-fixes/
------------------------------------------------------------------------------
Binary property 'svnmerge-integrated' - no diff available.

Modified: cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/HttpUtils.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/HttpUtils.java?rev=1207335&r1=1207334&r2=1207335&view=diff
==============================================================================
--- cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/HttpUtils.java
(original)
+++ cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/HttpUtils.java
Mon Nov 28 15:58:26 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/branches/2.4.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/utils/HttpUtilsTest.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/utils/HttpUtilsTest.java?rev=1207335&r1=1207334&r2=1207335&view=diff
==============================================================================
--- cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/utils/HttpUtilsTest.java
(original)
+++ cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/utils/HttpUtilsTest.java
Mon Nov 28 15:58:26 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