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.HttpConnection;
29  import org.eclipse.jetty.server.Request;
30  import org.eclipse.jetty.server.Server;
31  import org.eclipse.jetty.util.ByteArrayISO8859Writer;
32  import org.eclipse.jetty.util.IO;
33  import org.eclipse.jetty.util.StringUtil;
34  import org.eclipse.jetty.util.log.Log;
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      final long _faviconModified=(System.currentTimeMillis()/1000)*1000;
52      byte[] _favicon;
53      boolean _serveIcon=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         writer.write("Contexts known to this server are: <ul>");
119 
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         for (int i=0;i<10;i++)
160             writer.write("\n<!-- Padding for IE                  -->");
161         
162         writer.write("\n</BODY>\n</HTML>\n");
163         writer.flush();
164         response.setContentLength(writer.size());
165         OutputStream out=response.getOutputStream();
166         writer.writeTo(out);
167         out.close();
168     }
169 
170     /* ------------------------------------------------------------ */
171     /**
172      * @return Returns true if the handle can server the jetty favicon.ico
173      */
174     public boolean getServeIcon()
175     {
176         return _serveIcon;
177     }
178 
179     /* ------------------------------------------------------------ */
180     /**
181      * @param serveIcon true if the handle can server the jetty favicon.ico
182      */
183     public void setServeIcon(boolean serveIcon)
184     {
185         _serveIcon = serveIcon;
186     }
187 
188 
189 }