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