|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| AbstractServletContextWrapper.java | 0% | 0% | 0% | 0% |
|
||||||||||||||
| 1 |
/*
|
|
| 2 |
* ====================================================================
|
|
| 3 |
*
|
|
| 4 |
* The Apache Software License, Version 1.1
|
|
| 5 |
*
|
|
| 6 |
* Copyright (c) 2001-2003 The Apache Software Foundation. All rights
|
|
| 7 |
* reserved.
|
|
| 8 |
*
|
|
| 9 |
* Redistribution and use in source and binary forms, with or without
|
|
| 10 |
* modification, are permitted provided that the following conditions
|
|
| 11 |
* are met:
|
|
| 12 |
*
|
|
| 13 |
* 1. Redistributions of source code must retain the above copyright
|
|
| 14 |
* notice, this list of conditions and the following disclaimer.
|
|
| 15 |
*
|
|
| 16 |
* 2. Redistributions in binary form must reproduce the above copyright
|
|
| 17 |
* notice, this list of conditions and the following disclaimer in
|
|
| 18 |
* the documentation and/or other materials provided with the
|
|
| 19 |
* distribution.
|
|
| 20 |
*
|
|
| 21 |
* 3. The end-user documentation included with the redistribution, if
|
|
| 22 |
* any, must include the following acknowlegement:
|
|
| 23 |
* "This product includes software developed by the
|
|
| 24 |
* Apache Software Foundation (http://www.apache.org/)."
|
|
| 25 |
* Alternately, this acknowlegement may appear in the software itself,
|
|
| 26 |
* if and wherever such third-party acknowlegements normally appear.
|
|
| 27 |
*
|
|
| 28 |
* 4. The names "The Jakarta Project", "Cactus" and "Apache Software
|
|
| 29 |
* Foundation" must not be used to endorse or promote products
|
|
| 30 |
* derived from this software without prior written permission. For
|
|
| 31 |
* written permission, please contact apache@apache.org.
|
|
| 32 |
*
|
|
| 33 |
* 5. Products derived from this software may not be called "Apache"
|
|
| 34 |
* nor may "Apache" appear in their names without prior written
|
|
| 35 |
* permission of the Apache Group.
|
|
| 36 |
*
|
|
| 37 |
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
|
| 38 |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
| 39 |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
| 40 |
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
|
| 41 |
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
| 42 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
| 43 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
|
| 44 |
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
| 45 |
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
| 46 |
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
|
| 47 |
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
| 48 |
* SUCH DAMAGE.
|
|
| 49 |
* ====================================================================
|
|
| 50 |
*
|
|
| 51 |
* This software consists of voluntary contributions made by many
|
|
| 52 |
* individuals on behalf of the Apache Software Foundation. For more
|
|
| 53 |
* information on the Apache Software Foundation, please see
|
|
| 54 |
* <http://www.apache.org/>.
|
|
| 55 |
*
|
|
| 56 |
*/
|
|
| 57 |
package org.apache.cactus.server;
|
|
| 58 |
|
|
| 59 |
import java.io.InputStream;
|
|
| 60 |
|
|
| 61 |
import java.net.MalformedURLException;
|
|
| 62 |
import java.net.URL;
|
|
| 63 |
|
|
| 64 |
import java.util.Enumeration;
|
|
| 65 |
import java.util.Vector;
|
|
| 66 |
|
|
| 67 |
import javax.servlet.RequestDispatcher;
|
|
| 68 |
import javax.servlet.Servlet;
|
|
| 69 |
import javax.servlet.ServletContext;
|
|
| 70 |
import javax.servlet.ServletException;
|
|
| 71 |
|
|
| 72 |
/**
|
|
| 73 |
* Abstract wrapper around <code>ServletContext</code>. This class provides
|
|
| 74 |
* a common implementation of the wrapper for the different servlet API. In
|
|
| 75 |
* addition to implementing the <code>ServletContext</code> interface it
|
|
| 76 |
* provides additional features helpful for writing unit tests. More
|
|
| 77 |
* specifically the <code>getRequestDispatcher()</code> method is overrided
|
|
| 78 |
* to return an request dispatcher wrapper. In addition logs generated by
|
|
| 79 |
* calls to the <code>log()</code> methods can be retrieved and asserted by
|
|
| 80 |
* calling the <code>getLogs()</code> method.
|
|
| 81 |
*
|
|
| 82 |
* @author <a href="mailto:vmassol@apache.org">Vincent Massol</a>
|
|
| 83 |
*
|
|
| 84 |
* @version $Id: AbstractServletContextWrapper.java,v 1.9 2003/05/26 11:45:22 cmlenz Exp $
|
|
| 85 |
*/
|
|
| 86 |
public abstract class AbstractServletContextWrapper implements ServletContext |
|
| 87 |
{
|
|
| 88 |
/**
|
|
| 89 |
* The original servlet context object
|
|
| 90 |
*/
|
|
| 91 |
protected ServletContext originalContext;
|
|
| 92 |
|
|
| 93 |
/**
|
|
| 94 |
* The logs resulting from calling the <code>log()</code> methods
|
|
| 95 |
*/
|
|
| 96 |
private Vector logs = new Vector(); |
|
| 97 |
|
|
| 98 |
// Interface methods ---------------------------------------------------
|
|
| 99 |
|
|
| 100 |
/**
|
|
| 101 |
* @param theOriginalContext the original servlet context object
|
|
| 102 |
*/
|
|
| 103 | 0 |
public AbstractServletContextWrapper(ServletContext theOriginalContext)
|
| 104 |
{
|
|
| 105 | 0 |
this.originalContext = theOriginalContext;
|
| 106 |
} |
|
| 107 |
|
|
| 108 |
// New methods ---------------------------------------------------------
|
|
| 109 |
|
|
| 110 |
/**
|
|
| 111 |
* Returns all the text logs that have been generated using the
|
|
| 112 |
* <code>log()</code> methods so that it is possible to easily assert the
|
|
| 113 |
* content of the logs. This method does not return the exceptions or
|
|
| 114 |
* throwable sent for logging; it only returns the messages.
|
|
| 115 |
*
|
|
| 116 |
* @return the logs as a vector of strings (each string contains the
|
|
| 117 |
* message that was sent for logging).
|
|
| 118 |
*/
|
|
| 119 | 0 |
public Vector getLogs()
|
| 120 |
{
|
|
| 121 | 0 |
return this.logs; |
| 122 |
} |
|
| 123 |
|
|
| 124 |
/**
|
|
| 125 |
* @see ServletContext#setAttribute(String, Object)
|
|
| 126 |
*/
|
|
| 127 | 0 |
public void setAttribute(String theName, Object theAttribute) |
| 128 |
{
|
|
| 129 | 0 |
this.originalContext.setAttribute(theName, theAttribute);
|
| 130 |
} |
|
| 131 |
|
|
| 132 |
/**
|
|
| 133 |
* @see ServletContext#removeAttribute(String)
|
|
| 134 |
*/
|
|
| 135 | 0 |
public void removeAttribute(String theName) |
| 136 |
{
|
|
| 137 | 0 |
this.originalContext.removeAttribute(theName);
|
| 138 |
} |
|
| 139 |
|
|
| 140 |
/**
|
|
| 141 |
* Intercept the log call and add the message to an internal vector of
|
|
| 142 |
* log messages that can then later be retrieved and asserted by the
|
|
| 143 |
* test case writer. Note that the throwable is not saved.
|
|
| 144 |
*
|
|
| 145 |
* @param theMessage a <code>String</code> that describes the error or
|
|
| 146 |
* exception
|
|
| 147 |
* @param theCause the <code>Throwable</code> error or exception
|
|
| 148 |
*
|
|
| 149 |
* @see #getLogs()
|
|
| 150 |
* @see ServletContext#log(String, Throwable)
|
|
| 151 |
*/
|
|
| 152 | 0 |
public void log(String theMessage, Throwable theCause) |
| 153 |
{
|
|
| 154 | 0 |
if (theMessage != null) |
| 155 |
{
|
|
| 156 | 0 |
this.logs.addElement(theMessage);
|
| 157 |
} |
|
| 158 |
|
|
| 159 | 0 |
this.originalContext.log(theMessage, theCause);
|
| 160 |
} |
|
| 161 |
|
|
| 162 |
/**
|
|
| 163 |
* Intercept the log call and add the message to an internal vector of
|
|
| 164 |
* log messages that can then later be retrieved and asserted by the
|
|
| 165 |
* test case writer. Note that the throwable is not saved.
|
|
| 166 |
*
|
|
| 167 |
* @param theMessage a <code>String</code> that describes the error or
|
|
| 168 |
* exception
|
|
| 169 |
*
|
|
| 170 |
* @see #getLogs()
|
|
| 171 |
* @see ServletContext#log(String)
|
|
| 172 |
*/
|
|
| 173 | 0 |
public void log(String theMessage) |
| 174 |
{
|
|
| 175 | 0 |
if (theMessage != null) |
| 176 |
{
|
|
| 177 | 0 |
this.logs.addElement(theMessage);
|
| 178 |
} |
|
| 179 |
|
|
| 180 | 0 |
this.originalContext.log(theMessage);
|
| 181 |
} |
|
| 182 |
|
|
| 183 |
/**
|
|
| 184 |
* Intercept the log call and add the message to an internal vector of
|
|
| 185 |
* log messages that can then later be retrieved and asserted by the
|
|
| 186 |
* test case writer. Note that the throwable is not saved.
|
|
| 187 |
*
|
|
| 188 |
* @param theException the exception to log
|
|
| 189 |
* @param theMessage a <code>String</code> that describes the error or
|
|
| 190 |
* exception
|
|
| 191 |
*
|
|
| 192 |
* @see #getLogs()
|
|
| 193 |
* @see ServletContext#log(Exception, String)
|
|
| 194 |
*
|
|
| 195 |
* @deprecated As of Java Servlet API 2.1, use
|
|
| 196 |
* {@link #log(String message, Throwable throwable)} instead.
|
|
| 197 |
* This method was originally defined to write an exception's
|
|
| 198 |
* stack trace and an explanatory error message to the servlet
|
|
| 199 |
* log file.
|
|
| 200 |
*/
|
|
| 201 | 0 |
public void log(Exception theException, String theMessage) |
| 202 |
{
|
|
| 203 | 0 |
if (theMessage != null) |
| 204 |
{
|
|
| 205 | 0 |
this.logs.addElement(theMessage);
|
| 206 |
} |
|
| 207 |
|
|
| 208 | 0 |
this.originalContext.log(theException, theMessage);
|
| 209 |
} |
|
| 210 |
|
|
| 211 |
/**
|
|
| 212 |
* @see ServletContext#getServlets()
|
|
| 213 |
*/
|
|
| 214 | 0 |
public Enumeration getServlets()
|
| 215 |
{
|
|
| 216 | 0 |
return this.originalContext.getServlets(); |
| 217 |
} |
|
| 218 |
|
|
| 219 |
/**
|
|
| 220 |
* @see ServletContext#getServletNames()
|
|
| 221 |
*/
|
|
| 222 | 0 |
public Enumeration getServletNames()
|
| 223 |
{
|
|
| 224 | 0 |
return this.originalContext.getServletNames(); |
| 225 |
} |
|
| 226 |
|
|
| 227 |
/**
|
|
| 228 |
* @see ServletContext#getServlet(String)
|
|
| 229 |
*/
|
|
| 230 | 0 |
public Servlet getServlet(String theName) throws ServletException |
| 231 |
{
|
|
| 232 | 0 |
return this.originalContext.getServlet(theName); |
| 233 |
} |
|
| 234 |
|
|
| 235 |
/**
|
|
| 236 |
* @see ServletContext#getServerInfo()
|
|
| 237 |
*/
|
|
| 238 | 0 |
public String getServerInfo()
|
| 239 |
{
|
|
| 240 | 0 |
return this.originalContext.getServerInfo(); |
| 241 |
} |
|
| 242 |
|
|
| 243 |
/**
|
|
| 244 |
* @see ServletContext#getResourceAsStream(String)
|
|
| 245 |
*/
|
|
| 246 | 0 |
public InputStream getResourceAsStream(String thePath)
|
| 247 |
{
|
|
| 248 | 0 |
return this.originalContext.getResourceAsStream(thePath); |
| 249 |
} |
|
| 250 |
|
|
| 251 |
/**
|
|
| 252 |
* @see ServletContext#getResource(String)
|
|
| 253 |
*/
|
|
| 254 | 0 |
public URL getResource(String thePath) throws MalformedURLException |
| 255 |
{
|
|
| 256 | 0 |
return this.originalContext.getResource(thePath); |
| 257 |
} |
|
| 258 |
|
|
| 259 |
/**
|
|
| 260 |
* @param thePath a string specifying the pathname to the resource
|
|
| 261 |
* @return our request dispatcher wrapper
|
|
| 262 |
* @see ServletContext#getRequestDispatcher(String)
|
|
| 263 |
*/
|
|
| 264 | 0 |
public RequestDispatcher getRequestDispatcher(String thePath)
|
| 265 |
{
|
|
| 266 | 0 |
RequestDispatcher wrappedDispatcher = null;
|
| 267 |
|
|
| 268 | 0 |
RequestDispatcher originalDispatcher = |
| 269 |
this.originalContext.getRequestDispatcher(thePath);
|
|
| 270 |
|
|
| 271 | 0 |
if (originalDispatcher != null) |
| 272 |
{
|
|
| 273 | 0 |
wrappedDispatcher = |
| 274 |
new RequestDispatcherWrapper(originalDispatcher);
|
|
| 275 |
} |
|
| 276 |
|
|
| 277 | 0 |
return wrappedDispatcher;
|
| 278 |
} |
|
| 279 |
|
|
| 280 |
/**
|
|
| 281 |
* @param theName a string specifying the name of a servlet to wrap
|
|
| 282 |
* @return our request dispatcher wrapper or null if the servlet cannot
|
|
| 283 |
* be found.
|
|
| 284 |
* @see ServletContext#getNamedDispatcher(String)
|
|
| 285 |
*/
|
|
| 286 | 0 |
public RequestDispatcher getNamedDispatcher(String theName)
|
| 287 |
{
|
|
| 288 | 0 |
RequestDispatcher wrappedDispatcher = null;
|
| 289 |
|
|
| 290 | 0 |
RequestDispatcher originalDispatcher = |
| 291 |
this.originalContext.getNamedDispatcher(theName);
|
|
| 292 |
|
|
| 293 | 0 |
if (originalDispatcher != null) |
| 294 |
{
|
|
| 295 | 0 |
wrappedDispatcher = |
| 296 |
new RequestDispatcherWrapper(originalDispatcher);
|
|
| 297 |
} |
|
| 298 |
|
|
| 299 | 0 |
return wrappedDispatcher;
|
| 300 |
} |
|
| 301 |
|
|
| 302 |
/**
|
|
| 303 |
* @see ServletContext#getRealPath(String)
|
|
| 304 |
*/
|
|
| 305 | 0 |
public String getRealPath(String thePath)
|
| 306 |
{
|
|
| 307 | 0 |
return this.originalContext.getRealPath(thePath); |
| 308 |
} |
|
| 309 |
|
|
| 310 |
/**
|
|
| 311 |
* @see ServletContext#getMinorVersion()
|
|
| 312 |
*/
|
|
| 313 | 0 |
public int getMinorVersion() |
| 314 |
{
|
|
| 315 | 0 |
return this.originalContext.getMinorVersion(); |
| 316 |
} |
|
| 317 |
|
|
| 318 |
/**
|
|
| 319 |
* @see ServletContext#getMimeType(String)
|
|
| 320 |
*/
|
|
| 321 | 0 |
public String getMimeType(String theFilename)
|
| 322 |
{
|
|
| 323 | 0 |
return this.originalContext.getMimeType(theFilename); |
| 324 |
} |
|
| 325 |
|
|
| 326 |
/**
|
|
| 327 |
* @see ServletContext#getMajorVersion()
|
|
| 328 |
*/
|
|
| 329 | 0 |
public int getMajorVersion() |
| 330 |
{
|
|
| 331 | 0 |
return this.originalContext.getMajorVersion(); |
| 332 |
} |
|
| 333 |
|
|
| 334 |
/**
|
|
| 335 |
* @see ServletContext#getInitParameterNames()
|
|
| 336 |
*/
|
|
| 337 | 0 |
public Enumeration getInitParameterNames()
|
| 338 |
{
|
|
| 339 | 0 |
return this.originalContext.getInitParameterNames(); |
| 340 |
} |
|
| 341 |
|
|
| 342 |
/**
|
|
| 343 |
* @see ServletContext#getInitParameter(String)
|
|
| 344 |
*/
|
|
| 345 | 0 |
public String getInitParameter(String theName)
|
| 346 |
{
|
|
| 347 | 0 |
return this.originalContext.getInitParameter(theName); |
| 348 |
} |
|
| 349 |
|
|
| 350 |
/**
|
|
| 351 |
* @param theUripath a String specifying the context path of another web
|
|
| 352 |
* application in the container
|
|
| 353 |
* @return our servlet context wrapper
|
|
| 354 |
* @see ServletContext#getContext(String)
|
|
| 355 |
*/
|
|
| 356 | 0 |
public ServletContext getContext(String theUripath)
|
| 357 |
{
|
|
| 358 | 0 |
ServletContext context = new ServletContextWrapper(
|
| 359 |
this.originalContext.getContext(theUripath));
|
|
| 360 |
|
|
| 361 | 0 |
return context;
|
| 362 |
} |
|
| 363 |
|
|
| 364 |
/**
|
|
| 365 |
* @see ServletContext#getAttributeNames()
|
|
| 366 |
*/
|
|
| 367 | 0 |
public Enumeration getAttributeNames()
|
| 368 |
{
|
|
| 369 | 0 |
return this.originalContext.getAttributeNames(); |
| 370 |
} |
|
| 371 |
|
|
| 372 |
/**
|
|
| 373 |
* @see ServletContext#getAttribute(String)
|
|
| 374 |
*/
|
|
| 375 | 0 |
public Object getAttribute(String theName)
|
| 376 |
{
|
|
| 377 | 0 |
return this.originalContext.getAttribute(theName); |
| 378 |
} |
|
| 379 |
} |
|
| 380 |
|
|
||||||||||