| Line | Hits | Source |
|---|---|---|
| 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 | */ | |
| 55 | 2 | public class ResourceMapper extends FilterSupport { |
| 56 | ||
| 57 | /** Key used to cache the target URI in the request. */ | |
| 58 | 2 | public static final String TARGET_URI = |
| 59 | ResourceMapper.class.getName() + ":TARGET_URI"; | |
| 60 | ||
| 61 | /** Resource filter singleton. */ | |
| 62 | 1 | private static final ResourceMapper RESOURCE_FILTER = new ResourceMapper(); |
| 63 | ||
| 64 | /** Resource filter singleton. */ | |
| 65 | 1 | 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) { | |
| 74 | 40 | String contextPath = request.getContextPath(); |
| 75 | 40 | 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) { | |
| 85 | 9 | int dot = uri.lastIndexOf('.'); |
| 86 | 9 | if (dot > 0) { |
| 87 | 7 | String extension = uri.substring(dot); |
| 88 | 7 | return (String) MAPPING.get(extension); |
| 89 | } else { | |
| 90 | 2 | 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) { | |
| 101 | 2 | getTargetURI(request); |
| 102 | 2 | 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) { | |
| 113 | 11 | String uri = (String) request.getAttribute(TARGET_URI); |
| 114 | 11 | if (uri == null) { |
| 115 | 7 | uri = getContextFreeURI(request); |
| 116 | 7 | request.setAttribute(TARGET_URI, uri); |
| 117 | } | |
| 118 | 11 | 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) { | |
| 128 | 7 | String uri = getTargetURI(request); |
| 129 | 7 | String mappedExtension = getMappedExtension(uri); |
| 130 | 7 | if (mappedExtension != null) { |
| 131 | 0 | int dot = uri.lastIndexOf('.'); |
| 132 | 0 | return uri.substring(0, dot) + mappedExtension; |
| 133 | } else { | |
| 134 | 7 | 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 { | |
| 146 | 7 | AllFilterConfig allFilterConfig = AllFilterConfig.getConfig(context); |
| 147 | 7 | MAPPING.putAll(allFilterConfig.getResourceMapping()); |
| 148 | 7 | } |
| 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 { | |
| 158 | 0 | init(config.getServletContext()); |
| 159 | 0 | } |
| 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 | ||
| 179 | 2 | String uri = getTargetURI((HttpServletRequest) request); |
| 180 | 2 | String mappedExtension = getMappedExtension(uri); |
| 181 | 2 | if (mappedExtension != null) { |
| 182 | 2 | int dot = uri.lastIndexOf('.'); |
| 183 | 2 | uri = uri.substring(0, dot) + mappedExtension; |
| 184 | 2 | ServletUtils.include(request, response, uri); |
| 185 | } else { | |
| 186 | 0 | int dot = uri.lastIndexOf('.'); |
| 187 | 0 | if (dot > 0) { |
| 188 | 0 | String extension = uri.substring(dot); |
| 189 | 0 | throw new ServletException("Unmapped extension '" + extension + "'."); |
| 190 | } else { | |
| 191 | 0 | throw new ServletException("Target resource has no extension."); |
| 192 | } | |
| 193 | } | |
| 194 | 2 | } |
| 195 | } |
|
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |