1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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.HttpHeader;
30 import org.eclipse.jetty.http.HttpMethod;
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
44
45
46
47
48
49
50
51
52 public class DefaultHandler extends AbstractHandler
53 {
54 private static final Logger LOG = Log.getLogger(DefaultHandler.class);
55
56 final long _faviconModified=(System.currentTimeMillis()/1000)*1000L;
57 byte[] _favicon;
58 boolean _serveIcon=true;
59 boolean _showContexts=true;
60
61 public DefaultHandler()
62 {
63 try
64 {
65 URL fav = this.getClass().getClassLoader().getResource("org/eclipse/jetty/favicon.ico");
66 if (fav!=null)
67 {
68 Resource r = Resource.newResource(fav);
69 _favicon=IO.readBytes(r.getInputStream());
70 }
71 }
72 catch(Exception e)
73 {
74 LOG.warn(e);
75 }
76 }
77
78
79
80
81
82 @Override
83 public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
84 {
85 if (response.isCommitted() || baseRequest.isHandled())
86 return;
87
88 baseRequest.setHandled(true);
89
90 String method=request.getMethod();
91
92
93 if (_serveIcon && _favicon!=null && HttpMethod.GET.is(method) && request.getRequestURI().equals("/favicon.ico"))
94 {
95 if (request.getDateHeader(HttpHeader.IF_MODIFIED_SINCE.toString())==_faviconModified)
96 response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
97 else
98 {
99 response.setStatus(HttpServletResponse.SC_OK);
100 response.setContentType("image/x-icon");
101 response.setContentLength(_favicon.length);
102 response.setDateHeader(HttpHeader.LAST_MODIFIED.toString(), _faviconModified);
103 response.setHeader(HttpHeader.CACHE_CONTROL.toString(),"max-age=360000,public");
104 response.getOutputStream().write(_favicon);
105 }
106 return;
107 }
108
109
110 if (!_showContexts || !HttpMethod.GET.is(method) || !request.getRequestURI().equals("/"))
111 {
112 response.sendError(HttpServletResponse.SC_NOT_FOUND);
113 return;
114 }
115
116 response.setStatus(HttpServletResponse.SC_NOT_FOUND);
117 response.setContentType(MimeTypes.Type.TEXT_HTML.toString());
118
119 try (ByteArrayISO8859Writer writer = new ByteArrayISO8859Writer(1500);)
120 {
121 writer.write("<HTML>\n<HEAD>\n<TITLE>Error 404 - Not Found");
122 writer.write("</TITLE>\n<BODY>\n<H2>Error 404 - Not Found.</H2>\n");
123 writer.write("No context on this server matched or handled this request.<BR>");
124 writer.write("Contexts known to this server are: <ul>");
125
126 Server server = getServer();
127 Handler[] handlers = server==null?null:server.getChildHandlersByClass(ContextHandler.class);
128
129 for (int i=0;handlers!=null && i<handlers.length;i++)
130 {
131 ContextHandler context = (ContextHandler)handlers[i];
132 if (context.isRunning())
133 {
134 writer.write("<li><a href=\"");
135 if (context.getVirtualHosts()!=null && context.getVirtualHosts().length>0)
136 writer.write(request.getScheme()+"://"+context.getVirtualHosts()[0]+":"+request.getLocalPort());
137 writer.write(context.getContextPath());
138 if (context.getContextPath().length()>1 && context.getContextPath().endsWith("/"))
139 writer.write("/");
140 writer.write("\">");
141 writer.write(context.getContextPath());
142 if (context.getVirtualHosts()!=null && context.getVirtualHosts().length>0)
143 writer.write(" @ "+context.getVirtualHosts()[0]+":"+request.getLocalPort());
144 writer.write(" ---> ");
145 writer.write(context.toString());
146 writer.write("</a></li>\n");
147 }
148 else
149 {
150 writer.write("<li>");
151 writer.write(context.getContextPath());
152 if (context.getVirtualHosts()!=null && context.getVirtualHosts().length>0)
153 writer.write(" @ "+context.getVirtualHosts()[0]+":"+request.getLocalPort());
154 writer.write(" ---> ");
155 writer.write(context.toString());
156 if (context.isFailed())
157 writer.write(" [failed]");
158 if (context.isStopped())
159 writer.write(" [stopped]");
160 writer.write("</li>\n");
161 }
162 }
163
164 writer.write("</ul><hr>");
165
166 baseRequest.getHttpChannel().getHttpConfiguration()
167 .writePoweredBy(writer,"<a href=\"http://eclipse.org/jetty\"><img border=0 src=\"/favicon.ico\"/></a> ","<hr/>\n");
168
169 writer.write("\n</BODY>\n</HTML>\n");
170 writer.flush();
171 response.setContentLength(writer.size());
172 try (OutputStream out=response.getOutputStream())
173 {
174 writer.writeTo(out);
175 }
176 }
177 }
178
179
180
181
182
183 public boolean getServeIcon()
184 {
185 return _serveIcon;
186 }
187
188
189
190
191
192 public void setServeIcon(boolean serveIcon)
193 {
194 _serveIcon = serveIcon;
195 }
196
197 public boolean getShowContexts()
198 {
199 return _showContexts;
200 }
201
202 public void setShowContexts(boolean show)
203 {
204 _showContexts = show;
205 }
206
207 }