]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms.ee/src/org/argeo/cms/servlet/httpserver/HttpContextServlet.java
Improve SSH server. Rename node directory to private.
[lgpl/argeo-commons.git] / org.argeo.cms.ee / src / org / argeo / cms / servlet / httpserver / HttpContextServlet.java
1 package org.argeo.cms.servlet.httpserver;
2
3 import java.io.IOException;
4
5 import javax.servlet.ServletException;
6 import javax.servlet.http.HttpServlet;
7 import javax.servlet.http.HttpServletRequest;
8 import javax.servlet.http.HttpServletResponse;
9
10 import org.argeo.cms.auth.RemoteAuthSession;
11 import org.argeo.cms.servlet.ServletHttpSession;
12
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;
17
18 public class HttpContextServlet extends HttpServlet {
19 private static final long serialVersionUID = 2321612280413662738L;
20
21 private final HttpContext httpContext;
22
23 public HttpContextServlet(HttpContext httpContext) {
24 this.httpContext = httpContext;
25 }
26
27 @Override
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()),
40 -1);
41 resp.flushBuffer();
42 return;
43 } else if (authenticationResult instanceof Authenticator.Failure) {
44 httpExchange.sendResponseHeaders(((Authenticator.Failure) authenticationResult).getResponseCode(),
45 -1);
46 resp.flushBuffer();
47 return;
48 } else {
49 throw new UnsupportedOperationException(
50 "Authentication result " + authenticationResult.getClass().getName() + " is not supported");
51 }
52 }
53
54 HttpHandler httpHandler = httpContext.getHandler();
55 httpHandler.handle(httpExchange);
56 }
57 }
58 }