public static void main(String[] args) throws IOException {
HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0);
ApplicationContext appContext = ApplicationWebContext.getInstance();
- HttpContext hc1 = server.createContext(
- ApplicationWebContext.WEB_CONTEXT, appContext.getWebHandler());
+
+ server.createContext(ApplicationWebContext.WEB_CONTEXT, appContext.getWebHandler());
+ server.createContext(ApplicationWebContext.LOGIN_CONTEXT, appContext.getLoginHandler());
server.setExecutor(null);
server.start();
}
DataSource getDataSource();
HttpHandler getWebHandler();
+
+ HttpHandler getLoginHandler();
}
--- /dev/null
+package com.prueba.core.context.security.handle;
+
+import java.io.IOException;
+import java.net.URI;
+import java.time.LocalDateTime;
+import java.util.Map;
+import java.util.UUID;
+import java.util.concurrent.ConcurrentHashMap;
+
+import com.prueba.core.context.security.persistence.SessionInfo;
+import com.sun.net.httpserver.Headers;
+import com.sun.net.httpserver.HttpExchange;
+import com.sun.net.httpserver.HttpHandler;
+
+
+public class LoginHandler implements HttpHandler {
+ public static final String LOGIN_PAGE = "/app/login/login.html?serviceName=http://localhost:8080";
+
+ @Override
+ public void handle(HttpExchange httpExchange) throws IOException {
+ final Headers headers = httpExchange.getRequestHeaders();
+
+
+
+ httpExchange.sendResponseHeaders(200, 0);
+ httpExchange.close();
+ }
+
+}
+++ /dev/null
-package com.prueba.core.context.security.handle;
-
-import java.io.IOException;
-import java.net.URI;
-import java.time.LocalDateTime;
-import java.util.Map;
-import java.util.UUID;
-import java.util.concurrent.ConcurrentHashMap;
-
-import com.prueba.core.context.security.persistence.SessionInfo;
-import com.sun.net.httpserver.Headers;
-import com.sun.net.httpserver.HttpExchange;
-import com.sun.net.httpserver.HttpHandler;
-
-
-public class SessionHandle implements HttpHandler {
- private static final String COOKIE_HEADER = "Cookie";
- private static final String LOGIN_PAGE = "http://localhost:8080/app/login/login.html?serviceName=http://localhost:8080";
-
- private final Map<UUID, SessionInfo> sessions = new ConcurrentHashMap<>();
-
- @Override
- public void handle(HttpExchange httpExchange) throws IOException {
- final Headers headers = httpExchange.getRequestHeaders();
- final String cookieValue = headers.getFirst(COOKIE_HEADER);
-
- if (cookieValue != null) {
- final UUID uuid = UUID.fromString(cookieValue);
- final SessionInfo sessionInfo = sessions.get(uuid);
- if (sessionInfo != null) {
- LocalDateTime currentDateTime = LocalDateTime.now();
- if (sessionInfo.getLastSessionTime().plusMinutes(5).compareTo(currentDateTime) > 0) {
- // Call next handler
-
- final SessionInfo newSessionInfo = new SessionInfo(sessionInfo.getUsername(), LocalDateTime.now());
- sessions.put(uuid, newSessionInfo);
- } else {
- this.doRedirec(httpExchange);
- }
- } else {
- this.doRedirec(httpExchange);
- }
- } else {
- this.doRedirec(httpExchange);
- }
-
- httpExchange.close();
- }
-
- protected void doRedirec(HttpExchange httpExchange) throws IOException {
- URI requestURI = httpExchange.getRequestURI();
- String requestURIString = requestURI.toString();
- Headers responseHeaders = httpExchange.getResponseHeaders();
- responseHeaders.add("Location", LOGIN_PAGE + requestURIString);
- httpExchange.sendResponseHeaders(302, 0);
- }
-}
--- /dev/null
+package com.prueba.core.context.security.handle;
+
+import java.io.IOException;
+import java.net.URI;
+import java.time.LocalDateTime;
+import java.util.Map;
+import java.util.UUID;
+import java.util.concurrent.ConcurrentHashMap;
+
+import com.prueba.core.context.security.persistence.SessionInfo;
+import com.sun.net.httpserver.Headers;
+import com.sun.net.httpserver.HttpExchange;
+import com.sun.net.httpserver.HttpHandler;
+
+
+public class SessionHandler implements HttpHandler {
+ private static final String COOKIE_HEADER = "Cookie";
+ private static final String SERVER_ADDRESS = "http://localhost:8080";
+
+ private final Map<UUID, SessionInfo> sessions = new ConcurrentHashMap<>();
+
+ @Override
+ public void handle(HttpExchange httpExchange) throws IOException {
+ final Headers headers = httpExchange.getRequestHeaders();
+ final String cookieValue = headers.getFirst(COOKIE_HEADER);
+
+ if (cookieValue != null) {
+ final UUID uuid = UUID.fromString(cookieValue);
+ final SessionInfo sessionInfo = sessions.get(uuid);
+ if (sessionInfo != null) {
+ LocalDateTime currentDateTime = LocalDateTime.now();
+ if (sessionInfo.getLastSessionTime().plusMinutes(5).compareTo(currentDateTime) > 0) {
+ // Call next handler
+
+ final SessionInfo newSessionInfo = new SessionInfo(sessionInfo.getUsername(), LocalDateTime.now());
+ sessions.put(uuid, newSessionInfo);
+ } else {
+ this.doRedirec(httpExchange);
+ }
+ } else {
+ this.doRedirec(httpExchange);
+ }
+ } else {
+ this.doRedirec(httpExchange);
+ }
+
+ httpExchange.close();
+ }
+
+ protected void doRedirec(HttpExchange httpExchange) throws IOException {
+ URI requestURI = httpExchange.getRequestURI();
+ String requestURIString = requestURI.toString();
+ Headers responseHeaders = httpExchange.getResponseHeaders();
+ responseHeaders.add("Location", SERVER_ADDRESS + LoginHandler.LOGIN_PAGE + requestURIString);
+ httpExchange.sendResponseHeaders(302, 0);
+ }
+}
import com.prueba.core.context.ApplicationContext;
import com.prueba.core.context.integration.datasource.impl.DoDataSourceContext;
import com.prueba.core.context.integration.liquibase.impl.LiquibaseContext;
-import com.prueba.core.context.security.handle.SessionHandle;
+import com.prueba.core.context.security.handle.LoginHandler;
+import com.prueba.core.context.security.handle.SessionHandler;
import com.sun.net.httpserver.HttpHandler;
public class ApplicationWebContext implements ApplicationContext {
public static final String WEB_CONTEXT = "/app/pages/";
+ public static final String LOGIN_CONTEXT = "/app/login/";
private final DataSource dataSource;
private final LiquibaseContext liquibaseContext;
private final HttpHandler webHttpHandler;
+ private final HttpHandler loginHandler;
private ApplicationWebContext() {
this.dataSource = DoDataSourceContext.getInstance().getDataSource();
this.liquibaseContext = new LiquibaseContext(dataSource);
this.liquibaseContext.init();
- this.webHttpHandler = new SessionHandle();
-
+ this.webHttpHandler = new SessionHandler();
+ this.loginHandler = new LoginHandler();
}
private static class ApplicationWebContextHolder {
public HttpHandler getWebHandler() {
return this.webHttpHandler;
}
+
+ @Override
+ public HttpHandler getLoginHandler() {
+ return this.loginHandler;
+ }
}