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