--- /dev/null
+package com.prueba;
+
+import java.io.IOException;
+import java.net.InetSocketAddress;
+
+import com.prueba.core.context.ApplicationContext;
+import com.prueba.core.context.web.application.ApplicationWebContext;
+import com.prueba.core.http.authentication.CustomBasicAuthenticator;
+import com.prueba.core.http.handles.ApiHandler;
+import com.prueba.core.http.handles.LoginHandler;
+import com.prueba.core.http.handles.PagesHandler;
+import com.sun.net.httpserver.HttpContext;
+import com.sun.net.httpserver.HttpServer;
+
+public class MainRun {
+
+ public static void main(String[] args) throws IOException {
+ ApplicationContext appContext = ApplicationWebContext.getInstance();
+
+ HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0);
+ server.createContext(PagesHandler.CONTEXT, appContext.getWebHandler());
+ server.createContext(LoginHandler.CONTEXT, appContext.getLoginHandler());
+
+ HttpContext apiContext = server.createContext(ApiHandler.CONTEXT, appContext.getApiHandler());
+ apiContext.setAuthenticator(new CustomBasicAuthenticator(ApiHandler.CONTEXT));
+
+ server.setExecutor(null);
+
+ server.start();
+ }
+}
import java.io.OutputStream;
import com.prueba.controllers.Controller;
-import com.prueba.core.context.security.persistence.SessionInfo;
-import com.prueba.core.context.security.persistence.context.SessionContext;
+import com.prueba.core.http.sessions.SessionContext;
+import com.prueba.core.http.sessions.SessionInfo;
import com.prueba.view.page.PageImpl;
import com.sun.net.httpserver.HttpExchange;
+++ /dev/null
-package com.prueba.core;
-
-import com.sun.net.httpserver.HttpExchange;
-
-public interface Handle {
-
- /**
- * Handle some HTTP request
- * @param httpExchange the HTTP request
- */
- public void handle(HttpExchange httpExchange);
-}
+++ /dev/null
-package com.prueba.core;
-
-import java.io.IOException;
-import java.net.InetSocketAddress;
-
-import com.prueba.core.context.ApplicationContext;
-import com.prueba.core.context.handles.LoginHandler;
-import com.prueba.core.context.handles.PagesHandler;
-import com.prueba.core.context.security.authenticator.CustomBasicAuthenticator;
-import com.prueba.core.context.handles.ApiHandler;
-import com.prueba.core.context.web.application.ApplicationWebContext;
-import com.sun.net.httpserver.HttpContext;
-import com.sun.net.httpserver.HttpServer;
-
-public class MainRun {
-
- public static void main(String[] args) throws IOException {
- ApplicationContext appContext = ApplicationWebContext.getInstance();
-
- HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0);
- server.createContext(PagesHandler.CONTEXT, appContext.getWebHandler());
- server.createContext(LoginHandler.CONTEXT, appContext.getLoginHandler());
-
- HttpContext apiContext = server.createContext(ApiHandler.CONTEXT, appContext.getApiHandler());
- apiContext.setAuthenticator(new CustomBasicAuthenticator(ApiHandler.CONTEXT));
-
- server.setExecutor(null);
-
- server.start();
- }
-}
+++ /dev/null
-package com.prueba.core.context.handles;
-
-import java.io.IOException;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import com.prueba.controllers.rest.ApiController;
-import com.prueba.core.context.security.authenticator.persistence.AuthenticationInfo;
-import com.prueba.core.context.security.persistence.context.BasicAuthenticationContext;
-import com.prueba.services.impl.AuthorizationServicesImpl;
-import com.sun.net.httpserver.HttpExchange;
-import com.sun.net.httpserver.HttpHandler;
-
-public class ApiHandler implements HttpHandler {
- public static final String CONTEXT = "/app/api/users/";
-
- private static final Logger LOGGER = LoggerFactory.getLogger(ApiHandler.class);
-
- private final ApiController apiController = new ApiController();
- private final AuthorizationServicesImpl authorizationService = new AuthorizationServicesImpl();
-
- @Override
- public void handle(HttpExchange httpExchange) throws IOException {
-
- try {
- this.handleThrowable(httpExchange);
- } catch (Exception exception) {
- LOGGER.error("ApiHandler error: ", exception);
-
- httpExchange.sendResponseHeaders(500, 0);
- } finally {
- httpExchange.close();
- }
- }
-
- protected void handleThrowable(HttpExchange httpExchange) throws IOException {
- AuthenticationInfo authenticationInfo = BasicAuthenticationContext.getAuthentication();
-
- if(authorizationService.isAuthorized(httpExchange.getRequestMethod(),
- httpExchange.getRequestURI().toString(), authenticationInfo.getUserName())) {
-
- apiController.handle(httpExchange);
-
- } else {
- httpExchange.sendResponseHeaders(403, 0);
- }
- }
-}
+++ /dev/null
-package com.prueba.core.context.handles;
-
-import java.io.IOException;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import com.prueba.controllers.web.LoginController;
-import com.sun.net.httpserver.HttpExchange;
-import com.sun.net.httpserver.HttpHandler;
-
-
-public class LoginHandler implements HttpHandler {
- public static final String CONTEXT = "/app/login/";
- public static final String LOGIN_PAGE = "/app/login/login.html?serviceName=http://localhost:8080";
-
- private static final Logger LOGGER = LoggerFactory.getLogger(LoginHandler.class);
-
-
- private final LoginController loginController = new LoginController();
- private final HttpHandler sessionHandler;
-
- public LoginHandler(HttpHandler sessionHandler) {
- this.sessionHandler = sessionHandler;
- }
-
- @Override
- public void handle(HttpExchange httpExchange) throws IOException {
- try {
- this.handleThrowable(httpExchange);
- } catch (Exception exception) {
- LOGGER.error("LoginHandler error: ", exception);
-
- httpExchange.sendResponseHeaders(500, 0);
- } finally {
- httpExchange.close();
- }
-
- }
-
- protected void handleThrowable(HttpExchange httpExchange) throws IOException {
- sessionHandler.handle(httpExchange);
-
- loginController.handle(httpExchange);
- }
-}
+++ /dev/null
-package com.prueba.core.context.handles;
-
-import java.io.IOException;
-import java.net.URI;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import com.prueba.controllers.web.PagesController;
-import com.prueba.core.context.security.persistence.SessionInfo;
-import com.prueba.core.context.security.persistence.Sessions;
-import com.prueba.core.context.security.persistence.context.SessionContext;
-import com.prueba.services.impl.AuthorizationServicesImpl;
-import com.sun.net.httpserver.Headers;
-import com.sun.net.httpserver.HttpExchange;
-import com.sun.net.httpserver.HttpHandler;
-
-
-public class PagesHandler implements HttpHandler {
- public static final String CONTEXT = "/app/pages/";
-
- private static final Logger LOGGER = LoggerFactory.getLogger(PagesHandler.class);
- private static final String SERVER_ADDRESS = "http://localhost:8080";
-
- private final PagesController pagesController = new PagesController();
- private final AuthorizationServicesImpl authorizationService = new AuthorizationServicesImpl();
- private final HttpHandler sessionHandler;
-
- public PagesHandler(HttpHandler sessionHandler) {
- this.sessionHandler = sessionHandler;
- }
-
- @Override
- public void handle(HttpExchange httpExchange) throws IOException {
- try {
- this.handleThrowable(httpExchange);
- } catch (Exception exception) {
- LOGGER.error("PagesHandler error: ", exception);
-
- httpExchange.sendResponseHeaders(500, 0);
- } finally {
- httpExchange.close();
- }
- }
-
- protected void handleThrowable(HttpExchange httpExchange) throws IOException {
- this.sessionHandler.handle(httpExchange);
-
- if (Sessions.getInstance().isValidSession(httpExchange)) {
- final SessionInfo sessionInfo = SessionContext.getSession();
-
- if(authorizationService.isAuthorized(httpExchange.getRequestMethod(),
- httpExchange.getRequestURI().toString(), sessionInfo.getUsername())) {
-
- pagesController.handle(httpExchange);
-
- } else {
- httpExchange.sendResponseHeaders(403, 0);
- }
-
- Sessions.getInstance().refreshSession(sessionInfo.getUUID(), sessionInfo.getUsername());
- } else {
- this.doRedirect(httpExchange);
- }
- }
-
- protected void doRedirect(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);
- }
-}
+++ /dev/null
-package com.prueba.core.context.handles;
-
-import java.io.IOException;
-import java.util.UUID;
-
-import com.prueba.core.context.security.persistence.SessionInfo;
-import com.prueba.core.context.security.persistence.Sessions;
-import com.prueba.core.context.security.persistence.context.SessionContext;
-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";
-
-
- @Override
- public void handle(HttpExchange httpExchange) throws IOException {
- final SessionInfo sessionInfo = getSessionInfo(httpExchange);
-
- SessionContext.setSession(sessionInfo);
- }
-
- protected SessionInfo getSessionInfo(HttpExchange httpExchange) {
- final Headers headers = httpExchange.getRequestHeaders();
- final String cookieValue = headers.getFirst(COOKIE_HEADER);
-
- SessionInfo sessionInfo = null;
-
- if (cookieValue != null) {
- final UUID uuid = UUID.fromString(cookieValue);
- sessionInfo = Sessions.getInstance().getSession(uuid);
- }
-
- return sessionInfo;
- }
-}
+++ /dev/null
-package com.prueba.core.context.security.authenticator;
-
-import com.prueba.core.context.security.authenticator.persistence.AuthenticationInfo;
-import com.prueba.core.context.security.persistence.context.BasicAuthenticationContext;
-import com.prueba.services.impl.LoginServiceImpl;
-import com.sun.net.httpserver.BasicAuthenticator;
-
-public class CustomBasicAuthenticator extends BasicAuthenticator {
-
- public CustomBasicAuthenticator(String context) {
- super(context);
- }
-
- @Override
- public boolean checkCredentials(String username, String password) {
- final LoginServiceImpl loginService = new LoginServiceImpl();
-
- if (loginService.isValidUser(username, password)) {
- BasicAuthenticationContext.setAuthentication(new AuthenticationInfo(username));
-
- return true;
- } else {
- BasicAuthenticationContext.setAuthentication(null);
-
- return false;
- }
- }
-}
+++ /dev/null
-package com.prueba.core.context.security.authenticator.persistence;
-
-public class AuthenticationInfo {
- private final String userName;
-
- public AuthenticationInfo(String userName) {
- this.userName = userName;
- }
-
- public String getUserName() {
- return userName;
- }
-}
+++ /dev/null
-package com.prueba.core.context.security.persistence;
-
-import java.time.LocalDateTime;
-import java.util.UUID;
-
-public class SessionInfo {
- private final UUID uuid;
- private final String username;
- private final LocalDateTime lastSessionTime;
-
- public SessionInfo(UUID uuid, String username, LocalDateTime lastSessionTime) {
- this.uuid = uuid;
- this.username = username;
- this.lastSessionTime = lastSessionTime;
- }
-
- public UUID getUUID() {
- return uuid;
- }
-
- public String getUsername() {
- return username;
- }
- public LocalDateTime getLastSessionTime() {
- return lastSessionTime;
- }
-
-}
+++ /dev/null
-package com.prueba.core.context.security.persistence;
-
-import java.time.LocalDateTime;
-import java.util.Map;
-import java.util.UUID;
-import java.util.concurrent.ConcurrentHashMap;
-
-import com.prueba.core.context.security.persistence.context.SessionContext;
-import com.sun.net.httpserver.HttpExchange;
-
-public class Sessions {
- private final Map<UUID, SessionInfo> sessions = new ConcurrentHashMap<>();
-
- private Sessions() {
-
- }
-
- private static class SessionsContextHolder {
- private static final Sessions INSTANCE = new Sessions();
- }
-
- public static Sessions getInstance() {
- return SessionsContextHolder.INSTANCE;
- }
-
- public SessionInfo getSession(UUID uuid) {
- return sessions.get(uuid);
- }
-
- public void refreshSession(UUID uuid, String username) {
- final SessionInfo newSessionInfo =
- new SessionInfo(uuid, username, LocalDateTime.now());
- sessions.put(uuid, newSessionInfo);
- }
-
- public void removeSession(UUID uuid) {
- sessions.remove(uuid);
- }
-
- public boolean isValidSession(HttpExchange httpExchange) {
- final SessionInfo sessionInfo = SessionContext.getSession();
-
- boolean isValid = false;
-
- if (sessionInfo != null) {
- LocalDateTime currentDateTime = LocalDateTime.now();
- if (sessionInfo.getLastSessionTime().plusMinutes(5).compareTo(currentDateTime) > 0) {
- isValid = true;
- } else {
- sessions.remove(sessionInfo.getUUID());
- }
- }
-
- return isValid;
- }
-}
+++ /dev/null
-package com.prueba.core.context.security.persistence.context;
-
-import com.prueba.core.context.security.authenticator.persistence.AuthenticationInfo;
-
-public class BasicAuthenticationContext {
- private static final ThreadLocal<AuthenticationInfo> localContextSession = new ThreadLocal<>();
-
- public static void setAuthentication(AuthenticationInfo authenticationInfo) {
- localContextSession.set(authenticationInfo);
- }
-
- public static AuthenticationInfo getAuthentication() {
- return localContextSession.get();
- }
-}
+++ /dev/null
-package com.prueba.core.context.security.persistence.context;
-
-import com.prueba.core.context.security.persistence.SessionInfo;
-
-public class SessionContext {
- private static final ThreadLocal<SessionInfo> localContextSession = new ThreadLocal<>();
-
- public static void setSession(SessionInfo sessionInfo) {
- localContextSession.set(sessionInfo);
- }
-
- public static SessionInfo getSession() {
- return localContextSession.get();
- }
-}
import javax.sql.DataSource;
import com.prueba.core.context.ApplicationContext;
-import com.prueba.core.context.handles.LoginHandler;
-import com.prueba.core.context.handles.PagesHandler;
-import com.prueba.core.context.handles.SessionHandler;
import com.prueba.core.context.integration.datasource.impl.DoDataSourceContext;
import com.prueba.core.context.integration.liquibase.impl.LiquibaseContext;
-import com.prueba.core.context.handles.ApiHandler;
+import com.prueba.core.http.handles.ApiHandler;
+import com.prueba.core.http.handles.LoginHandler;
+import com.prueba.core.http.handles.PagesHandler;
+import com.prueba.core.http.handles.SessionHandler;
import com.sun.net.httpserver.HttpHandler;
--- /dev/null
+package com.prueba.core.http.authentication;
+
+public class AuthenticationInfo {
+ private final String userName;
+
+ public AuthenticationInfo(String userName) {
+ this.userName = userName;
+ }
+
+ public String getUserName() {
+ return userName;
+ }
+}
--- /dev/null
+package com.prueba.core.http.authentication;
+
+public class BasicAuthenticationContext {
+ private static final ThreadLocal<AuthenticationInfo> localContextSession = new ThreadLocal<>();
+
+ public static void setAuthentication(AuthenticationInfo authenticationInfo) {
+ localContextSession.set(authenticationInfo);
+ }
+
+ public static AuthenticationInfo getAuthentication() {
+ return localContextSession.get();
+ }
+}
--- /dev/null
+package com.prueba.core.http.authentication;
+
+import com.prueba.services.impl.LoginServiceImpl;
+import com.sun.net.httpserver.BasicAuthenticator;
+
+public class CustomBasicAuthenticator extends BasicAuthenticator {
+
+ public CustomBasicAuthenticator(String context) {
+ super(context);
+ }
+
+ @Override
+ public boolean checkCredentials(String username, String password) {
+ final LoginServiceImpl loginService = new LoginServiceImpl();
+
+ if (loginService.isValidUser(username, password)) {
+ BasicAuthenticationContext.setAuthentication(new AuthenticationInfo(username));
+
+ return true;
+ } else {
+ BasicAuthenticationContext.setAuthentication(null);
+
+ return false;
+ }
+ }
+}
--- /dev/null
+package com.prueba.core.http.handles;
+
+import java.io.IOException;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.prueba.controllers.rest.ApiController;
+import com.prueba.core.http.authentication.AuthenticationInfo;
+import com.prueba.core.http.authentication.BasicAuthenticationContext;
+import com.prueba.services.impl.AuthorizationServicesImpl;
+import com.sun.net.httpserver.HttpExchange;
+import com.sun.net.httpserver.HttpHandler;
+
+public class ApiHandler implements HttpHandler {
+ public static final String CONTEXT = "/app/api/users/";
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(ApiHandler.class);
+
+ private final ApiController apiController = new ApiController();
+ private final AuthorizationServicesImpl authorizationService = new AuthorizationServicesImpl();
+
+ @Override
+ public void handle(HttpExchange httpExchange) throws IOException {
+
+ try {
+ this.handleThrowable(httpExchange);
+ } catch (Exception exception) {
+ LOGGER.error("ApiHandler error: ", exception);
+
+ httpExchange.sendResponseHeaders(500, 0);
+ } finally {
+ httpExchange.close();
+ }
+ }
+
+ protected void handleThrowable(HttpExchange httpExchange) throws IOException {
+ AuthenticationInfo authenticationInfo = BasicAuthenticationContext.getAuthentication();
+
+ if(authorizationService.isAuthorized(httpExchange.getRequestMethod(),
+ httpExchange.getRequestURI().toString(), authenticationInfo.getUserName())) {
+
+ apiController.handle(httpExchange);
+
+ } else {
+ httpExchange.sendResponseHeaders(403, 0);
+ }
+ }
+}
--- /dev/null
+package com.prueba.core.http.handles;
+
+import com.sun.net.httpserver.HttpExchange;
+
+public interface Handle {
+
+ /**
+ * Handle some HTTP request
+ * @param httpExchange the HTTP request
+ */
+ public void handle(HttpExchange httpExchange);
+}
--- /dev/null
+package com.prueba.core.http.handles;
+
+import java.io.IOException;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.prueba.controllers.web.LoginController;
+import com.sun.net.httpserver.HttpExchange;
+import com.sun.net.httpserver.HttpHandler;
+
+
+public class LoginHandler implements HttpHandler {
+ public static final String CONTEXT = "/app/login/";
+ public static final String LOGIN_PAGE = "/app/login/login.html?serviceName=http://localhost:8080";
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(LoginHandler.class);
+
+
+ private final LoginController loginController = new LoginController();
+ private final HttpHandler sessionHandler;
+
+ public LoginHandler(HttpHandler sessionHandler) {
+ this.sessionHandler = sessionHandler;
+ }
+
+ @Override
+ public void handle(HttpExchange httpExchange) throws IOException {
+ try {
+ this.handleThrowable(httpExchange);
+ } catch (Exception exception) {
+ LOGGER.error("LoginHandler error: ", exception);
+
+ httpExchange.sendResponseHeaders(500, 0);
+ } finally {
+ httpExchange.close();
+ }
+
+ }
+
+ protected void handleThrowable(HttpExchange httpExchange) throws IOException {
+ sessionHandler.handle(httpExchange);
+
+ loginController.handle(httpExchange);
+ }
+}
--- /dev/null
+package com.prueba.core.http.handles;
+
+import java.io.IOException;
+import java.net.URI;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.prueba.controllers.web.PagesController;
+import com.prueba.core.http.sessions.SessionContext;
+import com.prueba.core.http.sessions.SessionInfo;
+import com.prueba.core.http.sessions.Sessions;
+import com.prueba.services.impl.AuthorizationServicesImpl;
+import com.sun.net.httpserver.Headers;
+import com.sun.net.httpserver.HttpExchange;
+import com.sun.net.httpserver.HttpHandler;
+
+
+public class PagesHandler implements HttpHandler {
+ public static final String CONTEXT = "/app/pages/";
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(PagesHandler.class);
+ private static final String SERVER_ADDRESS = "http://localhost:8080";
+
+ private final PagesController pagesController = new PagesController();
+ private final AuthorizationServicesImpl authorizationService = new AuthorizationServicesImpl();
+ private final HttpHandler sessionHandler;
+
+ public PagesHandler(HttpHandler sessionHandler) {
+ this.sessionHandler = sessionHandler;
+ }
+
+ @Override
+ public void handle(HttpExchange httpExchange) throws IOException {
+ try {
+ this.handleThrowable(httpExchange);
+ } catch (Exception exception) {
+ LOGGER.error("PagesHandler error: ", exception);
+
+ httpExchange.sendResponseHeaders(500, 0);
+ } finally {
+ httpExchange.close();
+ }
+ }
+
+ protected void handleThrowable(HttpExchange httpExchange) throws IOException {
+ this.sessionHandler.handle(httpExchange);
+
+ if (Sessions.getInstance().isValidSession(httpExchange)) {
+ final SessionInfo sessionInfo = SessionContext.getSession();
+
+ if(authorizationService.isAuthorized(httpExchange.getRequestMethod(),
+ httpExchange.getRequestURI().toString(), sessionInfo.getUsername())) {
+
+ pagesController.handle(httpExchange);
+
+ } else {
+ httpExchange.sendResponseHeaders(403, 0);
+ }
+
+ Sessions.getInstance().refreshSession(sessionInfo.getUUID(), sessionInfo.getUsername());
+ } else {
+ this.doRedirect(httpExchange);
+ }
+ }
+
+ protected void doRedirect(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);
+ }
+}
--- /dev/null
+package com.prueba.core.http.handles;
+
+import java.io.IOException;
+import java.util.UUID;
+
+import com.prueba.core.http.sessions.SessionContext;
+import com.prueba.core.http.sessions.SessionInfo;
+import com.prueba.core.http.sessions.Sessions;
+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";
+
+
+ @Override
+ public void handle(HttpExchange httpExchange) throws IOException {
+ final SessionInfo sessionInfo = getSessionInfo(httpExchange);
+
+ SessionContext.setSession(sessionInfo);
+ }
+
+ protected SessionInfo getSessionInfo(HttpExchange httpExchange) {
+ final Headers headers = httpExchange.getRequestHeaders();
+ final String cookieValue = headers.getFirst(COOKIE_HEADER);
+
+ SessionInfo sessionInfo = null;
+
+ if (cookieValue != null) {
+ final UUID uuid = UUID.fromString(cookieValue);
+ sessionInfo = Sessions.getInstance().getSession(uuid);
+ }
+
+ return sessionInfo;
+ }
+}
--- /dev/null
+package com.prueba.core.http.sessions;
+
+public class SessionContext {
+ private static final ThreadLocal<SessionInfo> localContextSession = new ThreadLocal<>();
+
+ public static void setSession(SessionInfo sessionInfo) {
+ localContextSession.set(sessionInfo);
+ }
+
+ public static SessionInfo getSession() {
+ return localContextSession.get();
+ }
+}
--- /dev/null
+package com.prueba.core.http.sessions;
+
+import java.time.LocalDateTime;
+import java.util.UUID;
+
+public class SessionInfo {
+ private final UUID uuid;
+ private final String username;
+ private final LocalDateTime lastSessionTime;
+
+ public SessionInfo(UUID uuid, String username, LocalDateTime lastSessionTime) {
+ this.uuid = uuid;
+ this.username = username;
+ this.lastSessionTime = lastSessionTime;
+ }
+
+ public UUID getUUID() {
+ return uuid;
+ }
+
+ public String getUsername() {
+ return username;
+ }
+ public LocalDateTime getLastSessionTime() {
+ return lastSessionTime;
+ }
+
+}
--- /dev/null
+package com.prueba.core.http.sessions;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.time.LocalDateTime;
+import java.util.Map;
+import java.util.Properties;
+import java.util.UUID;
+import java.util.concurrent.ConcurrentHashMap;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.sun.net.httpserver.HttpExchange;
+
+public class Sessions {
+ private static final Logger LOGGER = LoggerFactory.getLogger(Sessions.class);
+
+ private final Long sessionTimeout;
+
+ private final Map<UUID, SessionInfo> sessions = new ConcurrentHashMap<>();
+
+ private Sessions() {
+ final Properties properties = this.loadProperties();
+
+ sessionTimeout = Long.valueOf(properties.getProperty("session.expire"));
+ }
+
+ private static class SessionsContextHolder {
+ private static final Sessions INSTANCE = new Sessions();
+ }
+
+ public static Sessions getInstance() {
+ return SessionsContextHolder.INSTANCE;
+ }
+
+ public SessionInfo getSession(UUID uuid) {
+ return sessions.get(uuid);
+ }
+
+ public void refreshSession(UUID uuid, String username) {
+ final SessionInfo newSessionInfo =
+ new SessionInfo(uuid, username, LocalDateTime.now());
+ sessions.put(uuid, newSessionInfo);
+ }
+
+ public void removeSession(UUID uuid) {
+ sessions.remove(uuid);
+ }
+
+ public boolean isValidSession(HttpExchange httpExchange) {
+ final SessionInfo sessionInfo = SessionContext.getSession();
+
+ boolean isValid = false;
+
+ if (sessionInfo != null) {
+ final LocalDateTime currentDateTime = LocalDateTime.now();
+ if (sessionInfo
+ .getLastSessionTime().plusMinutes(sessionTimeout).compareTo(currentDateTime) > 0) {
+ isValid = true;
+ } else {
+ sessions.remove(sessionInfo.getUUID());
+ }
+ }
+
+ return isValid;
+ }
+
+ private Properties loadProperties() {
+ final Properties properties = new Properties();
+
+ try(final InputStream in = this.getClass().getResourceAsStream("/prueba.properties")) {
+ properties.load(in);
+ } catch (IOException exception) {
+ LOGGER.error("Load properties file error: ", exception);
+
+ throw new IllegalStateException("Load properties file error", exception);
+ }
+
+ return properties;
+ }
+}
import java.nio.charset.Charset;
import java.util.UUID;
-import com.prueba.core.context.security.persistence.SessionInfo;
-import com.prueba.core.context.security.persistence.Sessions;
-import com.prueba.core.context.security.persistence.context.SessionContext;
+import com.prueba.core.http.sessions.SessionContext;
+import com.prueba.core.http.sessions.SessionInfo;
+import com.prueba.core.http.sessions.Sessions;
import com.prueba.model.dao.AccountDao;
import com.prueba.view.login.LoginFormImpl;
import com.sun.net.httpserver.Headers;
--- /dev/null
+# Session expire timeout in minutes
+session.expire = 5
\ No newline at end of file
+++ /dev/null
-package com.prueba.core.context.security.persistence;
-
-import static org.junit.Assert.*;
-
-import java.time.LocalDateTime;
-import java.util.UUID;
-
-import org.junit.Test;
-
-public class SessionInfoTest {
- private static final UUID UUID_VALUE = UUID.randomUUID();
- private static final String USERNAME = "GUMARTINM";
- private static final LocalDateTime LAST_SESSION = LocalDateTime.now();
-
- @Test
- public void shouldCallGetters() {
- SessionInfo sessionInfo = new SessionInfo(UUID_VALUE, USERNAME, LAST_SESSION);
-
- assertEquals(UUID_VALUE, sessionInfo.getUUID());
- assertEquals(USERNAME, sessionInfo.getUsername());
- assertEquals(LAST_SESSION, sessionInfo.getLastSessionTime());
- }
-
-}
--- /dev/null
+package com.prueba.core.http.sessions;
+
+import static org.junit.Assert.*;
+
+import java.time.LocalDateTime;
+import java.util.UUID;
+
+import org.junit.Test;
+
+public class SessionInfoTest {
+ private static final UUID UUID_VALUE = UUID.randomUUID();
+ private static final String USERNAME = "GUMARTINM";
+ private static final LocalDateTime LAST_SESSION = LocalDateTime.now();
+
+ @Test
+ public void shouldCallGetters() {
+ SessionInfo sessionInfo = new SessionInfo(UUID_VALUE, USERNAME, LAST_SESSION);
+
+ assertEquals(UUID_VALUE, sessionInfo.getUUID());
+ assertEquals(USERNAME, sessionInfo.getUsername());
+ assertEquals(LAST_SESSION, sessionInfo.getLastSessionTime());
+ }
+
+}