View Javadoc

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