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