1   /*
2    * Copyright 2002-2004 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.huihoo.jfox.soaf.util.filter;
18  
19  import java.io.InputStream;
20  import java.net.MalformedURLException;
21  import java.net.URL;
22  import java.util.Enumeration;
23  import java.util.Hashtable;
24  import java.util.Properties;
25  import java.util.Set;
26  
27  import javax.servlet.RequestDispatcher;
28  import javax.servlet.Servlet;
29  import javax.servlet.ServletContext;
30  
31  import org.apache.commons.logging.Log;
32  import org.apache.commons.logging.LogFactory;
33  
34  
35  /***
36   * Mock implementation of the ServletContext interface.
37   *
38   * <p>Used for testing the web framework; only rarely necessary for
39   * testing application controllers, as long as they don't explicitly access
40   * the ServletContext. In the latter case, ClassPathXmlApplicationContext
41   * can be used to load them; else, XmlWebApplicationContext needs to be
42   * used, possibly with this MockServletContext class.
43   *
44   * @author Rod Johnson
45   * @author Juergen Hoeller
46   * @since 1.0.2
47   */
48  public class MockServletContext implements ServletContext {
49  
50  	private static final String TEMP_DIR_SYSTEM_PROPERTY = "java.io.tmpdir";
51  
52  
53  	private final Log logger = LogFactory.getLog(getClass());
54  
55  	private final String resourceBasePath;
56  	
57  	private final Properties initParameters = new Properties();
58  
59  	private final Hashtable attributes = new Hashtable();
60  
61  
62  	/***
63  	 * Create a new MockServletContext, using no base path and a
64  	 * DefaultResourceLoader (i.e. the classpath root as WAR root).
65  	 * @see org.springframework.core.io.DefaultResourceLoader
66  	 */
67  	public MockServletContext() {
68  		this("");
69  	}
70  
71  	/***
72  	 * Create a new MockServletContext, using a DefaultResourceLoader.
73  	 * @param resourceBasePath the WAR root directory (should not end with a /)
74  	 * @see org.springframework.core.io.DefaultResourceLoader
75  	 */
76  	public MockServletContext(String resourceBasePath) {
77  		this.resourceBasePath = resourceBasePath;
78  	}
79  
80  	/***
81  	 * Build a full resource location for the given path,
82  	 * prepending the resource base path of this MockServletContext.
83  	 * @param path the path as specified
84  	 * @return the full resource path
85  	 */
86  	protected String getResourceLocation(String path) {
87  		if (!path.startsWith("/")) {
88  			path = "/" + path;
89  		}
90  		return this.resourceBasePath + path;
91  	}
92  
93  
94  	public ServletContext getContext(String name) {
95  		throw new UnsupportedOperationException("getContext");
96  	}
97  
98  	public int getMajorVersion() {
99  		return 2;
100 	}
101 
102 	public int getMinorVersion() {
103 		return 3;
104 	}
105 
106 	public String getMimeType(String filePath) {
107 		throw new UnsupportedOperationException("getMimeType");
108 	}
109 
110 	public Set getResourcePaths(String path) {
111 		return null;
112 	}
113 
114 	public URL getResource(String path) throws MalformedURLException {
115 		return null;
116 	}
117 
118 	public InputStream getResourceAsStream(String path) {
119 		return null;
120 	}
121 
122 	public RequestDispatcher getRequestDispatcher(String path) {
123 		if (!path.startsWith("/")) {
124 			throw new IllegalArgumentException("RequestDispatcher path at ServletContext level must start with '/'");
125 		}
126 		return new MockRequestDispatcher(path);
127 	}
128 
129 	public RequestDispatcher getNamedDispatcher(String path) {
130 		throw new UnsupportedOperationException("getNamedDispatcher");
131 	}
132 
133 	public Servlet getServlet(String name) {
134 		throw new UnsupportedOperationException("getServlet");
135 	}
136 
137 	public Enumeration getServlets() {
138 		throw new UnsupportedOperationException("getServlets");
139 	}
140 
141 	public Enumeration getServletNames() {
142 		throw new UnsupportedOperationException("getServletNames");
143 	}
144 
145 	public void log(String message) {
146 		logger.info(message);
147 	}
148 
149 	public void log(Exception e, String message) {
150 		logger.info(message, e);
151 	}
152 
153 	public void log(String message, Throwable t) {
154 		logger.info(message, t);
155 	}
156 
157 	public String getRealPath(String path) {
158 		return null;
159 	}
160 
161 	public String getServerInfo() {
162 		return "MockServletContext";
163 	}
164 
165 	public String getInitParameter(String name) {
166 		return this.initParameters.getProperty(name);
167 	}
168 
169 	public void addInitParameter(String name, String value) {
170 		this.initParameters.put(name, value);
171 	}
172 
173 	public Enumeration getInitParameterNames() {
174 		return this.initParameters.keys();
175 	}
176 
177 	public Object getAttribute(String name) {
178 		return this.attributes.get(name);
179 	}
180 
181 	public Enumeration getAttributeNames() {
182 		return this.attributes.keys();
183 	}
184 
185 	public void setAttribute(String name, Object value) {
186 		if (value != null) {
187 			this.attributes.put(name, value);
188 		}
189 		else {
190 			this.attributes.remove(name);
191 		}
192 	}
193 
194 	public void removeAttribute(String name) {
195 		this.attributes.remove(name);
196 	}
197 
198 	public String getServletContextName() {
199 		return "MockServletContext";
200 	}
201 
202 }