annotate src/org/eclipse/jetty/server/handler/DefaultHandler.java @ 991:688c39c50ee3

simplify ContextHandler
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 18 Oct 2016 00:22:46 -0600
parents 8e9db0bbf4f9
children 32d4b569567c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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.URL;
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.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.MimeTypes;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 import org.eclipse.jetty.server.Handler;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 import org.eclipse.jetty.server.Request;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34 import org.eclipse.jetty.server.Server;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 import org.eclipse.jetty.util.ByteArrayISO8859Writer;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36 import org.eclipse.jetty.util.IO;
820
8e9db0bbf4f9 remove org.eclipse.jetty.util.log and upgrade slf4j
Franklin Schmidt <fschmidt@gmail.com>
parents: 807
diff changeset
37 import org.slf4j.Logger;
8e9db0bbf4f9 remove org.eclipse.jetty.util.log and upgrade slf4j
Franklin Schmidt <fschmidt@gmail.com>
parents: 807
diff changeset
38 import org.slf4j.LoggerFactory;
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39 import org.eclipse.jetty.util.resource.Resource;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
40
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
41
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
42 /* ------------------------------------------------------------ */
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
43 /** Default Handler.
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 * This handle will deal with unhandled requests in the server.
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46 * For requests for favicon.ico, the Jetty icon is served.
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47 * For reqests to '/' a 404 with a list of known contexts is served.
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48 * For all other requests a normal 404 is served.
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
49 * TODO Implement OPTIONS and TRACE methods for the server.
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 *
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
52 * @org.apache.xbean.XBean
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 public class DefaultHandler extends AbstractHandler
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
55 {
820
8e9db0bbf4f9 remove org.eclipse.jetty.util.log and upgrade slf4j
Franklin Schmidt <fschmidt@gmail.com>
parents: 807
diff changeset
56 private static final Logger LOG = LoggerFactory.getLogger(DefaultHandler.class);
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
57
807
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
58 boolean _showContexts=true;
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
59
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
60 /* ------------------------------------------------------------ */
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
61 /*
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
62 * @see org.eclipse.jetty.server.server.Handler#handle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, int)
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
63 */
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
64 public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
65 {
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
66 if (response.isCommitted() || baseRequest.isHandled())
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
67 return;
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
68
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
69 baseRequest.setHandled(true);
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
70
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
71 String method=request.getMethod();
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
72
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
73 if (!method.equals(HttpMethods.GET) || !request.getRequestURI().equals("/"))
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
74 {
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
75 response.sendError(HttpServletResponse.SC_NOT_FOUND);
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
76 return;
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
77 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
78
807
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
79 response.setStatus(HttpServletResponse.SC_NOT_FOUND);
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
80 response.setContentType(MimeTypes.TEXT_HTML);
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
81
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
82 ByteArrayISO8859Writer writer = new ByteArrayISO8859Writer(1500);
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
83
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
84 writer.write("<HTML>\n<HEAD>\n<TITLE>Error 404 - Not Found");
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
85 writer.write("</TITLE>\n<BODY>\n<H2>Error 404 - Not Found.</H2>\n");
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
86 writer.write("No context on this server matched or handled this request.<BR>");
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
87
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
88 if (_showContexts)
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
89 {
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
90 writer.write("Contexts known to this server are: <ul>");
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
91
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
92 Server server = getServer();
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
93 Handler[] handlers = server==null?null:server.getChildHandlersByClass(ContextHandler.class);
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
94
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
95 for (int i=0;handlers!=null && i<handlers.length;i++)
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
96 {
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
97 ContextHandler context = (ContextHandler)handlers[i];
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
98 if (context.isRunning())
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
99 {
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
100 writer.write("<li><a href=\"");
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
101 writer.write(context.getContextPath());
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
102 if (context.getContextPath().length()>1 && context.getContextPath().endsWith("/"))
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
103 writer.write("/");
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
104 writer.write("\">");
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
105 writer.write(context.getContextPath());
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
106 writer.write("&nbsp;--->&nbsp;");
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
107 writer.write(context.toString());
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
108 writer.write("</a></li>\n");
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
109 }
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
110 else
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
111 {
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
112 writer.write("<li>");
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
113 writer.write(context.getContextPath());
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
114 writer.write("&nbsp;--->&nbsp;");
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
115 writer.write(context.toString());
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
116 if (context.isFailed())
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
117 writer.write(" [failed]");
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
118 if (context.isStopped())
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
119 writer.write(" [stopped]");
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
120 writer.write("</li>\n");
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
121 }
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
122 }
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
123 }
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
124
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
125 for (int i=0;i<10;i++)
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
126 writer.write("\n<!-- Padding for IE -->");
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
127
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
128 writer.write("\n</BODY>\n</HTML>\n");
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
129 writer.flush();
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
130 response.setContentLength(writer.size());
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
131 OutputStream out=response.getOutputStream();
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
132 writer.writeTo(out);
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
133 out.close();
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
134 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
135
807
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
136 public boolean getShowContexts()
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
137 {
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
138 return _showContexts;
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
139 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
140
807
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
141 public void setShowContexts(boolean show)
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
142 {
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
143 _showContexts = show;
947b11aa3157 remove jetty favicon
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
144 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
145
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
146 }