--- /dev/null
+package com.prueba.core;
+
+import com.sun.net.httpserver.HttpExchange;
+
+public interface Handle {
+
+ /**
+ * Handle some HTTP request
+ * @param httpExchange the HTTP request
+ */
+ public void handle(HttpExchange httpExchange);
+}
--- /dev/null
+package com.prueba.core;
+
+import java.io.IOException;
+import java.net.InetSocketAddress;
+
+import com.prueba.core.context.ApplicationContext;
+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();
+ HttpContext hc1 = server.createContext(
+ ApplicationWebContext.WEB_CONTEXT, appContext.getWebHandler());
+ server.setExecutor(null);
+ server.start();
+ }
+}
import javax.sql.DataSource;
+import com.sun.net.httpserver.HttpHandler;
+
+
public interface ApplicationContext {
DataSource getDataSource();
+ HttpHandler getWebHandler();
}
--- /dev/null
+package com.prueba.core.context.security.authenticator;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import javax.sql.DataSource;
+
+import com.prueba.core.context.integration.database.DataBaseAccess;
+import com.prueba.core.context.integration.database.impl.DataBaseAccessImpl;
+import com.sun.net.httpserver.BasicAuthenticator;
+
+public class CustomBasicAuthenticator extends BasicAuthenticator {
+ private static final String CODE = "CODE";
+ private static final String NAME = "NAME";
+ private static final String SURNAME = "SURNAME";
+ private static final String PASSWORD = "PASSWORD";
+ private static final String APP_ROLE_CODE = "APPLICATION_ROLE_CODE";
+
+ public CustomBasicAuthenticator(String context, DataSource dataSource) {
+ super(context);
+ }
+
+ @Override
+ public boolean checkCredentials(String username, String password) {
+ List<Map<String, String>> results = this.doQuery(username, password);
+
+ if (results.isEmpty()) {
+ return false;
+ } else {
+ return true;
+ }
+ }
+
+
+ protected List<Map<String, String>> doQuery(String username, String password) {
+ final DataBaseAccess dataBaseAccess = new DataBaseAccessImpl();
+ return dataBaseAccess.executeQuery("SELECT * FROM ACCOUNT WHERE CODE = ? AND PASSWORD = ?",
+ answer ->
+ {
+ final List<Map<String, String>> result = new ArrayList<>();
+ while (answer.next()) {
+ final Map<String, String> row = new HashMap<>();
+ row.put(CODE, answer.getString(CODE));
+ row.put(NAME, answer.getString(NAME));
+ row.put(SURNAME, answer.getString(SURNAME));
+ row.put(PASSWORD, answer.getString(PASSWORD));
+ row.put(APP_ROLE_CODE, answer.getString(APP_ROLE_CODE));
+ result.add(row);
+ }
+
+ return result;
+ },
+ preparedStatement -> {
+ preparedStatement.setString(1, username);
+ preparedStatement.setString(2, password);
+ });
+ }
+}
--- /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.sun.net.httpserver.Headers;
+import com.sun.net.httpserver.HttpExchange;
+import com.sun.net.httpserver.HttpHandler;
+
+
+public class SessionHandle implements HttpHandler {
+ private static final String COOKIE_HEADER = "Cookie";
+ private static final String LOGIN_PAGE = "http://localhost:8080/app/login/login.html?serviceName=http://localhost:8080";
+
+ private final Map<UUID, SessionInfo> sessions = new ConcurrentHashMap<>();
+
+ @Override
+ public void handle(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);
+ final SessionInfo sessionInfo = sessions.get(uuid);
+ if (sessionInfo != null) {
+ LocalDateTime currentDateTime = LocalDateTime.now();
+ if (sessionInfo.getLastSessionTime().plusMinutes(5).compareTo(currentDateTime) > 0) {
+ // Call next handler
+
+ final SessionInfo newSessionInfo = new SessionInfo(sessionInfo.getUsername(), LocalDateTime.now());
+ sessions.put(uuid, newSessionInfo);
+ } else {
+ this.doRedirec(httpExchange);
+ }
+ } else {
+ this.doRedirec(httpExchange);
+ }
+ } else {
+ this.doRedirec(httpExchange);
+ }
+
+ httpExchange.close();
+ }
+
+ protected void doRedirec(HttpExchange httpExchange) throws IOException {
+ URI requestURI = httpExchange.getRequestURI();
+ String requestURIString = requestURI.toString();
+ Headers responseHeaders = httpExchange.getResponseHeaders();
+ responseHeaders.add("Location", LOGIN_PAGE + requestURIString);
+ httpExchange.sendResponseHeaders(302, 0);
+ }
+}
--- /dev/null
+package com.prueba.core.context.security.persistence;
+
+import java.time.LocalDateTime;
+
+public class SessionInfo {
+ private final String username;
+ private final LocalDateTime lastSessionTime;
+
+ public SessionInfo(String username, LocalDateTime lastSessionTime) {
+ this.username = username;
+ this.lastSessionTime = lastSessionTime;
+ }
+
+ public String getUsername() {
+ return username;
+ }
+ public LocalDateTime getLastSessionTime() {
+ return lastSessionTime;
+ }
+
+}
-package com.prueba.core.web.application.context;
+package com.prueba.core.context.web.application;
import javax.sql.DataSource;
import com.prueba.core.context.ApplicationContext;
import com.prueba.core.context.integration.datasource.impl.DoDataSourceContext;
+import com.prueba.core.context.integration.liquibase.impl.LiquibaseContext;
+import com.prueba.core.context.security.handle.SessionHandle;
+import com.sun.net.httpserver.HttpHandler;
-public class ApplicationWebContext implements ApplicationContext {
+public class ApplicationWebContext implements ApplicationContext {
+ public static final String WEB_CONTEXT = "/app/pages/";
+
private final DataSource dataSource;
+ private final LiquibaseContext liquibaseContext;
+ private final HttpHandler webHttpHandler;
private ApplicationWebContext() {
this.dataSource = DoDataSourceContext.getInstance().getDataSource();
+ this.liquibaseContext = new LiquibaseContext(dataSource);
+ this.liquibaseContext.init();
+ this.webHttpHandler = new SessionHandle();
+
}
private static class ApplicationWebContextHolder {
public DataSource getDataSource() {
return this.dataSource;
}
-
-}
+
+ @Override
+ public HttpHandler getWebHandler() {
+ return this.webHttpHandler;
+ }
+}
import javax.sql.DataSource;
+import org.junit.Before;
import org.junit.Test;
-import com.prueba.core.web.application.context.ApplicationWebContext;
+import com.prueba.core.context.web.application.ApplicationWebContext;
+
+import com.sun.net.httpserver.HttpHandler;
public class ApplicationContextIntegrationTest {
+ private ApplicationContext context;
+
+ @Before
+ public void init() {
+ this.context = ApplicationWebContext.getInstance();
+ }
@Test
- public void whenGetDataSourceThenReturnCreatedDataSource() {
- final ApplicationContext context = ApplicationWebContext.getInstance();
-
- final DataSource dataSource = context.getDataSource();
+ public void whenGetDataSourceThenReturnCreatedDataSource() {
+ final DataSource dataSource = this.context.getDataSource();
assertNotNull(dataSource);
}
+
+ @Test
+ public void whenGetWebHanderThenReturnCreatedWebHandler() {
+
+ final HttpHandler webHandler = this.context.getWebHandler();
+
+ assertNotNull(webHandler);
+ }
+
}
--- /dev/null
+package com.prueba.core.context.security.handle;
+
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+
+public class SessionHandleTest {
+
+ @Test
+ public void test() {
+ fail("Not yet implemented");
+ }
+
+}
--- /dev/null
+package com.prueba.core.context.security.persistence;
+
+import static org.junit.Assert.*;
+
+import java.time.LocalDateTime;
+
+import org.junit.Test;
+
+public class SessionInfoTest {
+ private static final String USERNAME = "GUMARTINM";
+ private static final LocalDateTime LAST_SESSION = LocalDateTime.now();
+
+ @Test
+ public void shouldCallGetters() {
+ SessionInfo sessionInfo = new SessionInfo(USERNAME, LAST_SESSION);
+
+ assertEquals(USERNAME, sessionInfo.getUsername());
+ assertEquals(LAST_SESSION, sessionInfo.getLastSessionTime());
+ }
+
+}