Mercurial Hosting > luan
annotate src/org/eclipse/jetty/server/handler/ResourceHandler.java @ 992:0608a6664bee
minor
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 18 Oct 2016 15:23:25 -0600 |
parents | 688c39c50ee3 |
children | 4e9d373bf6e9 |
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 ContextHandler _context; |
63 Resource _baseResource; | |
64 Resource _defaultStylesheet; | |
65 Resource _stylesheet; | |
66 String[] _welcomeFiles={"index.html"}; | |
67 MimeTypes _mimeTypes = new MimeTypes(); | |
68 ByteArrayBuffer _cacheControl; | |
69 boolean _aliases; | |
70 boolean _directory; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
71 |
805 | 72 /* ------------------------------------------------------------ */ |
73 public ResourceHandler() | |
74 { | |
75 | |
76 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
77 |
805 | 78 /* ------------------------------------------------------------ */ |
79 public MimeTypes getMimeTypes() | |
80 { | |
81 return _mimeTypes; | |
82 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
83 |
805 | 84 /* ------------------------------------------------------------ */ |
85 public void setMimeTypes(MimeTypes mimeTypes) | |
86 { | |
87 _mimeTypes = mimeTypes; | |
88 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
89 |
805 | 90 /* ------------------------------------------------------------ */ |
91 /** | |
92 * @return True if resource aliases are allowed. | |
93 */ | |
94 public boolean isAliases() | |
95 { | |
96 return _aliases; | |
97 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
98 |
805 | 99 /* ------------------------------------------------------------ */ |
100 /** | |
101 * Set if resource aliases (eg symlink, 8.3 names, case insensitivity) are allowed. | |
102 * Allowing aliases can significantly increase security vulnerabilities. | |
103 * If this handler is deployed inside a ContextHandler, then the | |
104 * {@link ContextHandler#isAliases()} takes precedent. | |
105 * @param aliases True if aliases are supported. | |
106 */ | |
107 public void setAliases(boolean aliases) | |
108 { | |
109 _aliases = aliases; | |
110 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
111 |
805 | 112 /* ------------------------------------------------------------ */ |
113 /** Get the directory option. | |
114 * @return true if directories are listed. | |
115 */ | |
116 public boolean isDirectoriesListed() | |
117 { | |
118 return _directory; | |
119 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
120 |
805 | 121 /* ------------------------------------------------------------ */ |
122 /** Set the directory. | |
123 * @param directory true if directories are listed. | |
124 */ | |
125 public void setDirectoriesListed(boolean directory) | |
126 { | |
127 _directory = directory; | |
128 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
129 |
805 | 130 /* ------------------------------------------------------------ */ |
131 @Override | |
132 public void doStart() | |
133 throws Exception | |
134 { | |
991
688c39c50ee3
simplify ContextHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
827
diff
changeset
|
135 _context = ContextHandler.getCurrentContext(); |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
136 |
805 | 137 if (!_aliases && !FileResource.getCheckAliases()) |
138 throw new IllegalStateException("Alias checking disabled"); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
139 |
805 | 140 super.doStart(); |
141 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
142 |
805 | 143 /* ------------------------------------------------------------ */ |
144 /** | |
145 * @return Returns the resourceBase. | |
146 */ | |
147 public Resource getBaseResource() | |
148 { | |
149 if (_baseResource==null) | |
150 return null; | |
151 return _baseResource; | |
152 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
153 |
805 | 154 /* ------------------------------------------------------------ */ |
155 /** | |
156 * @return Returns the base resource as a string. | |
157 */ | |
158 public String getResourceBase() | |
159 { | |
160 if (_baseResource==null) | |
161 return null; | |
162 return _baseResource.toString(); | |
163 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
164 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
165 |
805 | 166 /* ------------------------------------------------------------ */ |
167 /** | |
168 * @param base The resourceBase to set. | |
169 */ | |
170 public void setBaseResource(Resource base) | |
171 { | |
172 _baseResource=base; | |
173 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
174 |
805 | 175 /* ------------------------------------------------------------ */ |
176 /** | |
177 * @param resourceBase The base resource as a string. | |
178 */ | |
179 public void setResourceBase(String resourceBase) | |
180 { | |
181 try | |
182 { | |
183 setBaseResource(Resource.newResource(resourceBase)); | |
184 } | |
185 catch (Exception e) | |
186 { | |
187 LOG.warn(e.toString()); | |
820
8e9db0bbf4f9
remove org.eclipse.jetty.util.log and upgrade slf4j
Franklin Schmidt <fschmidt@gmail.com>
parents:
805
diff
changeset
|
188 LOG.debug("",e); |
805 | 189 throw new IllegalArgumentException(resourceBase); |
190 } | |
191 } | |
192 | |
193 /* ------------------------------------------------------------ */ | |
194 /** | |
195 * @return Returns the stylesheet as a Resource. | |
196 */ | |
197 public Resource getStylesheet() | |
198 { | |
199 if(_stylesheet != null) | |
200 { | |
201 return _stylesheet; | |
202 } | |
203 else | |
204 { | |
205 if(_defaultStylesheet == null) | |
206 { | |
207 try | |
208 { | |
209 _defaultStylesheet = Resource.newResource(this.getClass().getResource("/org/eclipse/jetty/jetty-dir.css")); | |
210 } | |
211 catch(IOException e) | |
212 { | |
213 LOG.warn(e.toString()); | |
820
8e9db0bbf4f9
remove org.eclipse.jetty.util.log and upgrade slf4j
Franklin Schmidt <fschmidt@gmail.com>
parents:
805
diff
changeset
|
214 LOG.debug("",e); |
805 | 215 } |
216 } | |
217 return _defaultStylesheet; | |
218 } | |
219 } | |
220 | |
221 /* ------------------------------------------------------------ */ | |
222 /** | |
223 * @param stylesheet The location of the stylesheet to be used as a String. | |
224 */ | |
225 public void setStylesheet(String stylesheet) | |
226 { | |
227 try | |
228 { | |
229 _stylesheet = Resource.newResource(stylesheet); | |
230 if(!_stylesheet.exists()) | |
231 { | |
232 LOG.warn("unable to find custom stylesheet: " + stylesheet); | |
233 _stylesheet = null; | |
234 } | |
235 } | |
236 catch(Exception e) | |
237 { | |
238 LOG.warn(e.toString()); | |
820
8e9db0bbf4f9
remove org.eclipse.jetty.util.log and upgrade slf4j
Franklin Schmidt <fschmidt@gmail.com>
parents:
805
diff
changeset
|
239 LOG.debug("",e); |
805 | 240 throw new IllegalArgumentException(stylesheet.toString()); |
241 } | |
242 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
243 |
805 | 244 /* ------------------------------------------------------------ */ |
245 /** | |
246 * @return the cacheControl header to set on all static content. | |
247 */ | |
248 public String getCacheControl() | |
249 { | |
250 return _cacheControl.toString(); | |
251 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
252 |
805 | 253 /* ------------------------------------------------------------ */ |
254 /** | |
255 * @param cacheControl the cacheControl header to set on all static content. | |
256 */ | |
257 public void setCacheControl(String cacheControl) | |
258 { | |
259 _cacheControl=cacheControl==null?null:new ByteArrayBuffer(cacheControl); | |
260 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
261 |
992 | 262 private Resource getResource(String path) throws MalformedURLException |
805 | 263 { |
264 if (path==null || !path.startsWith("/")) | |
265 throw new MalformedURLException(path); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
266 |
805 | 267 Resource base = _baseResource; |
268 if (base==null) | |
269 { | |
270 if (_context==null) | |
271 return null; | |
992 | 272 base = _context.getBaseResource(); |
805 | 273 if (base==null) |
274 return null; | |
275 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
276 |
805 | 277 try |
278 { | |
992 | 279 path = URIUtil.canonicalPath(path); |
805 | 280 return base.addPath(path); |
281 } | |
282 catch(Exception e) | |
283 { | |
820
8e9db0bbf4f9
remove org.eclipse.jetty.util.log and upgrade slf4j
Franklin Schmidt <fschmidt@gmail.com>
parents:
805
diff
changeset
|
284 LOG.trace("",e); |
805 | 285 } |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
286 |
805 | 287 return null; |
288 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
289 |
992 | 290 private Resource getResource(HttpServletRequest request) throws MalformedURLException |
805 | 291 { |
292 String servletPath; | |
293 String pathInfo; | |
822 | 294 Boolean included = request.getAttribute(RequestDispatcher.INCLUDE_REQUEST_URI) != null; |
805 | 295 if (included != null && included.booleanValue()) |
296 { | |
822 | 297 servletPath = (String)request.getAttribute(RequestDispatcher.INCLUDE_SERVLET_PATH); |
298 pathInfo = (String)request.getAttribute(RequestDispatcher.INCLUDE_PATH_INFO); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
299 |
805 | 300 if (servletPath == null && pathInfo == null) |
301 { | |
302 servletPath = request.getServletPath(); | |
303 pathInfo = request.getPathInfo(); | |
304 } | |
305 } | |
306 else | |
307 { | |
308 servletPath = request.getServletPath(); | |
309 pathInfo = request.getPathInfo(); | |
310 } | |
311 | |
992 | 312 String pathInContext = URIUtil.addPaths(servletPath,pathInfo); |
805 | 313 return getResource(pathInContext); |
314 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
315 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
316 |
805 | 317 /* ------------------------------------------------------------ */ |
318 public String[] getWelcomeFiles() | |
319 { | |
320 return _welcomeFiles; | |
321 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
322 |
805 | 323 /* ------------------------------------------------------------ */ |
324 public void setWelcomeFiles(String[] welcomeFiles) | |
325 { | |
326 _welcomeFiles=welcomeFiles; | |
327 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
328 |
805 | 329 /* ------------------------------------------------------------ */ |
330 protected Resource getWelcome(Resource directory) throws MalformedURLException, IOException | |
331 { | |
332 for (int i=0;i<_welcomeFiles.length;i++) | |
333 { | |
334 Resource welcome=directory.addPath(_welcomeFiles[i]); | |
335 if (welcome.exists() && !welcome.isDirectory()) | |
336 return welcome; | |
337 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
338 |
805 | 339 return null; |
340 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
341 |
805 | 342 /* ------------------------------------------------------------ */ |
343 /* | |
344 * @see org.eclipse.jetty.server.Handler#handle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, int) | |
345 */ | |
346 public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException | |
347 { | |
348 if (baseRequest.isHandled()) | |
349 return; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
350 |
805 | 351 boolean skipContentBody = false; |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
352 |
805 | 353 if(!HttpMethods.GET.equals(request.getMethod())) |
354 { | |
355 if(!HttpMethods.HEAD.equals(request.getMethod())) | |
356 { | |
357 //try another handler | |
358 super.handle(target, baseRequest, request, response); | |
359 return; | |
360 } | |
361 skipContentBody = true; | |
362 } | |
363 | |
364 Resource resource = getResource(request); | |
365 | |
366 if (resource==null || !resource.exists()) | |
367 { | |
368 if (target.endsWith("/jetty-dir.css")) | |
369 { | |
370 resource = getStylesheet(); | |
371 if (resource==null) | |
372 return; | |
373 response.setContentType("text/css"); | |
374 } | |
375 else | |
376 { | |
377 //no resource - try other handlers | |
378 super.handle(target, baseRequest, request, response); | |
379 return; | |
380 } | |
381 } | |
382 | |
383 if (!_aliases && resource.getAlias()!=null) | |
384 { | |
385 LOG.info(resource+" aliased to "+resource.getAlias()); | |
386 return; | |
387 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
388 |
805 | 389 // We are going to serve something |
390 baseRequest.setHandled(true); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
391 |
805 | 392 if (resource.isDirectory()) |
393 { | |
394 if (!request.getPathInfo().endsWith(URIUtil.SLASH)) | |
395 { | |
396 response.sendRedirect(response.encodeRedirectURL(URIUtil.addPaths(request.getRequestURI(),URIUtil.SLASH))); | |
397 return; | |
398 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
399 |
805 | 400 Resource welcome=getWelcome(resource); |
401 if (welcome!=null && welcome.exists()) | |
402 resource=welcome; | |
403 else | |
404 { | |
405 doDirectory(request,response,resource); | |
406 baseRequest.setHandled(true); | |
407 return; | |
408 } | |
409 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
410 |
805 | 411 // set some headers |
412 long last_modified=resource.lastModified(); | |
413 if (last_modified>0) | |
414 { | |
415 long if_modified=request.getDateHeader(HttpHeaders.IF_MODIFIED_SINCE); | |
416 if (if_modified>0 && last_modified/1000<=if_modified/1000) | |
417 { | |
418 response.setStatus(HttpStatus.NOT_MODIFIED_304); | |
419 return; | |
420 } | |
421 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
422 |
805 | 423 Buffer mime=_mimeTypes.getMimeByExtension(resource.toString()); |
424 if (mime==null) | |
425 mime=_mimeTypes.getMimeByExtension(request.getPathInfo()); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
426 |
805 | 427 // set the headers |
428 doResponseHeaders(response,resource,mime!=null?mime.toString():null); | |
429 response.setDateHeader(HttpHeaders.LAST_MODIFIED,last_modified); | |
430 | |
431 if(skipContentBody) | |
432 return; | |
433 // Send the content | |
434 OutputStream out =null; | |
435 try {out = response.getOutputStream();} | |
436 catch(IllegalStateException e) {out = new WriterOutputStream(response.getWriter());} | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
437 |
805 | 438 // See if a short direct method can be used? |
439 if (out instanceof AbstractHttpConnection.Output) | |
440 { | |
441 // TODO file mapped buffers | |
442 ((AbstractHttpConnection.Output)out).sendContent(resource.getInputStream()); | |
443 } | |
444 else | |
445 { | |
446 // Write content normally | |
447 resource.writeTo(out,0,resource.length()); | |
448 } | |
449 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
450 |
805 | 451 /* ------------------------------------------------------------ */ |
452 protected void doDirectory(HttpServletRequest request,HttpServletResponse response, Resource resource) | |
453 throws IOException | |
454 { | |
455 if (_directory) | |
456 { | |
457 String listing = resource.getListHTML(request.getRequestURI(),request.getPathInfo().lastIndexOf("/") > 0); | |
458 response.setContentType("text/html; charset=UTF-8"); | |
459 response.getWriter().println(listing); | |
460 } | |
461 else | |
462 response.sendError(HttpStatus.FORBIDDEN_403); | |
463 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
464 |
805 | 465 /* ------------------------------------------------------------ */ |
466 /** Set the response headers. | |
467 * This method is called to set the response headers such as content type and content length. | |
468 * May be extended to add additional headers. | |
469 * @param response | |
470 * @param resource | |
471 * @param mimeType | |
472 */ | |
473 protected void doResponseHeaders(HttpServletResponse response, Resource resource, String mimeType) | |
474 { | |
475 if (mimeType!=null) | |
476 response.setContentType(mimeType); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
477 |
805 | 478 long length=resource.length(); |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
479 |
805 | 480 if (response instanceof Response) |
481 { | |
482 HttpFields fields = ((Response)response).getHttpFields(); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
483 |
805 | 484 if (length>0) |
485 fields.putLongField(HttpHeaders.CONTENT_LENGTH_BUFFER,length); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
486 |
805 | 487 if (_cacheControl!=null) |
488 fields.put(HttpHeaders.CACHE_CONTROL_BUFFER,_cacheControl); | |
489 } | |
490 else | |
491 { | |
492 if (length>0) | |
493 response.setHeader(HttpHeaders.CONTENT_LENGTH,Long.toString(length)); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
494 |
805 | 495 if (_cacheControl!=null) |
496 response.setHeader(HttpHeaders.CACHE_CONTROL,_cacheControl.toString()); | |
497 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
498 |
805 | 499 } |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
500 } |