View Javadoc

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