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