1 16 17 package org.springframework.web.portlet.context; 18 19 import java.io.File ; 20 import java.io.FileNotFoundException ; 21 import java.io.IOException ; 22 import java.io.InputStream ; 23 import java.net.URL ; 24 25 import javax.portlet.PortletContext; 26 27 import org.springframework.core.io.AbstractResource; 28 import org.springframework.core.io.Resource; 29 import org.springframework.util.Assert; 30 import org.springframework.util.StringUtils; 31 import org.springframework.web.portlet.util.PortletUtils; 32 33 47 public class PortletContextResource extends AbstractResource { 48 49 private final PortletContext portletContext; 50 51 private final String path; 52 53 54 63 public PortletContextResource(PortletContext portletContext, String path) { 64 Assert.notNull(portletContext, "Cannot resolve PortletContextResource without PortletContext"); 66 this.portletContext = portletContext; 67 68 Assert.notNull(path, "path is required"); 70 if (!path.startsWith("/")) { 71 path = "/" + path; 72 } 73 this.path = path; 74 } 75 76 79 public PortletContext getPortletContext() { 80 return portletContext; 81 } 82 83 86 public String getPath() { 87 return path; 88 } 89 90 91 96 public InputStream getInputStream() throws IOException { 97 InputStream is = this.portletContext.getResourceAsStream(this.path); 98 if (is == null) { 99 throw new FileNotFoundException ("Could not open " + getDescription()); 100 } 101 return is; 102 } 103 104 public URL getURL() throws IOException { 105 URL url = this.portletContext.getResource(this.path); 106 if (url == null) { 107 throw new FileNotFoundException ( 108 getDescription() + " cannot be resolved to URL because it does not exist"); 109 } 110 return url; 111 } 112 113 118 public File getFile() throws IOException { 119 String realPath = PortletUtils.getRealPath(this.portletContext, this.path); 120 return new File (realPath); 121 } 122 123 public Resource createRelative(String relativePath) throws IOException { 124 String pathToUse = StringUtils.applyRelativePath(this.path, relativePath); 125 return new PortletContextResource(this.portletContext, pathToUse); 126 } 127 128 public String getFilename() { 129 return StringUtils.getFilename(this.path); 130 } 131 132 public String getDescription() { 133 return "PortletContext resource [" + this.path + "]"; 134 } 135 136 137 public boolean equals(Object obj) { 138 if (obj == this) { 139 return true; 140 } 141 if (obj instanceof PortletContextResource) { 142 PortletContextResource otherRes = (PortletContextResource) obj; 143 return (this.portletContext.equals(otherRes.portletContext) && this.path.equals(otherRes.path)); 144 } 145 return false; 146 } 147 148 public int hashCode() { 149 return this.path.hashCode(); 150 } 151 152 } 153 | Popular Tags |