View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2015 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.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.Jetty;
38  import org.eclipse.jetty.util.log.Log;
39  import org.eclipse.jetty.util.log.Logger;
40  import org.eclipse.jetty.util.resource.Resource;
41  
42  
43  /* ------------------------------------------------------------ */
44  /** Default Handler.
45   *
46   * This handle will deal with unhandled requests in the server.
47   * For requests for favicon.ico, the Jetty icon is served.
48   * For reqests to '/' a 404 with a list of known contexts is served.
49   * For all other requests a normal 404 is served.
50   *
51   *
52   */
53  public class DefaultHandler extends AbstractHandler
54  {
55      private static final Logger LOG = Log.getLogger(DefaultHandler.class);
56  
57      final long _faviconModified=(System.currentTimeMillis()/1000)*1000L;
58      byte[] _favicon;
59      boolean _serveIcon=true;
60      boolean _showContexts=true;
61  
62      public DefaultHandler()
63      {
64          try
65          {
66              URL fav = this.getClass().getClassLoader().getResource("org/eclipse/jetty/favicon.ico");
67              if (fav!=null)
68              {
69                  Resource r = Resource.newResource(fav);
70                  _favicon=IO.readBytes(r.getInputStream());
71              }
72          }
73          catch(Exception e)
74          {
75              LOG.warn(e);
76          }
77      }
78  
79      /* ------------------------------------------------------------ */
80      /*
81       * @see org.eclipse.jetty.server.server.Handler#handle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, int)
82       */
83      @Override
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 && HttpMethod.GET.is(method) && request.getRequestURI().equals("/favicon.ico"))
95          {
96              if (request.getDateHeader(HttpHeader.IF_MODIFIED_SINCE.toString())==_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(HttpHeader.LAST_MODIFIED.toString(), _faviconModified);
104                 response.setHeader(HttpHeader.CACHE_CONTROL.toString(),"max-age=360000,public");
105                 response.getOutputStream().write(_favicon);
106             }
107             return;
108         }
109 
110 
111         if (!_showContexts || !HttpMethod.GET.is(method) || !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.Type.TEXT_HTML.toString());
119 
120         try (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             writer.write("Contexts known to this server are: <ul>");
126 
127             Server server = getServer();
128             Handler[] handlers = server==null?null:server.getChildHandlersByClass(ContextHandler.class);
129 
130             for (int i=0;handlers!=null && i<handlers.length;i++)
131             {
132                 ContextHandler context = (ContextHandler)handlers[i];
133                 if (context.isRunning())
134                 {
135                     writer.write("<li><a href=\"");
136                     if (context.getVirtualHosts()!=null && context.getVirtualHosts().length>0)
137                         writer.write("http://"+context.getVirtualHosts()[0]+":"+request.getLocalPort());
138                     writer.write(context.getContextPath());
139                     if (context.getContextPath().length()>1 && context.getContextPath().endsWith("/"))
140                         writer.write("/");
141                     writer.write("\">");
142                     writer.write(context.getContextPath());
143                     if (context.getVirtualHosts()!=null && context.getVirtualHosts().length>0)
144                         writer.write("&nbsp;@&nbsp;"+context.getVirtualHosts()[0]+":"+request.getLocalPort());
145                     writer.write("&nbsp;--->&nbsp;");
146                     writer.write(context.toString());
147                     writer.write("</a></li>\n");
148                 }
149                 else
150                 {
151                     writer.write("<li>");
152                     writer.write(context.getContextPath());
153                     if (context.getVirtualHosts()!=null && context.getVirtualHosts().length>0)
154                         writer.write("&nbsp;@&nbsp;"+context.getVirtualHosts()[0]+":"+request.getLocalPort());
155                     writer.write("&nbsp;--->&nbsp;");
156                     writer.write(context.toString());
157                     if (context.isFailed())
158                         writer.write(" [failed]");
159                     if (context.isStopped())
160                         writer.write(" [stopped]");
161                     writer.write("</li>\n");
162                 }
163             }
164 
165             writer.write("</ul><hr>");
166             
167             baseRequest.getHttpChannel().getHttpConfiguration()
168                 .writePoweredBy(writer,"<a href=\"http://eclipse.org/jetty\"><img border=0 src=\"/favicon.ico\"/></a>&nbsp;","<hr/>\n");
169 
170             writer.write("\n</BODY>\n</HTML>\n");
171             writer.flush();
172             response.setContentLength(writer.size());
173             try (OutputStream out=response.getOutputStream())
174             {
175                 writer.writeTo(out);
176             }
177         } 
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 }