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  
35  
36  /* ------------------------------------------------------------ */
37  /** Default Handler.
38   * 
39   * This handle will deal with unhandled requests in the server.
40   * For requests for favicon.ico, the Jetty icon is served. 
41   * For reqests to '/' a 404 with a list of known contexts is served.
42   * For all other requests a normal 404 is served.
43   * TODO Implement OPTIONS and TRACE methods for the server.
44   * 
45   * 
46   * @org.apache.xbean.XBean
47   */
48  public class DefaultHandler extends AbstractHandler
49  {
50      final long _faviconModified=(System.currentTimeMillis()/1000)*1000;
51      byte[] _favicon;
52      boolean _serveIcon=true;
53      boolean _showContexts=true;
54      
55      public DefaultHandler()
56      {
57          try
58          {
59              URL fav = this.getClass().getClassLoader().getResource("org/eclipse/jetty/favicon.ico");
60              if (fav!=null)
61              _favicon=IO.readBytes(fav.openStream());
62          }
63          catch(Exception e)
64          {
65              Log.warn(e);
66          }
67      }
68      
69      /* ------------------------------------------------------------ */
70      /* 
71       * @see org.eclipse.jetty.server.server.Handler#handle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, int)
72       */
73      public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
74      {              
75          if (response.isCommitted() || baseRequest.isHandled())
76              return;
77          
78          baseRequest.setHandled(true);
79          
80          String method=request.getMethod();
81  
82          // little cheat for common request
83          if (_serveIcon && _favicon!=null && method.equals(HttpMethods.GET) && request.getRequestURI().equals("/favicon.ico"))
84          {
85              if (request.getDateHeader(HttpHeaders.IF_MODIFIED_SINCE)==_faviconModified)
86                  response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
87              else
88              {
89                  response.setStatus(HttpServletResponse.SC_OK);
90                  response.setContentType("image/x-icon");
91                  response.setContentLength(_favicon.length);
92                  response.setDateHeader(HttpHeaders.LAST_MODIFIED, _faviconModified);
93                  response.setHeader(HttpHeaders.CACHE_CONTROL,"max-age=360000,public");
94                  response.getOutputStream().write(_favicon);
95              }
96              return;
97          }
98          
99          
100         if (!method.equals(HttpMethods.GET) || !request.getRequestURI().equals("/"))
101         {
102             response.sendError(HttpServletResponse.SC_NOT_FOUND);
103             return;   
104         }
105 
106         response.setStatus(HttpServletResponse.SC_NOT_FOUND);
107         response.setContentType(MimeTypes.TEXT_HTML);
108         
109         ByteArrayISO8859Writer writer = new ByteArrayISO8859Writer(1500);
110 
111         String uri=request.getRequestURI();
112         uri=StringUtil.replace(uri,"<","&lt;");
113         uri=StringUtil.replace(uri,">","&gt;");
114         
115         writer.write("<HTML>\n<HEAD>\n<TITLE>Error 404 - Not Found");
116         writer.write("</TITLE>\n<BODY>\n<H2>Error 404 - Not Found.</H2>\n");
117         writer.write("No context on this server matched or handled this request.<BR>");
118         
119         if (_showContexts)
120         {
121             writer.write("Contexts known to this server are: <ul>");
122             
123             Server server = getServer();
124             Handler[] handlers = server==null?null:server.getChildHandlersByClass(ContextHandler.class);
125      
126             for (int i=0;handlers!=null && i<handlers.length;i++)
127             {
128                 ContextHandler context = (ContextHandler)handlers[i];
129                 if (context.isRunning())
130                 {
131                     writer.write("<li><a href=\"");
132                     if (context.getVirtualHosts()!=null && context.getVirtualHosts().length>0)
133                         writer.write("http://"+context.getVirtualHosts()[0]+":"+request.getLocalPort());
134                     writer.write(context.getContextPath());
135                     if (context.getContextPath().length()>1 && context.getContextPath().endsWith("/"))
136                         writer.write("/");
137                     writer.write("\">");
138                     writer.write(context.getContextPath());
139                     if (context.getVirtualHosts()!=null && context.getVirtualHosts().length>0)
140                         writer.write("&nbsp;@&nbsp;"+context.getVirtualHosts()[0]+":"+request.getLocalPort());
141                     writer.write("&nbsp;--->&nbsp;");
142                     writer.write(context.toString());
143                     writer.write("</a></li>\n");
144                 }
145                 else
146                 {
147                     writer.write("<li>");
148                     writer.write(context.getContextPath());
149                     if (context.getVirtualHosts()!=null && context.getVirtualHosts().length>0)
150                         writer.write("&nbsp;@&nbsp;"+context.getVirtualHosts()[0]+":"+request.getLocalPort());
151                     writer.write("&nbsp;--->&nbsp;");
152                     writer.write(context.toString());
153                     if (context.isFailed())
154                         writer.write(" [failed]");
155                     if (context.isStopped())
156                         writer.write(" [stopped]");
157                     writer.write("</li>\n");
158                 }
159             }
160         }
161         
162         for (int i=0;i<10;i++)
163             writer.write("\n<!-- Padding for IE                  -->");
164         
165         writer.write("\n</BODY>\n</HTML>\n");
166         writer.flush();
167         response.setContentLength(writer.size());
168         OutputStream out=response.getOutputStream();
169         writer.writeTo(out);
170         out.close();
171     }
172 
173     /* ------------------------------------------------------------ */
174     /**
175      * @return Returns true if the handle can server the jetty favicon.ico
176      */
177     public boolean getServeIcon()
178     {
179         return _serveIcon;
180     }
181 
182     /* ------------------------------------------------------------ */
183     /**
184      * @param serveIcon true if the handle can server the jetty favicon.ico
185      */
186     public void setServeIcon(boolean serveIcon)
187     {
188         _serveIcon = serveIcon;
189     }
190     
191     public boolean getShowContexts()
192     {
193         return _showContexts;
194     }
195 
196     public void setShowContexts(boolean show)
197     {
198         _showContexts = show;
199     }
200 
201 }