--- /dev/null
+package com.prueba.controllers;
+
+import com.sun.net.httpserver.HttpHandler;
+
+public interface Controller extends HttpHandler {
+
+}
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import com.prueba.controllers.Controller;
import com.prueba.controllers.deserializers.AccountResourceDeserializer;
import com.prueba.controllers.serializers.AccountResourceSerializer;
import com.prueba.core.context.util.AntPathMatcher;
import com.prueba.services.impl.ApiServiceImpl;
import com.sun.net.httpserver.HttpExchange;
-public class ApiController implements RestController {
+public class ApiController implements Controller {
private static final Logger LOGGER = LoggerFactory.getLogger(ApiController.class);
private static final String USER_NAME_PARAM = "username";
private static final String API_URL_PATTERN = "/app/api/users/{" + USER_NAME_PARAM + "}";
+++ /dev/null
-package com.prueba.controllers.rest;
-
-import java.io.IOException;
-
-import com.prueba.services.impl.LoginServiceImpl;
-import com.sun.net.httpserver.HttpExchange;
-
-public class LoginController implements RestController {
-
- @Override
- public void handle(HttpExchange httpExchange) throws IOException {
- final LoginServiceImpl loginService = new LoginServiceImpl();
- final String requestedURI = httpExchange.getRequestURI().toString();
-
- if (requestedURI.startsWith("/app/login/login.html")) {
-
- final String requestMethod = httpExchange.getRequestMethod();
-
- switch (requestMethod) {
- case "GET":
- loginService.processLoginGet(httpExchange);
- break;
- case "POST":
- loginService.processLoginPost(httpExchange);
- break;
- default:
- httpExchange.sendResponseHeaders(404, 0);
- break;
- }
-
- } else if (requestedURI.startsWith("/app/login/logout.html")) {
- loginService.processLogoutGet(httpExchange);
- } else {
- httpExchange.sendResponseHeaders(404, 0);
- }
-
- }
-
-
-
-}
+++ /dev/null
-package com.prueba.controllers.rest;
-
-import java.io.IOException;
-import java.io.OutputStream;
-
-import com.prueba.core.context.security.persistence.SessionInfo;
-import com.prueba.core.context.security.persistence.context.SessionContext;
-import com.prueba.view.page.PageImpl;
-import com.sun.net.httpserver.HttpExchange;
-
-public class PagesController implements RestController {
-
- @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();
- final PageImpl pageImpl = new PageImpl();
-
- int responseStatus = 200;
- 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(3, getSafeUserName());
- break;
- default:
- responseStatus = 404;
- break;
- }
-
- httpExchange.sendResponseHeaders(responseStatus, html.length());
- try (final OutputStream os = httpExchange.getResponseBody()) {
- os.write(html.getBytes());
- }
- }
-
- protected String getSafeUserName() {
- SessionInfo sessionInfo = SessionContext.getSession();
- String userName = "";
-
- if (sessionInfo != null) {
- userName = sessionInfo.getUsername();
- }
-
- return userName;
- }
-
-}
+++ /dev/null
-package com.prueba.controllers.rest;
-
-import com.sun.net.httpserver.HttpHandler;
-
-public interface RestController extends HttpHandler {
-
-}
--- /dev/null
+package com.prueba.controllers.web;
+
+import java.io.IOException;
+
+import com.prueba.controllers.Controller;
+import com.prueba.services.impl.LoginServiceImpl;
+import com.sun.net.httpserver.HttpExchange;
+
+public class LoginController implements Controller {
+
+ @Override
+ public void handle(HttpExchange httpExchange) throws IOException {
+ final LoginServiceImpl loginService = new LoginServiceImpl();
+ final String requestedURI = httpExchange.getRequestURI().toString();
+
+ if (requestedURI.startsWith("/app/login/login.html")) {
+
+ final String requestMethod = httpExchange.getRequestMethod();
+
+ switch (requestMethod) {
+ case "GET":
+ loginService.processLoginGet(httpExchange);
+ break;
+ case "POST":
+ loginService.processLoginPost(httpExchange);
+ break;
+ default:
+ httpExchange.sendResponseHeaders(404, 0);
+ break;
+ }
+
+ } else if (requestedURI.startsWith("/app/login/logout.html")) {
+ loginService.processLogoutGet(httpExchange);
+ } else {
+ httpExchange.sendResponseHeaders(404, 0);
+ }
+
+ }
+
+
+
+}
--- /dev/null
+package com.prueba.controllers.web;
+
+import java.io.IOException;
+import java.io.OutputStream;
+
+import com.prueba.controllers.Controller;
+import com.prueba.core.context.security.persistence.SessionInfo;
+import com.prueba.core.context.security.persistence.context.SessionContext;
+import com.prueba.view.page.PageImpl;
+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();
+ final PageImpl pageImpl = new PageImpl();
+
+ int responseStatus = 200;
+ 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(3, getSafeUserName());
+ break;
+ default:
+ responseStatus = 404;
+ break;
+ }
+
+ httpExchange.sendResponseHeaders(responseStatus, html.length());
+ try (final OutputStream os = httpExchange.getResponseBody()) {
+ os.write(html.getBytes());
+ }
+ }
+
+ protected String getSafeUserName() {
+ SessionInfo sessionInfo = SessionContext.getSession();
+ String userName = "";
+
+ if (sessionInfo != null) {
+ userName = sessionInfo.getUsername();
+ }
+
+ return userName;
+ }
+
+}
import java.net.InetSocketAddress;
import com.prueba.core.context.ApplicationContext;
+import com.prueba.core.context.handles.LoginHandler;
+import com.prueba.core.context.handles.PagesHandler;
import com.prueba.core.context.security.authenticator.CustomBasicAuthenticator;
-import com.prueba.core.context.security.handle.ApiHandler;
-import com.prueba.core.context.security.handle.LoginHandler;
-import com.prueba.core.context.security.handle.PagesHandler;
+import com.prueba.core.context.handles.ApiHandler;
import com.prueba.core.context.web.application.ApplicationWebContext;
-
import com.sun.net.httpserver.HttpContext;
import com.sun.net.httpserver.HttpServer;
--- /dev/null
+package com.prueba.core.context.handles;
+
+import java.io.IOException;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.prueba.controllers.rest.ApiController;
+import com.prueba.core.context.security.authenticator.persistence.AuthenticationInfo;
+import com.prueba.core.context.security.persistence.context.BasicAuthenticationContext;
+import com.prueba.services.impl.AuthorizationServicesImpl;
+import com.sun.net.httpserver.HttpExchange;
+import com.sun.net.httpserver.HttpHandler;
+
+public class ApiHandler implements HttpHandler {
+ public static final String CONTEXT = "/app/api/users/";
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(ApiHandler.class);
+
+ private final ApiController apiController = new ApiController();
+ private final AuthorizationServicesImpl authorizationService = new AuthorizationServicesImpl();
+
+ @Override
+ public void handle(HttpExchange httpExchange) throws IOException {
+
+ try {
+ this.handleThrowable(httpExchange);
+ } catch (Exception exception) {
+ LOGGER.error("ApiHandler error: ", exception);
+
+ httpExchange.sendResponseHeaders(500, 0);
+ } finally {
+ httpExchange.close();
+ }
+ }
+
+ protected void handleThrowable(HttpExchange httpExchange) throws IOException {
+ AuthenticationInfo authenticationInfo = BasicAuthenticationContext.getAuthentication();
+
+ if(authorizationService.isAuthorized(httpExchange.getRequestMethod(),
+ httpExchange.getRequestURI().toString(), authenticationInfo.getUserName())) {
+
+ apiController.handle(httpExchange);
+
+ } else {
+ httpExchange.sendResponseHeaders(403, 0);
+ }
+ }
+}
--- /dev/null
+package com.prueba.core.context.handles;
+
+import java.io.IOException;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.prueba.controllers.web.LoginController;
+import com.sun.net.httpserver.HttpExchange;
+import com.sun.net.httpserver.HttpHandler;
+
+
+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 static final Logger LOGGER = LoggerFactory.getLogger(LoginHandler.class);
+
+
+ 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 {
+ try {
+ this.handleThrowable(httpExchange);
+ } catch (Exception exception) {
+ LOGGER.error("LoginHandler error: ", exception);
+
+ httpExchange.sendResponseHeaders(500, 0);
+ } finally {
+ httpExchange.close();
+ }
+
+ }
+
+ protected void handleThrowable(HttpExchange httpExchange) throws IOException {
+ sessionHandler.handle(httpExchange);
+
+ loginController.handle(httpExchange);
+ }
+}
--- /dev/null
+package com.prueba.core.context.handles;
+
+import java.io.IOException;
+import java.net.URI;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.prueba.controllers.web.PagesController;
+import com.prueba.core.context.security.persistence.SessionInfo;
+import com.prueba.core.context.security.persistence.Sessions;
+import com.prueba.core.context.security.persistence.context.SessionContext;
+import com.prueba.services.impl.AuthorizationServicesImpl;
+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 Logger LOGGER = LoggerFactory.getLogger(PagesHandler.class);
+ private static final String SERVER_ADDRESS = "http://localhost:8080";
+
+ private final PagesController pagesController = new PagesController();
+ private final AuthorizationServicesImpl authorizationService = new AuthorizationServicesImpl();
+ private final HttpHandler sessionHandler;
+
+ public PagesHandler(HttpHandler sessionHandler) {
+ this.sessionHandler = sessionHandler;
+ }
+
+ @Override
+ public void handle(HttpExchange httpExchange) throws IOException {
+ try {
+ this.handleThrowable(httpExchange);
+ } catch (Exception exception) {
+ LOGGER.error("PagesHandler error: ", exception);
+
+ httpExchange.sendResponseHeaders(500, 0);
+ } finally {
+ httpExchange.close();
+ }
+ }
+
+ protected void handleThrowable(HttpExchange httpExchange) throws IOException {
+ this.sessionHandler.handle(httpExchange);
+
+ if (Sessions.getInstance().isValidSession(httpExchange)) {
+ final SessionInfo sessionInfo = SessionContext.getSession();
+
+ if(authorizationService.isAuthorized(httpExchange.getRequestMethod(),
+ httpExchange.getRequestURI().toString(), sessionInfo.getUsername())) {
+
+ pagesController.handle(httpExchange);
+
+ } else {
+ httpExchange.sendResponseHeaders(403, 0);
+ }
+
+ Sessions.getInstance().refreshSession(sessionInfo.getUUID(), sessionInfo.getUsername());
+ } else {
+ this.doRedirect(httpExchange);
+ }
+ }
+
+ 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);
+ }
+}
--- /dev/null
+package com.prueba.core.context.handles;
+
+import java.io.IOException;
+import java.util.UUID;
+
+import com.prueba.core.context.security.persistence.SessionInfo;
+import com.prueba.core.context.security.persistence.Sessions;
+import com.prueba.core.context.security.persistence.context.SessionContext;
+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";
+
+
+ @Override
+ public void handle(HttpExchange httpExchange) throws IOException {
+ final SessionInfo sessionInfo = getSessionInfo(httpExchange);
+
+ SessionContext.setSession(sessionInfo);
+ }
+
+ 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.getInstance().getSession(uuid);
+ }
+
+ return sessionInfo;
+ }
+}
+++ /dev/null
-package com.prueba.core.context.security.handle;
-
-import java.io.IOException;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import com.prueba.controllers.rest.ApiController;
-import com.prueba.core.context.security.authenticator.persistence.AuthenticationInfo;
-import com.prueba.core.context.security.persistence.context.BasicAuthenticationContext;
-import com.prueba.services.impl.AuthorizationServicesImpl;
-import com.sun.net.httpserver.HttpExchange;
-import com.sun.net.httpserver.HttpHandler;
-
-public class ApiHandler implements HttpHandler {
- public static final String CONTEXT = "/app/api/users/";
-
- private static final Logger LOGGER = LoggerFactory.getLogger(ApiHandler.class);
-
- private final ApiController apiController = new ApiController();
- private final AuthorizationServicesImpl authorizationService = new AuthorizationServicesImpl();
-
- @Override
- public void handle(HttpExchange httpExchange) throws IOException {
-
- try {
- this.handleThrowable(httpExchange);
- } catch (Exception exception) {
- LOGGER.error("ApiHandler error: ", exception);
-
- httpExchange.sendResponseHeaders(500, 0);
- } finally {
- httpExchange.close();
- }
- }
-
- protected void handleThrowable(HttpExchange httpExchange) throws IOException {
- AuthenticationInfo authenticationInfo = BasicAuthenticationContext.getAuthentication();
-
- if(authorizationService.isAuthorized(httpExchange.getRequestMethod(),
- httpExchange.getRequestURI().toString(), authenticationInfo.getUserName())) {
-
- apiController.handle(httpExchange);
-
- } else {
- httpExchange.sendResponseHeaders(403, 0);
- }
- }
-}
+++ /dev/null
-package com.prueba.core.context.security.handle;
-
-import java.io.IOException;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import com.prueba.controllers.rest.LoginController;
-import com.sun.net.httpserver.HttpExchange;
-import com.sun.net.httpserver.HttpHandler;
-
-
-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 static final Logger LOGGER = LoggerFactory.getLogger(LoginHandler.class);
-
-
- 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 {
- try {
- this.handleThrowable(httpExchange);
- } catch (Exception exception) {
- LOGGER.error("LoginHandler error: ", exception);
-
- httpExchange.sendResponseHeaders(500, 0);
- } finally {
- httpExchange.close();
- }
-
- }
-
- protected void handleThrowable(HttpExchange httpExchange) throws IOException {
- sessionHandler.handle(httpExchange);
-
- loginController.handle(httpExchange);
- }
-}
+++ /dev/null
-package com.prueba.core.context.security.handle;
-
-import java.io.IOException;
-import java.net.URI;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import com.prueba.controllers.rest.PagesController;
-import com.prueba.core.context.security.persistence.SessionInfo;
-import com.prueba.core.context.security.persistence.Sessions;
-import com.prueba.core.context.security.persistence.context.SessionContext;
-import com.prueba.services.impl.AuthorizationServicesImpl;
-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 Logger LOGGER = LoggerFactory.getLogger(PagesHandler.class);
- private static final String SERVER_ADDRESS = "http://localhost:8080";
-
- private final PagesController pagesController = new PagesController();
- private final AuthorizationServicesImpl authorizationService = new AuthorizationServicesImpl();
- private final HttpHandler sessionHandler;
-
- public PagesHandler(HttpHandler sessionHandler) {
- this.sessionHandler = sessionHandler;
- }
-
- @Override
- public void handle(HttpExchange httpExchange) throws IOException {
- try {
- this.handleThrowable(httpExchange);
- } catch (Exception exception) {
- LOGGER.error("PagesHandler error: ", exception);
-
- httpExchange.sendResponseHeaders(500, 0);
- } finally {
- httpExchange.close();
- }
- }
-
- protected void handleThrowable(HttpExchange httpExchange) throws IOException {
- this.sessionHandler.handle(httpExchange);
-
- if (Sessions.getInstance().isValidSession(httpExchange)) {
- final SessionInfo sessionInfo = SessionContext.getSession();
-
- if(authorizationService.isAuthorized(httpExchange.getRequestMethod(),
- httpExchange.getRequestURI().toString(), sessionInfo.getUsername())) {
-
- pagesController.handle(httpExchange);
-
- } else {
- httpExchange.sendResponseHeaders(403, 0);
- }
-
- Sessions.getInstance().refreshSession(sessionInfo.getUUID(), sessionInfo.getUsername());
- } else {
- this.doRedirect(httpExchange);
- }
- }
-
- 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);
- }
-}
+++ /dev/null
-package com.prueba.core.context.security.handle;
-
-import java.io.IOException;
-import java.util.UUID;
-
-import com.prueba.core.context.security.persistence.SessionInfo;
-import com.prueba.core.context.security.persistence.Sessions;
-import com.prueba.core.context.security.persistence.context.SessionContext;
-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";
-
-
- @Override
- public void handle(HttpExchange httpExchange) throws IOException {
- final SessionInfo sessionInfo = getSessionInfo(httpExchange);
-
- SessionContext.setSession(sessionInfo);
- }
-
- 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.getInstance().getSession(uuid);
- }
-
- return sessionInfo;
- }
-}
import javax.sql.DataSource;
import com.prueba.core.context.ApplicationContext;
+import com.prueba.core.context.handles.LoginHandler;
+import com.prueba.core.context.handles.PagesHandler;
+import com.prueba.core.context.handles.SessionHandler;
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.ApiHandler;
-import com.prueba.core.context.security.handle.LoginHandler;
-import com.prueba.core.context.security.handle.PagesHandler;
-import com.prueba.core.context.security.handle.SessionHandler;
+import com.prueba.core.context.handles.ApiHandler;
import com.sun.net.httpserver.HttpHandler;
import com.prueba.model.domain.AccountResource;
public class ApiServiceImpl {
+ private final AccountDao accountDao = new AccountDao();
public AccountResource findAccountByCode(String accountCode) {
- AccountDao accountDao = new AccountDao();
return accountDao.findByCode(accountCode);
}
public void createAccount(AccountResource account) {
- AccountDao accountDao = new AccountDao();
-
+
accountDao.create(account);
}
public void deleteAccountByCode(String accountCode) {
- AccountDao accountDao = new AccountDao();
accountDao.deleteByCode(accountCode);
}