Coverage Report - org.acegisecurity.util.UrlUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
UrlUtils
79% 
100% 
1.429
 
 1  
 /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
 2  
  *
 3  
  * Licensed under the Apache License, Version 2.0 (the "License");
 4  
  * you may not use this file except in compliance with the License.
 5  
  * You may obtain a copy of the License at
 6  
  *
 7  
  *     http://www.apache.org/licenses/LICENSE-2.0
 8  
  *
 9  
  * Unless required by applicable law or agreed to in writing, software
 10  
  * distributed under the License is distributed on an "AS IS" BASIS,
 11  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12  
  * See the License for the specific language governing permissions and
 13  
  * limitations under the License.
 14  
  */
 15  
 
 16  
 package org.acegisecurity.util;
 17  
 
 18  
 import org.acegisecurity.intercept.web.FilterInvocation;
 19  
 
 20  
 import org.acegisecurity.ui.savedrequest.SavedRequest;
 21  
 
 22  
 import javax.servlet.http.HttpServletRequest;
 23  
 
 24  
 
 25  
 /**
 26  
  * Provides static methods for composing URLs.<p>Placed into a separate class for visibility, so that changes to
 27  
  * URL formatting conventions will affect all users.</p>
 28  
  *
 29  
  * @author Ben Alex
 30  
  * @version $Id: UrlUtils.java 1784 2007-02-24 21:00:24Z luke_t $
 31  
  */
 32  
 public final class UrlUtils {
 33  
     //~ Constructors ===================================================================================================
 34  
 
 35  0
     private UrlUtils() {
 36  0
     }
 37  
 
 38  
     //~ Methods ========================================================================================================
 39  
 
 40  
     /**
 41  
      * Obtains the full URL the client used to make the request.<p>Note that the server port will not be shown
 42  
      * if it is the default server port for HTTP or HTTPS (ie 80 and 443 respectively).</p>
 43  
      *
 44  
      * @param scheme DOCUMENT ME!
 45  
      * @param serverName DOCUMENT ME!
 46  
      * @param serverPort DOCUMENT ME!
 47  
      * @param contextPath DOCUMENT ME!
 48  
      * @param requestUrl DOCUMENT ME!
 49  
      * @param servletPath DOCUMENT ME!
 50  
      * @param requestURI DOCUMENT ME!
 51  
      * @param pathInfo DOCUMENT ME!
 52  
      * @param queryString DOCUMENT ME!
 53  
      *
 54  
      * @return the full URL
 55  
      */
 56  
     private static String buildFullRequestUrl(String scheme, String serverName, int serverPort, String contextPath,
 57  
         String requestUrl, String servletPath, String requestURI, String pathInfo, String queryString) {
 58  
 
 59  6
         boolean includePort = true;
 60  
 
 61  6
         if ("http".equals(scheme.toLowerCase()) && (serverPort == 80)) {
 62  5
             includePort = false;
 63  
         }
 64  
 
 65  6
         if ("https".equals(scheme.toLowerCase()) && (serverPort == 443)) {
 66  0
             includePort = false;
 67  
         }
 68  
 
 69  6
         return scheme + "://" + serverName + ((includePort) ? (":" + serverPort) : "") + contextPath
 70  
         + buildRequestUrl(servletPath, requestURI, contextPath, pathInfo, queryString);
 71  
     }
 72  
 
 73  
     /**
 74  
      * Obtains the web application-specific fragment of the URL.
 75  
      *
 76  
      * @param servletPath DOCUMENT ME!
 77  
      * @param requestURI DOCUMENT ME!
 78  
      * @param contextPath DOCUMENT ME!
 79  
      * @param pathInfo DOCUMENT ME!
 80  
      * @param queryString DOCUMENT ME!
 81  
      *
 82  
      * @return the URL, excluding any server name, context path or servlet path
 83  
      */
 84  
     private static String buildRequestUrl(String servletPath, String requestURI, String contextPath, String pathInfo,
 85  
         String queryString) {
 86  
 
 87  38
         String uri = servletPath;
 88  
 
 89  38
         if (uri == null) {
 90  3
             uri = requestURI;
 91  3
             uri = uri.substring(contextPath.length());
 92  
         }
 93  
 
 94  38
         return uri + ((pathInfo == null) ? "" : pathInfo) + ((queryString == null) ? "" : ("?" + queryString));
 95  
     }
 96  
 
 97  
     public static String getFullRequestUrl(FilterInvocation fi) {
 98  3
         HttpServletRequest r = fi.getHttpRequest();
 99  
 
 100  3
         return buildFullRequestUrl(r.getScheme(), r.getServerName(), r.getServerPort(), r.getContextPath(),
 101  
             r.getRequestURL().toString(), r.getServletPath(), r.getRequestURI(), r.getPathInfo(), r.getQueryString());
 102  
     }
 103  
 
 104  
     public static String getFullRequestUrl(SavedRequest sr) {
 105  3
         return buildFullRequestUrl(sr.getScheme(), sr.getServerName(), sr.getServerPort(), sr.getContextPath(),
 106  
             sr.getRequestURL(), sr.getServletPath(), sr.getRequestURI(), sr.getPathInfo(), sr.getQueryString());
 107  
     }
 108  
 
 109  
     public static String getRequestUrl(FilterInvocation fi) {
 110  32
         HttpServletRequest r = fi.getHttpRequest();
 111  
 
 112  32
         return buildRequestUrl(r.getServletPath(), r.getRequestURI(), r.getContextPath(), r.getPathInfo(),
 113  
             r.getQueryString());
 114  
     }
 115  
 
 116  
     public static String getRequestUrl(SavedRequest sr) {
 117  0
         return buildRequestUrl(sr.getServletPath(), sr.getRequestURI(), sr.getContextPath(), sr.getPathInfo(),
 118  
             sr.getQueryString());
 119  
     }
 120  
 }