e.g. Calendar Search Help
You must enter a value before pressing Search
spring

Class: org.springframework.web.util.UrlPathHelper   ©

 OK to copy?
001 /*
002  * Copyright 2002-2004 the original author or authors.
003  * 
004  * Licensed under the Apache License, Version 2.0 (the "License");
005  * you may not use this file except in compliance with the License.
006  * You may obtain a copy of the License at
007  * 
008  *      http://www.apache.org/licenses/LICENSE-2.0
009  * 
010  * Unless required by applicable law or agreed to in writing, software
011  * distributed under the License is distributed on an "AS IS" BASIS,
012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013  * See the License for the specific language governing permissions and
014  * limitations under the License.
015  */ 
016 
017 package org.springframework.web.util;
018 
019 import java.io.UnsupportedEncodingException;
020 import java.net.URLDecoder;
021 
022 import javax.servlet.http.HttpServletRequest;
023 
024 import org.apache.commons.logging.Log;
025 import org.apache.commons.logging.LogFactory;
026 
027 /**
028  * Helper class for URL path matching. Provides support for URL paths
029  * in ServletDispatcher includes, and support for URL decoding.
030  *
031  * <p>Used by AbstractUrlHandlerMapping, AbstractMethodNameResolver
032  * and RequestContext for path matching and/or URI determination.
033  *
034  * @author Juergen Hoeller
035  * @since 14.01.2004
036  * @see org.springframework.web.servlet.handler.AbstractUrlHandlerMapping
037  * @see org.springframework.web.servlet.mvc.multiaction.AbstractUrlMethodNameResolver
038  * @see org.springframework.web.servlet.support.RequestContext
039  */
040 public class UrlPathHelper {
041 
042     /**
043      * Standard servlet spec request attributes for include URI and paths.
044      * <p>If included via a RequestDispatcher, the current resource will see the
045      * original request. Its own URI and paths are exposed as request attributes.
046      */
047     public static final String INCLUDE_URI_REQUEST_ATTRIBUTE = "javax.servlet.include.request_uri";
048     public static final String INCLUDE_CONTEXT_PATH_REQUEST_ATTRIBUTE = "javax.servlet.include.context_path";
049     public static final String INCLUDE_SERVLET_PATH_REQUEST_ATTRIBUTE = "javax.servlet.include.servlet_path";
050 
051     private final Log logger = LogFactory.getLog(getClass());
052 
053     private boolean alwaysUseFullPath = false;
054 
055     private boolean urlDecode = false;
056 
057     private String defaultEncoding = WebUtils.DEFAULT_CHARACTER_ENCODING;
058 
059 
060     /**
061      * Set if URL lookup should always use full path within current servlet
062      * context. Else, the path within the current servlet mapping is used
063      * if applicable (i.e. in the case of a ".../*" servlet mapping in web.xml).
064      * Default is false.
065      */
066     public void setAlwaysUseFullPath(boolean alwaysUseFullPath) {
067         this.alwaysUseFullPath = alwaysUseFullPath;
068     }
069 
070     /**
071      * Set if context path and request URI should be URL-decoded.
072      * Both are returned <i>undecoded</i> by the Servlet API,
073      * in contrast to the servlet path.
074      * <p>Uses either the request encoding or the default encoding according
075      * to the Servlet spec (ISO-8859-1).
076      * <p>Note: Setting this to true requires J2SE 1.4, as J2SE 1.3's
077      * URLDecoder class does not offer a way to specify the encoding.
078      * @see #getServletPath
079      * @see #getContextPath
080      * @see #getRequestUri
081      * @see WebUtils#DEFAULT_CHARACTER_ENCODING
082      * @see javax.servlet.ServletRequest#getCharacterEncoding
083      * @see java.net.URLDecoder#decode(String, String)
084      */
085     public void setUrlDecode(boolean urlDecode) {
086         this.urlDecode = urlDecode;
087     }
088 
089     /**
090      * Set the default character encoding to use for URL decoding.
091      * Default is ISO-8859-1, according to the Servlet spec.
092      * <p>If the request specifies a character encoding itself, the request
093      * encoding will override this setting. This also allows for generically
094      * overriding the character encoding in a filter that invokes the
095      * ServletRequest.setCharacterEncoding method.
096      * @param defaultEncoding the character encoding to use
097      * @see #determineEncoding
098      * @see javax.servlet.ServletRequest#getCharacterEncoding
099      * @see javax.servlet.ServletRequest#setCharacterEncoding
100      * @see WebUtils#DEFAULT_CHARACTER_ENCODING
101      */
102     public void setDefaultEncoding(String defaultEncoding) {
103         this.defaultEncoding = defaultEncoding;
104     }
105 
106     /**
107      * Return the default character encoding to use for URL decoding.
108      */
109     protected String getDefaultEncoding() {
110         return defaultEncoding;
111     }
112 
113 
114     /**
115      * Return the mapping lookup path for the given request, within the current
116      * servlet mapping if applicable, else within the web application.
117      * <p>Regards include request URL if called within a RequestDispatcher include.
118      * @param request current HTTP request
119      * @return the lookup path
120      * @see #getPathWithinApplication
121      * @see #getPathWithinServletMapping
122      */
123     public String getLookupPathForRequest(HttpServletRequest request) {
124         // Always use full path within current servlet context?
125         if (this.alwaysUseFullPath) {
126             return getPathWithinApplication(request);
127         }
128         // Else, use path within current servlet mapping if applicable
129         String rest = getPathWithinServletMapping(request);
130         if (!"".equals(rest)) {
131             return rest;
132         }
133         else {
134             return getPathWithinApplication(request);
135         }
136     }
137 
138     /**
139      * Return the path within the servlet mapping for the given request,
140      * i.e. the part of the request's URL beyond the part that called the servlet,
141      * or "" if the whole URL has been used to identify the servlet.
142      * <p>Regards include request URL if called within a RequestDispatcher include.
143      * <p>E.g.: servlet mapping = "/test/*"; request URI = "/test/a" -> "/a".
144      * <p>E.g.: servlet mapping = "/test"; request URI = "/test" -> "".
145      * <p>E.g.: servlet mapping = "/*.test"; request URI = "/a.test" -> "".
146      * @param request current HTTP request
147      * @return the path within the servlet mapping, or ""
148      */
149     public String getPathWithinServletMapping(HttpServletRequest request) {
150         String pathWithinApp = getPathWithinApplication(request);
151         String servletPath = getServletPath(request);
152         if (pathWithinApp.startsWith(servletPath)) {
153             // Normal case: URI contains servlet path.
154             return pathWithinApp.substring(servletPath.length());
155         }
156         else {
157             // Special case: URI is different from servlet path.
158             // Can happen e.g. with index page: URI="/", servletPath="/index.html"
159             // Use servlet path in this case, as it indicates the actual target path.
160             return servletPath;
161         }
162     }
163 
164     /**
165      * Return the path within the web application for the given request.
166      * <p>Regards include request URL if called within a RequestDispatcher include.
167      * @param request current HTTP request
168      * @return the path within the web application
169      */
170     public String getPathWithinApplication(HttpServletRequest request) {
171         String contextPath = getContextPath(request);
172         String requestUri = getRequestUri(request);
173         if (requestUri.startsWith(contextPath)) {
174             // Normal case: URI contains context path.
175             return requestUri.substring(contextPath.length());
176         }
177         else {
178             // Special case: rather unusual.
179             return requestUri;
180         }
181     }
182 
183     /**
184      * Return the servlet path for the given request, regarding an include request
185      * URL if called within a RequestDispatcher include.
186      * <p>As the value returned by request.getServletPath() is already decoded by
187      * the servlet container, this method will not attempt to decode it.
188      * @param request current HTTP request
189      * @return the servlet path
190      */
191     public String getServletPath(HttpServletRequest request) {
192         String servletPath = (String) request.getAttribute(INCLUDE_SERVLET_PATH_REQUEST_ATTRIBUTE);
193         if (servletPath == null) {
194             servletPath = request.getServletPath();
195         }
196         return servletPath;
197     }
198 
199     /**
200      * Return the context path for the given request, regarding an include request
201      * URL if called within a RequestDispatcher include.
202      * <p>As the value returned by request.getContextPath() is <i>not</i> decoded by
203      * the servlet container, this method will decode it.
204      * @param request current HTTP request
205      * @return the context path
206      */
207     public String getContextPath(HttpServletRequest request) {
208         String contextPath = (String) request.getAttribute(INCLUDE_CONTEXT_PATH_REQUEST_ATTRIBUTE);
209         if (contextPath == null) {
210             contextPath = request.getContextPath();
211         }
212         return decodeRequestString(request, contextPath);
213     }
214 
215     /**
216      * Return the request URI for the given request, regarding an include request
217      * URL if called within a RequestDispatcher include.
218      * <p>As the value returned by request.getRequestURI() is <i>not</i> decoded by
219      * the servlet container, this method will decode it.
220      * <p>The URI that the web container resolves <i>should</i> be correct, but some
221      * containers like JBoss/Jetty incorrectly include ";" strings like ";jsessionid"
222      * in the URI. This method cuts off such incorrect appendices.
223      * @param request current HTTP request
224      * @return the request URI
225      */
226     public String getRequestUri(HttpServletRequest request) {
227         String uri = (String) request.getAttribute(INCLUDE_URI_REQUEST_ATTRIBUTE);
228         if (uri == null) {
229             uri = request.getRequestURI();
230         }
231         uri = decodeRequestString(request, uri);
232         int semicolonIndex = uri.indexOf(';');
233         return (semicolonIndex != -1 ? uri.substring(0, semicolonIndex) : uri);
234     }
235 
236     /**
237      * Decode the given source string with a URLEncoder. The encoding will be taken
238      * from the request, falling back to the default "ISO-8859-1".
239      * @param request current HTTP request
240      * @param source the String to decode
241      * @return the decoded String
242      * @see WebUtils#DEFAULT_CHARACTER_ENCODING
243      * @see javax.servlet.ServletRequest#getCharacterEncoding
244      * @see java.net.URLDecoder
245      */
246     public String decodeRequestString(HttpServletRequest request, String source) {
247         if (this.urlDecode) {
248             String enc = determineEncoding(request);
249             try {
250                 return URLDecoder.decode(source, enc);
251             }
252             catch (UnsupportedEncodingException ex) {
253                 logger.warn("Could not decode request string [" + source + "] with encoding '" +
254                     enc + "': using platform default");
255                 return URLDecoder.decode(source);
256             }
257         }
258         return source;
259     }
260 
261     /**
262      * Determine the encoding for the given request.
263      * Can be overridden in subclasses.
264      * <p>The default implementation checks the request encoding,
265      * falling back to the default encoding specified for this resolver.
266      * @param request current HTTP request
267      * @return the encoding for the request (never null)
268      * @see javax.servlet.ServletRequest#getCharacterEncoding
269      * @see #setDefaultEncoding
270      */
271     protected String determineEncoding(HttpServletRequest request) {
Rate272         String enc = request.getCharacterEncoding();
273         if (enc == null) {
274             enc = this.defaultEncoding;
275         }
276         return enc;
277     }
278 
279 }

            
All Examples in File:
Example
Line
Rating (found
useful by...)
272 0% of 0