KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > portlet > context > PortletContextResource


1 /*
2  * Copyright 2002-2005 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.springframework.web.portlet.context;
18
19 import java.io.File JavaDoc;
20 import java.io.FileNotFoundException JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.net.URL JavaDoc;
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 /**
34  * Resource implementation for PortletContext resources,
35  * interpreting relative paths within the portlet application root.
36  *
37  * <p>Always supports stream access, but only allows java.io.File
38  * access when the portlet application archive is expanded.
39  * Always supports resolution as URL.
40  *
41  * @author Juergen Hoeller
42  * @author John A. Lewis
43  * @since 2.0
44  * @see javax.portlet.PortletContext#getResourceAsStream
45  * @see javax.portlet.PortletContext#getRealPath
46  */

47 public class PortletContextResource extends AbstractResource {
48
49     private final PortletContext portletContext;
50
51     private final String JavaDoc path;
52
53
54     /**
55      * Create a new PortletContextResource.
56      * <p>The Portlet spec requires that resource paths start with a slash,
57      * even if many containers accept paths without leading slash too.
58      * Consequently, the given path will be prepended with a slash if it
59      * doesn't already start with one.
60      * @param portletContext the PortletContext to load from
61      * @param path the path of the resource
62      */

63     public PortletContextResource(PortletContext portletContext, String JavaDoc path) {
64         // check PortletContext
65
Assert.notNull(portletContext, "Cannot resolve PortletContextResource without PortletContext");
66         this.portletContext = portletContext;
67
68         // check path
69
Assert.notNull(path, "path is required");
70         if (!path.startsWith("/")) {
71             path = "/" + path;
72         }
73         this.path = path;
74     }
75
76     /**
77      * Return the PortletContext for this resource.
78      */

79     public PortletContext getPortletContext() {
80         return portletContext;
81     }
82
83     /**
84      * Return the path for this resource.
85      */

86     public String JavaDoc getPath() {
87         return path;
88     }
89
90
91     /**
92      * This implementation delegates to PortletContext.getResourceAsStream,
93      * but throws a FileNotFoundException if not found.
94      * @see javax.portlet.PortletContext#getResourceAsStream
95      */

96     public InputStream JavaDoc getInputStream() throws IOException JavaDoc {
97         InputStream JavaDoc is = this.portletContext.getResourceAsStream(this.path);
98         if (is == null) {
99             throw new FileNotFoundException JavaDoc("Could not open " + getDescription());
100         }
101         return is;
102     }
103
104     public URL JavaDoc getURL() throws IOException JavaDoc {
105         URL JavaDoc url = this.portletContext.getResource(this.path);
106         if (url == null) {
107             throw new FileNotFoundException JavaDoc(
108                     getDescription() + " cannot be resolved to URL because it does not exist");
109         }
110         return url;
111     }
112
113     /**
114      * This implementation delegates to PortletContext.getRealPath,
115      * but throws a FileNotFoundException if not found or not resolvable.
116      * @see javax.portlet.PortletContext#getRealPath
117      */

118     public File JavaDoc getFile() throws IOException JavaDoc {
119         String JavaDoc realPath = PortletUtils.getRealPath(this.portletContext, this.path);
120         return new File JavaDoc(realPath);
121     }
122
123     public Resource createRelative(String JavaDoc relativePath) throws IOException JavaDoc {
124         String JavaDoc pathToUse = StringUtils.applyRelativePath(this.path, relativePath);
125         return new PortletContextResource(this.portletContext, pathToUse);
126     }
127
128     public String JavaDoc getFilename() {
129         return StringUtils.getFilename(this.path);
130     }
131
132     public String JavaDoc getDescription() {
133         return "PortletContext resource [" + this.path + "]";
134     }
135
136
137     public boolean equals(Object JavaDoc 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