View Javadoc

1   package org.eclipse.jetty.http.spi;
2   //========================================================================
3   //Copyright (c) 2004-2009 Mort Bay Consulting Pty. Ltd.
4   //------------------------------------------------------------------------
5   //All rights reserved. This program and the accompanying materials
6   //are made available under the terms of the Eclipse Public License v1.0
7   //and Apache License v2.0 which accompanies this distribution.
8   //The Eclipse Public License is available at 
9   //http://www.eclipse.org/legal/epl-v10.html
10  //The Apache License v2.0 is available at
11  //http://www.opensource.org/licenses/apache2.0.php
12  //You may elect to redistribute this code under either of these licenses. 
13  //========================================================================
14  
15  import com.sun.net.httpserver.Authenticator;
16  import com.sun.net.httpserver.Authenticator.Result;
17  import com.sun.net.httpserver.HttpContext;
18  import com.sun.net.httpserver.HttpHandler;
19  import com.sun.net.httpserver.HttpPrincipal;
20  import org.eclipse.jetty.server.HttpConnection;
21  import org.eclipse.jetty.server.Request;
22  import org.eclipse.jetty.server.handler.ContextHandler;
23  
24  import javax.servlet.ServletException;
25  import javax.servlet.http.HttpServletRequest;
26  import javax.servlet.http.HttpServletResponse;
27  import java.io.IOException;
28  import java.io.PrintWriter;
29  
30  /**
31   * Jetty handler that bridges requests to {@link HttpHandler}.
32   */
33  public class HttpSpiContextHandler extends ContextHandler
34  {
35  
36      private HttpContext _httpContext;
37  
38      private HttpHandler _httpHandler;
39  
40  
41      public HttpSpiContextHandler(HttpContext httpContext, HttpHandler httpHandler)
42      {
43          this._httpContext = httpContext;
44          this._httpHandler = httpHandler;
45      }
46  
47      @Override
48      public void doScope(String target, Request baseRequest, HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException
49      {
50          if (!target.startsWith(getContextPath())) return;
51  
52          JettyHttpExchange jettyHttpExchange = new JettyHttpExchange(_httpContext, req, resp);
53  
54          // TODO: add filters processing
55  
56          try
57          {
58              Authenticator auth = _httpContext.getAuthenticator();
59              if (auth != null)
60                  handleAuthentication(resp, jettyHttpExchange, auth);
61              else
62                  _httpHandler.handle(jettyHttpExchange);
63          }
64          catch(Exception ex)
65          {
66              PrintWriter writer = new PrintWriter(jettyHttpExchange.getResponseBody());
67              
68              resp.setStatus(500);
69              writer.println("<h2>HTTP ERROR: 500</h2>");
70              writer.println("<pre>INTERNAL_SERVER_ERROR</pre>");
71              writer.println("<p>RequestURI=" + req.getRequestURI() + "</p>");
72              
73              writer.println("<pre>");
74              ex.printStackTrace(writer);
75              writer.println("</pre>");
76              
77              writer.println("<p><i><small><a href=\"http://jetty.mortbay.org\">Powered by jetty://</a></small></i></p>");
78              
79              writer.close();
80          }
81          finally
82          {
83              Request base_request = (req instanceof Request) ? (Request)req:HttpConnection.getCurrentConnection().getRequest();
84              base_request.setHandled(true);
85          }
86          
87      }
88  
89  
90      private void handleAuthentication(HttpServletResponse resp, JettyHttpExchange jettyHttpExchange, Authenticator auth) throws IOException
91      {
92          Result result = auth.authenticate(jettyHttpExchange);
93          if (result instanceof Authenticator.Failure)
94          {
95              int rc = ((Authenticator.Failure)result).getResponseCode();
96              resp.sendError(rc);
97          }
98          else if (result instanceof Authenticator.Retry)
99          {
100             int rc = ((Authenticator.Retry)result).getResponseCode();
101             resp.sendError(rc);
102         }
103         else if (result instanceof Authenticator.Success)
104         {
105             HttpPrincipal principal = ((Authenticator.Success)result).getPrincipal();
106             jettyHttpExchange.setPrincipal(principal);
107             _httpHandler.handle(jettyHttpExchange);
108         }
109     }
110 
111     public HttpHandler getHttpHandler()
112     {
113         return _httpHandler;
114     }
115 
116     public void setHttpHandler(HttpHandler handler)
117     {
118         this._httpHandler = handler;
119     }
120 
121 }