1 16 17 package org.springframework.mock.web; 18 19 import java.io.File ; 20 import java.io.IOException ; 21 import java.io.InputStream ; 22 import java.net.MalformedURLException ; 23 import java.net.URL ; 24 import java.util.Enumeration ; 25 import java.util.Hashtable ; 26 import java.util.Properties ; 27 import java.util.Set ; 28 29 import javax.servlet.RequestDispatcher ; 30 import javax.servlet.Servlet ; 31 import javax.servlet.ServletContext ; 32 33 import org.apache.commons.logging.Log; 34 import org.apache.commons.logging.LogFactory; 35 36 import org.springframework.core.CollectionFactory; 37 import org.springframework.core.io.DefaultResourceLoader; 38 import org.springframework.core.io.Resource; 39 import org.springframework.core.io.ResourceLoader; 40 import org.springframework.util.Assert; 41 import org.springframework.util.ObjectUtils; 42 import org.springframework.web.util.WebUtils; 43 44 76 public class MockServletContext implements ServletContext { 77 78 private static final String TEMP_DIR_SYSTEM_PROPERTY = "java.io.tmpdir"; 79 80 81 private final Log logger = LogFactory.getLog(getClass()); 82 83 private final String resourceBasePath; 84 85 private final ResourceLoader resourceLoader; 86 87 private final Properties initParameters = new Properties (); 88 89 private final Hashtable attributes = new Hashtable (); 90 91 private String servletContextName = "MockServletContext"; 92 93 94 99 public MockServletContext() { 100 this("", null); 101 } 102 103 108 public MockServletContext(String resourceBasePath) { 109 this(resourceBasePath, null); 110 } 111 112 117 public MockServletContext(ResourceLoader resourceLoader) { 118 this("", resourceLoader); 119 } 120 121 126 public MockServletContext(String resourceBasePath, ResourceLoader resourceLoader) { 127 this.resourceBasePath = (resourceBasePath != null ? resourceBasePath : ""); 128 this.resourceLoader = (resourceLoader != null ? resourceLoader : new DefaultResourceLoader()); 129 130 String tempDir = System.getProperty(TEMP_DIR_SYSTEM_PROPERTY); 132 if (tempDir != null) { 133 this.attributes.put(WebUtils.TEMP_DIR_CONTEXT_ATTRIBUTE, new File (tempDir)); 134 } 135 } 136 137 143 protected String getResourceLocation(String path) { 144 if (!path.startsWith("/")) { 145 path = "/" + path; 146 } 147 return this.resourceBasePath + path; 148 } 149 150 151 public ServletContext getContext(String name) { 152 throw new UnsupportedOperationException ("getContext"); 153 } 154 155 public int getMajorVersion() { 156 return 2; 157 } 158 159 public int getMinorVersion() { 160 return 4; 161 } 162 163 public String getMimeType(String filePath) { 164 throw new UnsupportedOperationException ("getMimeType"); 165 } 166 167 public Set getResourcePaths(String path) { 168 String actualPath = (path.endsWith("/") ? path : path + "/"); 169 Resource resource = this.resourceLoader.getResource(getResourceLocation(actualPath)); 170 try { 171 File file = resource.getFile(); 172 String [] fileList = file.list(); 173 if (ObjectUtils.isEmpty(fileList)) { 174 return null; 175 } 176 Set resourcePaths = CollectionFactory.createLinkedSetIfPossible(fileList.length); 177 for (int i = 0; i < fileList.length; i++) { 178 String resultPath = actualPath + fileList[i]; 179 if (resource.createRelative(fileList[i]).getFile().isDirectory()) { 180 resultPath += "/"; 181 } 182 resourcePaths.add(resultPath); 183 } 184 return resourcePaths; 185 } 186 catch (IOException ex) { 187 logger.warn("Couldn't get resource paths for " + resource, ex); 188 return null; 189 } 190 } 191 192 public URL getResource(String path) throws MalformedURLException { 193 Resource resource = this.resourceLoader.getResource(getResourceLocation(path)); 194 if (!resource.exists()) { 195 return null; 196 } 197 try { 198 return resource.getURL(); 199 } 200 catch (MalformedURLException ex) { 201 throw ex; 202 } 203 catch (IOException ex) { 204 logger.warn("Couldn't get URL for " + resource, ex); 205 return null; 206 } 207 } 208 209 public InputStream getResourceAsStream(String path) { 210 Resource resource = this.resourceLoader.getResource(getResourceLocation(path)); 211 if (!resource.exists()) { 212 return null; 213 } 214 try { 215 return resource.getInputStream(); 216 } 217 catch (IOException ex) { 218 logger.warn("Couldn't open InputStream for " + resource, ex); 219 return null; 220 } 221 } 222 223 public RequestDispatcher getRequestDispatcher(String path) { 224 if (!path.startsWith("/")) { 225 throw new IllegalArgumentException ("RequestDispatcher path at ServletContext level must start with '/'"); 226 } 227 return new MockRequestDispatcher(path); 228 } 229 230 public RequestDispatcher getNamedDispatcher(String path) { 231 throw new UnsupportedOperationException ("getNamedDispatcher"); 232 } 233 234 public Servlet getServlet(String name) { 235 throw new UnsupportedOperationException ("getServlet"); 236 } 237 238 public Enumeration getServlets() { 239 throw new UnsupportedOperationException ("getServlets"); 240 } 241 242 public Enumeration getServletNames() { 243 throw new UnsupportedOperationException ("getServletNames"); 244 } 245 246 public void log(String message) { 247 logger.info(message); 248 } 249 250 public void log(Exception ex, String message) { 251 logger.info(message, ex); 252 } 253 254 public void log(String message, Throwable ex) { 255 logger.info(message, ex); 256 } 257 258 public String getRealPath(String path) { 259 Resource resource = this.resourceLoader.getResource(getResourceLocation(path)); 260 try { 261 return resource.getFile().getAbsolutePath(); 262 } 263 catch (IOException ex) { 264 logger.warn("Couldn't determine real path of resource " + resource, ex); 265 return null; 266 } 267 } 268 269 public String getServerInfo() { 270 return "MockServletContext"; 271 } 272 273 public String getInitParameter(String name) { 274 Assert.notNull(name, "Parameter name must not be null"); 275 return this.initParameters.getProperty(name); 276 } 277 278 public void addInitParameter(String name, String value) { 279 Assert.notNull(name, "Parameter name must not be null"); 280 this.initParameters.setProperty(name, value); 281 } 282 283 public Enumeration getInitParameterNames() { 284 return this.initParameters.keys(); 285 } 286 287 public Object getAttribute(String name) { 288 Assert.notNull(name, "Attribute name must not be null"); 289 return this.attributes.get(name); 290 } 291 292 public Enumeration getAttributeNames() { 293 return this.attributes.keys(); 294 } 295 296 public void setAttribute(String name, Object value) { 297 Assert.notNull(name, "Attribute name must not be null"); 298 if (value != null) { 299 this.attributes.put(name, value); 300 } 301 else { 302 this.attributes.remove(name); 303 } 304 } 305 306 public void removeAttribute(String name) { 307 Assert.notNull(name, "Attribute name must not be null"); 308 this.attributes.remove(name); 309 } 310 311 public void setServletContextName(String servletContextName) { 312 this.servletContextName = servletContextName; 313 } 314 315 public String getServletContextName() { 316 return servletContextName; 317 } 318 319 } 320 | Popular Tags |