1 package org
.argeo
.cms
.servlet
.httpserver
;
3 import java
.io
.IOException
;
5 import javax
.servlet
.ServletException
;
6 import javax
.servlet
.http
.HttpServlet
;
7 import javax
.servlet
.http
.HttpServletRequest
;
8 import javax
.servlet
.http
.HttpServletResponse
;
10 import org
.argeo
.cms
.auth
.RemoteAuthSession
;
11 import org
.argeo
.cms
.servlet
.ServletHttpSession
;
13 import com
.sun
.net
.httpserver
.Authenticator
;
14 import com
.sun
.net
.httpserver
.HttpContext
;
15 import com
.sun
.net
.httpserver
.HttpHandler
;
16 import com
.sun
.net
.httpserver
.HttpPrincipal
;
19 * An {@link HttpServlet} which integrates an {@link HttpContext} and its
20 * {@link Authenticator} in a servlet container.
22 public class HttpContextServlet
extends HttpServlet
{
23 private static final long serialVersionUID
= 2321612280413662738L;
25 private final HttpContext httpContext
;
27 public HttpContextServlet(HttpContext httpContext
) {
28 this.httpContext
= httpContext
;
32 protected void service(HttpServletRequest req
, HttpServletResponse resp
) throws ServletException
, IOException
{
33 try (ServletHttpExchange httpExchange
= new ServletHttpExchange(httpContext
, req
, resp
)) {
34 ServletHttpSession httpSession
= new ServletHttpSession(req
.getSession());
35 httpExchange
.setAttribute(RemoteAuthSession
.class.getName(), httpSession
);
36 Authenticator authenticator
= httpContext
.getAuthenticator();
37 if (authenticator
!= null) {
38 Authenticator
.Result authenticationResult
= authenticator
.authenticate(httpExchange
);
39 if (authenticationResult
instanceof Authenticator
.Success
) {
40 HttpPrincipal httpPrincipal
= ((Authenticator
.Success
) authenticationResult
).getPrincipal();
41 httpExchange
.setPrincipal(httpPrincipal
);
42 } else if (authenticationResult
instanceof Authenticator
.Retry
) {
43 httpExchange
.sendResponseHeaders((((Authenticator
.Retry
) authenticationResult
).getResponseCode()),
47 } else if (authenticationResult
instanceof Authenticator
.Failure
) {
48 httpExchange
.sendResponseHeaders(((Authenticator
.Failure
) authenticationResult
).getResponseCode(),
53 throw new UnsupportedOperationException(
54 "Authentication result " + authenticationResult
.getClass().getName() + " is not supported");
58 HttpHandler httpHandler
= httpContext
.getHandler();
59 httpHandler
.handle(httpExchange
);