View Javadoc

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