Coverage details for org.chwf.servlet.filter.ResourceMapper

LineHitsSource
1 /*
2 Chrysalis Web Framework [http://chrysalis.sourceforge.net]
3 Copyright (c) 2002, 2003, 2004, Paul Strack
4  
5 All rights reserved.
6  
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9  
10 1. Redistributions of source code must retain the above copyright notice, this
11 list of conditions and the following disclaimer.
12  
13 2. Redistributions in binary form must reproduce the above copyright notice,
14 this list of conditions and the following disclaimer in the documentation
15 and/or other materials provided with the distribution.
16  
17 3. Neither the name of the copyright holder nor the names of its contributors
18 may be used to endorse or promote products derived from this software without
19 specific prior written permission.
20  
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
22 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
25 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
28 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32  
33 package org.chwf.servlet.filter;
34  
35 import java.io.IOException;
36 import java.util.HashMap;
37 import java.util.Map;
38  
39 import javax.servlet.FilterChain;
40 import javax.servlet.FilterConfig;
41 import javax.servlet.ServletContext;
42 import javax.servlet.ServletException;
43 import javax.servlet.ServletRequest;
44 import javax.servlet.ServletResponse;
45 import javax.servlet.http.HttpServletRequest;
46  
47 import org.chwf.servlet.ServletUtils;
48  
49 /**
50  * <p>Filter that maps one file extension to another file extension. It is
51  * used as a generic terminal filter.</p>
52  *
53  * @author <a href="mailto:pfstrack@users.sourceforge.net">Paul Strack</a>
54  */
552public class ResourceMapper extends FilterSupport {
56  
57   /** Key used to cache the target URI in the request. */
582  public static final String TARGET_URI =
59     ResourceMapper.class.getName() + ":TARGET_URI";
60  
61   /** Resource filter singleton. */
621  private static final ResourceMapper RESOURCE_FILTER = new ResourceMapper();
63  
64   /** Resource filter singleton. */
651  private static final Map MAPPING = new HashMap();
66  
67   /**
68    * Retrieves the request URI with the context-path remove. <p>
69    *
70    * @param request The request.
71    * @return The context-free URI.
72    */
73   public static String getContextFreeURI(HttpServletRequest request) {
7440    String contextPath = request.getContextPath();
7540    return request.getRequestURI().substring(contextPath.length());
76   }
77  
78   /**
79    * Get the map extension for this URI, or null if there is none.
80    *
81    * @param uri The URI being mapped.
82    * @return The map extension for this URI, or null if there is none.
83    */
84   private static String getMappedExtension(String uri) {
859    int dot = uri.lastIndexOf('.');
869    if (dot > 0) {
877      String extension = uri.substring(dot);
887      return (String) MAPPING.get(extension);
89     } else {
902      return null;
91     }
92   }
93  
94   /**
95    * Get the resource mapper.
96    *
97    * @param request The request.
98    * @return The Resource Mapper (a singleton).
99    */
100   public static ResourceMapper getFilter(HttpServletRequest request) {
1012    getTargetURI(request);
1022    return RESOURCE_FILTER;
103   }
104  
105   /**
106    * Retrieve the original context-free URI from the request, if cached.
107    * Otherwise it returns (and caches) the current context-free URI.
108    *
109    * @param request The request.
110    * @return The context-relative URI.
111    */
112   public static String getTargetURI(HttpServletRequest request) {
11311    String uri = (String) request.getAttribute(TARGET_URI);
11411    if (uri == null) {
1157      uri = getContextFreeURI(request);
1167      request.setAttribute(TARGET_URI, uri);
117     }
11811    return uri;
119   }
120  
121   /**
122    * Retrieve the mapped URI, or the original URI if it is not mapped.
123    *
124    * @param request The request.
125    * @return The mapped uri.
126    */
127   public static String getMappedURI(HttpServletRequest request) {
1287    String uri = getTargetURI(request);
1297    String mappedExtension = getMappedExtension(uri);
1307    if (mappedExtension != null) {
1310      int dot = uri.lastIndexOf('.');
1320      return uri.substring(0, dot) + mappedExtension;
133     } else {
1347      return uri;
135     }
136   }
137  
138   /**
139    * Initialize the resource mapping. Loads the resource configuration from the
140    * "WEB-INF/filter.xml" file.
141    *
142    * @param context The servlet context.
143    * @throws ServletException If the filter configuration cannot be loaded.
144    */
145   public static void init(ServletContext context) throws ServletException {
1467    AllFilterConfig allFilterConfig = AllFilterConfig.getConfig(context);
1477    MAPPING.putAll(allFilterConfig.getResourceMapping());
1487  }
149  
150   /**
151    * Initialize the resource mapping. Delegates to the
152    * {@link #init(ServletContext)} method.
153    *
154    * @param config The filter configuration.
155    * @throws ServletException If the filter configuration cannot be loaded.
156    */
157   public void init(FilterConfig config) throws ServletException {
1580    init(config.getServletContext());
1590  }
160  
161   /**
162    * The ResourceMapping invokes to the mapped resource using the
163    * <code>RequestDispatcher.include()</code> method. If the resource is not
164    * mapped, it either (a) generates an error [Servlet 2.2] or (b) invokes
165    * the resource at the end of the filter chain [Servlet 2.3+].
166    *
167    * @param request The request.
168    * @param response The response.
169    * @param chain The filter chain.
170    * @throws ServletException For servlet errors.
171    * @throws IOException For I/O errors.
172    */
173   public void doFilter(
174     ServletRequest request,
175     ServletResponse response,
176     FilterChain chain)
177     throws IOException, ServletException {
178  
1792    String uri = getTargetURI((HttpServletRequest) request);
1802    String mappedExtension = getMappedExtension(uri);
1812    if (mappedExtension != null) {
1822      int dot = uri.lastIndexOf('.');
1832      uri = uri.substring(0, dot) + mappedExtension;
1842      ServletUtils.include(request, response, uri);
185     } else {
1860      int dot = uri.lastIndexOf('.');
1870      if (dot > 0) {
1880        String extension = uri.substring(dot);
1890        throw new ServletException("Unmapped extension '" + extension + "'.");
190       } else {
1910        throw new ServletException("Target resource has no extension.");
192       }
193     }
1942  }
195 }

this report was generated by version 1.0.5 of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.