PagesHandler
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Mon, 3 Oct 2016 12:26:46 +0000 (14:26 +0200)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Mon, 3 Oct 2016 12:26:46 +0000 (14:26 +0200)
src/main/java/com/prueba/core/context/security/handle/PagesHandler.java
src/main/java/com/prueba/resources/controllers/LoginController.java
src/main/java/com/prueba/resources/controllers/PagesController.java
src/main/java/com/prueba/view/login/PageImpl.java

index b51e3c9..b10261a 100644 (file)
@@ -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);
index 82da5db..275aa6a 100644 (file)
@@ -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());
index b057df6..167261c 100644 (file)
@@ -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;
+       }
        
 }
index 889b214..aa03c46 100644 (file)
@@ -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;
-       }
+
 }