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;
private static final String SERVER_ADDRESS = "http://localhost:8080";
+ private final PagesController pagesController = new PagesController();
private final HttpHandler sessionHandler;
public PagesHandler(HttpHandler sessionHandler) {
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);
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());
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 {
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;
+ }
}
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;
- }
+
}