comparison src/org/eclipse/jetty/server/handler/DefaultHandler.java @ 802:3428c60d7cfc

replace jetty jars with source
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 07 Sep 2016 21:15:48 -0600
parents
children 947b11aa3157
comparison
equal deleted inserted replaced
801:6a21393191c1 802:3428c60d7cfc
1 //
2 // ========================================================================
3 // Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
4 // ------------------------------------------------------------------------
5 // All rights reserved. This program and the accompanying materials
6 // are made available under the terms of the Eclipse Public License v1.0
7 // and Apache License v2.0 which accompanies this distribution.
8 //
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
11 //
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
14 //
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
17 //
18
19 package org.eclipse.jetty.server.handler;
20
21 import java.io.IOException;
22 import java.io.OutputStream;
23 import java.net.URL;
24
25 import javax.servlet.ServletException;
26 import javax.servlet.http.HttpServletRequest;
27 import javax.servlet.http.HttpServletResponse;
28
29 import org.eclipse.jetty.http.HttpHeaders;
30 import org.eclipse.jetty.http.HttpMethods;
31 import org.eclipse.jetty.http.MimeTypes;
32 import org.eclipse.jetty.server.Handler;
33 import org.eclipse.jetty.server.Request;
34 import org.eclipse.jetty.server.Server;
35 import org.eclipse.jetty.util.ByteArrayISO8859Writer;
36 import org.eclipse.jetty.util.IO;
37 import org.eclipse.jetty.util.log.Log;
38 import org.eclipse.jetty.util.log.Logger;
39 import org.eclipse.jetty.util.resource.Resource;
40
41
42 /* ------------------------------------------------------------ */
43 /** Default Handler.
44 *
45 * This handle will deal with unhandled requests in the server.
46 * For requests for favicon.ico, the Jetty icon is served.
47 * For reqests to '/' a 404 with a list of known contexts is served.
48 * For all other requests a normal 404 is served.
49 * TODO Implement OPTIONS and TRACE methods for the server.
50 *
51 *
52 * @org.apache.xbean.XBean
53 */
54 public class DefaultHandler extends AbstractHandler
55 {
56 private static final Logger LOG = Log.getLogger(DefaultHandler.class);
57
58 final long _faviconModified=(System.currentTimeMillis()/1000)*1000L;
59 byte[] _favicon;
60 boolean _serveIcon=true;
61 boolean _showContexts=true;
62
63 public DefaultHandler()
64 {
65 try
66 {
67 URL fav = this.getClass().getClassLoader().getResource("org/eclipse/jetty/favicon.ico");
68 if (fav!=null)
69 {
70 Resource r = Resource.newResource(fav);
71 _favicon=IO.readBytes(r.getInputStream());
72 }
73 }
74 catch(Exception e)
75 {
76 LOG.warn(e);
77 }
78 }
79
80 /* ------------------------------------------------------------ */
81 /*
82 * @see org.eclipse.jetty.server.server.Handler#handle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, int)
83 */
84 public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
85 {
86 if (response.isCommitted() || baseRequest.isHandled())
87 return;
88
89 baseRequest.setHandled(true);
90
91 String method=request.getMethod();
92
93 // little cheat for common request
94 if (_serveIcon && _favicon!=null && method.equals(HttpMethods.GET) && request.getRequestURI().equals("/favicon.ico"))
95 {
96 if (request.getDateHeader(HttpHeaders.IF_MODIFIED_SINCE)==_faviconModified)
97 response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
98 else
99 {
100 response.setStatus(HttpServletResponse.SC_OK);
101 response.setContentType("image/x-icon");
102 response.setContentLength(_favicon.length);
103 response.setDateHeader(HttpHeaders.LAST_MODIFIED, _faviconModified);
104 response.setHeader(HttpHeaders.CACHE_CONTROL,"max-age=360000,public");
105 response.getOutputStream().write(_favicon);
106 }
107 return;
108 }
109
110
111 if (!method.equals(HttpMethods.GET) || !request.getRequestURI().equals("/"))
112 {
113 response.sendError(HttpServletResponse.SC_NOT_FOUND);
114 return;
115 }
116
117 response.setStatus(HttpServletResponse.SC_NOT_FOUND);
118 response.setContentType(MimeTypes.TEXT_HTML);
119
120 ByteArrayISO8859Writer writer = new ByteArrayISO8859Writer(1500);
121
122 writer.write("<HTML>\n<HEAD>\n<TITLE>Error 404 - Not Found");
123 writer.write("</TITLE>\n<BODY>\n<H2>Error 404 - Not Found.</H2>\n");
124 writer.write("No context on this server matched or handled this request.<BR>");
125
126 if (_showContexts)
127 {
128 writer.write("Contexts known to this server are: <ul>");
129
130 Server server = getServer();
131 Handler[] handlers = server==null?null:server.getChildHandlersByClass(ContextHandler.class);
132
133 for (int i=0;handlers!=null && i<handlers.length;i++)
134 {
135 ContextHandler context = (ContextHandler)handlers[i];
136 if (context.isRunning())
137 {
138 writer.write("<li><a href=\"");
139 if (context.getVirtualHosts()!=null && context.getVirtualHosts().length>0)
140 writer.write("http://"+context.getVirtualHosts()[0]+":"+request.getLocalPort());
141 writer.write(context.getContextPath());
142 if (context.getContextPath().length()>1 && context.getContextPath().endsWith("/"))
143 writer.write("/");
144 writer.write("\">");
145 writer.write(context.getContextPath());
146 if (context.getVirtualHosts()!=null && context.getVirtualHosts().length>0)
147 writer.write("&nbsp;@&nbsp;"+context.getVirtualHosts()[0]+":"+request.getLocalPort());
148 writer.write("&nbsp;--->&nbsp;");
149 writer.write(context.toString());
150 writer.write("</a></li>\n");
151 }
152 else
153 {
154 writer.write("<li>");
155 writer.write(context.getContextPath());
156 if (context.getVirtualHosts()!=null && context.getVirtualHosts().length>0)
157 writer.write("&nbsp;@&nbsp;"+context.getVirtualHosts()[0]+":"+request.getLocalPort());
158 writer.write("&nbsp;--->&nbsp;");
159 writer.write(context.toString());
160 if (context.isFailed())
161 writer.write(" [failed]");
162 if (context.isStopped())
163 writer.write(" [stopped]");
164 writer.write("</li>\n");
165 }
166 }
167 }
168
169 for (int i=0;i<10;i++)
170 writer.write("\n<!-- Padding for IE -->");
171
172 writer.write("\n</BODY>\n</HTML>\n");
173 writer.flush();
174 response.setContentLength(writer.size());
175 OutputStream out=response.getOutputStream();
176 writer.writeTo(out);
177 out.close();
178 }
179
180 /* ------------------------------------------------------------ */
181 /**
182 * @return Returns true if the handle can server the jetty favicon.ico
183 */
184 public boolean getServeIcon()
185 {
186 return _serveIcon;
187 }
188
189 /* ------------------------------------------------------------ */
190 /**
191 * @param serveIcon true if the handle can server the jetty favicon.ico
192 */
193 public void setServeIcon(boolean serveIcon)
194 {
195 _serveIcon = serveIcon;
196 }
197
198 public boolean getShowContexts()
199 {
200 return _showContexts;
201 }
202
203 public void setShowContexts(boolean show)
204 {
205 _showContexts = show;
206 }
207
208 }