Mercurial Hosting > luan
annotate src/org/eclipse/jetty/server/handler/ResourceHandler.java @ 997:7d28be82ab75
simplify Request
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 18 Oct 2016 22:43:17 -0600 |
parents | 4e9d373bf6e9 |
children | 32d4b569567c |
rev | line source |
---|---|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
2 // ======================================================================== |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
3 // Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd. |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
4 // ------------------------------------------------------------------------ |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
5 // All rights reserved. This program and the accompanying materials |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
6 // are made available under the terms of the Eclipse Public License v1.0 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
7 // and Apache License v2.0 which accompanies this distribution. |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
8 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
9 // The Eclipse Public License is available at |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
10 // http://www.eclipse.org/legal/epl-v10.html |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
11 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
12 // The Apache License v2.0 is available at |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
13 // http://www.opensource.org/licenses/apache2.0.php |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
14 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
15 // You may elect to redistribute this code under either of these licenses. |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
16 // ======================================================================== |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
17 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
18 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
19 package org.eclipse.jetty.server.handler; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
20 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
21 import java.io.IOException; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
22 import java.io.OutputStream; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
23 import java.net.MalformedURLException; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
24 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
25 import javax.servlet.ServletException; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
26 import javax.servlet.http.HttpServletRequest; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
27 import javax.servlet.http.HttpServletResponse; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
28 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
29 import org.eclipse.jetty.http.HttpFields; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
30 import org.eclipse.jetty.http.HttpHeaders; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
31 import org.eclipse.jetty.http.HttpMethods; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
32 import org.eclipse.jetty.http.HttpStatus; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
33 import org.eclipse.jetty.http.MimeTypes; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
34 import org.eclipse.jetty.io.Buffer; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
35 import org.eclipse.jetty.io.ByteArrayBuffer; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
36 import org.eclipse.jetty.io.WriterOutputStream; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
37 import org.eclipse.jetty.server.AbstractHttpConnection; |
822 | 38 import javax.servlet.RequestDispatcher; |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
39 import org.eclipse.jetty.server.Request; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
40 import org.eclipse.jetty.server.Response; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
41 import org.eclipse.jetty.util.URIUtil; |
820
8e9db0bbf4f9
remove org.eclipse.jetty.util.log and upgrade slf4j
Franklin Schmidt <fschmidt@gmail.com>
parents:
805
diff
changeset
|
42 import org.slf4j.Logger; |
8e9db0bbf4f9
remove org.eclipse.jetty.util.log and upgrade slf4j
Franklin Schmidt <fschmidt@gmail.com>
parents:
805
diff
changeset
|
43 import org.slf4j.LoggerFactory; |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
44 import org.eclipse.jetty.util.resource.FileResource; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
45 import org.eclipse.jetty.util.resource.Resource; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
46 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
47 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
48 /* ------------------------------------------------------------ */ |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
49 /** Resource Handler. |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
50 * |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
51 * This handle will serve static content and handle If-Modified-Since headers. |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
52 * No caching is done. |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
53 * Requests for resources that do not exist are let pass (Eg no 404's). |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
54 * |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
55 * |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
56 * @org.apache.xbean.XBean |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
57 */ |
992 | 58 public final class ResourceHandler extends HandlerWrapper |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
59 { |
820
8e9db0bbf4f9
remove org.eclipse.jetty.util.log and upgrade slf4j
Franklin Schmidt <fschmidt@gmail.com>
parents:
805
diff
changeset
|
60 private static final Logger LOG = LoggerFactory.getLogger(ResourceHandler.class); |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
61 |
805 | 62 Resource _baseResource; |
63 Resource _defaultStylesheet; | |
64 Resource _stylesheet; | |
65 String[] _welcomeFiles={"index.html"}; | |
66 MimeTypes _mimeTypes = new MimeTypes(); | |
67 ByteArrayBuffer _cacheControl; | |
68 boolean _aliases; | |
69 boolean _directory; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
70 |
805 | 71 /* ------------------------------------------------------------ */ |
72 public ResourceHandler() | |
73 { | |
74 | |
75 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
76 |
805 | 77 /* ------------------------------------------------------------ */ |
78 public MimeTypes getMimeTypes() | |
79 { | |
80 return _mimeTypes; | |
81 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
82 |
805 | 83 /* ------------------------------------------------------------ */ |
84 public void setMimeTypes(MimeTypes mimeTypes) | |
85 { | |
86 _mimeTypes = mimeTypes; | |
87 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
88 |
805 | 89 /* ------------------------------------------------------------ */ |
90 /** | |
91 * @return True if resource aliases are allowed. | |
92 */ | |
93 public boolean isAliases() | |
94 { | |
95 return _aliases; | |
96 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
97 |
805 | 98 /* ------------------------------------------------------------ */ |
99 /** | |
100 * Set if resource aliases (eg symlink, 8.3 names, case insensitivity) are allowed. | |
101 * Allowing aliases can significantly increase security vulnerabilities. | |
102 * If this handler is deployed inside a ContextHandler, then the | |
103 * {@link ContextHandler#isAliases()} takes precedent. | |
104 * @param aliases True if aliases are supported. | |
105 */ | |
106 public void setAliases(boolean aliases) | |
107 { | |
108 _aliases = aliases; | |
109 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
110 |
805 | 111 /* ------------------------------------------------------------ */ |
112 /** Get the directory option. | |
113 * @return true if directories are listed. | |
114 */ | |
115 public boolean isDirectoriesListed() | |
116 { | |
117 return _directory; | |
118 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
119 |
805 | 120 /* ------------------------------------------------------------ */ |
121 /** Set the directory. | |
122 * @param directory true if directories are listed. | |
123 */ | |
124 public void setDirectoriesListed(boolean directory) | |
125 { | |
126 _directory = directory; | |
127 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
128 |
805 | 129 /* ------------------------------------------------------------ */ |
130 @Override | |
131 public void doStart() | |
132 throws Exception | |
133 { | |
134 if (!_aliases && !FileResource.getCheckAliases()) | |
135 throw new IllegalStateException("Alias checking disabled"); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
136 |
805 | 137 super.doStart(); |
138 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
139 |
805 | 140 /* ------------------------------------------------------------ */ |
141 /** | |
142 * @return Returns the resourceBase. | |
143 */ | |
144 public Resource getBaseResource() | |
145 { | |
146 if (_baseResource==null) | |
147 return null; | |
148 return _baseResource; | |
149 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
150 |
805 | 151 /* ------------------------------------------------------------ */ |
152 /** | |
153 * @return Returns the base resource as a string. | |
154 */ | |
155 public String getResourceBase() | |
156 { | |
157 if (_baseResource==null) | |
158 return null; | |
159 return _baseResource.toString(); | |
160 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
161 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
162 |
805 | 163 /* ------------------------------------------------------------ */ |
164 /** | |
165 * @param base The resourceBase to set. | |
166 */ | |
167 public void setBaseResource(Resource base) | |
168 { | |
169 _baseResource=base; | |
170 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
171 |
805 | 172 /* ------------------------------------------------------------ */ |
173 /** | |
174 * @param resourceBase The base resource as a string. | |
175 */ | |
176 public void setResourceBase(String resourceBase) | |
177 { | |
178 try | |
179 { | |
180 setBaseResource(Resource.newResource(resourceBase)); | |
181 } | |
182 catch (Exception e) | |
183 { | |
184 LOG.warn(e.toString()); | |
820
8e9db0bbf4f9
remove org.eclipse.jetty.util.log and upgrade slf4j
Franklin Schmidt <fschmidt@gmail.com>
parents:
805
diff
changeset
|
185 LOG.debug("",e); |
805 | 186 throw new IllegalArgumentException(resourceBase); |
187 } | |
188 } | |
189 | |
994
4e9d373bf6e9
remove ContextHandler.Context
Franklin Schmidt <fschmidt@gmail.com>
parents:
992
diff
changeset
|
190 private Resource getStylesheet() |
805 | 191 { |
192 if(_stylesheet != null) | |
193 { | |
194 return _stylesheet; | |
195 } | |
196 else | |
197 { | |
198 if(_defaultStylesheet == null) | |
199 { | |
200 try | |
201 { | |
202 _defaultStylesheet = Resource.newResource(this.getClass().getResource("/org/eclipse/jetty/jetty-dir.css")); | |
203 } | |
204 catch(IOException e) | |
205 { | |
206 LOG.warn(e.toString()); | |
820
8e9db0bbf4f9
remove org.eclipse.jetty.util.log and upgrade slf4j
Franklin Schmidt <fschmidt@gmail.com>
parents:
805
diff
changeset
|
207 LOG.debug("",e); |
805 | 208 } |
209 } | |
210 return _defaultStylesheet; | |
211 } | |
212 } | |
213 | |
214 /* ------------------------------------------------------------ */ | |
215 /** | |
216 * @param stylesheet The location of the stylesheet to be used as a String. | |
217 */ | |
218 public void setStylesheet(String stylesheet) | |
219 { | |
220 try | |
221 { | |
222 _stylesheet = Resource.newResource(stylesheet); | |
223 if(!_stylesheet.exists()) | |
224 { | |
225 LOG.warn("unable to find custom stylesheet: " + stylesheet); | |
226 _stylesheet = null; | |
227 } | |
228 } | |
229 catch(Exception e) | |
230 { | |
231 LOG.warn(e.toString()); | |
820
8e9db0bbf4f9
remove org.eclipse.jetty.util.log and upgrade slf4j
Franklin Schmidt <fschmidt@gmail.com>
parents:
805
diff
changeset
|
232 LOG.debug("",e); |
805 | 233 throw new IllegalArgumentException(stylesheet.toString()); |
234 } | |
235 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
236 |
805 | 237 /* ------------------------------------------------------------ */ |
238 /** | |
239 * @return the cacheControl header to set on all static content. | |
240 */ | |
241 public String getCacheControl() | |
242 { | |
243 return _cacheControl.toString(); | |
244 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
245 |
805 | 246 /* ------------------------------------------------------------ */ |
247 /** | |
248 * @param cacheControl the cacheControl header to set on all static content. | |
249 */ | |
250 public void setCacheControl(String cacheControl) | |
251 { | |
252 _cacheControl=cacheControl==null?null:new ByteArrayBuffer(cacheControl); | |
253 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
254 |
997 | 255 private Resource getResource(Request request) throws MalformedURLException |
805 | 256 { |
997 | 257 String path = request.getPathInfo(); |
805 | 258 if (path==null || !path.startsWith("/")) |
259 throw new MalformedURLException(path); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
260 |
805 | 261 Resource base = _baseResource; |
262 if (base==null) | |
263 { | |
994
4e9d373bf6e9
remove ContextHandler.Context
Franklin Schmidt <fschmidt@gmail.com>
parents:
992
diff
changeset
|
264 ContextHandler ch = request._contextHandler; |
4e9d373bf6e9
remove ContextHandler.Context
Franklin Schmidt <fschmidt@gmail.com>
parents:
992
diff
changeset
|
265 if (ch==null) |
805 | 266 return null; |
994
4e9d373bf6e9
remove ContextHandler.Context
Franklin Schmidt <fschmidt@gmail.com>
parents:
992
diff
changeset
|
267 base = ch.getBaseResource(); |
805 | 268 if (base==null) |
269 return null; | |
270 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
271 |
805 | 272 try |
273 { | |
992 | 274 path = URIUtil.canonicalPath(path); |
805 | 275 return base.addPath(path); |
276 } | |
277 catch(Exception e) | |
278 { | |
820
8e9db0bbf4f9
remove org.eclipse.jetty.util.log and upgrade slf4j
Franklin Schmidt <fschmidt@gmail.com>
parents:
805
diff
changeset
|
279 LOG.trace("",e); |
805 | 280 } |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
281 |
805 | 282 return null; |
283 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
284 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
285 |
805 | 286 /* ------------------------------------------------------------ */ |
287 public String[] getWelcomeFiles() | |
288 { | |
289 return _welcomeFiles; | |
290 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
291 |
805 | 292 /* ------------------------------------------------------------ */ |
293 public void setWelcomeFiles(String[] welcomeFiles) | |
294 { | |
295 _welcomeFiles=welcomeFiles; | |
296 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
297 |
805 | 298 /* ------------------------------------------------------------ */ |
299 protected Resource getWelcome(Resource directory) throws MalformedURLException, IOException | |
300 { | |
301 for (int i=0;i<_welcomeFiles.length;i++) | |
302 { | |
303 Resource welcome=directory.addPath(_welcomeFiles[i]); | |
304 if (welcome.exists() && !welcome.isDirectory()) | |
305 return welcome; | |
306 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
307 |
805 | 308 return null; |
309 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
310 |
805 | 311 /* ------------------------------------------------------------ */ |
312 /* | |
313 * @see org.eclipse.jetty.server.Handler#handle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, int) | |
314 */ | |
315 public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException | |
316 { | |
317 if (baseRequest.isHandled()) | |
318 return; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
319 |
805 | 320 boolean skipContentBody = false; |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
321 |
805 | 322 if(!HttpMethods.GET.equals(request.getMethod())) |
323 { | |
324 if(!HttpMethods.HEAD.equals(request.getMethod())) | |
325 { | |
326 //try another handler | |
327 super.handle(target, baseRequest, request, response); | |
328 return; | |
329 } | |
330 skipContentBody = true; | |
331 } | |
332 | |
994
4e9d373bf6e9
remove ContextHandler.Context
Franklin Schmidt <fschmidt@gmail.com>
parents:
992
diff
changeset
|
333 Resource resource = getResource(baseRequest); |
805 | 334 |
335 if (resource==null || !resource.exists()) | |
336 { | |
337 if (target.endsWith("/jetty-dir.css")) | |
338 { | |
339 resource = getStylesheet(); | |
340 if (resource==null) | |
341 return; | |
342 response.setContentType("text/css"); | |
343 } | |
344 else | |
345 { | |
346 //no resource - try other handlers | |
347 super.handle(target, baseRequest, request, response); | |
348 return; | |
349 } | |
350 } | |
351 | |
352 if (!_aliases && resource.getAlias()!=null) | |
353 { | |
354 LOG.info(resource+" aliased to "+resource.getAlias()); | |
355 return; | |
356 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
357 |
805 | 358 // We are going to serve something |
359 baseRequest.setHandled(true); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
360 |
805 | 361 if (resource.isDirectory()) |
362 { | |
363 if (!request.getPathInfo().endsWith(URIUtil.SLASH)) | |
364 { | |
365 response.sendRedirect(response.encodeRedirectURL(URIUtil.addPaths(request.getRequestURI(),URIUtil.SLASH))); | |
366 return; | |
367 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
368 |
805 | 369 Resource welcome=getWelcome(resource); |
370 if (welcome!=null && welcome.exists()) | |
371 resource=welcome; | |
372 else | |
373 { | |
374 doDirectory(request,response,resource); | |
375 baseRequest.setHandled(true); | |
376 return; | |
377 } | |
378 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
379 |
805 | 380 // set some headers |
381 long last_modified=resource.lastModified(); | |
382 if (last_modified>0) | |
383 { | |
384 long if_modified=request.getDateHeader(HttpHeaders.IF_MODIFIED_SINCE); | |
385 if (if_modified>0 && last_modified/1000<=if_modified/1000) | |
386 { | |
387 response.setStatus(HttpStatus.NOT_MODIFIED_304); | |
388 return; | |
389 } | |
390 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
391 |
805 | 392 Buffer mime=_mimeTypes.getMimeByExtension(resource.toString()); |
393 if (mime==null) | |
394 mime=_mimeTypes.getMimeByExtension(request.getPathInfo()); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
395 |
805 | 396 // set the headers |
397 doResponseHeaders(response,resource,mime!=null?mime.toString():null); | |
398 response.setDateHeader(HttpHeaders.LAST_MODIFIED,last_modified); | |
399 | |
400 if(skipContentBody) | |
401 return; | |
402 // Send the content | |
403 OutputStream out =null; | |
404 try {out = response.getOutputStream();} | |
405 catch(IllegalStateException e) {out = new WriterOutputStream(response.getWriter());} | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
406 |
805 | 407 // See if a short direct method can be used? |
408 if (out instanceof AbstractHttpConnection.Output) | |
409 { | |
410 // TODO file mapped buffers | |
411 ((AbstractHttpConnection.Output)out).sendContent(resource.getInputStream()); | |
412 } | |
413 else | |
414 { | |
415 // Write content normally | |
416 resource.writeTo(out,0,resource.length()); | |
417 } | |
418 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
419 |
805 | 420 /* ------------------------------------------------------------ */ |
421 protected void doDirectory(HttpServletRequest request,HttpServletResponse response, Resource resource) | |
422 throws IOException | |
423 { | |
424 if (_directory) | |
425 { | |
426 String listing = resource.getListHTML(request.getRequestURI(),request.getPathInfo().lastIndexOf("/") > 0); | |
427 response.setContentType("text/html; charset=UTF-8"); | |
428 response.getWriter().println(listing); | |
429 } | |
430 else | |
431 response.sendError(HttpStatus.FORBIDDEN_403); | |
432 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
433 |
805 | 434 /* ------------------------------------------------------------ */ |
435 /** Set the response headers. | |
436 * This method is called to set the response headers such as content type and content length. | |
437 * May be extended to add additional headers. | |
438 * @param response | |
439 * @param resource | |
440 * @param mimeType | |
441 */ | |
442 protected void doResponseHeaders(HttpServletResponse response, Resource resource, String mimeType) | |
443 { | |
444 if (mimeType!=null) | |
445 response.setContentType(mimeType); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
446 |
805 | 447 long length=resource.length(); |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
448 |
805 | 449 if (response instanceof Response) |
450 { | |
451 HttpFields fields = ((Response)response).getHttpFields(); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
452 |
805 | 453 if (length>0) |
454 fields.putLongField(HttpHeaders.CONTENT_LENGTH_BUFFER,length); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
455 |
805 | 456 if (_cacheControl!=null) |
457 fields.put(HttpHeaders.CACHE_CONTROL_BUFFER,_cacheControl); | |
458 } | |
459 else | |
460 { | |
461 if (length>0) | |
462 response.setHeader(HttpHeaders.CONTENT_LENGTH,Long.toString(length)); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
463 |
805 | 464 if (_cacheControl!=null) |
465 response.setHeader(HttpHeaders.CACHE_CONTROL,_cacheControl.toString()); | |
466 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
467 |
805 | 468 } |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
469 } |