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
;
18 public class HttpContextServlet
extends HttpServlet
{
19 private static final long serialVersionUID
= 2321612280413662738L;
21 private final HttpContext httpContext
;
23 public HttpContextServlet(HttpContext httpContext
) {
24 this.httpContext
= httpContext
;
28 protected void service(HttpServletRequest req
, HttpServletResponse resp
) throws ServletException
, IOException
{
29 try (ServletHttpExchange httpExchange
= new ServletHttpExchange(httpContext
, req
, resp
)) {
30 ServletHttpSession httpSession
= new ServletHttpSession(req
.getSession());
31 httpExchange
.setAttribute(RemoteAuthSession
.class.getName(), httpSession
);
32 Authenticator authenticator
= httpContext
.getAuthenticator();
33 if (authenticator
!= null) {
34 Authenticator
.Result authenticationResult
= authenticator
.authenticate(httpExchange
);
35 if (authenticationResult
instanceof Authenticator
.Success
) {
36 HttpPrincipal httpPrincipal
= ((Authenticator
.Success
) authenticationResult
).getPrincipal();
37 httpExchange
.setPrincipal(httpPrincipal
);
38 } else if (authenticationResult
instanceof Authenticator
.Retry
) {
39 httpExchange
.sendResponseHeaders((((Authenticator
.Retry
) authenticationResult
).getResponseCode()),
43 } else if (authenticationResult
instanceof Authenticator
.Failure
) {
44 httpExchange
.sendResponseHeaders(((Authenticator
.Failure
) authenticationResult
).getResponseCode(),
49 throw new UnsupportedOperationException(
50 "Authentication result " + authenticationResult
.getClass().getName() + " is not supported");
54 HttpHandler httpHandler
= httpContext
.getHandler();
55 httpHandler
.handle(httpExchange
);