Mercurial Hosting > luan
annotate src/org/eclipse/jetty/server/handler/ResourceHandler.java @ 994:4e9d373bf6e9
remove ContextHandler.Context
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 18 Oct 2016 16:34:18 -0600 |
parents | 0608a6664bee |
children | 7d28be82ab75 |
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 |
994
4e9d373bf6e9
remove ContextHandler.Context
Franklin Schmidt <fschmidt@gmail.com>
parents:
992
diff
changeset
|
255 private Resource getResource(Request request,String path) throws MalformedURLException |
805 | 256 { |
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 |
994
4e9d373bf6e9
remove ContextHandler.Context
Franklin Schmidt <fschmidt@gmail.com>
parents:
992
diff
changeset
|
284 private Resource getResource(Request request) throws MalformedURLException |
805 | 285 { |
286 String servletPath; | |
287 String pathInfo; | |
822 | 288 Boolean included = request.getAttribute(RequestDispatcher.INCLUDE_REQUEST_URI) != null; |
805 | 289 if (included != null && included.booleanValue()) |
290 { | |
822 | 291 servletPath = (String)request.getAttribute(RequestDispatcher.INCLUDE_SERVLET_PATH); |
292 pathInfo = (String)request.getAttribute(RequestDispatcher.INCLUDE_PATH_INFO); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
293 |
805 | 294 if (servletPath == null && pathInfo == null) |
295 { | |
296 servletPath = request.getServletPath(); | |
297 pathInfo = request.getPathInfo(); | |
298 } | |
299 } | |
300 else | |
301 { | |
302 servletPath = request.getServletPath(); | |
303 pathInfo = request.getPathInfo(); | |
304 } | |
305 | |
992 | 306 String pathInContext = URIUtil.addPaths(servletPath,pathInfo); |
994
4e9d373bf6e9
remove ContextHandler.Context
Franklin Schmidt <fschmidt@gmail.com>
parents:
992
diff
changeset
|
307 return getResource(request,pathInContext); |
805 | 308 } |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
309 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
310 |
805 | 311 /* ------------------------------------------------------------ */ |
312 public String[] getWelcomeFiles() | |
313 { | |
314 return _welcomeFiles; | |
315 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
316 |
805 | 317 /* ------------------------------------------------------------ */ |
318 public void setWelcomeFiles(String[] welcomeFiles) | |
319 { | |
320 _welcomeFiles=welcomeFiles; | |
321 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
322 |
805 | 323 /* ------------------------------------------------------------ */ |
324 protected Resource getWelcome(Resource directory) throws MalformedURLException, IOException | |
325 { | |
326 for (int i=0;i<_welcomeFiles.length;i++) | |
327 { | |
328 Resource welcome=directory.addPath(_welcomeFiles[i]); | |
329 if (welcome.exists() && !welcome.isDirectory()) | |
330 return welcome; | |
331 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
332 |
805 | 333 return null; |
334 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
335 |
805 | 336 /* ------------------------------------------------------------ */ |
337 /* | |
338 * @see org.eclipse.jetty.server.Handler#handle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, int) | |
339 */ | |
340 public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException | |
341 { | |
342 if (baseRequest.isHandled()) | |
343 return; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
344 |
805 | 345 boolean skipContentBody = false; |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
346 |
805 | 347 if(!HttpMethods.GET.equals(request.getMethod())) |
348 { | |
349 if(!HttpMethods.HEAD.equals(request.getMethod())) | |
350 { | |
351 //try another handler | |
352 super.handle(target, baseRequest, request, response); | |
353 return; | |
354 } | |
355 skipContentBody = true; | |
356 } | |
357 | |
994
4e9d373bf6e9
remove ContextHandler.Context
Franklin Schmidt <fschmidt@gmail.com>
parents:
992
diff
changeset
|
358 Resource resource = getResource(baseRequest); |
805 | 359 |
360 if (resource==null || !resource.exists()) | |
361 { | |
362 if (target.endsWith("/jetty-dir.css")) | |
363 { | |
364 resource = getStylesheet(); | |
365 if (resource==null) | |
366 return; | |
367 response.setContentType("text/css"); | |
368 } | |
369 else | |
370 { | |
371 //no resource - try other handlers | |
372 super.handle(target, baseRequest, request, response); | |
373 return; | |
374 } | |
375 } | |
376 | |
377 if (!_aliases && resource.getAlias()!=null) | |
378 { | |
379 LOG.info(resource+" aliased to "+resource.getAlias()); | |
380 return; | |
381 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
382 |
805 | 383 // We are going to serve something |
384 baseRequest.setHandled(true); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
385 |
805 | 386 if (resource.isDirectory()) |
387 { | |
388 if (!request.getPathInfo().endsWith(URIUtil.SLASH)) | |
389 { | |
390 response.sendRedirect(response.encodeRedirectURL(URIUtil.addPaths(request.getRequestURI(),URIUtil.SLASH))); | |
391 return; | |
392 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
393 |
805 | 394 Resource welcome=getWelcome(resource); |
395 if (welcome!=null && welcome.exists()) | |
396 resource=welcome; | |
397 else | |
398 { | |
399 doDirectory(request,response,resource); | |
400 baseRequest.setHandled(true); | |
401 return; | |
402 } | |
403 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
404 |
805 | 405 // set some headers |
406 long last_modified=resource.lastModified(); | |
407 if (last_modified>0) | |
408 { | |
409 long if_modified=request.getDateHeader(HttpHeaders.IF_MODIFIED_SINCE); | |
410 if (if_modified>0 && last_modified/1000<=if_modified/1000) | |
411 { | |
412 response.setStatus(HttpStatus.NOT_MODIFIED_304); | |
413 return; | |
414 } | |
415 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
416 |
805 | 417 Buffer mime=_mimeTypes.getMimeByExtension(resource.toString()); |
418 if (mime==null) | |
419 mime=_mimeTypes.getMimeByExtension(request.getPathInfo()); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
420 |
805 | 421 // set the headers |
422 doResponseHeaders(response,resource,mime!=null?mime.toString():null); | |
423 response.setDateHeader(HttpHeaders.LAST_MODIFIED,last_modified); | |
424 | |
425 if(skipContentBody) | |
426 return; | |
427 // Send the content | |
428 OutputStream out =null; | |
429 try {out = response.getOutputStream();} | |
430 catch(IllegalStateException e) {out = new WriterOutputStream(response.getWriter());} | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
431 |
805 | 432 // See if a short direct method can be used? |
433 if (out instanceof AbstractHttpConnection.Output) | |
434 { | |
435 // TODO file mapped buffers | |
436 ((AbstractHttpConnection.Output)out).sendContent(resource.getInputStream()); | |
437 } | |
438 else | |
439 { | |
440 // Write content normally | |
441 resource.writeTo(out,0,resource.length()); | |
442 } | |
443 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
444 |
805 | 445 /* ------------------------------------------------------------ */ |
446 protected void doDirectory(HttpServletRequest request,HttpServletResponse response, Resource resource) | |
447 throws IOException | |
448 { | |
449 if (_directory) | |
450 { | |
451 String listing = resource.getListHTML(request.getRequestURI(),request.getPathInfo().lastIndexOf("/") > 0); | |
452 response.setContentType("text/html; charset=UTF-8"); | |
453 response.getWriter().println(listing); | |
454 } | |
455 else | |
456 response.sendError(HttpStatus.FORBIDDEN_403); | |
457 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
458 |
805 | 459 /* ------------------------------------------------------------ */ |
460 /** Set the response headers. | |
461 * This method is called to set the response headers such as content type and content length. | |
462 * May be extended to add additional headers. | |
463 * @param response | |
464 * @param resource | |
465 * @param mimeType | |
466 */ | |
467 protected void doResponseHeaders(HttpServletResponse response, Resource resource, String mimeType) | |
468 { | |
469 if (mimeType!=null) | |
470 response.setContentType(mimeType); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
471 |
805 | 472 long length=resource.length(); |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
473 |
805 | 474 if (response instanceof Response) |
475 { | |
476 HttpFields fields = ((Response)response).getHttpFields(); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
477 |
805 | 478 if (length>0) |
479 fields.putLongField(HttpHeaders.CONTENT_LENGTH_BUFFER,length); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
480 |
805 | 481 if (_cacheControl!=null) |
482 fields.put(HttpHeaders.CACHE_CONTROL_BUFFER,_cacheControl); | |
483 } | |
484 else | |
485 { | |
486 if (length>0) | |
487 response.setHeader(HttpHeaders.CONTENT_LENGTH,Long.toString(length)); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
488 |
805 | 489 if (_cacheControl!=null) |
490 response.setHeader(HttpHeaders.CACHE_CONTROL,_cacheControl.toString()); | |
491 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
492 |
805 | 493 } |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
494 } |