View Javadoc

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