From 91bd14fad9eb132dee5d97161f753a496c4ca77d Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Tue, 4 Oct 2016 19:25:20 +0200 Subject: [PATCH] New packages for Controllers: REST and WEB --- .../java/com/prueba/controllers/Controller.java | 7 ++ .../com/prueba/controllers/rest/ApiController.java | 3 +- .../prueba/controllers/rest/LoginController.java | 41 ------------ .../prueba/controllers/rest/PagesController.java | 66 ------------------- .../prueba/controllers/rest/RestController.java | 7 -- .../prueba/controllers/web/LoginController.java | 42 ++++++++++++ .../prueba/controllers/web/PagesController.java | 67 +++++++++++++++++++ src/main/java/com/prueba/core/MainRun.java | 7 +- .../prueba/core/context/handles/ApiHandler.java | 49 ++++++++++++++ .../prueba/core/context/handles/LoginHandler.java | 46 +++++++++++++ .../prueba/core/context/handles/PagesHandler.java | 75 ++++++++++++++++++++++ .../core/context/handles/SessionHandler.java | 37 +++++++++++ .../core/context/security/handle/ApiHandler.java | 49 -------------- .../core/context/security/handle/LoginHandler.java | 46 ------------- .../core/context/security/handle/PagesHandler.java | 75 ---------------------- .../context/security/handle/SessionHandler.java | 37 ----------- .../web/application/ApplicationWebContext.java | 8 +-- .../com/prueba/services/impl/ApiServiceImpl.java | 6 +- 18 files changed, 334 insertions(+), 334 deletions(-) create mode 100644 src/main/java/com/prueba/controllers/Controller.java delete mode 100644 src/main/java/com/prueba/controllers/rest/LoginController.java delete mode 100644 src/main/java/com/prueba/controllers/rest/PagesController.java delete mode 100644 src/main/java/com/prueba/controllers/rest/RestController.java create mode 100644 src/main/java/com/prueba/controllers/web/LoginController.java create mode 100644 src/main/java/com/prueba/controllers/web/PagesController.java create mode 100644 src/main/java/com/prueba/core/context/handles/ApiHandler.java create mode 100644 src/main/java/com/prueba/core/context/handles/LoginHandler.java create mode 100644 src/main/java/com/prueba/core/context/handles/PagesHandler.java create mode 100644 src/main/java/com/prueba/core/context/handles/SessionHandler.java delete mode 100644 src/main/java/com/prueba/core/context/security/handle/ApiHandler.java delete mode 100644 src/main/java/com/prueba/core/context/security/handle/LoginHandler.java delete mode 100644 src/main/java/com/prueba/core/context/security/handle/PagesHandler.java delete mode 100644 src/main/java/com/prueba/core/context/security/handle/SessionHandler.java diff --git a/src/main/java/com/prueba/controllers/Controller.java b/src/main/java/com/prueba/controllers/Controller.java new file mode 100644 index 0000000..40c1052 --- /dev/null +++ b/src/main/java/com/prueba/controllers/Controller.java @@ -0,0 +1,7 @@ +package com.prueba.controllers; + +import com.sun.net.httpserver.HttpHandler; + +public interface Controller extends HttpHandler { + +} diff --git a/src/main/java/com/prueba/controllers/rest/ApiController.java b/src/main/java/com/prueba/controllers/rest/ApiController.java index a42140f..1bf2f0d 100644 --- a/src/main/java/com/prueba/controllers/rest/ApiController.java +++ b/src/main/java/com/prueba/controllers/rest/ApiController.java @@ -7,6 +7,7 @@ import java.util.Map; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.prueba.controllers.Controller; import com.prueba.controllers.deserializers.AccountResourceDeserializer; import com.prueba.controllers.serializers.AccountResourceSerializer; import com.prueba.core.context.util.AntPathMatcher; @@ -14,7 +15,7 @@ import com.prueba.model.domain.AccountResource; import com.prueba.services.impl.ApiServiceImpl; import com.sun.net.httpserver.HttpExchange; -public class ApiController implements RestController { +public class ApiController implements Controller { private static final Logger LOGGER = LoggerFactory.getLogger(ApiController.class); private static final String USER_NAME_PARAM = "username"; private static final String API_URL_PATTERN = "/app/api/users/{" + USER_NAME_PARAM + "}"; diff --git a/src/main/java/com/prueba/controllers/rest/LoginController.java b/src/main/java/com/prueba/controllers/rest/LoginController.java deleted file mode 100644 index 663b524..0000000 --- a/src/main/java/com/prueba/controllers/rest/LoginController.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.prueba.controllers.rest; - -import java.io.IOException; - -import com.prueba.services.impl.LoginServiceImpl; -import com.sun.net.httpserver.HttpExchange; - -public class LoginController implements RestController { - - @Override - public void handle(HttpExchange httpExchange) throws IOException { - final LoginServiceImpl loginService = new LoginServiceImpl(); - final String requestedURI = httpExchange.getRequestURI().toString(); - - if (requestedURI.startsWith("/app/login/login.html")) { - - final String requestMethod = httpExchange.getRequestMethod(); - - switch (requestMethod) { - case "GET": - loginService.processLoginGet(httpExchange); - break; - case "POST": - loginService.processLoginPost(httpExchange); - break; - default: - httpExchange.sendResponseHeaders(404, 0); - break; - } - - } else if (requestedURI.startsWith("/app/login/logout.html")) { - loginService.processLogoutGet(httpExchange); - } else { - httpExchange.sendResponseHeaders(404, 0); - } - - } - - - -} diff --git a/src/main/java/com/prueba/controllers/rest/PagesController.java b/src/main/java/com/prueba/controllers/rest/PagesController.java deleted file mode 100644 index 376ce77..0000000 --- a/src/main/java/com/prueba/controllers/rest/PagesController.java +++ /dev/null @@ -1,66 +0,0 @@ -package com.prueba.controllers.rest; - -import java.io.IOException; -import java.io.OutputStream; - -import com.prueba.core.context.security.persistence.SessionInfo; -import com.prueba.core.context.security.persistence.context.SessionContext; -import com.prueba.view.page.PageImpl; -import com.sun.net.httpserver.HttpExchange; - -public class PagesController implements RestController { - - @Override - public void handle(HttpExchange httpExchange) throws IOException { - final String requestMethod = httpExchange.getRequestMethod(); - - switch (requestMethod) { - case "GET": - this.processPages(httpExchange); - break; - default: - httpExchange.sendResponseHeaders(404, 0); - break; - } - - } - - protected void processPages(HttpExchange httpExchange) throws IOException { - final String requestedURI = httpExchange.getRequestURI().toString(); - final PageImpl pageImpl = new PageImpl(); - - int responseStatus = 200; - String html = ""; - switch (requestedURI) { - case "/app/pages/page_1.html": - html = pageImpl.doPage(1, getSafeUserName()); - break; - case "/app/pages/page_2.html": - html = pageImpl.doPage(2, getSafeUserName()); - break; - case "/app/pages/page_3.html": - html = pageImpl.doPage(3, getSafeUserName()); - break; - default: - responseStatus = 404; - break; - } - - httpExchange.sendResponseHeaders(responseStatus, html.length()); - try (final OutputStream os = httpExchange.getResponseBody()) { - os.write(html.getBytes()); - } - } - - protected String getSafeUserName() { - SessionInfo sessionInfo = SessionContext.getSession(); - String userName = ""; - - if (sessionInfo != null) { - userName = sessionInfo.getUsername(); - } - - return userName; - } - -} diff --git a/src/main/java/com/prueba/controllers/rest/RestController.java b/src/main/java/com/prueba/controllers/rest/RestController.java deleted file mode 100644 index 4324091..0000000 --- a/src/main/java/com/prueba/controllers/rest/RestController.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.prueba.controllers.rest; - -import com.sun.net.httpserver.HttpHandler; - -public interface RestController extends HttpHandler { - -} diff --git a/src/main/java/com/prueba/controllers/web/LoginController.java b/src/main/java/com/prueba/controllers/web/LoginController.java new file mode 100644 index 0000000..8fc4f67 --- /dev/null +++ b/src/main/java/com/prueba/controllers/web/LoginController.java @@ -0,0 +1,42 @@ +package com.prueba.controllers.web; + +import java.io.IOException; + +import com.prueba.controllers.Controller; +import com.prueba.services.impl.LoginServiceImpl; +import com.sun.net.httpserver.HttpExchange; + +public class LoginController implements Controller { + + @Override + public void handle(HttpExchange httpExchange) throws IOException { + final LoginServiceImpl loginService = new LoginServiceImpl(); + final String requestedURI = httpExchange.getRequestURI().toString(); + + if (requestedURI.startsWith("/app/login/login.html")) { + + final String requestMethod = httpExchange.getRequestMethod(); + + switch (requestMethod) { + case "GET": + loginService.processLoginGet(httpExchange); + break; + case "POST": + loginService.processLoginPost(httpExchange); + break; + default: + httpExchange.sendResponseHeaders(404, 0); + break; + } + + } else if (requestedURI.startsWith("/app/login/logout.html")) { + loginService.processLogoutGet(httpExchange); + } else { + httpExchange.sendResponseHeaders(404, 0); + } + + } + + + +} diff --git a/src/main/java/com/prueba/controllers/web/PagesController.java b/src/main/java/com/prueba/controllers/web/PagesController.java new file mode 100644 index 0000000..cf9c8de --- /dev/null +++ b/src/main/java/com/prueba/controllers/web/PagesController.java @@ -0,0 +1,67 @@ +package com.prueba.controllers.web; + +import java.io.IOException; +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.view.page.PageImpl; +import com.sun.net.httpserver.HttpExchange; + +public class PagesController implements Controller { + + @Override + public void handle(HttpExchange httpExchange) throws IOException { + final String requestMethod = httpExchange.getRequestMethod(); + + switch (requestMethod) { + case "GET": + this.processPages(httpExchange); + break; + default: + httpExchange.sendResponseHeaders(404, 0); + break; + } + + } + + protected void processPages(HttpExchange httpExchange) throws IOException { + final String requestedURI = httpExchange.getRequestURI().toString(); + final PageImpl pageImpl = new PageImpl(); + + int responseStatus = 200; + String html = ""; + switch (requestedURI) { + case "/app/pages/page_1.html": + html = pageImpl.doPage(1, getSafeUserName()); + break; + case "/app/pages/page_2.html": + html = pageImpl.doPage(2, getSafeUserName()); + break; + case "/app/pages/page_3.html": + html = pageImpl.doPage(3, getSafeUserName()); + break; + default: + responseStatus = 404; + break; + } + + httpExchange.sendResponseHeaders(responseStatus, html.length()); + try (final OutputStream os = httpExchange.getResponseBody()) { + os.write(html.getBytes()); + } + } + + protected String getSafeUserName() { + SessionInfo sessionInfo = SessionContext.getSession(); + String userName = ""; + + if (sessionInfo != null) { + userName = sessionInfo.getUsername(); + } + + return userName; + } + +} diff --git a/src/main/java/com/prueba/core/MainRun.java b/src/main/java/com/prueba/core/MainRun.java index cd074ed..7169c09 100644 --- a/src/main/java/com/prueba/core/MainRun.java +++ b/src/main/java/com/prueba/core/MainRun.java @@ -4,12 +4,11 @@ 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.security.handle.ApiHandler; -import com.prueba.core.context.security.handle.LoginHandler; -import com.prueba.core.context.security.handle.PagesHandler; +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; diff --git a/src/main/java/com/prueba/core/context/handles/ApiHandler.java b/src/main/java/com/prueba/core/context/handles/ApiHandler.java new file mode 100644 index 0000000..e7313f2 --- /dev/null +++ b/src/main/java/com/prueba/core/context/handles/ApiHandler.java @@ -0,0 +1,49 @@ +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); + } + } +} diff --git a/src/main/java/com/prueba/core/context/handles/LoginHandler.java b/src/main/java/com/prueba/core/context/handles/LoginHandler.java new file mode 100644 index 0000000..423eb7b --- /dev/null +++ b/src/main/java/com/prueba/core/context/handles/LoginHandler.java @@ -0,0 +1,46 @@ +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); + } +} diff --git a/src/main/java/com/prueba/core/context/handles/PagesHandler.java b/src/main/java/com/prueba/core/context/handles/PagesHandler.java new file mode 100644 index 0000000..468924b --- /dev/null +++ b/src/main/java/com/prueba/core/context/handles/PagesHandler.java @@ -0,0 +1,75 @@ +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); + } +} diff --git a/src/main/java/com/prueba/core/context/handles/SessionHandler.java b/src/main/java/com/prueba/core/context/handles/SessionHandler.java new file mode 100644 index 0000000..e84a94a --- /dev/null +++ b/src/main/java/com/prueba/core/context/handles/SessionHandler.java @@ -0,0 +1,37 @@ +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; + } +} diff --git a/src/main/java/com/prueba/core/context/security/handle/ApiHandler.java b/src/main/java/com/prueba/core/context/security/handle/ApiHandler.java deleted file mode 100644 index 109bdeb..0000000 --- a/src/main/java/com/prueba/core/context/security/handle/ApiHandler.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.prueba.core.context.security.handle; - -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); - } - } -} 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 deleted file mode 100644 index 62b180b..0000000 --- a/src/main/java/com/prueba/core/context/security/handle/LoginHandler.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.prueba.core.context.security.handle; - -import java.io.IOException; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.prueba.controllers.rest.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); - } -} diff --git a/src/main/java/com/prueba/core/context/security/handle/PagesHandler.java b/src/main/java/com/prueba/core/context/security/handle/PagesHandler.java deleted file mode 100644 index bc0eab4..0000000 --- a/src/main/java/com/prueba/core/context/security/handle/PagesHandler.java +++ /dev/null @@ -1,75 +0,0 @@ -package com.prueba.core.context.security.handle; - -import java.io.IOException; -import java.net.URI; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.prueba.controllers.rest.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); - } -} 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 deleted file mode 100644 index 4d39a0c..0000000 --- a/src/main/java/com/prueba/core/context/security/handle/SessionHandler.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.prueba.core.context.security.handle; - -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; - } -} diff --git a/src/main/java/com/prueba/core/context/web/application/ApplicationWebContext.java b/src/main/java/com/prueba/core/context/web/application/ApplicationWebContext.java index 76e36c4..1ab3604 100644 --- a/src/main/java/com/prueba/core/context/web/application/ApplicationWebContext.java +++ b/src/main/java/com/prueba/core/context/web/application/ApplicationWebContext.java @@ -3,12 +3,12 @@ package com.prueba.core.context.web.application; 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.security.handle.ApiHandler; -import com.prueba.core.context.security.handle.LoginHandler; -import com.prueba.core.context.security.handle.PagesHandler; -import com.prueba.core.context.security.handle.SessionHandler; +import com.prueba.core.context.handles.ApiHandler; import com.sun.net.httpserver.HttpHandler; diff --git a/src/main/java/com/prueba/services/impl/ApiServiceImpl.java b/src/main/java/com/prueba/services/impl/ApiServiceImpl.java index 7dad01c..d098b1d 100644 --- a/src/main/java/com/prueba/services/impl/ApiServiceImpl.java +++ b/src/main/java/com/prueba/services/impl/ApiServiceImpl.java @@ -4,21 +4,19 @@ import com.prueba.model.dao.AccountDao; import com.prueba.model.domain.AccountResource; public class ApiServiceImpl { + private final AccountDao accountDao = new AccountDao(); public AccountResource findAccountByCode(String accountCode) { - AccountDao accountDao = new AccountDao(); return accountDao.findByCode(accountCode); } public void createAccount(AccountResource account) { - AccountDao accountDao = new AccountDao(); - + accountDao.create(account); } public void deleteAccountByCode(String accountCode) { - AccountDao accountDao = new AccountDao(); accountDao.deleteByCode(accountCode); } -- 2.1.4