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