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.StringUtil;
33  import org.eclipse.jetty.util.log.Log;
34  import org.eclipse.jetty.util.log.Logger;
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              _favicon=IO.readBytes(fav.openStream());
65          }
66          catch(Exception e)
67          {
68              LOG.warn(e);
69          }
70      }
71      
72      /* ------------------------------------------------------------ */
73      /* 
74       * @see org.eclipse.jetty.server.server.Handler#handle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, int)
75       */
76      public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
77      {              
78          if (response.isCommitted() || baseRequest.isHandled())
79              return;
80          
81          baseRequest.setHandled(true);
82          
83          String method=request.getMethod();
84  
85          // little cheat for common request
86          if (_serveIcon && _favicon!=null && method.equals(HttpMethods.GET) && request.getRequestURI().equals("/favicon.ico"))
87          {
88              if (request.getDateHeader(HttpHeaders.IF_MODIFIED_SINCE)==_faviconModified)
89                  response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
90              else
91              {
92                  response.setStatus(HttpServletResponse.SC_OK);
93                  response.setContentType("image/x-icon");
94                  response.setContentLength(_favicon.length);
95                  response.setDateHeader(HttpHeaders.LAST_MODIFIED, _faviconModified);
96                  response.setHeader(HttpHeaders.CACHE_CONTROL,"max-age=360000,public");
97                  response.getOutputStream().write(_favicon);
98              }
99              return;
100         }
101         
102         
103         if (!method.equals(HttpMethods.GET) || !request.getRequestURI().equals("/"))
104         {
105             response.sendError(HttpServletResponse.SC_NOT_FOUND);
106             return;   
107         }
108 
109         response.setStatus(HttpServletResponse.SC_NOT_FOUND);
110         response.setContentType(MimeTypes.TEXT_HTML);
111         
112         ByteArrayISO8859Writer writer = new ByteArrayISO8859Writer(1500);
113 
114         String uri=request.getRequestURI();
115         uri=StringUtil.replace(uri,"<","&lt;");
116         uri=StringUtil.replace(uri,">","&gt;");
117         
118         writer.write("<HTML>\n<HEAD>\n<TITLE>Error 404 - Not Found");
119         writer.write("</TITLE>\n<BODY>\n<H2>Error 404 - Not Found.</H2>\n");
120         writer.write("No context on this server matched or handled this request.<BR>");
121         
122         if (_showContexts)
123         {
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("http://"+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("&nbsp;@&nbsp;"+context.getVirtualHosts()[0]+":"+request.getLocalPort());
144                     writer.write("&nbsp;--->&nbsp;");
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("&nbsp;@&nbsp;"+context.getVirtualHosts()[0]+":"+request.getLocalPort());
154                     writer.write("&nbsp;--->&nbsp;");
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         
165         for (int i=0;i<10;i++)
166             writer.write("\n<!-- Padding for IE                  -->");
167         
168         writer.write("\n</BODY>\n</HTML>\n");
169         writer.flush();
170         response.setContentLength(writer.size());
171         OutputStream out=response.getOutputStream();
172         writer.writeTo(out);
173         out.close();
174     }
175 
176     /* ------------------------------------------------------------ */
177     /**
178      * @return Returns true if the handle can server the jetty favicon.ico
179      */
180     public boolean getServeIcon()
181     {
182         return _serveIcon;
183     }
184 
185     /* ------------------------------------------------------------ */
186     /**
187      * @param serveIcon true if the handle can server the jetty favicon.ico
188      */
189     public void setServeIcon(boolean serveIcon)
190     {
191         _serveIcon = serveIcon;
192     }
193     
194     public boolean getShowContexts()
195     {
196         return _showContexts;
197     }
198 
199     public void setShowContexts(boolean show)
200     {
201         _showContexts = show;
202     }
203 
204 }