|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| package org.apache.tapestry.asset; |
|
16 |
| |
|
17 |
| import java.io.BufferedInputStream; |
|
18 |
| import java.io.IOException; |
|
19 |
| import java.io.InputStream; |
|
20 |
| import java.io.OutputStream; |
|
21 |
| import java.net.URL; |
|
22 |
| import java.net.URLConnection; |
|
23 |
| import java.util.HashMap; |
|
24 |
| import java.util.Map; |
|
25 |
| |
|
26 |
| import javax.servlet.http.HttpServletResponse; |
|
27 |
| |
|
28 |
| import org.apache.hivemind.ApplicationRuntimeException; |
|
29 |
| import org.apache.hivemind.ClassResolver; |
|
30 |
| import org.apache.hivemind.util.Defense; |
|
31 |
| import org.apache.hivemind.util.IOUtils; |
|
32 |
| import org.apache.tapestry.IRequestCycle; |
|
33 |
| import org.apache.tapestry.Tapestry; |
|
34 |
| import org.apache.tapestry.engine.IEngineService; |
|
35 |
| import org.apache.tapestry.engine.ILink; |
|
36 |
| import org.apache.tapestry.error.RequestExceptionReporter; |
|
37 |
| import org.apache.tapestry.services.LinkFactory; |
|
38 |
| import org.apache.tapestry.services.ServiceConstants; |
|
39 |
| import org.apache.tapestry.util.ContentType; |
|
40 |
| import org.apache.tapestry.web.WebContext; |
|
41 |
| import org.apache.tapestry.web.WebRequest; |
|
42 |
| import org.apache.tapestry.web.WebResponse; |
|
43 |
| |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
| |
|
56 |
| |
|
57 |
| |
|
58 |
| public class AssetService implements IEngineService |
|
59 |
| { |
|
60 |
| |
|
61 |
| |
|
62 |
| private ClassResolver _classResolver; |
|
63 |
| |
|
64 |
| |
|
65 |
| private LinkFactory _linkFactory; |
|
66 |
| |
|
67 |
| |
|
68 |
| private WebContext _context; |
|
69 |
| |
|
70 |
| |
|
71 |
| |
|
72 |
| private WebRequest _request; |
|
73 |
| |
|
74 |
| |
|
75 |
| private WebResponse _response; |
|
76 |
| |
|
77 |
| |
|
78 |
| private ResourceDigestSource _digestSource; |
|
79 |
| |
|
80 |
| |
|
81 |
| |
|
82 |
| |
|
83 |
| |
|
84 |
| |
|
85 |
| private final static Map _mimeTypes; |
|
86 |
| |
|
87 |
| static |
|
88 |
| { |
|
89 |
3
| _mimeTypes = new HashMap(17);
|
|
90 |
3
| _mimeTypes.put("css", "text/css");
|
|
91 |
3
| _mimeTypes.put("gif", "image/gif");
|
|
92 |
3
| _mimeTypes.put("jpg", "image/jpeg");
|
|
93 |
3
| _mimeTypes.put("jpeg", "image/jpeg");
|
|
94 |
3
| _mimeTypes.put("htm", "text/html");
|
|
95 |
3
| _mimeTypes.put("html", "text/html");
|
|
96 |
| } |
|
97 |
| |
|
98 |
| private static final int BUFFER_SIZE = 10240; |
|
99 |
| |
|
100 |
| |
|
101 |
| |
|
102 |
| |
|
103 |
| |
|
104 |
| |
|
105 |
| |
|
106 |
| private final long _startupTime = System.currentTimeMillis(); |
|
107 |
| |
|
108 |
| |
|
109 |
| |
|
110 |
| |
|
111 |
| |
|
112 |
| |
|
113 |
| private final long _expireTime = _startupTime + 365 * 24 * 60 * 60 * 1000; |
|
114 |
| |
|
115 |
| |
|
116 |
| |
|
117 |
| private RequestExceptionReporter _exceptionReporter; |
|
118 |
| |
|
119 |
| |
|
120 |
| |
|
121 |
| |
|
122 |
| |
|
123 |
| |
|
124 |
| |
|
125 |
| public static final String PATH = "path"; |
|
126 |
| |
|
127 |
| |
|
128 |
| |
|
129 |
| |
|
130 |
| |
|
131 |
| |
|
132 |
| |
|
133 |
| |
|
134 |
| public static final String DIGEST = "digest"; |
|
135 |
| |
|
136 |
| |
|
137 |
| |
|
138 |
| |
|
139 |
| |
|
140 |
| |
|
141 |
| |
|
142 |
| |
|
143 |
219
| public ILink getLink(boolean post, Object parameter)
|
|
144 |
| { |
|
145 |
219
| Defense.isAssignable(parameter, String.class, "parameter");
|
|
146 |
| |
|
147 |
219
| String path = (String) parameter;
|
|
148 |
| |
|
149 |
219
| String digest = _digestSource.getDigestForResource(path);
|
|
150 |
| |
|
151 |
219
| Map parameters = new HashMap();
|
|
152 |
| |
|
153 |
219
| parameters.put(ServiceConstants.SERVICE, getName());
|
|
154 |
219
| parameters.put(PATH, path);
|
|
155 |
219
| parameters.put(DIGEST, digest);
|
|
156 |
| |
|
157 |
| |
|
158 |
| |
|
159 |
219
| return _linkFactory.constructLink(this, post, parameters, false);
|
|
160 |
| } |
|
161 |
| |
|
162 |
513
| public String getName()
|
|
163 |
| { |
|
164 |
513
| return Tapestry.ASSET_SERVICE;
|
|
165 |
| } |
|
166 |
| |
|
167 |
3
| private String getMimeType(String path)
|
|
168 |
| { |
|
169 |
3
| String result = _context.getMimeType(path);
|
|
170 |
| |
|
171 |
3
| if (result == null)
|
|
172 |
| { |
|
173 |
0
| int dotx = path.lastIndexOf('.');
|
|
174 |
0
| if (dotx > -1) {
|
|
175 |
0
| String key = path.substring(dotx + 1).toLowerCase();
|
|
176 |
0
| result = (String) _mimeTypes.get(key);
|
|
177 |
| } |
|
178 |
| |
|
179 |
0
| if (result == null)
|
|
180 |
0
| result = "text/plain";
|
|
181 |
| } |
|
182 |
| |
|
183 |
3
| return result;
|
|
184 |
| } |
|
185 |
| |
|
186 |
| |
|
187 |
| |
|
188 |
| |
|
189 |
| |
|
190 |
| |
|
191 |
3
| public void service(IRequestCycle cycle) throws IOException
|
|
192 |
| { |
|
193 |
3
| String path = cycle.getParameter(PATH);
|
|
194 |
3
| String md5Digest = cycle.getParameter(DIGEST);
|
|
195 |
| |
|
196 |
3
| try
|
|
197 |
| { |
|
198 |
3
| if (!_digestSource.getDigestForResource(path).equals(md5Digest))
|
|
199 |
| { |
|
200 |
0
| _response.sendError(HttpServletResponse.SC_FORBIDDEN, AssetMessages
|
|
201 |
| .md5Mismatch(path)); |
|
202 |
0
| return;
|
|
203 |
| } |
|
204 |
| |
|
205 |
| |
|
206 |
| |
|
207 |
| |
|
208 |
3
| if (_request.getHeader("If-Modified-Since") != null)
|
|
209 |
| { |
|
210 |
0
| _response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
|
|
211 |
0
| return;
|
|
212 |
| } |
|
213 |
| |
|
214 |
3
| URL resourceURL = _classResolver.getResource(path);
|
|
215 |
| |
|
216 |
3
| if (resourceURL == null)
|
|
217 |
0
| throw new ApplicationRuntimeException(AssetMessages.noSuchResource(path));
|
|
218 |
| |
|
219 |
3
| URLConnection resourceConnection = resourceURL.openConnection();
|
|
220 |
| |
|
221 |
3
| writeAssetContent(cycle, path, resourceConnection);
|
|
222 |
| } |
|
223 |
| catch (Throwable ex) |
|
224 |
| { |
|
225 |
0
| _exceptionReporter.reportRequestException(AssetMessages.exceptionReportTitle(path), ex);
|
|
226 |
| } |
|
227 |
| |
|
228 |
| } |
|
229 |
| |
|
230 |
| |
|
231 |
| |
|
232 |
3
| private void writeAssetContent(IRequestCycle cycle, String resourcePath,
|
|
233 |
| URLConnection resourceConnection) throws IOException |
|
234 |
| { |
|
235 |
3
| InputStream input = null;
|
|
236 |
| |
|
237 |
3
| try
|
|
238 |
| { |
|
239 |
| |
|
240 |
| |
|
241 |
| |
|
242 |
| |
|
243 |
3
| String contentType = getMimeType(resourcePath);
|
|
244 |
3
| int contentLength = resourceConnection.getContentLength();
|
|
245 |
| |
|
246 |
3
| if (contentLength > 0)
|
|
247 |
3
| _response.setContentLength(contentLength);
|
|
248 |
| |
|
249 |
3
| _response.setDateHeader("Last-Modified", _startupTime);
|
|
250 |
3
| _response.setDateHeader("Expires", _expireTime);
|
|
251 |
| |
|
252 |
| |
|
253 |
| |
|
254 |
| |
|
255 |
3
| if (contentType == null || contentType.length() == 0)
|
|
256 |
0
| contentType = getMimeType(resourcePath);
|
|
257 |
| |
|
258 |
3
| OutputStream output = _response.getOutputStream(new ContentType(contentType));
|
|
259 |
| |
|
260 |
3
| input = new BufferedInputStream(resourceConnection.getInputStream());
|
|
261 |
| |
|
262 |
3
| byte[] buffer = new byte[BUFFER_SIZE];
|
|
263 |
| |
|
264 |
3
| while (true)
|
|
265 |
| { |
|
266 |
6
| int bytesRead = input.read(buffer);
|
|
267 |
| |
|
268 |
6
| if (bytesRead < 0)
|
|
269 |
3
| break;
|
|
270 |
| |
|
271 |
3
| output.write(buffer, 0, bytesRead);
|
|
272 |
| } |
|
273 |
| |
|
274 |
3
| input.close();
|
|
275 |
3
| input = null;
|
|
276 |
| } |
|
277 |
| finally |
|
278 |
| { |
|
279 |
3
| IOUtils.close(input);
|
|
280 |
| } |
|
281 |
| } |
|
282 |
| |
|
283 |
| |
|
284 |
| |
|
285 |
75
| public void setExceptionReporter(RequestExceptionReporter exceptionReporter)
|
|
286 |
| { |
|
287 |
75
| _exceptionReporter = exceptionReporter;
|
|
288 |
| } |
|
289 |
| |
|
290 |
| |
|
291 |
75
| public void setLinkFactory(LinkFactory linkFactory)
|
|
292 |
| { |
|
293 |
75
| _linkFactory = linkFactory;
|
|
294 |
| } |
|
295 |
| |
|
296 |
| |
|
297 |
75
| public void setClassResolver(ClassResolver classResolver)
|
|
298 |
| { |
|
299 |
75
| _classResolver = classResolver;
|
|
300 |
| } |
|
301 |
| |
|
302 |
| |
|
303 |
75
| public void setContext(WebContext context)
|
|
304 |
| { |
|
305 |
75
| _context = context;
|
|
306 |
| } |
|
307 |
| |
|
308 |
| |
|
309 |
75
| public void setResponse(WebResponse response)
|
|
310 |
| { |
|
311 |
75
| _response = response;
|
|
312 |
| } |
|
313 |
| |
|
314 |
| |
|
315 |
75
| public void setDigestSource(ResourceDigestSource md5Source)
|
|
316 |
| { |
|
317 |
75
| _digestSource = md5Source;
|
|
318 |
| } |
|
319 |
| |
|
320 |
| |
|
321 |
75
| public void setRequest(WebRequest request)
|
|
322 |
| { |
|
323 |
75
| _request = request;
|
|
324 |
| } |
|
325 |
| } |