|
|||||||||||||||||||
| 30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| AbstractServletContextWrapper.java | 11.1% | 18.2% | 11.5% | 15.2% |
|
||||||||||||||
| 1 |
/*
|
|
| 2 |
* ========================================================================
|
|
| 3 |
*
|
|
| 4 |
* Copyright 2001-2004 The Apache Software Foundation.
|
|
| 5 |
*
|
|
| 6 |
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
| 7 |
* you may not use this file except in compliance with the License.
|
|
| 8 |
* You may obtain a copy of the License at
|
|
| 9 |
*
|
|
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
| 11 |
*
|
|
| 12 |
* Unless required by applicable law or agreed to in writing, software
|
|
| 13 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
| 14 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
| 15 |
* See the License for the specific language governing permissions and
|
|
| 16 |
* limitations under the License.
|
|
| 17 |
*
|
|
| 18 |
* ========================================================================
|
|
| 19 |
*/
|
|
| 20 |
package org.apache.cactus.server;
|
|
| 21 |
|
|
| 22 |
import java.io.InputStream;
|
|
| 23 |
|
|
| 24 |
import java.net.MalformedURLException;
|
|
| 25 |
import java.net.URL;
|
|
| 26 |
|
|
| 27 |
import java.util.Enumeration;
|
|
| 28 |
import java.util.Hashtable;
|
|
| 29 |
import java.util.Vector;
|
|
| 30 |
|
|
| 31 |
import javax.servlet.RequestDispatcher;
|
|
| 32 |
import javax.servlet.Servlet;
|
|
| 33 |
import javax.servlet.ServletContext;
|
|
| 34 |
import javax.servlet.ServletException;
|
|
| 35 |
|
|
| 36 |
/**
|
|
| 37 |
* Abstract wrapper around <code>ServletContext</code>. This class provides
|
|
| 38 |
* a common implementation of the wrapper for the different servlet API. In
|
|
| 39 |
* addition to implementing the <code>ServletContext</code> interface it
|
|
| 40 |
* provides additional features helpful for writing unit tests. More
|
|
| 41 |
* specifically the <code>getRequestDispatcher()</code> method is overrided
|
|
| 42 |
* to return an request dispatcher wrapper. In addition logs generated by
|
|
| 43 |
* calls to the <code>log()</code> methods can be retrieved and asserted by
|
|
| 44 |
* calling the <code>getLogs()</code> method.
|
|
| 45 |
*
|
|
| 46 |
* @version $Id: AbstractServletContextWrapper.java 239054 2004-10-24 01:30:23Z felipeal $
|
|
| 47 |
*/
|
|
| 48 |
public abstract class AbstractServletContextWrapper implements ServletContext |
|
| 49 |
{
|
|
| 50 |
/**
|
|
| 51 |
* The original servlet context object
|
|
| 52 |
*/
|
|
| 53 |
protected ServletContext originalContext;
|
|
| 54 |
|
|
| 55 |
/**
|
|
| 56 |
* List of parameters set using the <code>setInitParameter()</code> method.
|
|
| 57 |
*/
|
|
| 58 |
protected Hashtable initParameters;
|
|
| 59 |
|
|
| 60 |
/**
|
|
| 61 |
* The logs resulting from calling the <code>log()</code> methods
|
|
| 62 |
*/
|
|
| 63 |
private Vector logs = new Vector(); |
|
| 64 |
|
|
| 65 |
// Constructors -------------------------------------------------------
|
|
| 66 |
|
|
| 67 |
/**
|
|
| 68 |
* @param theOriginalContext the original servlet context object
|
|
| 69 |
*/
|
|
| 70 | 3 |
public AbstractServletContextWrapper(ServletContext theOriginalContext)
|
| 71 |
{
|
|
| 72 | 3 |
this.originalContext = theOriginalContext;
|
| 73 | 3 |
this.initParameters = new Hashtable(); |
| 74 |
} |
|
| 75 |
|
|
| 76 |
// New methods ---------------------------------------------------------
|
|
| 77 |
|
|
| 78 |
/**
|
|
| 79 |
* @return the original unmodified config object
|
|
| 80 |
* @since 1.6
|
|
| 81 |
*/
|
|
| 82 | 0 |
public ServletContext getOriginalContext()
|
| 83 |
{
|
|
| 84 | 0 |
return this.originalContext; |
| 85 |
} |
|
| 86 |
|
|
| 87 |
/**
|
|
| 88 |
* Sets a parameter as if it were set in the <code>web.xml</code> file
|
|
| 89 |
* (using the <context-param> element).
|
|
| 90 |
*
|
|
| 91 |
* @param theName the parameter's name
|
|
| 92 |
* @param theValue the parameter's value
|
|
| 93 |
*/
|
|
| 94 | 0 |
public void setInitParameter(String theName, String theValue) |
| 95 |
{
|
|
| 96 | 0 |
this.initParameters.put(theName, theValue);
|
| 97 |
} |
|
| 98 |
|
|
| 99 |
/**
|
|
| 100 |
* Returns all the text logs that have been generated using the
|
|
| 101 |
* <code>log()</code> methods so that it is possible to easily assert the
|
|
| 102 |
* content of the logs. This method does not return the exceptions or
|
|
| 103 |
* throwable sent for logging; it only returns the messages.
|
|
| 104 |
*
|
|
| 105 |
* @return the logs as a vector of strings (each string contains the
|
|
| 106 |
* message that was sent for logging).
|
|
| 107 |
*/
|
|
| 108 | 0 |
public Vector getLogs()
|
| 109 |
{
|
|
| 110 | 0 |
return this.logs; |
| 111 |
} |
|
| 112 |
|
|
| 113 |
// Overridden methods --------------------------------------------------
|
|
| 114 |
|
|
| 115 |
/**
|
|
| 116 |
* @see ServletContext#setAttribute(String, Object)
|
|
| 117 |
*/
|
|
| 118 | 0 |
public void setAttribute(String theName, Object theAttribute) |
| 119 |
{
|
|
| 120 | 0 |
this.originalContext.setAttribute(theName, theAttribute);
|
| 121 |
} |
|
| 122 |
|
|
| 123 |
/**
|
|
| 124 |
* @see ServletContext#removeAttribute(String)
|
|
| 125 |
*/
|
|
| 126 | 0 |
public void removeAttribute(String theName) |
| 127 |
{
|
|
| 128 | 0 |
this.originalContext.removeAttribute(theName);
|
| 129 |
} |
|
| 130 |
|
|
| 131 |
/**
|
|
| 132 |
* Intercept the log call and add the message to an internal vector of
|
|
| 133 |
* log messages that can then later be retrieved and asserted by the
|
|
| 134 |
* test case writer. Note that the throwable is not saved.
|
|
| 135 |
*
|
|
| 136 |
* @param theMessage a <code>String</code> that describes the error or
|
|
| 137 |
* exception
|
|
| 138 |
* @param theCause the <code>Throwable</code> error or exception
|
|
| 139 |
*
|
|
| 140 |
* @see #getLogs()
|
|
| 141 |
* @see ServletContext#log(String, Throwable)
|
|
| 142 |
*/
|
|
| 143 | 0 |
public void log(String theMessage, Throwable theCause) |
| 144 |
{
|
|
| 145 | 0 |
if (theMessage != null) |
| 146 |
{
|
|
| 147 | 0 |
this.logs.addElement(theMessage);
|
| 148 |
} |
|
| 149 |
|
|
| 150 | 0 |
this.originalContext.log(theMessage, theCause);
|
| 151 |
} |
|
| 152 |
|
|
| 153 |
/**
|
|
| 154 |
* Intercept the log call and add the message to an internal vector of
|
|
| 155 |
* log messages that can then later be retrieved and asserted by the
|
|
| 156 |
* test case writer. Note that the throwable is not saved.
|
|
| 157 |
*
|
|
| 158 |
* @param theMessage a <code>String</code> that describes the error or
|
|
| 159 |
* exception
|
|
| 160 |
*
|
|
| 161 |
* @see #getLogs()
|
|
| 162 |
* @see ServletContext#log(String)
|
|
| 163 |
*/
|
|
| 164 | 1 |
public void log(String theMessage) |
| 165 |
{
|
|
| 166 | 1 |
if (theMessage != null) |
| 167 |
{
|
|
| 168 | 1 |
this.logs.addElement(theMessage);
|
| 169 |
} |
|
| 170 |
|
|
| 171 | 1 |
this.originalContext.log(theMessage);
|
| 172 |
} |
|
| 173 |
|
|
| 174 |
/**
|
|
| 175 |
* Intercept the log call and add the message to an internal vector of
|
|
| 176 |
* log messages that can then later be retrieved and asserted by the
|
|
| 177 |
* test case writer. Note that the throwable is not saved.
|
|
| 178 |
*
|
|
| 179 |
* @param theException the exception to log
|
|
| 180 |
* @param theMessage a <code>String</code> that describes the error or
|
|
| 181 |
* exception
|
|
| 182 |
*
|
|
| 183 |
* @see #getLogs()
|
|
| 184 |
* @see ServletContext#log(Exception, String)
|
|
| 185 |
*
|
|
| 186 |
* @deprecated As of Java Servlet API 2.1, use
|
|
| 187 |
* {@link #log(String message, Throwable throwable)} instead.
|
|
| 188 |
* This method was originally defined to write an exception's
|
|
| 189 |
* stack trace and an explanatory error message to the servlet
|
|
| 190 |
* log file.
|
|
| 191 |
*/
|
|
| 192 | 0 |
public void log(Exception theException, String theMessage) |
| 193 |
{
|
|
| 194 | 0 |
if (theMessage != null) |
| 195 |
{
|
|
| 196 | 0 |
this.logs.addElement(theMessage);
|
| 197 |
} |
|
| 198 |
|
|
| 199 | 0 |
this.originalContext.log(theException, theMessage);
|
| 200 |
} |
|
| 201 |
|
|
| 202 |
/**
|
|
| 203 |
* @see ServletContext#getServlets()
|
|
| 204 |
*/
|
|
| 205 | 0 |
public Enumeration getServlets()
|
| 206 |
{
|
|
| 207 | 0 |
return this.originalContext.getServlets(); |
| 208 |
} |
|
| 209 |
|
|
| 210 |
/**
|
|
| 211 |
* @see ServletContext#getServletNames()
|
|
| 212 |
*/
|
|
| 213 | 0 |
public Enumeration getServletNames()
|
| 214 |
{
|
|
| 215 | 0 |
return this.originalContext.getServletNames(); |
| 216 |
} |
|
| 217 |
|
|
| 218 |
/**
|
|
| 219 |
* @see ServletContext#getServlet(String)
|
|
| 220 |
*/
|
|
| 221 | 0 |
public Servlet getServlet(String theName) throws ServletException |
| 222 |
{
|
|
| 223 | 0 |
return this.originalContext.getServlet(theName); |
| 224 |
} |
|
| 225 |
|
|
| 226 |
/**
|
|
| 227 |
* @see ServletContext#getServerInfo()
|
|
| 228 |
*/
|
|
| 229 | 0 |
public String getServerInfo()
|
| 230 |
{
|
|
| 231 | 0 |
return this.originalContext.getServerInfo(); |
| 232 |
} |
|
| 233 |
|
|
| 234 |
/**
|
|
| 235 |
* @see ServletContext#getResourceAsStream(String)
|
|
| 236 |
*/
|
|
| 237 | 0 |
public InputStream getResourceAsStream(String thePath)
|
| 238 |
{
|
|
| 239 | 0 |
return this.originalContext.getResourceAsStream(thePath); |
| 240 |
} |
|
| 241 |
|
|
| 242 |
/**
|
|
| 243 |
* @see ServletContext#getResource(String)
|
|
| 244 |
*/
|
|
| 245 | 0 |
public URL getResource(String thePath) throws MalformedURLException |
| 246 |
{
|
|
| 247 | 0 |
return this.originalContext.getResource(thePath); |
| 248 |
} |
|
| 249 |
|
|
| 250 |
/**
|
|
| 251 |
* @param thePath a string specifying the pathname to the resource
|
|
| 252 |
* @return our request dispatcher wrapper
|
|
| 253 |
* @see ServletContext#getRequestDispatcher(String)
|
|
| 254 |
*/
|
|
| 255 | 2 |
public RequestDispatcher getRequestDispatcher(String thePath)
|
| 256 |
{
|
|
| 257 | 2 |
RequestDispatcher wrappedDispatcher = null;
|
| 258 |
|
|
| 259 | 2 |
RequestDispatcher originalDispatcher = |
| 260 |
this.originalContext.getRequestDispatcher(thePath);
|
|
| 261 |
|
|
| 262 | 2 |
if (originalDispatcher != null) |
| 263 |
{
|
|
| 264 | 2 |
wrappedDispatcher = |
| 265 |
new RequestDispatcherWrapper(originalDispatcher);
|
|
| 266 |
} |
|
| 267 |
|
|
| 268 | 2 |
return wrappedDispatcher;
|
| 269 |
} |
|
| 270 |
|
|
| 271 |
/**
|
|
| 272 |
* @param theName a string specifying the name of a servlet to wrap
|
|
| 273 |
* @return our request dispatcher wrapper or null if the servlet cannot
|
|
| 274 |
* be found.
|
|
| 275 |
* @see ServletContext#getNamedDispatcher(String)
|
|
| 276 |
*/
|
|
| 277 | 0 |
public RequestDispatcher getNamedDispatcher(String theName)
|
| 278 |
{
|
|
| 279 | 0 |
RequestDispatcher wrappedDispatcher = null;
|
| 280 |
|
|
| 281 | 0 |
RequestDispatcher originalDispatcher = |
| 282 |
this.originalContext.getNamedDispatcher(theName);
|
|
| 283 |
|
|
| 284 | 0 |
if (originalDispatcher != null) |
| 285 |
{
|
|
| 286 | 0 |
wrappedDispatcher = |
| 287 |
new RequestDispatcherWrapper(originalDispatcher);
|
|
| 288 |
} |
|
| 289 |
|
|
| 290 | 0 |
return wrappedDispatcher;
|
| 291 |
} |
|
| 292 |
|
|
| 293 |
/**
|
|
| 294 |
* @see ServletContext#getRealPath(String)
|
|
| 295 |
*/
|
|
| 296 | 0 |
public String getRealPath(String thePath)
|
| 297 |
{
|
|
| 298 | 0 |
return this.originalContext.getRealPath(thePath); |
| 299 |
} |
|
| 300 |
|
|
| 301 |
/**
|
|
| 302 |
* @see ServletContext#getMinorVersion()
|
|
| 303 |
*/
|
|
| 304 | 0 |
public int getMinorVersion() |
| 305 |
{
|
|
| 306 | 0 |
return this.originalContext.getMinorVersion(); |
| 307 |
} |
|
| 308 |
|
|
| 309 |
/**
|
|
| 310 |
* @see ServletContext#getMimeType(String)
|
|
| 311 |
*/
|
|
| 312 | 0 |
public String getMimeType(String theFilename)
|
| 313 |
{
|
|
| 314 | 0 |
return this.originalContext.getMimeType(theFilename); |
| 315 |
} |
|
| 316 |
|
|
| 317 |
/**
|
|
| 318 |
* @see ServletContext#getMajorVersion()
|
|
| 319 |
*/
|
|
| 320 | 0 |
public int getMajorVersion() |
| 321 |
{
|
|
| 322 | 0 |
return this.originalContext.getMajorVersion(); |
| 323 |
} |
|
| 324 |
|
|
| 325 |
/**
|
|
| 326 |
* @return the union of the parameters defined in the Redirector
|
|
| 327 |
* <code>web.xml</code> file and the one set using the
|
|
| 328 |
* <code>setInitParameter()</code> method.
|
|
| 329 |
*/
|
|
| 330 | 0 |
public Enumeration getInitParameterNames()
|
| 331 |
{
|
|
| 332 | 0 |
Vector names = new Vector();
|
| 333 |
|
|
| 334 |
// Add parameters that were added using setInitParameter()
|
|
| 335 | 0 |
Enumeration en = this.initParameters.keys();
|
| 336 |
|
|
| 337 | 0 |
while (en.hasMoreElements())
|
| 338 |
{
|
|
| 339 | 0 |
String value = (String) en.nextElement(); |
| 340 |
|
|
| 341 | 0 |
names.add(value); |
| 342 |
} |
|
| 343 |
|
|
| 344 |
// Add parameters from web.xml
|
|
| 345 | 0 |
en = this.originalContext.getInitParameterNames();
|
| 346 |
|
|
| 347 | 0 |
while (en.hasMoreElements())
|
| 348 |
{
|
|
| 349 | 0 |
String value = (String) en.nextElement(); |
| 350 |
|
|
| 351 |
// Do not add parameters that have been overriden by calling
|
|
| 352 |
// the setInitParameter() method.
|
|
| 353 | 0 |
if (!names.contains(value))
|
| 354 |
{
|
|
| 355 | 0 |
names.add(value); |
| 356 |
} |
|
| 357 |
} |
|
| 358 |
|
|
| 359 | 0 |
return names.elements();
|
| 360 |
} |
|
| 361 |
|
|
| 362 |
/**
|
|
| 363 |
* @param theName the name of the parameter's value to return
|
|
| 364 |
* @return the value of the parameter, looking for it first in the list of
|
|
| 365 |
* parameters set using the <code>setInitParameter()</code> method
|
|
| 366 |
* and then in those set in <code>web.xml</code>.
|
|
| 367 |
*/
|
|
| 368 | 0 |
public String getInitParameter(String theName)
|
| 369 |
{
|
|
| 370 |
// Look first in the list of parameters set using the
|
|
| 371 |
// setInitParameter() method.
|
|
| 372 | 0 |
String value = (String) this.initParameters.get(theName);
|
| 373 |
|
|
| 374 | 0 |
if (value == null) |
| 375 |
{
|
|
| 376 | 0 |
value = this.originalContext.getInitParameter(theName);
|
| 377 |
} |
|
| 378 |
|
|
| 379 | 0 |
return value;
|
| 380 |
} |
|
| 381 |
|
|
| 382 |
/**
|
|
| 383 |
* @param theUripath a String specifying the context path of another web
|
|
| 384 |
* application in the container
|
|
| 385 |
* @return our servlet context wrapper
|
|
| 386 |
* @see ServletContext#getContext(String)
|
|
| 387 |
*/
|
|
| 388 | 0 |
public ServletContext getContext(String theUripath)
|
| 389 |
{
|
|
| 390 | 0 |
ServletContext context = new ServletContextWrapper(
|
| 391 |
this.originalContext.getContext(theUripath));
|
|
| 392 |
|
|
| 393 | 0 |
return context;
|
| 394 |
} |
|
| 395 |
|
|
| 396 |
/**
|
|
| 397 |
* @see ServletContext#getAttributeNames()
|
|
| 398 |
*/
|
|
| 399 | 0 |
public Enumeration getAttributeNames()
|
| 400 |
{
|
|
| 401 | 0 |
return this.originalContext.getAttributeNames(); |
| 402 |
} |
|
| 403 |
|
|
| 404 |
/**
|
|
| 405 |
* @see ServletContext#getAttribute(String)
|
|
| 406 |
*/
|
|
| 407 | 0 |
public Object getAttribute(String theName)
|
| 408 |
{
|
|
| 409 | 0 |
return this.originalContext.getAttribute(theName); |
| 410 |
} |
|
| 411 |
} |
|
| 412 |
|
|
||||||||||