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   * @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      @Override
85      public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
86      {
87          if (response.isCommitted() || baseRequest.isHandled())
88              return;
89  
90          baseRequest.setHandled(true);
91  
92          String method=request.getMethod();
93  
94          // little cheat for common request
95          if (_serveIcon && _favicon!=null && HttpMethod.GET.is(method) && request.getRequestURI().equals("/favicon.ico"))
96          {
97              if (request.getDateHeader(HttpHeader.IF_MODIFIED_SINCE.toString())==_faviconModified)
98                  response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
99              else
100             {
101                 response.setStatus(HttpServletResponse.SC_OK);
102                 response.setContentType("image/x-icon");
103                 response.setContentLength(_favicon.length);
104                 response.setDateHeader(HttpHeader.LAST_MODIFIED.toString(), _faviconModified);
105                 response.setHeader(HttpHeader.CACHE_CONTROL.toString(),"max-age=360000,public");
106                 response.getOutputStream().write(_favicon);
107             }
108             return;
109         }
110 
111 
112         if (!_showContexts || !HttpMethod.GET.is(method) || !request.getRequestURI().equals("/"))
113         {
114             response.sendError(HttpServletResponse.SC_NOT_FOUND);
115             return;
116         }
117 
118         response.setStatus(HttpServletResponse.SC_NOT_FOUND);
119         response.setContentType(MimeTypes.Type.TEXT_HTML.toString());
120 
121         try (ByteArrayISO8859Writer writer = new ByteArrayISO8859Writer(1500);)
122         {
123             writer.write("<HTML>\n<HEAD>\n<TITLE>Error 404 - Not Found");
124             writer.write("</TITLE>\n<BODY>\n<H2>Error 404 - Not Found.</H2>\n");
125             writer.write("No context on this server matched or handled this request.<BR>");
126             writer.write("Contexts known to this server are: <ul>");
127 
128             Server server = getServer();
129             Handler[] handlers = server==null?null:server.getChildHandlersByClass(ContextHandler.class);
130 
131             for (int i=0;handlers!=null && i<handlers.length;i++)
132             {
133                 ContextHandler context = (ContextHandler)handlers[i];
134                 if (context.isRunning())
135                 {
136                     writer.write("<li><a href=\"");
137                     if (context.getVirtualHosts()!=null && context.getVirtualHosts().length>0)
138                         writer.write("http://"+context.getVirtualHosts()[0]+":"+request.getLocalPort());
139                     writer.write(context.getContextPath());
140                     if (context.getContextPath().length()>1 && context.getContextPath().endsWith("/"))
141                         writer.write("/");
142                     writer.write("\">");
143                     writer.write(context.getContextPath());
144                     if (context.getVirtualHosts()!=null && context.getVirtualHosts().length>0)
145                         writer.write("&nbsp;@&nbsp;"+context.getVirtualHosts()[0]+":"+request.getLocalPort());
146                     writer.write("&nbsp;--->&nbsp;");
147                     writer.write(context.toString());
148                     writer.write("</a></li>\n");
149                 }
150                 else
151                 {
152                     writer.write("<li>");
153                     writer.write(context.getContextPath());
154                     if (context.getVirtualHosts()!=null && context.getVirtualHosts().length>0)
155                         writer.write("&nbsp;@&nbsp;"+context.getVirtualHosts()[0]+":"+request.getLocalPort());
156                     writer.write("&nbsp;--->&nbsp;");
157                     writer.write(context.toString());
158                     if (context.isFailed())
159                         writer.write(" [failed]");
160                     if (context.isStopped())
161                         writer.write(" [stopped]");
162                     writer.write("</li>\n");
163                 }
164             }
165 
166             writer.write("</ul><hr>");
167             
168             baseRequest.getHttpChannel().getHttpConfiguration()
169                 .writePoweredBy(writer,"<a href=\"http://eclipse.org/jetty\"><img border=0 src=\"/favicon.ico\"/></a>&nbsp;","<hr/>\n");
170 
171             writer.write("\n</BODY>\n</HTML>\n");
172             writer.flush();
173             response.setContentLength(writer.size());
174             try (OutputStream out=response.getOutputStream())
175             {
176                 writer.writeTo(out);
177             }
178         } 
179     }
180 
181     /* ------------------------------------------------------------ */
182     /**
183      * @return Returns true if the handle can server the jetty favicon.ico
184      */
185     public boolean getServeIcon()
186     {
187         return _serveIcon;
188     }
189 
190     /* ------------------------------------------------------------ */
191     /**
192      * @param serveIcon true if the handle can server the jetty favicon.ico
193      */
194     public void setServeIcon(boolean serveIcon)
195     {
196         _serveIcon = serveIcon;
197     }
198 
199     public boolean getShowContexts()
200     {
201         return _showContexts;
202     }
203 
204     public void setShowContexts(boolean show)
205     {
206         _showContexts = show;
207     }
208 
209 }