From 4cd638d5e145528174286fcb0a232a9fc606853b Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Mon, 3 Oct 2016 14:26:46 +0200 Subject: [PATCH] PagesHandler --- .../core/context/security/handle/PagesHandler.java | 5 +++- .../resources/controllers/LoginController.java | 2 +- .../resources/controllers/PagesController.java | 28 +++++++++++++++++++--- src/main/java/com/prueba/view/login/PageImpl.java | 19 +++------------ 4 files changed, 33 insertions(+), 21 deletions(-) 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 index b51e3c9..b10261a 100644 --- a/src/main/java/com/prueba/core/context/security/handle/PagesHandler.java +++ b/src/main/java/com/prueba/core/context/security/handle/PagesHandler.java @@ -9,6 +9,7 @@ import java.util.concurrent.ConcurrentHashMap; import com.prueba.core.context.security.persistence.SessionInfo; import com.prueba.core.context.security.persistence.Sessions; +import com.prueba.resources.controllers.PagesController; import com.sun.net.httpserver.Headers; import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpHandler; @@ -19,6 +20,7 @@ public class PagesHandler implements HttpHandler { private static final String SERVER_ADDRESS = "http://localhost:8080"; + private final PagesController pagesController = new PagesController(); private final HttpHandler sessionHandler; public PagesHandler(HttpHandler sessionHandler) { @@ -32,7 +34,8 @@ public class PagesHandler implements HttpHandler { final SessionInfo sessionInfo = SessionHandler.getLocalSession(); if (Sessions.getInstance().isValidSession(httpExchange)) { - // Call controller + pagesController.handle(httpExchange); + Sessions.getInstance().refreshSession(sessionInfo.getUUID(), sessionInfo.getUsername()); } else { this.doRedirect(httpExchange); diff --git a/src/main/java/com/prueba/resources/controllers/LoginController.java b/src/main/java/com/prueba/resources/controllers/LoginController.java index 82da5db..275aa6a 100644 --- a/src/main/java/com/prueba/resources/controllers/LoginController.java +++ b/src/main/java/com/prueba/resources/controllers/LoginController.java @@ -43,7 +43,7 @@ public class LoginController implements Controller { final SessionInfo sessionInfo = SessionHandler.getLocalSession(); final LoginFormImpl loginForm = new LoginFormImpl(); - String html; + String html = ""; if (Sessions.getInstance().isValidSession(httpExchange)) { html = loginForm.doNoRequiredLogin(); Sessions.getInstance().refreshSession(sessionInfo.getUUID(), sessionInfo.getUsername()); diff --git a/src/main/java/com/prueba/resources/controllers/PagesController.java b/src/main/java/com/prueba/resources/controllers/PagesController.java index b057df6..167261c 100644 --- a/src/main/java/com/prueba/resources/controllers/PagesController.java +++ b/src/main/java/com/prueba/resources/controllers/PagesController.java @@ -1,8 +1,12 @@ package com.prueba.resources.controllers; import java.io.IOException; +import java.io.OutputStream; +import com.prueba.core.context.security.handle.SessionHandler; +import com.prueba.core.context.security.persistence.SessionInfo; import com.prueba.core.web.controller.Controller; +import com.prueba.view.login.PageImpl; import com.sun.net.httpserver.HttpExchange; public class PagesController implements Controller { @@ -24,22 +28,40 @@ public class PagesController implements Controller { protected void processPages(HttpExchange httpExchange) throws IOException { final String requestedURI = httpExchange.getRequestURI().toString(); + final PageImpl pageImpl = new PageImpl(); + 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(2, getSafeUserName()); break; default: httpExchange.sendResponseHeaders(404, 0); break; } + + httpExchange.sendResponseHeaders(200, html.length()); + + try (final OutputStream os = httpExchange.getResponseBody()) { + os.write(html.getBytes()); + } } + protected String getSafeUserName() { + SessionInfo sessionInfo = SessionHandler.getLocalSession(); + String userName = ""; + + if (sessionInfo != null) { + userName = sessionInfo.getUsername(); + } + + return userName; + } } diff --git a/src/main/java/com/prueba/view/login/PageImpl.java b/src/main/java/com/prueba/view/login/PageImpl.java index 889b214..aa03c46 100644 --- a/src/main/java/com/prueba/view/login/PageImpl.java +++ b/src/main/java/com/prueba/view/login/PageImpl.java @@ -4,31 +4,18 @@ import java.io.IOException; import org.rendersnake.HtmlCanvas; -import com.prueba.core.context.security.handle.SessionHandler; -import com.prueba.core.context.security.persistence.SessionInfo; - public class PageImpl { - public String doPage(int number) throws IOException { + public String doPage(int number, String userName) throws IOException { final HtmlCanvas html = new HtmlCanvas(); return html .html() .body() .h1().content("PAGE: " + number) - .output().content("Hello: " + getSafeUserName()) + .output().content("Hello: " + userName) ._body() ._html() .toHtml(); } - - private String getSafeUserName() { - SessionInfo sessionInfo = SessionHandler.getLocalSession(); - String userName = null; - - if (sessionInfo != null) { - userName = sessionInfo.getUsername(); - } - - return userName; - } + } -- 2.1.4