import java.net.InetSocketAddress;
import com.prueba.core.context.ApplicationContext;
+import com.prueba.core.context.security.handle.LoginHandler;
+import com.prueba.core.context.security.handle.PagesHandler;
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 {
- HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0);
ApplicationContext appContext = ApplicationWebContext.getInstance();
- server.createContext(ApplicationWebContext.WEB_CONTEXT, appContext.getWebHandler());
- server.createContext(ApplicationWebContext.LOGIN_CONTEXT, appContext.getLoginHandler());
+ HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0);
+ server.createContext(PagesHandler.CONTEXT, appContext.getWebHandler());
+ server.createContext(LoginHandler.CONTEXT, appContext.getLoginHandler());
server.setExecutor(null);
+
server.start();
}
}
HttpHandler getWebHandler();
HttpHandler getLoginHandler();
+
+ public HttpHandler getSessionHandler();
}
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 final LoginController loginController = new LoginController();
+ private final HttpHandler sessionHandler;
+
+ public LoginHandler(HttpHandler sessionHandler) {
+ this.sessionHandler = sessionHandler;
+ }
@Override
public void handle(HttpExchange httpExchange) throws IOException {
- final Headers headers = httpExchange.getRequestHeaders();
+ sessionHandler.handle(httpExchange);
loginController.handle(httpExchange);
--- /dev/null
+package com.prueba.core.context.security.handle;
+
+import java.io.IOException;
+import java.net.URI;
+import java.time.LocalDateTime;
+import java.util.Map;
+import java.util.UUID;
+import java.util.concurrent.ConcurrentHashMap;
+
+import com.prueba.core.context.security.persistence.SessionInfo;
+import com.prueba.core.context.security.persistence.Sessions;
+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 String SERVER_ADDRESS = "http://localhost:8080";
+
+ private final HttpHandler sessionHandler;
+
+ public PagesHandler(HttpHandler sessionHandler) {
+ this.sessionHandler = sessionHandler;
+ }
+
+ @Override
+ public void handle(HttpExchange httpExchange) throws IOException {
+ this.sessionHandler.handle(httpExchange);
+
+ final SessionInfo sessionInfo = SessionHandler.getLocalSession();
+ if (Sessions.getInstance().isValidSession(httpExchange)) {
+
+ // Call controller
+ Sessions.getInstance().refreshSession(sessionInfo.getUUID(), sessionInfo.getUsername());
+ } else {
+ this.doRedirect(httpExchange);
+ }
+
+ httpExchange.close();
+ }
+
+ 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);
+ }
+}
package com.prueba.core.context.security.handle;
import java.io.IOException;
-import java.net.URI;
-import java.time.LocalDateTime;
-import java.util.Map;
import java.util.UUID;
-import java.util.concurrent.ConcurrentHashMap;
import com.prueba.core.context.security.persistence.SessionInfo;
+import com.prueba.core.context.security.persistence.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";
- private static final String SERVER_ADDRESS = "http://localhost:8080";
-
- private final Map<UUID, SessionInfo> sessions = new ConcurrentHashMap<>();
+ private static final ThreadLocal<SessionInfo> localContextSession = new ThreadLocal<>();
+
@Override
public void handle(HttpExchange httpExchange) throws IOException {
- final Headers headers = httpExchange.getRequestHeaders();
- final String cookieValue = headers.getFirst(COOKIE_HEADER);
-
final SessionInfo sessionInfo = getSessionInfo(httpExchange);
- if (this.isValidSession(httpExchange)) {
- // Call next handler
-
- this.refreshSession(sessionInfo.getUUID(), sessionInfo.getUsername());
- } else {
- this.doRedirect(httpExchange);
- }
- httpExchange.close();
+ localContextSession.set(sessionInfo);
}
-
- public boolean isValidSession(HttpExchange httpExchange) {
- final SessionInfo sessionInfo = getSessionInfo(httpExchange);
-
- 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;
- }
-
- public void refreshSession(UUID uuid, String username) {
- final SessionInfo newSessionInfo =
- new SessionInfo(uuid, username, LocalDateTime.now());
- sessions.put(uuid, newSessionInfo);
- }
-
- public Map<UUID, SessionInfo> getSessions() {
- return this.sessions;
- }
-
- public SessionInfo getSessionInfo(HttpExchange httpExchange) {
+
+ 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.get(uuid);
+ sessionInfo = Sessions.getInstance().getSession(uuid);
}
return sessionInfo;
}
- 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);
+ public static SessionInfo getLocalSession() {
+ return localContextSession.get();
}
}
--- /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.handle.SessionHandler;
+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(final 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(SessionInfo sessionInfo) {
+ sessions.remove(sessionInfo);
+ }
+
+ public boolean isValidSession(HttpExchange httpExchange) {
+ final SessionInfo sessionInfo = SessionHandler.getLocalSession();
+
+ boolean isValid = false;
+
+ if (sessionInfo != null) {
+ LocalDateTime currentDateTime = LocalDateTime.now();
+ if (sessionInfo.getLastSessionTime().plusMinutes(5).compareTo(currentDateTime) > 0) {
+ isValid = true;
+ } else {
+ sessions.remove(sessionInfo);
+ }
+ }
+
+ return isValid;
+ }
+}
import com.prueba.core.context.integration.liquibase.impl.LiquibaseContext;
import com.prueba.core.context.security.handle.LoginHandler;
import com.prueba.core.context.security.handle.SessionHandler;
+import com.prueba.core.context.security.handle.PagesHandler;
import com.prueba.resources.controllers.LoginController;
import com.sun.net.httpserver.HttpHandler;
-public class ApplicationWebContext implements ApplicationContext {
- public static final String WEB_CONTEXT = "/app/pages/";
- public static final String LOGIN_CONTEXT = "/app/login/";
-
+public class ApplicationWebContext implements ApplicationContext {
private final DataSource dataSource;
private final LiquibaseContext liquibaseContext;
+ private final HttpHandler sessionHandler;
private final HttpHandler webHttpHandler;
private final HttpHandler loginHandler;
this.dataSource = DoDataSourceContext.getInstance().getDataSource();
this.liquibaseContext = new LiquibaseContext(dataSource);
this.liquibaseContext.init();
- this.webHttpHandler = new SessionHandler();
- this.loginHandler = new LoginHandler();
+ this.sessionHandler = new SessionHandler();
+ this.webHttpHandler = new PagesHandler(sessionHandler);
+ this.loginHandler = new LoginHandler(sessionHandler);
}
private static class ApplicationWebContextHolder {
public HttpHandler getLoginHandler() {
return this.loginHandler;
}
+
+ @Override
+ public HttpHandler getSessionHandler() {
+ return this.sessionHandler;
+ }
}
public interface Controller extends HttpHandler {
- public String getURI();
}
import com.prueba.core.context.security.handle.SessionHandler;
import com.prueba.core.context.security.persistence.SessionInfo;
-import com.prueba.core.context.web.application.ApplicationWebContext;
+import com.prueba.core.context.security.persistence.Sessions;
import com.prueba.core.web.controller.Controller;
import com.prueba.services.impl.LoginServiceImpl;
import com.prueba.view.login.LoginFormImpl;
@Override
public void handle(HttpExchange httpExchange) throws IOException {
- final SessionHandler handler = (SessionHandler) ApplicationWebContext.getInstance().getWebHandler();
final String requestMethod = httpExchange.getRequestMethod();
switch (requestMethod) {
case "GET":
- this.processLogin(handler, httpExchange);
+ this.processLoginGet(httpExchange);
break;
case "POST":
- this.processLoginPost(handler, httpExchange);
+ this.processLoginPost(httpExchange);
break;
default:
httpExchange.sendResponseHeaders(404, 0);
}
- protected void processLogin(SessionHandler handler, HttpExchange httpExchange) throws IOException {
+ protected void processLoginGet(HttpExchange httpExchange) throws IOException {
final String requestedURI = httpExchange.getRequestURI().toString();
- final SessionInfo sessionInfo = handler.getSessionInfo(httpExchange);
+ final SessionInfo sessionInfo = SessionHandler.getLocalSession();
final LoginFormImpl loginForm = new LoginFormImpl();
String html;
- if (handler.isValidSession(httpExchange)) {
+ if (Sessions.getInstance().isValidSession(httpExchange)) {
html = loginForm.doNoRequiredLogin();
- handler.refreshSession(sessionInfo.getUUID(), sessionInfo.getUsername());
+ Sessions.getInstance().refreshSession(sessionInfo.getUUID(), sessionInfo.getUsername());
} else {
html = loginForm.doRequiredLogin(requestedURI);
}
}
- private void processLoginPost(SessionHandler handler, HttpExchange httpExchange) throws IOException {
- final SessionInfo sessionInfo = handler.getSessionInfo(httpExchange);
+ private void processLoginPost(HttpExchange httpExchange) throws IOException {
+ final SessionInfo sessionInfo = SessionHandler.getLocalSession();
- if (!handler.isValidSession(httpExchange)) {
+ if (!Sessions.getInstance().isValidSession(httpExchange)) {
String body = this.getBody(httpExchange);
String [] formData = body.split("&");
if (formData.length == 2) {
LoginServiceImpl loginService = new LoginServiceImpl();
if (loginService.isValidUser(username, password)) {
UUID uuid = UUID.randomUUID();
- setCookieHeader(httpExchange, uuid.toString());
- handler.refreshSession(uuid, username);
- doRedirect(httpExchange);
+ this.setCookieHeader(httpExchange, uuid.toString());
+ Sessions.getInstance().refreshSession(uuid, username);
+ this.doRedirect(httpExchange);
} else {
httpExchange.sendResponseHeaders(401, 0);
}
}
} else {
- handler.refreshSession(sessionInfo.getUUID(), sessionInfo.getUsername());
+ Sessions.getInstance().refreshSession(sessionInfo.getUUID(), sessionInfo.getUsername());
doRedirect(httpExchange);
}
}
protected void setCookieHeader(HttpExchange httpExchange, String UUIDString) {
Headers headers = httpExchange.getResponseHeaders();
+
headers.remove("Set-Cookie");
headers.set("Set-Cookie", UUIDString + "; path=/");
}
protected void doRedirect(HttpExchange httpExchange) throws IOException {
- URI requestURI = httpExchange.getRequestURI();
- String requestURIString = requestURI.toString();
+ String requestURIString = httpExchange.getRequestURI().toString();
String[] urls = requestURIString.split("serviceName=");
+ String serviceName = "";
if (urls.length == 2) {
- String serviceName = urls[1];
- Headers responseHeaders = httpExchange.getResponseHeaders();
- responseHeaders.add("Location", serviceName);
- httpExchange.sendResponseHeaders(302, 0);
+ serviceName = urls[1];
}
- }
-
- @Override
- public String getURI() {
- return URI;
+
+ Headers responseHeaders = httpExchange.getResponseHeaders();
+ responseHeaders.add("Location", serviceName);
+ httpExchange.sendResponseHeaders(302, 0);
}
}
--- /dev/null
+package com.prueba.resources.controllers;
+
+import java.io.IOException;
+
+import com.prueba.core.web.controller.Controller;
+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();
+
+ switch (requestedURI) {
+ case "/app/pages/page_1.html":
+
+ break;
+ case "/app/pages/page_2.html":
+
+ break;
+ case "/app/pages/page_3.html":
+
+ break;
+ default:
+ httpExchange.sendResponseHeaders(404, 0);
+ break;
+ }
+ }
+
+
+}
--- /dev/null
+package com.prueba.view.login;
+
+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 {
+ final HtmlCanvas html = new HtmlCanvas();
+ return html
+ .html()
+ .body()
+ .h1().content("PAGE: " + number)
+ .output().content("Hello: " + getSafeUserName())
+ ._body()
+ ._html()
+ .toHtml();
+ }
+
+ private String getSafeUserName() {
+ SessionInfo sessionInfo = SessionHandler.getLocalSession();
+ String userName = null;
+
+ if (sessionInfo != null) {
+ userName = sessionInfo.getUsername();
+ }
+
+ return userName;
+ }
+}