View Javadoc

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