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.Request;
21  import org.eclipse.jetty.server.handler.ContextHandler;
22  
23  import javax.servlet.ServletException;
24  import javax.servlet.http.HttpServletRequest;
25  import javax.servlet.http.HttpServletResponse;
26  import java.io.IOException;
27  import java.io.PrintWriter;
28  
29  /**
30   * Jetty handler that bridges requests to {@link HttpHandler}.
31   */
32  public class HttpSpiContextHandler extends ContextHandler
33  {
34  
35      private HttpContext _httpContext;
36  
37      private HttpHandler _httpHandler;
38  
39  
40      public HttpSpiContextHandler(HttpContext httpContext, HttpHandler httpHandler)
41      {
42          this._httpContext = httpContext;
43          this._httpHandler = httpHandler;
44      }
45  
46      @Override
47      public void doScope(String target, Request baseRequest, HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException
48      {
49          if (!target.startsWith(getContextPath())) return;
50  
51          JettyHttpExchange jettyHttpExchange = new JettyHttpExchange(_httpContext, req, resp);
52  
53          // TODO: add filters processing
54  
55          try
56          {
57              Authenticator auth = _httpContext.getAuthenticator();
58              if (auth != null)
59                  handleAuthentication(resp, jettyHttpExchange, auth);
60              else
61                  _httpHandler.handle(jettyHttpExchange);
62          }
63          catch(Exception ex)
64          {
65              PrintWriter writer = new PrintWriter(jettyHttpExchange.getResponseBody());
66              
67              resp.setStatus(500);
68              writer.println("<h2>HTTP ERROR: 500</h2>");
69              writer.println("<pre>INTERNAL_SERVER_ERROR</pre>");
70              writer.println("<p>RequestURI=" + req.getRequestURI() + "</p>");
71              
72              writer.println("<pre>");
73              ex.printStackTrace(writer);
74              writer.println("</pre>");
75              
76              writer.println("<p><i><small><a href=\"http://jetty.mortbay.org\">Powered by jetty://</a></small></i></p>");
77              
78              writer.close();
79          }
80          finally
81          {
82              baseRequest.setHandled(true);
83          }
84          
85      }
86  
87  
88      private void handleAuthentication(HttpServletResponse resp, JettyHttpExchange jettyHttpExchange, Authenticator auth) throws IOException
89      {
90          Result result = auth.authenticate(jettyHttpExchange);
91          if (result instanceof Authenticator.Failure)
92          {
93              int rc = ((Authenticator.Failure)result).getResponseCode();
94              resp.sendError(rc);
95          }
96          else if (result instanceof Authenticator.Retry)
97          {
98              int rc = ((Authenticator.Retry)result).getResponseCode();
99              resp.sendError(rc);
100         }
101         else if (result instanceof Authenticator.Success)
102         {
103             HttpPrincipal principal = ((Authenticator.Success)result).getPrincipal();
104             jettyHttpExchange.setPrincipal(principal);
105             _httpHandler.handle(jettyHttpExchange);
106         }
107     }
108 
109     public HttpHandler getHttpHandler()
110     {
111         return _httpHandler;
112     }
113 
114     public void setHttpHandler(HttpHandler handler)
115     {
116         this._httpHandler = handler;
117     }
118 
119 }