| 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; | |
| 34 | ||
| 35 | import java.io.IOException; | |
| 36 | import java.net.URLEncoder; | |
| 37 | ||
| 38 | import javax.servlet.RequestDispatcher; | |
| 39 | import javax.servlet.ServletException; | |
| 40 | import javax.servlet.ServletRequest; | |
| 41 | import javax.servlet.ServletResponse; | |
| 42 | import javax.servlet.http.HttpServletRequest; | |
| 43 | import javax.servlet.http.HttpUtils; | |
| 44 | import javax.servlet.jsp.PageContext; | |
| 45 | import javax.servlet.jsp.tagext.BodyTag; | |
| 46 | ||
| 47 | /** | |
| 48 | * Class with utility methods for servlets and JSP. | |
| 49 | * | |
| 50 | * @author <a href="mailto:pfstrack@users.sourceforge.net">Paul Strack</a> | |
| 51 | */ | |
| 52 | 0 | public class ServletUtils { |
| 53 | ||
| 54 | /** The name used to cache errors. Identical to PageContext.EXCEPTION. */ | |
| 55 | public static final String EXCEPTION_CACHE_NAME = PageContext.EXCEPTION; | |
| 56 | ||
| 57 | /** BodyTag.EVAL_BODY_TAG. Defined to cut down on deprecation warnings. */ | |
| 58 | public static final int EVAL_BODY_TAG = BodyTag.EVAL_BODY_TAG; | |
| 59 | ||
| 60 | /** | |
| 61 | * Cached an error in the request.<p> | |
| 62 | * | |
| 63 | * @param request The request. | |
| 64 | * @param error The error. | |
| 65 | */ | |
| 66 | public static void cacheError(HttpServletRequest request, Throwable error) { | |
| 67 | 9 | request.setAttribute(EXCEPTION_CACHE_NAME, error); |
| 68 | 9 | } |
| 69 | ||
| 70 | /** | |
| 71 | * Retrieves a cached error from the request. <p> | |
| 72 | * | |
| 73 | * @param request The request. | |
| 74 | * @return The error. | |
| 75 | */ | |
| 76 | public static Throwable retrieveError(HttpServletRequest request) { | |
| 77 | 477 | return (Throwable) request.getAttribute(EXCEPTION_CACHE_NAME); |
| 78 | } | |
| 79 | ||
| 80 | /** | |
| 81 | * Place holder to deal with deprecated java.net.URLEncoder.encode() in | |
| 82 | * JDK 1.4.<p> | |
| 83 | * | |
| 84 | * @param value The value to encode. | |
| 85 | * @return The encoded value. | |
| 86 | */ | |
| 87 | public static String encode(String value) { | |
| 88 | 19 | return URLEncoder.encode(value); |
| 89 | } | |
| 90 | ||
| 91 | /** | |
| 92 | * Forward to the indicated URI. | |
| 93 | * | |
| 94 | * @param request The request. | |
| 95 | * @param response The response. | |
| 96 | * @param uri The target URI. | |
| 97 | * @throws ServletException For servlet exceptions, or if the URI is not found | |
| 98 | * @throws IOException For IO exceptions. | |
| 99 | */ | |
| 100 | public static void forward( | |
| 101 | ServletRequest request, | |
| 102 | ServletResponse response, | |
| 103 | String uri) | |
| 104 | throws ServletException, IOException { | |
| 105 | ||
| 106 | try { | |
| 107 | 3 | RequestDispatcher dispatcher = request.getRequestDispatcher(uri); |
| 108 | 3 | dispatcher.forward(request, response); |
| 109 | 3 | } catch (NullPointerException ex) { |
| 110 | 0 | throw new ServletException("Could not find URI " + uri); |
| 111 | } | |
| 112 | 3 | } |
| 113 | ||
| 114 | /** | |
| 115 | * Include the indicated URI. | |
| 116 | * | |
| 117 | * @param request The request. | |
| 118 | * @param response The response. | |
| 119 | * @param uri The target URI. | |
| 120 | * @throws ServletException For servlet exceptions, or if the URI is not found | |
| 121 | * @throws IOException For IO exceptions. | |
| 122 | */ | |
| 123 | public static void include( | |
| 124 | ServletRequest request, | |
| 125 | ServletResponse response, | |
| 126 | String uri) | |
| 127 | throws ServletException, IOException { | |
| 128 | ||
| 129 | try { | |
| 130 | 6 | RequestDispatcher dispatcher = request.getRequestDispatcher(uri); |
| 131 | 6 | dispatcher.include(request, response); |
| 132 | 5 | } catch (NullPointerException ex) { |
| 133 | 1 | throw new ServletException("Could not find URI " + uri); |
| 134 | } | |
| 135 | 5 | } |
| 136 | ||
| 137 | /** | |
| 138 | * Get the URL, as per <code>HttpUtils.getRequestURL()</code>, and append | |
| 139 | * the query string. | |
| 140 | * | |
| 141 | * @param request The request. | |
| 142 | * @return The absolute request URL with the query string. | |
| 143 | */ | |
| 144 | public static StringBuffer getURLWithQueryString(HttpServletRequest request) { | |
| 145 | 4 | StringBuffer url = HttpUtils.getRequestURL(request); |
| 146 | 4 | String queryString = request.getQueryString(); |
| 147 | 4 | if ((queryString != null) && (queryString != "")) { |
| 148 | 1 | if (queryString.charAt(0) != '?') { |
| 149 | 1 | url.append('?'); |
| 150 | } | |
| 151 | 1 | url.append(queryString); |
| 152 | } | |
| 153 | 4 | return url; |
| 154 | } | |
| 155 | ||
| 156 | /** | |
| 157 | * Utility method to compensate for the "feature" of PageContext that it | |
| 158 | * throws a NullPointerException when setting a null attribute value. This | |
| 159 | * method call the removeAttribute() method if the value is null. Otherwise | |
| 160 | * it calls setAttribute() as usual. If the key is null, it does nothing. | |
| 161 | * | |
| 162 | * @param context The page context. | |
| 163 | * @param key The key value. | |
| 164 | * @param value The attribute value. | |
| 165 | * @throws NullPointerException If the key is null. | |
| 166 | */ | |
| 167 | public static void setPageAttribute( | |
| 168 | PageContext context, | |
| 169 | String key, | |
| 170 | Object value) | |
| 171 | throws NullPointerException { | |
| 172 | ||
| 173 | 46 | if (value == null) { |
| 174 | 3 | context.removeAttribute(key, PageContext.PAGE_SCOPE); |
| 175 | } else { | |
| 176 | 43 | context.setAttribute(key, value); |
| 177 | } | |
| 178 | 46 | } |
| 179 | } |
|
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |