return SessionsContextHolder.INSTANCE;
}
- public SessionInfo getSession(final UUID uuid) {
+ public SessionInfo getSession(UUID uuid) {
return sessions.get(uuid);
}
sessions.put(uuid, newSessionInfo);
}
- public void removeSession(SessionInfo sessionInfo) {
- sessions.remove(sessionInfo);
+ public void removeSession(UUID uuid) {
+ sessions.remove(uuid);
}
public boolean isValidSession(HttpExchange httpExchange) {
if (sessionInfo.getLastSessionTime().plusMinutes(5).compareTo(currentDateTime) > 0) {
isValid = true;
} else {
- sessions.remove(sessionInfo);
+ sessions.remove(sessionInfo.getUUID());
}
}
package com.prueba.resources.controllers;
-import java.io.ByteArrayOutputStream;
import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.net.URI;
-import java.nio.charset.Charset;
-import java.util.UUID;
-import com.prueba.core.context.security.handle.SessionHandler;
-import com.prueba.core.context.security.persistence.SessionInfo;
-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;
-import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpExchange;
public class LoginController implements Controller {
- private static final String URI = "/login/login.html";
-
+
@Override
public void handle(HttpExchange httpExchange) throws IOException {
- final String requestMethod = httpExchange.getRequestMethod();
-
- switch (requestMethod) {
- case "GET":
- this.processLoginGet(httpExchange);
- break;
- case "POST":
- this.processLoginPost(httpExchange);
- break;
- default:
- httpExchange.sendResponseHeaders(404, 0);
- break;
- }
-
- }
-
- protected void processLoginGet(HttpExchange httpExchange) throws IOException {
+ final LoginServiceImpl loginService = new LoginServiceImpl();
final String requestedURI = httpExchange.getRequestURI().toString();
- final SessionInfo sessionInfo = SessionHandler.getLocalSession();
- final LoginFormImpl loginForm = new LoginFormImpl();
- String html = "";
- if (Sessions.getInstance().isValidSession(httpExchange)) {
- html = loginForm.doNoRequiredLogin();
- Sessions.getInstance().refreshSession(sessionInfo.getUUID(), sessionInfo.getUsername());
+ if (requestedURI.startsWith("/app/login/login.html")) {
+ loginService.processLogin(httpExchange);
+ } else if (requestedURI.startsWith("/app/login/logout.html")) {
+ loginService.processLogoutGet(httpExchange);
} else {
- html = loginForm.doRequiredLogin(requestedURI);
- }
-
- httpExchange.sendResponseHeaders(200, html.length());
-
- try (final OutputStream os = httpExchange.getResponseBody()) {
- os.write(html.getBytes());
+ httpExchange.sendResponseHeaders(404, 0);
}
-
- }
- private void processLoginPost(HttpExchange httpExchange) throws IOException {
- final SessionInfo sessionInfo = SessionHandler.getLocalSession();
-
- if (!Sessions.getInstance().isValidSession(httpExchange)) {
- String body = this.getBody(httpExchange);
- String [] formData = body.split("&");
- if (formData.length == 2) {
- String username = formData[0].split("=")[1];
- String password = formData[1].split("=")[1];
-
- LoginServiceImpl loginService = new LoginServiceImpl();
- if (loginService.isValidUser(username, password)) {
- UUID uuid = UUID.randomUUID();
- this.setCookieHeader(httpExchange, uuid.toString());
- Sessions.getInstance().refreshSession(uuid, username);
- this.doRedirect(httpExchange);
- } else {
- httpExchange.sendResponseHeaders(401, 0);
- }
- }
- } else {
- Sessions.getInstance().refreshSession(sessionInfo.getUUID(), sessionInfo.getUsername());
- doRedirect(httpExchange);
- }
}
- private String getBody (HttpExchange httpExchange) throws IOException {
- try(final InputStream inputStream = httpExchange.getRequestBody();
- final ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream()) {
-
- final int bufferSize = 1024;
- final byte[] buffer = new byte[bufferSize];
-
- int len = 0;
- while ((len = inputStream.read(buffer)) != -1) {
- byteBuffer.write(buffer, 0, len);
- }
-
- return new String(byteBuffer.toByteArray(), Charset.forName("UTF-8"));
- }
- }
- 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 {
- String requestURIString = httpExchange.getRequestURI().toString();
- String[] urls = requestURIString.split("serviceName=");
- String serviceName = "";
- if (urls.length == 2) {
- serviceName = urls[1];
- }
-
- Headers responseHeaders = httpExchange.getResponseHeaders();
- responseHeaders.add("Location", serviceName);
- httpExchange.sendResponseHeaders(302, 0);
- }
}
html = pageImpl.doPage(2, getSafeUserName());
break;
case "/app/pages/page_3.html":
- html = pageImpl.doPage(2, getSafeUserName());
+ html = pageImpl.doPage(3, getSafeUserName());
break;
default:
responseStatus = 404;
package com.prueba.services.impl;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.nio.charset.Charset;
+import java.util.UUID;
+
+import com.prueba.core.context.security.handle.SessionHandler;
+import com.prueba.core.context.security.persistence.SessionInfo;
+import com.prueba.core.context.security.persistence.Sessions;
import com.prueba.persistence.dao.LoginDao;
+import com.prueba.view.login.LoginFormImpl;
+import com.sun.net.httpserver.Headers;
+import com.sun.net.httpserver.HttpExchange;
public class LoginServiceImpl {
+ private static final String COOKIE_HEADER = "Cookie";
public boolean isValidUser(String username, String password) {
final LoginDao dao = new LoginDao();
}
}
+
+ public void processLogoutGet(HttpExchange httpExchange) throws IOException {
+ final Headers headers = httpExchange.getRequestHeaders();
+ final String cookieValue = headers.getFirst(COOKIE_HEADER);
+ if (cookieValue != null) {
+ final UUID uuid = UUID.fromString(cookieValue);
+ Sessions.getInstance().removeSession(uuid);
+ }
+
+ httpExchange.sendResponseHeaders(200, 0);
+ }
+
+ public void processLogin(HttpExchange httpExchange) throws IOException {
+ final String requestMethod = httpExchange.getRequestMethod();
+
+ switch (requestMethod) {
+ case "GET":
+ this.processLoginGet(httpExchange);
+ break;
+ case "POST":
+ this.processLoginPost(httpExchange);
+ break;
+ default:
+ httpExchange.sendResponseHeaders(404, 0);
+ break;
+ }
+ }
+
+ protected void processLoginGet(HttpExchange httpExchange) throws IOException {
+ final String requestedURI = httpExchange.getRequestURI().toString();
+ final SessionInfo sessionInfo = SessionHandler.getLocalSession();
+ final LoginFormImpl loginForm = new LoginFormImpl();
+
+ String html = "";
+ if (Sessions.getInstance().isValidSession(httpExchange)) {
+ html = loginForm.doNoRequiredLogin();
+ Sessions.getInstance().refreshSession(sessionInfo.getUUID(), sessionInfo.getUsername());
+ } else {
+ html = loginForm.doRequiredLogin(requestedURI);
+ }
+
+ httpExchange.sendResponseHeaders(200, html.length());
+
+ try (final OutputStream os = httpExchange.getResponseBody()) {
+ os.write(html.getBytes());
+ }
+
+ }
+
+ protected void processLoginPost(HttpExchange httpExchange) throws IOException {
+ final SessionInfo sessionInfo = SessionHandler.getLocalSession();
+
+ if (!Sessions.getInstance().isValidSession(httpExchange)) {
+ String body = this.getBody(httpExchange);
+ String [] formData = body.split("&");
+ if (formData.length == 2) {
+ String username = formData[0].split("=")[1];
+ String password = formData[1].split("=")[1];
+
+ LoginServiceImpl loginService = new LoginServiceImpl();
+ if (loginService.isValidUser(username, password)) {
+ UUID uuid = UUID.randomUUID();
+ this.setCookieHeader(httpExchange, uuid.toString());
+ Sessions.getInstance().refreshSession(uuid, username);
+ this.doRedirect(httpExchange);
+ } else {
+ httpExchange.sendResponseHeaders(401, 0);
+ }
+ }
+ } else {
+ Sessions.getInstance().refreshSession(sessionInfo.getUUID(), sessionInfo.getUsername());
+ doRedirect(httpExchange);
+ }
+ }
+
+ protected String getBody (HttpExchange httpExchange) throws IOException {
+ try(final InputStream inputStream = httpExchange.getRequestBody();
+ final ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream()) {
+
+ final int bufferSize = 1024;
+ final byte[] buffer = new byte[bufferSize];
+
+ int len = 0;
+ while ((len = inputStream.read(buffer)) != -1) {
+ byteBuffer.write(buffer, 0, len);
+ }
+
+ return new String(byteBuffer.toByteArray(), Charset.forName("UTF-8"));
+ }
+ }
+
+ 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 {
+ String requestURIString = httpExchange.getRequestURI().toString();
+ String[] urls = requestURIString.split("serviceName=");
+ String serviceName = "";
+ if (urls.length == 2) {
+ serviceName = urls[1];
+ }
+
+ Headers responseHeaders = httpExchange.getResponseHeaders();
+ responseHeaders.add("Location", serviceName);
+ httpExchange.sendResponseHeaders(302, 0);
+ }
}