Author: kohei
Date: Tue May 19 12:09:50 2015
New Revision: 1680265

URL: http://svn.apache.org/r1680265
Log:
ROL-2075 InitFilter#getAbsoluteUrl() returns wrong URL which got last character 
removed

Added:
    roller/trunk/app/src/test/java/org/apache/roller/weblogger/ui/core/
    roller/trunk/app/src/test/java/org/apache/roller/weblogger/ui/core/filters/
    
roller/trunk/app/src/test/java/org/apache/roller/weblogger/ui/core/filters/InitFilterTest.java
Modified:
    
roller/trunk/app/src/main/java/org/apache/roller/weblogger/ui/core/filters/InitFilter.java

Modified: 
roller/trunk/app/src/main/java/org/apache/roller/weblogger/ui/core/filters/InitFilter.java
URL: 
http://svn.apache.org/viewvc/roller/trunk/app/src/main/java/org/apache/roller/weblogger/ui/core/filters/InitFilter.java?rev=1680265&r1=1680264&r2=1680265&view=diff
==============================================================================
--- 
roller/trunk/app/src/main/java/org/apache/roller/weblogger/ui/core/filters/InitFilter.java
 (original)
+++ 
roller/trunk/app/src/main/java/org/apache/roller/weblogger/ui/core/filters/InitFilter.java
 Tue May 19 12:09:50 2015
@@ -74,29 +74,40 @@ public class InitFilter implements Filte
     }
 
     private String getAbsoluteUrl(HttpServletRequest request) {
+        return getAbsoluteUrl(request.isSecure(),
+                request.getServerName(), request.getContextPath(),
+                request.getRequestURI(), request.getRequestURL().toString());
+    }
+
+    public void init(FilterConfig filterConfig) throws ServletException {
+    }
+
+    public void destroy() {
+    }
+
+    protected static String getAbsoluteUrl(boolean secure, String serverName, 
String contextPath, String requestURI, String requestURL){
 
         String url = null;
 
         String fullUrl = null;
 
-        if (!request.isSecure()) {
-            fullUrl = request.getRequestURL().toString();
+        if (!secure) {
+            fullUrl = requestURL;
         } else {
-            fullUrl = "http://"; + request.getServerName()
-                    + request.getContextPath();
+            fullUrl = "http://"; + serverName + contextPath;
         }
 
         // if the uri is only "/" then we are basically done
-        if ("/".equals(request.getRequestURI())) {
+        if ("/".equals(requestURI)) {
             if (log.isDebugEnabled()) {
-                log.debug(fullUrl.substring(0, fullUrl.length() - 1));
+                log.debug("requestURI is only '/'. fullUrl: " + fullUrl);
             }
-            return fullUrl.substring(0, fullUrl.length() - 1);
+            return removeTrailingSlash(fullUrl);
         }
 
         // find first "/" starting after hostname is specified
         int index = fullUrl.indexOf('/',
-                fullUrl.indexOf(request.getServerName()));
+                fullUrl.indexOf(serverName));
 
         if (index != -1) {
             // extract just the part leading up to uri
@@ -106,20 +117,16 @@ public class InitFilter implements Filte
         }
 
         // then just add on the context path
-        url += request.getContextPath();
+        url += contextPath;
 
         // make certain that we don't end with a /
+        return removeTrailingSlash(url);
+    }
+
+    protected static String removeTrailingSlash(String url) {
         if (url.endsWith("/")) {
-            url = url.substring(0, url.length() - 1);
+            return url.substring(0, url.length() - 1);
         }
-
         return url;
     }
-
-    public void init(FilterConfig filterConfig) throws ServletException {
-    }
-
-    public void destroy() {
-    }
-
 }

Added: 
roller/trunk/app/src/test/java/org/apache/roller/weblogger/ui/core/filters/InitFilterTest.java
URL: 
http://svn.apache.org/viewvc/roller/trunk/app/src/test/java/org/apache/roller/weblogger/ui/core/filters/InitFilterTest.java?rev=1680265&view=auto
==============================================================================
--- 
roller/trunk/app/src/test/java/org/apache/roller/weblogger/ui/core/filters/InitFilterTest.java
 (added)
+++ 
roller/trunk/app/src/test/java/org/apache/roller/weblogger/ui/core/filters/InitFilterTest.java
 Tue May 19 12:09:50 2015
@@ -0,0 +1,75 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  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.  For additional information regarding
+ * copyright in this work, please see the NOTICE file in the top level
+ * directory of this distribution.
+ */
+
+package org.apache.roller.weblogger.ui.core.filters;
+
+import junit.framework.TestCase;
+
+/**
+ * @author Kohei Nozaki
+ */
+public class InitFilterTest extends TestCase {
+
+    private static final String SERVER_NAME = "roller.example.com";
+
+    public void testGetAbsoluteUrlOnRootWithHttp() throws Exception {
+        boolean secure = false;
+        String contextPath = "";
+        String requestURI = "/";
+        String requestURL = "http://roller.example.com/";;
+
+        String absoluteUrl = InitFilter.getAbsoluteUrl(secure, SERVER_NAME, 
contextPath, requestURI, requestURL);
+        assertEquals("http://roller.example.com";, absoluteUrl);
+    }
+
+    public void testGetAbsoluteUrlOnRootWithHttps() throws Exception {
+        boolean secure = true;
+        String contextPath = "";
+        String requestURI = "/";
+        String requestURL = "https://roller.example.com/";;
+
+        String absoluteUrl = InitFilter.getAbsoluteUrl(secure, SERVER_NAME, 
contextPath, requestURI, requestURL);
+        assertEquals("http://roller.example.com";, absoluteUrl);
+    }
+
+    public void testGetAbsoluteUrlAgainstTop() throws Exception {
+        boolean secure = false;
+        String contextPath = "/roller";
+        String requestURI = "/roller/";
+        String requestURL = "http://roller.example.com/roller/";;
+
+        String absoluteUrl = InitFilter.getAbsoluteUrl(secure, SERVER_NAME, 
contextPath, requestURI, requestURL);
+        assertEquals("http://roller.example.com/roller";, absoluteUrl);
+    }
+
+    public void testGetAbsoluteUrlAgainstPermalink() throws Exception {
+        boolean secure = false;
+        String contextPath = "/roller";
+        String requestURI = "/roller/handle/entry/title";
+        String requestURL = 
"http://roller.example.com/roller/handle/entry/title";;
+
+        String absoluteUrl = InitFilter.getAbsoluteUrl(secure, SERVER_NAME, 
contextPath, requestURI, requestURL);
+        assertEquals("http://roller.example.com/roller";, absoluteUrl);
+    }
+
+    public void testRemoveTrailingSlash() throws Exception {
+        assertEquals("http://www.example.com";, 
InitFilter.removeTrailingSlash("http://www.example.com/";));
+        assertEquals("http://www.example.com";, 
InitFilter.removeTrailingSlash("http://www.example.com";));
+    }
+
+}


Reply via email to