cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1154143 - in /cxf/trunk/rt/transports/http/src: main/java/org/apache/cxf/transport/servlet/BaseUrlHelper.java test/java/org/apache/cxf/transport/servlet/servicelist/BaseUrlHelperTest.java
Date Fri, 05 Aug 2011 09:35:22 GMT
Author: sergeyb
Date: Fri Aug  5 09:35:22 2011
New Revision: 1154143

URL: http://svn.apache.org/viewvc?rev=1154143&view=rev
Log:
[CXF-3709] Removing index-based calculation of base URL

Modified:
    cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/BaseUrlHelper.java
    cxf/trunk/rt/transports/http/src/test/java/org/apache/cxf/transport/servlet/servicelist/BaseUrlHelperTest.java

Modified: cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/BaseUrlHelper.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/BaseUrlHelper.java?rev=1154143&r1=1154142&r2=1154143&view=diff
==============================================================================
--- cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/BaseUrlHelper.java
(original)
+++ cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/BaseUrlHelper.java
Fri Aug  5 09:35:22 2011
@@ -18,6 +18,8 @@
  */
 package org.apache.cxf.transport.servlet;
 
+import java.net.URI;
+
 import javax.servlet.http.HttpServletRequest;
 
 import org.apache.cxf.transport.AbstractDestination;
@@ -28,36 +30,31 @@ public final class BaseUrlHelper {
     private BaseUrlHelper() {
     }
 
+    /**
+     * Returns base URL which includes scheme, host, port, Servlet context and servlet paths
+     * @param request current HttpServletRequest
+     * @return base URL
+     */
     public static String getBaseURL(HttpServletRequest request) {
         String reqPrefix = request.getRequestURL().toString();        
         String pathInfo = request.getPathInfo() == null ? "" : request.getPathInfo();
         //fix for CXF-898
         if (!"/".equals(pathInfo) || reqPrefix.endsWith("/")) {
-            String basePath = request.getContextPath() + request.getServletPath();
-            int index;
-            if (basePath.length() == 0) {
-                index = reqPrefix.indexOf(request.getRequestURI());
-            } else {
-                index = reqPrefix.indexOf(basePath);
-            }
-            reqPrefix = reqPrefix.substring(0, index + basePath.length());
+            StringBuilder sb = new StringBuilder();
+            // request.getScheme(), request.getLocalName() and request.getLocalPort()
+            // should be marginally cheaper - provided request.getLocalName() does 
+            // return the actual name used in request URI as opposed to localhost
+            // consistently across the Servlet stacks
+            
+            URI uri = URI.create(reqPrefix);
+            sb.append(uri.getScheme()).append("://").append(uri.getRawAuthority());
+            sb.append(request.getContextPath()).append(request.getServletPath());
+            
+            reqPrefix = sb.toString();
         }
         return reqPrefix;
     }
     
-    public static void makeAddressesAbsolute(HttpServletRequest request, String baseAddress,

-                                              AbstractDestination[] destinations) {
-        for (AbstractDestination dest : destinations) {
-            String addr = dest.getEndpointInfo().getAddress();
-            if (addr == null || addr.length() == 0) {
-                addr = "/";
-            }
-            if (addr != null && !addr.startsWith("http")) {
-                String base = baseAddress == null ? BaseUrlHelper.getBaseURL(request) : baseAddress;
-                setAddress(dest, base + addr);
-            }
-        }
-    }
 
     public static void setAddress(AbstractDestination dest, String absAddress) {
         dest.getEndpointInfo().setAddress(absAddress);

Modified: cxf/trunk/rt/transports/http/src/test/java/org/apache/cxf/transport/servlet/servicelist/BaseUrlHelperTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/transports/http/src/test/java/org/apache/cxf/transport/servlet/servicelist/BaseUrlHelperTest.java?rev=1154143&r1=1154142&r2=1154143&view=diff
==============================================================================
--- cxf/trunk/rt/transports/http/src/test/java/org/apache/cxf/transport/servlet/servicelist/BaseUrlHelperTest.java
(original)
+++ cxf/trunk/rt/transports/http/src/test/java/org/apache/cxf/transport/servlet/servicelist/BaseUrlHelperTest.java
Fri Aug  5 09:35:22 2011
@@ -52,6 +52,12 @@ public class BaseUrlHelperTest {
     }
 
     @Test
+    public void testGetRequestURLWithRepeatingValues() throws Exception {
+        String url = testGetBaseURL("http://services.com/services/bar", "/services", "",
"/bar");
+        Assert.assertEquals("http://services.com/services", url);
+    }
+    
+    @Test
     public void testGetRequestURL() throws Exception {
         String url = testGetBaseURL("http://localhost:8080/services/bar", "", "/services",
"/bar");
         Assert.assertEquals("http://localhost:8080/services", url);



Mime
View raw message