1 16 17 package org.springframework.mock.web.portlet; 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.HashSet ; 26 import java.util.Hashtable ; 27 import java.util.Properties ; 28 import java.util.Set ; 29 30 import javax.portlet.PortletContext; 31 import javax.portlet.PortletRequestDispatcher; 32 33 import org.apache.commons.logging.Log; 34 import org.apache.commons.logging.LogFactory; 35 36 import org.springframework.core.io.DefaultResourceLoader; 37 import org.springframework.core.io.Resource; 38 import org.springframework.core.io.ResourceLoader; 39 import org.springframework.util.Assert; 40 import org.springframework.web.util.WebUtils; 41 42 49 public class MockPortletContext implements PortletContext { 50 51 private static final String TEMP_DIR_SYSTEM_PROPERTY = "java.io.tmpdir"; 52 53 54 private final Log logger = LogFactory.getLog(getClass()); 55 56 private final String resourceBasePath; 57 58 private final ResourceLoader resourceLoader; 59 60 private final Hashtable attributes = new Hashtable (); 61 62 private final Properties initParameters = new Properties (); 63 64 private String portletContextName = "MockPortletContext"; 65 66 67 72 public MockPortletContext() { 73 this("", null); 74 } 75 76 81 public MockPortletContext(String resourceBasePath) { 82 this(resourceBasePath, null); 83 } 84 85 90 public MockPortletContext(ResourceLoader resourceLoader) { 91 this("", resourceLoader); 92 } 93 94 99 public MockPortletContext(String resourceBasePath, ResourceLoader resourceLoader) { 100 this.resourceBasePath = (resourceBasePath != null ? resourceBasePath : ""); 101 this.resourceLoader = (resourceLoader != null ? resourceLoader : new DefaultResourceLoader()); 102 103 String tempDir = System.getProperty(TEMP_DIR_SYSTEM_PROPERTY); 105 if (tempDir != null) { 106 this.attributes.put(WebUtils.TEMP_DIR_CONTEXT_ATTRIBUTE, new File (tempDir)); 107 } 108 } 109 110 116 protected String getResourceLocation(String path) { 117 if (!path.startsWith("/")) { 118 path = "/" + path; 119 } 120 return this.resourceBasePath + path; 121 } 122 123 124 public String getServerInfo() { 125 return "MockPortal/1.0"; 126 } 127 128 public PortletRequestDispatcher getRequestDispatcher(String path) { 129 if (!path.startsWith("/")) { 130 throw new IllegalArgumentException ( 131 "PortletRequestDispatcher path at PortletContext level must start with '/'"); 132 } 133 return new MockPortletRequestDispatcher(path); 134 } 135 136 public PortletRequestDispatcher getNamedDispatcher(String path) { 137 return null; 138 } 139 140 public InputStream getResourceAsStream(String path) { 141 Resource resource = this.resourceLoader.getResource(getResourceLocation(path)); 142 try { 143 return resource.getInputStream(); 144 } 145 catch (IOException ex) { 146 logger.info("Couldn't open InputStream for " + resource, ex); 147 return null; 148 } 149 } 150 151 public int getMajorVersion() { 152 return 1; 153 } 154 155 public int getMinorVersion() { 156 return 0; 157 } 158 159 public String getMimeType(String filePath) { 160 return null; 161 } 162 163 public String getRealPath(String path) { 164 Resource resource = this.resourceLoader.getResource(getResourceLocation(path)); 165 try { 166 return resource.getFile().getAbsolutePath(); 167 } 168 catch (IOException ex) { 169 logger.info("Couldn't determine real path of resource " + resource, ex); 170 return null; 171 } 172 } 173 174 public Set getResourcePaths(String path) { 175 Resource resource = this.resourceLoader.getResource(getResourceLocation(path)); 176 try { 177 File file = resource.getFile(); 178 String [] fileList = file.list(); 179 String prefix = (path.endsWith("/") ? path : path + "/"); 180 Set resourcePaths = new HashSet (fileList.length); 181 for (int i = 0; i < fileList.length; i++) { 182 resourcePaths.add(prefix + fileList[i]); 183 } 184 return resourcePaths; 185 } 186 catch (IOException ex) { 187 logger.info("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 try { 195 return resource.getURL(); 196 } 197 catch (IOException ex) { 198 logger.info("Couldn't get URL for " + resource, ex); 199 return null; 200 } 201 } 202 203 public Object getAttribute(String name) { 204 return this.attributes.get(name); 205 } 206 207 public Enumeration getAttributeNames() { 208 return this.attributes.keys(); 209 } 210 211 public void setAttribute(String name, Object value) { 212 if (value != null) { 213 this.attributes.put(name, value); 214 } 215 else { 216 this.attributes.remove(name); 217 } 218 } 219 220 public void removeAttribute(String name) { 221 this.attributes.remove(name); 222 } 223 224 public void addInitParameter(String name, String value) { 225 Assert.notNull(name, "Parameter name must not be null"); 226 this.initParameters.setProperty(name, value); 227 } 228 229 public String getInitParameter(String name) { 230 Assert.notNull(name, "Parameter name must not be null"); 231 return this.initParameters.getProperty(name); 232 } 233 234 public Enumeration getInitParameterNames() { 235 return this.initParameters.keys(); 236 } 237 238 public void log(String message) { 239 logger.info(message); 240 } 241 242 public void log(String message, Throwable t) { 243 logger.info(message, t); 244 } 245 246 public void setPortletContextName(String portletContextName) { 247 this.portletContextName = portletContextName; 248 } 249 250 public String getPortletContextName() { 251 return portletContextName; 252 } 253 254 } 255 | Popular Tags |