001: package com.meterware.servletunit;
002:
003: /********************************************************************************************************************
004: * $Id: ServletUnitServletContext.java,v 1.15 2006/03/24 19:59:12 russgold Exp $
005: *
006: * Copyright (c) 2000-2003, Russell Gold
007: *
008: * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
009: * documentation files (the "Software"), to deal in the Software without restriction, including without limitation
010: * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
011: * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included in all copies or substantial portions
014: * of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
017: * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
018: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
019: * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
020: * DEALINGS IN THE SOFTWARE.
021: *
022: *******************************************************************************************************************/
023:
024: import java.io.File;
025: import java.io.FileInputStream;
026: import java.io.FileNotFoundException;
027:
028: import java.net.MalformedURLException;
029: import java.net.URL;
030: import java.net.URLConnection;
031:
032: import java.util.Enumeration;
033: import java.util.Hashtable;
034: import java.util.Vector;
035: import java.util.Set;
036:
037: import javax.servlet.*;
038:
039: /**
040: * This class is a private implementation of the ServletContext class.
041: **/
042: class ServletUnitServletContext implements ServletContext {
043:
044: ServletUnitServletContext(WebApplication application) {
045: _application = application;
046: }
047:
048: /**
049: * Returns a ServletContext object that corresponds to a specified URL on the server.
050: * <p>
051: * This method allows servlets to gain access to the context for various parts of the server,
052: * and as needed obtain RequestDispatcher objects from the context. The given path must be
053: * absolute (beginning with "/") and is interpreted based on the server's document root.
054: * <p>
055: * In a security conscious environment, the servlet container may return null for a given URL.
056: **/
057: public javax.servlet.ServletContext getContext(java.lang.String A) {
058: return null;
059: }
060:
061: /**
062: * Returns the major version of the Java Servlet API that this servlet container supports.
063: * All implementations that comply with Version 2.4 must have this method return the integer 2.
064: **/
065: public int getMajorVersion() {
066: return 2;
067: }
068:
069: /**
070: * Returns the minor version of the Servlet API that this servlet container supports.
071: * All implementations that comply with Version 2.4 must have this method return the integer 4.
072: **/
073: public int getMinorVersion() {
074: return 4;
075: }
076:
077: /**
078: * Returns the MIME type of the specified file, or null if the MIME type is not known.
079: * The MIME type is determined by the configuration of the servlet container, and
080: * may be specified in a web application deployment descriptor. Common MIME types are
081: * "text/html" and "image/gif".
082: **/
083: public java.lang.String getMimeType(String filePath) {
084: return URLConnection.getFileNameMap().getContentTypeFor(
085: filePath);
086: }
087:
088: /**
089: * Returns a URL to the resource that is mapped to a specified path. The path must begin
090: * with a "/" and is interpreted as relative to the current context root.
091: * <p>
092: * This method allows the servlet container to make a resource available to servlets from any source.
093: * Resources can be located on a local or remote file system, in a database, or in a .war file.
094: * <p>
095: * The servlet container must implement the URL handlers and URLConnection objects that are necessary to access the resource.
096: * <p>
097: * This method returns null if no resource is mapped to the pathname.
098: *
099: * Some containers may allow writing to the URL returned by this method using the methods of the URL class.
100: *
101: * The resource content is returned directly, so be aware that requesting a .jsp page returns the JSP source code. Use a
102: * RequestDispatcher instead to include results of an execution.
103: *
104: * This method has a different purpose than java.lang.Class.getResource, which looks up resources based on a class loader. This
105: * method does not use class loaders.
106: **/
107: public java.net.URL getResource(String path) {
108: try {
109: File resourceFile = _application.getResourceFile(path);
110: return resourceFile == null ? null : resourceFile.toURL();
111: } catch (MalformedURLException e) {
112: return null;
113: }
114: }
115:
116: /**
117: * Returns the resource located at the named path as an InputStream object.
118: *
119: * The data in the InputStream can be of any type or length. The path must be specified according to the rules given in getResource.
120: * This method returns null if no resource exists at the specified path.
121:
122: * Meta-information such as content length and content type that is available via getResource method is lost when using this method.
123:
124: * The servlet container must implement the URL handlers and URLConnection objects necessary to access the resource.
125:
126: * This method is different from java.lang.Class.getResourceAsStream, which uses a class loader. This method allows servlet
127: * containers to make a resource available to a servlet from any location, without using a class loader.
128: **/
129: public java.io.InputStream getResourceAsStream(String path) {
130: try {
131: File resourceFile = _application.getResourceFile(path);
132: return resourceFile == null ? null : new FileInputStream(
133: resourceFile);
134: } catch (FileNotFoundException e) {
135: return null;
136: }
137: }
138:
139: /**
140: * Returns a RequestDispatcher object that acts as a wrapper for the resource located at the given path. A RequestDispatcher
141: * object can be used to forward a request to the resource or to include the resource in a response. The resource can be dynamic or static.
142:
143: * The pathname must begin with a "/" and is interpreted as relative to the current context root. Use getContext to obtain a
144: * RequestDispatcher for resources in foreign contexts. This method returns null if the ServletContext cannot return a
145: * RequestDispatcher.
146: **/
147: public javax.servlet.RequestDispatcher getRequestDispatcher(
148: String path) {
149: try {
150: URL url = new URL("http", "localhost", _application
151: .getContextPath()
152: + path);
153: return new RequestDispatcherImpl(_application, url);
154: } catch (ServletException e) {
155: return null;
156: } catch (MalformedURLException e) {
157: return null;
158: }
159: }
160:
161: /**
162: * Returns a RequestDispatcher object that acts as a wrapper for the named servlet.
163: *
164: * Servlets (and JSP pages also) may be given names via server administration or via a web application deployment descriptor. A servlet
165: * instance can determine its name using ServletConfig.getServletName().
166: *
167: * This method returns null if the ServletContext cannot return a RequestDispatcher for any reason.
168: **/
169: public javax.servlet.RequestDispatcher getNamedDispatcher(
170: java.lang.String A) {
171: return null; // XXX not implemented
172: }
173:
174: /**
175: * @deprecated as of Servlet API 2.1
176: **/
177: public javax.servlet.Servlet getServlet(java.lang.String A) {
178: return null;
179: }
180:
181: /**
182: * @deprecated as of Servlet API 2.0
183: **/
184: public java.util.Enumeration getServlets() {
185: return EMPTY_VECTOR.elements();
186: }
187:
188: /**
189: * @deprecated as of Servlet API 2.1
190: **/
191: public java.util.Enumeration getServletNames() {
192: return EMPTY_VECTOR.elements();
193: }
194:
195: /**
196: * Writes the specified message to a servlet log file, usually an event log.
197: * The name and type of the servlet log file is specific to the servlet container.
198: **/
199: public void log(String message) { // XXX change this to use something testable
200: }
201:
202: /**
203: * @deprecated use log( String, Throwable )
204: **/
205: public void log(Exception e, String message) {
206: log(message, e);
207: }
208:
209: /**
210: * Writes an explanatory message and a stack trace for a given Throwable exception to the servlet log file.
211: * The name and type of the servlet log file is specific to the servlet container, usually an event log.
212: **/
213: public void log(String message, Throwable t) {
214: log(message);
215: log(" " + t);
216: }
217:
218: /**
219: * Returns a String containing the real path for a given virtual path. For example, the virtual path "/index.html" has a real path of
220: * whatever file on the server's filesystem would be served by a request for "/index.html".
221: *
222: * The real path returned will be in a form appropriate to the computer and operating system on which the servlet container is running,
223: * including the proper path separators. This method returns null if the servlet container cannot translate the virtual path to a real path for
224: * any reason (such as when the content is being made available from a .war archive).
225: **/
226: public String getRealPath(String path) {
227: return _application.getResourceFile(path).getAbsolutePath();
228: }
229:
230: /**
231: * Returns the name and version of the servlet container on which the servlet is running.
232:
233: * The form of the returned string is servername/versionnumber. For example, the JavaServer Web Development Kit may return the
234: * string JavaServer Web Dev Kit/1.0.
235:
236: * The servlet container may return other optional information after the primary string in parentheses, for example, JavaServer Web
237: * Dev Kit/1.0 (JDK 1.1.6; Windows NT 4.0 x86).
238: **/
239: public String getServerInfo() {
240: return "ServletUnit test framework";
241: }
242:
243: /**
244: * Returns a String containing the value of the named context-wide initialization parameter, or null if the parameter does not exist.
245: *
246: * This method can make available configuration information useful to an entire "web application". For example, it can provide a
247: * webmaster's email address or the name of a system that holds critical data.
248: **/
249: public java.lang.String getInitParameter(String name) {
250: return (String) getContextParams().get(name);
251: }
252:
253: /**
254: * Returns the names of the context's initialization parameters as an Enumeration of String objects,
255: * or an empty Enumeration if the context has no initialization parameters.
256: **/
257: public java.util.Enumeration getInitParameterNames() {
258: return getContextParams().keys();
259: }
260:
261: /**
262: * Returns the servlet container attribute with the given name, or null if there is no attribute by that name.
263: * An attribute allows a servlet container to give the servlet additional information not already
264: * provided by this interface. See your server documentation for information
265: * about its attributes. A list of supported attributes can be retrieved using getAttributeNames.
266: **/
267: public Object getAttribute(String name) {
268: return _attributes.get(name);
269: }
270:
271: public Enumeration getAttributeNames() {
272: return _attributes.keys();
273: }
274:
275: public void setAttribute(String name, Object attribute) {
276: if (!_attributes.containsKey(name)) {
277: _attributes.put(name, attribute);
278: _application.sendAttributeAdded(name, attribute);
279: } else {
280: Object oldValue = _attributes.get(name);
281: _attributes.put(name, attribute);
282: _application.sendAttributeReplaced(name, oldValue);
283: }
284: }
285:
286: public void removeAttribute(String name) {
287: Object oldValue = _attributes.get(name);
288: _attributes.remove(name);
289: _application.sendAttributeRemoved(name, oldValue);
290: }
291:
292: //----------------------------- methods added to ServletContext in JSDK 2.3 --------------------------------------
293:
294: /**
295: * Returns a directory-like listing of all the paths to resources within the web application
296: * whose longest sub-path matches the supplied path argument. Paths indicating subdirectory paths end with a '/'.
297: * The returned paths are all relative to the root of the web application and have a leading '/'.
298: * For example, for a web application containing
299: * <p>
300: * /welcome.html<br />
301: * /catalog/index.html<br /><br />
302: * /catalog/products.html<br />
303: * /catalog/offers/books.html<br />
304: * /catalog/offers/music.html<br />
305: * /customer/login.jsp<br />
306: * /WEB-INF/web.xml<br />
307: * /WEB-INF/classes/com.acme.OrderServlet.class,<br />
308: * <br />
309: * getResourcePaths("/") returns {"/welcome.html", "/catalog/", "/customer/", "/WEB-INF/"}<br />
310: * getResourcePaths("/catalog/") returns {"/catalog/index.html", "/catalog/products.html", "/catalog/offers/"}.
311: *
312: * @param path partial path used to match the resources, which must start with a /
313: * @return a Set containing the directory listing, or null if there are no resources
314: * in the web application whose path begins with the supplied path.
315: * @since HttpUnit 1.3
316: */
317: public Set getResourcePaths(String path) {
318: return null;
319: }
320:
321: /**
322: * Returns the name of this web application correponding to this ServletContext as specified
323: * in the deployment descriptor for this web application by the display-name element.
324: *
325: * @return The name of the web application or null if no name has been declared in the deployment descriptor
326: * @since HttpUnit 1.3
327: */
328: public String getServletContextName() {
329: return _application.getDisplayName();
330: }
331:
332: //------------------------------------------- package members ----------------------------------------------------
333:
334: void setInitParameter(String name, Object initParameter) {
335: getContextParams().put(name, initParameter);
336: }
337:
338: void removeInitParameter(String name) {
339: getContextParams().remove(name);
340: }
341:
342: //------------------------------------------- private members ----------------------------------------------------
343:
344: private final static Vector EMPTY_VECTOR = new Vector();
345:
346: private Hashtable _attributes = new Hashtable();
347: private WebApplication _application;
348:
349: private Hashtable getContextParams() {
350: return _application.getContextParameters();
351: }
352: }
|