Login handler
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Sun, 2 Oct 2016 23:51:34 +0000 (01:51 +0200)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Sun, 2 Oct 2016 23:51:34 +0000 (01:51 +0200)
src/main/java/com/prueba/core/MainRun.java
src/main/java/com/prueba/core/context/ApplicationContext.java
src/main/java/com/prueba/core/context/security/handle/LoginHandler.java [new file with mode: 0644]
src/main/java/com/prueba/core/context/security/handle/SessionHandle.java [deleted file]
src/main/java/com/prueba/core/context/security/handle/SessionHandler.java [new file with mode: 0644]
src/main/java/com/prueba/core/context/web/application/ApplicationWebContext.java

index 496904d..65c9f0b 100644 (file)
@@ -13,8 +13,9 @@ public class MainRun {
        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();
     }
index 1a6b1d4..acb35db 100644 (file)
@@ -10,4 +10,6 @@ public interface ApplicationContext {
        DataSource getDataSource();
        
        HttpHandler getWebHandler();
+       
+       HttpHandler getLoginHandler();
 }
diff --git a/src/main/java/com/prueba/core/context/security/handle/LoginHandler.java b/src/main/java/com/prueba/core/context/security/handle/LoginHandler.java
new file mode 100644 (file)
index 0000000..34d1c25
--- /dev/null
@@ -0,0 +1,29 @@
+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();   
+       }
+
+}
diff --git a/src/main/java/com/prueba/core/context/security/handle/SessionHandle.java b/src/main/java/com/prueba/core/context/security/handle/SessionHandle.java
deleted file mode 100644 (file)
index 29832b7..0000000
+++ /dev/null
@@ -1,57 +0,0 @@
-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);
-       }
-}
diff --git a/src/main/java/com/prueba/core/context/security/handle/SessionHandler.java b/src/main/java/com/prueba/core/context/security/handle/SessionHandler.java
new file mode 100644 (file)
index 0000000..981a524
--- /dev/null
@@ -0,0 +1,57 @@
+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);
+       }
+}
index fbcd65a..cedcf51 100644 (file)
@@ -5,23 +5,26 @@ import javax.sql.DataSource;
 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 {
@@ -41,4 +44,9 @@ public class ApplicationWebContext implements ApplicationContext {
        public HttpHandler getWebHandler() {
                return this.webHttpHandler;
        }
+
+       @Override
+       public HttpHandler getLoginHandler() {
+               return this.loginHandler;
+       }
 }