001: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
002: *
003: * Licensed under the Apache License, Version 2.0 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software
010: * distributed under the License is distributed on an "AS IS" BASIS,
011: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: * See the License for the specific language governing permissions and
013: * limitations under the License.
014: */
015:
016: package org.acegisecurity.util;
017:
018: import org.acegisecurity.intercept.web.FilterInvocation;
019:
020: import org.acegisecurity.ui.savedrequest.SavedRequest;
021:
022: import javax.servlet.http.HttpServletRequest;
023:
024: /**
025: * Provides static methods for composing URLs.<p>Placed into a separate class for visibility, so that changes to
026: * URL formatting conventions will affect all users.</p>
027: *
028: * @author Ben Alex
029: * @version $Id: UrlUtils.java 1784 2007-02-24 21:00:24Z luke_t $
030: */
031: public final class UrlUtils {
032: //~ Constructors ===================================================================================================
033:
034: private UrlUtils() {
035: }
036:
037: //~ Methods ========================================================================================================
038:
039: /**
040: * Obtains the full URL the client used to make the request.<p>Note that the server port will not be shown
041: * if it is the default server port for HTTP or HTTPS (ie 80 and 443 respectively).</p>
042: *
043: * @param scheme DOCUMENT ME!
044: * @param serverName DOCUMENT ME!
045: * @param serverPort DOCUMENT ME!
046: * @param contextPath DOCUMENT ME!
047: * @param requestUrl DOCUMENT ME!
048: * @param servletPath DOCUMENT ME!
049: * @param requestURI DOCUMENT ME!
050: * @param pathInfo DOCUMENT ME!
051: * @param queryString DOCUMENT ME!
052: *
053: * @return the full URL
054: */
055: private static String buildFullRequestUrl(String scheme,
056: String serverName, int serverPort, String contextPath,
057: String requestUrl, String servletPath, String requestURI,
058: String pathInfo, String queryString) {
059:
060: boolean includePort = true;
061:
062: if ("http".equals(scheme.toLowerCase()) && (serverPort == 80)) {
063: includePort = false;
064: }
065:
066: if ("https".equals(scheme.toLowerCase()) && (serverPort == 443)) {
067: includePort = false;
068: }
069:
070: return scheme
071: + "://"
072: + serverName
073: + ((includePort) ? (":" + serverPort) : "")
074: + contextPath
075: + buildRequestUrl(servletPath, requestURI, contextPath,
076: pathInfo, queryString);
077: }
078:
079: /**
080: * Obtains the web application-specific fragment of the URL.
081: *
082: * @param servletPath DOCUMENT ME!
083: * @param requestURI DOCUMENT ME!
084: * @param contextPath DOCUMENT ME!
085: * @param pathInfo DOCUMENT ME!
086: * @param queryString DOCUMENT ME!
087: *
088: * @return the URL, excluding any server name, context path or servlet path
089: */
090: private static String buildRequestUrl(String servletPath,
091: String requestURI, String contextPath, String pathInfo,
092: String queryString) {
093:
094: String uri = servletPath;
095:
096: if (uri == null) {
097: uri = requestURI;
098: uri = uri.substring(contextPath.length());
099: }
100:
101: return uri + ((pathInfo == null) ? "" : pathInfo)
102: + ((queryString == null) ? "" : ("?" + queryString));
103: }
104:
105: public static String getFullRequestUrl(FilterInvocation fi) {
106: HttpServletRequest r = fi.getHttpRequest();
107:
108: return buildFullRequestUrl(r.getScheme(), r.getServerName(), r
109: .getServerPort(), r.getContextPath(), r.getRequestURL()
110: .toString(), r.getServletPath(), r.getRequestURI(), r
111: .getPathInfo(), r.getQueryString());
112: }
113:
114: public static String getFullRequestUrl(SavedRequest sr) {
115: return buildFullRequestUrl(sr.getScheme(), sr.getServerName(),
116: sr.getServerPort(), sr.getContextPath(), sr
117: .getRequestURL(), sr.getServletPath(), sr
118: .getRequestURI(), sr.getPathInfo(), sr
119: .getQueryString());
120: }
121:
122: public static String getRequestUrl(FilterInvocation fi) {
123: HttpServletRequest r = fi.getHttpRequest();
124:
125: return buildRequestUrl(r.getServletPath(), r.getRequestURI(), r
126: .getContextPath(), r.getPathInfo(), r.getQueryString());
127: }
128:
129: public static String getRequestUrl(SavedRequest sr) {
130: return buildRequestUrl(sr.getServletPath(), sr.getRequestURI(),
131: sr.getContextPath(), sr.getPathInfo(), sr
132: .getQueryString());
133: }
134: }
|