API classes
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Mon, 3 Oct 2016 19:04:27 +0000 (21:04 +0200)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Mon, 3 Oct 2016 19:04:27 +0000 (21:04 +0200)
23 files changed:
src/main/java/com/prueba/api/persistence/Account.java [new file with mode: 0644]
src/main/java/com/prueba/authorization/services/impl/AuthorizationServicesImpl.java
src/main/java/com/prueba/core/MainRun.java
src/main/java/com/prueba/core/context/ApplicationContext.java
src/main/java/com/prueba/core/context/integration/database/DataBaseAccess.java
src/main/java/com/prueba/core/context/integration/database/impl/DataBaseAccessImpl.java
src/main/java/com/prueba/core/context/security/authenticator/CustomBasicAuthenticator.java
src/main/java/com/prueba/core/context/security/authenticator/persistence/AuthenticationInfo.java [new file with mode: 0644]
src/main/java/com/prueba/core/context/security/handle/ApiHandler.java [new file with mode: 0644]
src/main/java/com/prueba/core/context/security/handle/PagesHandler.java
src/main/java/com/prueba/core/context/security/handle/SessionHandler.java
src/main/java/com/prueba/core/context/security/persistence/Sessions.java
src/main/java/com/prueba/core/context/security/persistence/context/BasicAuthenticationContext.java [new file with mode: 0644]
src/main/java/com/prueba/core/context/security/persistence/context/SessionContext.java [new file with mode: 0644]
src/main/java/com/prueba/core/context/web/application/ApplicationWebContext.java
src/main/java/com/prueba/persistence/dao/AccountDao.java [new file with mode: 0644]
src/main/java/com/prueba/resources/controllers/ApiController.java [new file with mode: 0644]
src/main/java/com/prueba/resources/controllers/LoginController.java
src/main/java/com/prueba/resources/controllers/PagesController.java
src/main/java/com/prueba/services/impl/ApiServiceImpl.java [new file with mode: 0644]
src/main/java/com/prueba/services/impl/LoginServiceImpl.java
src/main/resources/liquibase/dml.sql
src/test/java/com/prueba/persistence/dao/AccountDaoIntegrationTest.java [new file with mode: 0644]

diff --git a/src/main/java/com/prueba/api/persistence/Account.java b/src/main/java/com/prueba/api/persistence/Account.java
new file mode 100644 (file)
index 0000000..ff34f40
--- /dev/null
@@ -0,0 +1,38 @@
+package com.prueba.api.persistence;
+
+public class Account {
+       private final String code;
+       private final String name;
+       private final String surname;
+       private final String password;  
+       private final String role;
+       
+       public Account(String code, String name, String surname, String password, String role) {
+               this.code = code;
+               this.name = name;
+               this.surname = surname;
+               this.password = password;
+               this.role = role;
+       }
+
+       public String getCode() {
+               return code;
+       }
+
+       public String getName() {
+               return name;
+       }
+
+       public String getSurname() {
+               return surname;
+       }
+
+       public String getPassword() {
+               return password;
+       }
+
+       public String getRole() {
+               return role;
+       }
+
+}
index 9e6d726..9b101fb 100644 (file)
@@ -6,6 +6,7 @@ import java.util.Map;
 import com.prueba.authorization.persistence.dao.AuthorizationDao;
 
 public class AuthorizationServicesImpl {
+       private static final String USER_NAME_PARAM = "{username}";
 
        public boolean isAuthorized(String httpMethod, String url, String userName) {
                final AuthorizationDao dao = new AuthorizationDao();
@@ -14,10 +15,11 @@ public class AuthorizationServicesImpl {
                
                return urls.stream().anyMatch(urlMap ->
                {
-                       String urlPatternValue = urlMap.get(AuthorizationDao.URL_PATTERN);
-                       String httpMethodValue = urlMap.get(AuthorizationDao.HTTP_METHOD);
+                       final String urlPatternValue = urlMap.get(AuthorizationDao.URL_PATTERN);
+                       final String urlReplacedPatternValue = urlPatternValue.replace(USER_NAME_PARAM, userName);
+                       final String httpMethodValue = urlMap.get(AuthorizationDao.HTTP_METHOD);
                        
-                       return urlPatternValue.equals(url) && httpMethodValue.equals(httpMethod);
+                       return urlReplacedPatternValue.equals(url) && httpMethodValue.equals(httpMethod);
                });
                
        }
index 87972a9..cd074ed 100644 (file)
@@ -4,9 +4,13 @@ import java.io.IOException;
 import java.net.InetSocketAddress;
 
 import com.prueba.core.context.ApplicationContext;
+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.web.application.ApplicationWebContext;
+
+import com.sun.net.httpserver.HttpContext;
 import com.sun.net.httpserver.HttpServer;
 
 public class MainRun {
@@ -17,6 +21,10 @@ public class MainRun {
         HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0);
         server.createContext(PagesHandler.CONTEXT, appContext.getWebHandler());
         server.createContext(LoginHandler.CONTEXT, appContext.getLoginHandler());
+        
+        HttpContext apiContext = server.createContext(ApiHandler.CONTEXT, appContext.getApiHandler());
+        apiContext.setAuthenticator(new CustomBasicAuthenticator(ApiHandler.CONTEXT));
+        
         server.setExecutor(null);
         
         server.start();
index b59f1e7..1cfb421 100644 (file)
@@ -13,5 +13,7 @@ public interface ApplicationContext {
        
        HttpHandler getLoginHandler();
        
-       public HttpHandler getSessionHandler();
+       HttpHandler getSessionHandler();
+       
+       HttpHandler getApiHandler();
 }
index 84922b6..dbf78f0 100644 (file)
@@ -16,6 +16,10 @@ public interface DataBaseAccess {
         List<Map<String, String>> executeResultSet(final TResult resultSet) throws SQLException;
     }
     
+    public interface ExecuteUpdateStatement<TResult> {
+        void executeStatement(final PreparedStatement statement) throws SQLException;
+    }
+    
     public interface FillPreparedStatement {
         void doFill(final PreparedStatement statement) throws SQLException;
     }
index 46bc785..47b5c6e 100644 (file)
@@ -26,6 +26,7 @@ public class DataBaseAccessImpl implements DataBaseAccess {
        @Override
        public List<Map<String, String>> executeQuery(
                        String query, ExecuteResultSet<ResultSet> executeResultSet, FillPreparedStatement fillStatement) {
+               
                List<Map<String, String>> result = null;
                try {
                        result = this.executeQueryThrowable(query, executeResultSet, fillStatement);
@@ -70,4 +71,43 @@ public class DataBaseAccessImpl implements DataBaseAccess {
 
        }
 
+       public void executeUpdate(String query, FillPreparedStatement fillStatement) {
+               
+               try {
+                       this.executeUpdateThrowable(query, fillStatement);
+               } catch (SQLException exception) {
+                       LOGGER.error("Query error: ", exception);
+               }
+
+       }
+       
+       protected void executeUpdateThrowable(
+                       String query, FillPreparedStatement fillStatement) throws SQLException {
+        try {
+            final Connection connection = this.dataSource.getConnection();
+
+            this.doExecuteUpdate(
+                       query,
+                    connection,
+                    preparedStatement ->
+                       {
+                               fillStatement.doFill(preparedStatement);
+                               
+                       preparedStatement.executeUpdate();
+                    });
+        } finally {
+               dataSource.getConnection().close();
+        }
+       }
+       
+       protected void doExecuteUpdate (
+                       final String query,
+                       final Connection connection,
+            final ExecuteUpdateStatement<ResultSet> executeUpdateStatement) throws SQLException {
+
+        try (final PreparedStatement preparedStatement  = connection.prepareStatement(query)) {
+               executeUpdateStatement.executeStatement(preparedStatement);
+        }
+
+       }
 }
index e148e56..2431825 100644 (file)
@@ -1,64 +1,28 @@
 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.prueba.core.context.security.authenticator.persistence.AuthenticationInfo;
+import com.prueba.core.context.security.persistence.context.BasicAuthenticationContext;
+import com.prueba.services.impl.LoginServiceImpl;
 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";
-       
-       private final DataSource dataSource;
 
-       public CustomBasicAuthenticator(String context, DataSource dataSource) {
+       public CustomBasicAuthenticator(String context) {
                super(context);
-               
-               this.dataSource = dataSource;
        }
        
        @Override
        public boolean checkCredentials(String username, String password) {
-               List<Map<String, String>> results = this.doQuery(username, password);
+               final LoginServiceImpl loginService = new LoginServiceImpl();
                
-               if (results.isEmpty()) {
-                       return false;
-               } else {
+               if (loginService.isValidUser(username, password)) {
+                       BasicAuthenticationContext.setAuthentication(new AuthenticationInfo(username));
+                       
                        return true;
+               } else {
+                       BasicAuthenticationContext.setAuthentication(null);
+                       
+                       return false;
                }
        }
-
-       
-       protected List<Map<String, String>> doQuery(String username, String password) {
-               final DataBaseAccess dataBaseAccess = new DataBaseAccessImpl(this.dataSource);
-               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);
-               });
-       }
 }
diff --git a/src/main/java/com/prueba/core/context/security/authenticator/persistence/AuthenticationInfo.java b/src/main/java/com/prueba/core/context/security/authenticator/persistence/AuthenticationInfo.java
new file mode 100644 (file)
index 0000000..78b8946
--- /dev/null
@@ -0,0 +1,13 @@
+package com.prueba.core.context.security.authenticator.persistence;
+
+public class AuthenticationInfo {
+       private final String userName;
+       
+       public AuthenticationInfo(String userName) {
+               this.userName = userName;
+       }
+       
+       public String getUserName() {
+               return userName;
+       }
+}
diff --git a/src/main/java/com/prueba/core/context/security/handle/ApiHandler.java b/src/main/java/com/prueba/core/context/security/handle/ApiHandler.java
new file mode 100644 (file)
index 0000000..f0bddf5
--- /dev/null
@@ -0,0 +1,31 @@
+package com.prueba.core.context.security.handle;
+
+import java.io.IOException;
+
+import com.prueba.authorization.services.impl.AuthorizationServicesImpl;
+import com.prueba.core.context.security.authenticator.persistence.AuthenticationInfo;
+import com.prueba.core.context.security.persistence.context.BasicAuthenticationContext;
+import com.prueba.resources.controllers.ApiController;
+import com.sun.net.httpserver.HttpExchange;
+import com.sun.net.httpserver.HttpHandler;
+
+public class ApiHandler implements HttpHandler {
+       public static final String CONTEXT = "/app/api/";
+       
+       private final ApiController apiController = new ApiController();
+       private final AuthorizationServicesImpl authorizationService = new AuthorizationServicesImpl();
+
+       @Override
+       public void handle(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);
+               }
+       }
+}
index 8b0d175..7a9287e 100644 (file)
@@ -6,6 +6,7 @@ import java.net.URI;
 import com.prueba.authorization.services.impl.AuthorizationServicesImpl;
 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.resources.controllers.PagesController;
 import com.sun.net.httpserver.Headers;
 import com.sun.net.httpserver.HttpExchange;
@@ -29,14 +30,13 @@ public class PagesHandler implements HttpHandler {
        public void handle(HttpExchange httpExchange) throws IOException  {     
                this.sessionHandler.handle(httpExchange);
                
-               final SessionInfo sessionInfo = SessionHandler.getLocalSession();
                if (Sessions.getInstance().isValidSession(httpExchange)) {
+                       final SessionInfo sessionInfo = SessionContext.getSession();
                        
                        if(authorizationService.isAuthorized(httpExchange.getRequestMethod(),
                                        httpExchange.getRequestURI().toString(), sessionInfo.getUsername())) {
                                
-                               pagesController.handle(httpExchange);
-                               
+                               pagesController.handle(httpExchange);                           
                        } else {
                                httpExchange.sendResponseHeaders(403, 0);
                        }
index 7caeaf8..4d39a0c 100644 (file)
@@ -5,20 +5,20 @@ 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";
-       private static final ThreadLocal<SessionInfo> localContextSession = new ThreadLocal<>();
        
 
        @Override
        public void handle(HttpExchange httpExchange) throws IOException  {
                final SessionInfo sessionInfo = getSessionInfo(httpExchange);
                
-               localContextSession.set(sessionInfo);
+               SessionContext.setSession(sessionInfo);
        }
 
        protected SessionInfo getSessionInfo(HttpExchange httpExchange) {
@@ -34,8 +34,4 @@ public class SessionHandler implements HttpHandler {
                
                return sessionInfo;
        }
-       
-       public static SessionInfo getLocalSession() {
-               return localContextSession.get();
-       }
 }
index 2a21a40..416c296 100644 (file)
@@ -5,7 +5,7 @@ import java.util.Map;
 import java.util.UUID;
 import java.util.concurrent.ConcurrentHashMap;
 
-import com.prueba.core.context.security.handle.SessionHandler;
+import com.prueba.core.context.security.persistence.context.SessionContext;
 import com.sun.net.httpserver.HttpExchange;
 
 public class Sessions {
@@ -38,7 +38,7 @@ public class Sessions {
        }
        
        public boolean isValidSession(HttpExchange httpExchange) {
-               final SessionInfo sessionInfo = SessionHandler.getLocalSession();
+               final SessionInfo sessionInfo = SessionContext.getSession();
                
                boolean isValid = false;
                
diff --git a/src/main/java/com/prueba/core/context/security/persistence/context/BasicAuthenticationContext.java b/src/main/java/com/prueba/core/context/security/persistence/context/BasicAuthenticationContext.java
new file mode 100644 (file)
index 0000000..34203f3
--- /dev/null
@@ -0,0 +1,15 @@
+package com.prueba.core.context.security.persistence.context;
+
+import com.prueba.core.context.security.authenticator.persistence.AuthenticationInfo;
+
+public class BasicAuthenticationContext {
+       private static final ThreadLocal<AuthenticationInfo> localContextSession = new ThreadLocal<>();
+
+       public static void setAuthentication(AuthenticationInfo authenticationInfo) {
+               localContextSession.set(authenticationInfo);
+       }
+
+       public static AuthenticationInfo getAuthentication() {
+               return localContextSession.get();
+       }
+}
diff --git a/src/main/java/com/prueba/core/context/security/persistence/context/SessionContext.java b/src/main/java/com/prueba/core/context/security/persistence/context/SessionContext.java
new file mode 100644 (file)
index 0000000..6827ec8
--- /dev/null
@@ -0,0 +1,15 @@
+package com.prueba.core.context.security.persistence.context;
+
+import com.prueba.core.context.security.persistence.SessionInfo;
+
+public class SessionContext {
+       private static final ThreadLocal<SessionInfo> localContextSession = new ThreadLocal<>();
+
+       public static void setSession(SessionInfo sessionInfo) {
+               localContextSession.set(sessionInfo);
+       }
+
+       public static SessionInfo getSession() {
+               return localContextSession.get();
+       }
+}
index a3c8fd1..923c139 100644 (file)
@@ -5,6 +5,7 @@ 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.ApiHandler;
 import com.prueba.core.context.security.handle.LoginHandler;
 import com.prueba.core.context.security.handle.SessionHandler;
 import com.prueba.core.context.security.handle.PagesHandler;
@@ -18,12 +19,14 @@ public class ApplicationWebContext implements ApplicationContext {
        private final HttpHandler sessionHandler;
        private final HttpHandler webHttpHandler;
        private final HttpHandler loginHandler;
+       private final HttpHandler apiHandler;
        
        private ApplicationWebContext() {
                this.dataSource = DoDataSourceContext.getInstance().getDataSource();
                this.liquibaseContext = new LiquibaseContext(dataSource);
                this.liquibaseContext.init();
                this.sessionHandler = new SessionHandler();
+               this.apiHandler = new ApiHandler();
                this.webHttpHandler = new PagesHandler(sessionHandler);
                this.loginHandler = new LoginHandler(sessionHandler);
        }
@@ -55,4 +58,9 @@ public class ApplicationWebContext implements ApplicationContext {
        public HttpHandler getSessionHandler() {
                return this.sessionHandler;
        }
+       
+       @Override
+       public HttpHandler getApiHandler() {
+               return this.apiHandler;
+       }
 } 
diff --git a/src/main/java/com/prueba/persistence/dao/AccountDao.java b/src/main/java/com/prueba/persistence/dao/AccountDao.java
new file mode 100644 (file)
index 0000000..4eff9ad
--- /dev/null
@@ -0,0 +1,83 @@
+package com.prueba.persistence.dao;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import javax.sql.DataSource;
+
+import com.prueba.api.persistence.Account;
+import com.prueba.core.context.integration.database.impl.DataBaseAccessImpl;
+import com.prueba.core.context.web.application.ApplicationWebContext;
+
+public class AccountDao {
+       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 Account findAccountByCode(String accountCode) {
+               final DataSource dataSource = ApplicationWebContext.getInstance().getDataSource();
+               final DataBaseAccessImpl dataBaseAccess = new DataBaseAccessImpl(dataSource);
+               
+               final List<Map<String, String>> results =
+                               dataBaseAccess.executeQuery("SELECT * FROM ACCOUNT WHERE CODE = ?",
+                               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, accountCode);
+               });
+               
+               Account account = null;
+               if (!results.isEmpty()) {
+                       final Map<String, String> row = results.get(0);
+                       
+                       account = new Account(row.get(CODE), row.get(NAME),
+                                       row.get(SURNAME), row.get(PASSWORD), row.get(APP_ROLE_CODE));
+               }
+               
+               return account;
+       }
+       
+       public void createAccount(Account account) {
+               final DataSource dataSource = ApplicationWebContext.getInstance().getDataSource();
+               final DataBaseAccessImpl dataBaseAccess = new DataBaseAccessImpl(dataSource);
+               
+               dataBaseAccess.executeUpdate(
+                               "INSERT INTO ACCOUNT VALUES (?, ?, ?, ?, ?)",
+               preparedStatement -> {
+                       preparedStatement.setString(1, account.getCode());
+                       preparedStatement.setString(2, account.getName());
+                       preparedStatement.setString(3, account.getSurname());
+                       preparedStatement.setString(4, account.getPassword());
+                       preparedStatement.setString(5, account.getRole());
+               });
+               
+       }
+       
+       public void deleteAccountByCode(String code) {
+               final DataSource dataSource = ApplicationWebContext.getInstance().getDataSource();
+               final DataBaseAccessImpl dataBaseAccess = new DataBaseAccessImpl(dataSource);
+               
+               dataBaseAccess.executeUpdate("DELETE FROM ACCOUNT WHERE CODE = ?",
+               preparedStatement -> {
+                       preparedStatement.setString(1, code);
+               });
+               
+       }
+}
diff --git a/src/main/java/com/prueba/resources/controllers/ApiController.java b/src/main/java/com/prueba/resources/controllers/ApiController.java
new file mode 100644 (file)
index 0000000..c163d5d
--- /dev/null
@@ -0,0 +1,60 @@
+package com.prueba.resources.controllers;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.charset.Charset;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.prueba.api.persistence.Account;
+import com.prueba.core.web.controller.Controller;
+import com.prueba.services.impl.ApiServiceImpl;
+import com.sun.net.httpserver.HttpExchange;
+
+public class ApiController implements Controller {
+
+       @Override
+       public void handle(HttpExchange httpExchange) throws IOException {
+               String requestMethod = httpExchange.getRequestMethod();
+               ApiServiceImpl apiService = new ApiServiceImpl();
+
+               String body = getBody(httpExchange);
+               
+               ObjectMapper mapper = new ObjectMapper();
+               Account account = mapper.readValue(body, Account.class);
+               
+               switch (requestMethod) {
+               case "GET":
+                       apiService.findAccountByCode("GUMARTIN");
+                       break;
+               case "POST":
+                       apiService.createAccount(account);
+                       break;
+               case "DELETE":
+                       apiService.deleteAccountByCode("GUMARTIN");
+                       break;
+               default:
+                       httpExchange.sendResponseHeaders(404, 0);
+                       break;
+       }
+               
+               
+               
+       }
+
+       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"));
+       }
+    }
+}
index 33aed71..5e78073 100644 (file)
@@ -14,7 +14,21 @@ public class LoginController implements Controller {
                final String requestedURI = httpExchange.getRequestURI().toString();
                
                if (requestedURI.startsWith("/app/login/login.html")) {
-                       loginService.processLogin(httpExchange);
+                       
+                       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 {
index e19100b..4fc7baa 100644 (file)
@@ -3,8 +3,8 @@ 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.context.security.persistence.context.SessionContext;
 import com.prueba.core.web.controller.Controller;
 import com.prueba.view.login.PageImpl;
 import com.sun.net.httpserver.HttpExchange;
@@ -54,7 +54,7 @@ public class PagesController implements Controller {
        }
        
        protected String getSafeUserName() {
-               SessionInfo sessionInfo = SessionHandler.getLocalSession();
+               SessionInfo sessionInfo = SessionContext.getSession();
                String userName = "";
                
                if (sessionInfo != null) {
diff --git a/src/main/java/com/prueba/services/impl/ApiServiceImpl.java b/src/main/java/com/prueba/services/impl/ApiServiceImpl.java
new file mode 100644 (file)
index 0000000..03bb5fc
--- /dev/null
@@ -0,0 +1,25 @@
+package com.prueba.services.impl;
+
+import com.prueba.api.persistence.Account;
+import com.prueba.persistence.dao.AccountDao;
+
+public class ApiServiceImpl {
+
+       public Account findAccountByCode(String accountCode) {
+               AccountDao accountDao = new AccountDao();
+               
+               return accountDao.findAccountByCode(accountCode);
+       }
+       
+       public void createAccount(Account account) {
+               AccountDao accountDao = new AccountDao();
+               
+               accountDao.createAccount(account);
+       }
+       
+       public void deleteAccountByCode(String accountCode) {
+               AccountDao accountDao = new AccountDao();
+               
+               accountDao.deleteAccountByCode(accountCode);
+       }
+}
index 772fe68..1416020 100644 (file)
@@ -7,9 +7,9 @@ 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.core.context.security.persistence.context.SessionContext;
 import com.prueba.persistence.dao.LoginDao;
 import com.prueba.view.login.LoginFormImpl;
 import com.sun.net.httpserver.Headers;
@@ -40,25 +40,9 @@ public class LoginServiceImpl {
                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 {
+       public void processLoginGet(HttpExchange httpExchange) throws IOException {
                final String requestedURI = httpExchange.getRequestURI().toString();
-               final SessionInfo sessionInfo = SessionHandler.getLocalSession();
+               final SessionInfo sessionInfo = SessionContext.getSession();
                final LoginFormImpl loginForm = new LoginFormImpl();
                
                String html = "";
@@ -77,8 +61,8 @@ public class LoginServiceImpl {
            
        }
        
-       protected void processLoginPost(HttpExchange httpExchange) throws IOException {
-               final SessionInfo sessionInfo = SessionHandler.getLocalSession();
+       public void processLoginPost(HttpExchange httpExchange) throws IOException {
+               final SessionInfo sessionInfo = SessionContext.getSession();
                
                if (!Sessions.getInstance().isValidSession(httpExchange)) {
                        String body = this.getBody(httpExchange);
index 81f8125..18b11f0 100644 (file)
@@ -12,7 +12,8 @@ INSERT INTO APPLICATION_RESOURCE (URL_PATTERN, HTTP_METHOD) values
 ('/app/pages/page_3.html', 'GET'),
 ('/app/api/{username}', 'GET'),
 ('/app/api/{username}', 'PUT'),
-('/app/api/{username}', 'POST');
+('/app/api/{username}', 'POST'),
+('/app/api/{username}', 'DELETE');
 
 
 INSERT INTO APPLICATION_RESOURCE_APPLICATION_ROLE (APPLICATION_RESOURCE_URL_PATTERN, APPLICATION_RESOURCE_HTTP_METHOD, APPLICATION_ROLE_CODE) values
@@ -27,7 +28,8 @@ INSERT INTO APPLICATION_RESOURCE_APPLICATION_ROLE (APPLICATION_RESOURCE_URL_PATT
 ('/app/api/{username}', 'GET', 'ROLE_APP_PAGE_3'),
 ('/app/api/{username}', 'GET', 'ROLE_APP_ADMIN'),
 ('/app/api/{username}', 'PUT', 'ROLE_APP_ADMIN'),
-('/app/api/{username}', 'POST', 'ROLE_APP_ADMIN');
+('/app/api/{username}', 'POST', 'ROLE_APP_ADMIN'),
+('/app/api/{username}', 'DELETE', 'ROLE_APP_ADMIN');
 
 
 INSERT INTO ACCOUNT (CODE, NAME, SURNAME, PASSWORD, APPLICATION_ROLE_CODE) values
diff --git a/src/test/java/com/prueba/persistence/dao/AccountDaoIntegrationTest.java b/src/test/java/com/prueba/persistence/dao/AccountDaoIntegrationTest.java
new file mode 100644 (file)
index 0000000..5b307e5
--- /dev/null
@@ -0,0 +1,84 @@
+package com.prueba.persistence.dao;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+
+import javax.sql.DataSource;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import com.prueba.api.persistence.Account;
+import com.prueba.core.context.integration.datasource.impl.DoDataSourceContext;
+import com.prueba.core.context.integration.liquibase.impl.LiquibaseContext;
+
+public class AccountDaoIntegrationTest {
+       private AccountDao accountDao;
+       private DataSource dataSource;
+       private LiquibaseContext liquibaseContext;
+       
+       @Before
+       public void init() {
+               dataSource = DoDataSourceContext.getInstance().getDataSource();
+               liquibaseContext = new LiquibaseContext(dataSource);
+               liquibaseContext.init();
+               accountDao = new AccountDao();
+       }
+
+       @Test
+       public void whenCreateNewAccountThenRetrieveNewAccount() {
+               Account expectedAccount = doSampleAccount();
+               
+               accountDao.createAccount(expectedAccount);
+               
+               Account account = accountDao.findAccountByCode(expectedAccount.getCode());
+               
+               assertNotNull(account);
+               assertEquals(expectedAccount.getCode(), account.getCode());
+               assertEquals(expectedAccount.getName(), account.getName());
+               assertEquals(expectedAccount.getSurname(), account.getSurname());
+               assertEquals(expectedAccount.getPassword(), account.getPassword());
+               assertEquals(expectedAccount.getRole(), account.getRole());
+       }
+       
+       @Test
+       public void whenDeleteAccountThenDoNotRetrieveAgainAccount() {
+               Account expectedAccount = doOtherSampleAccount();
+               
+               accountDao.createAccount(expectedAccount);              
+               Account account = accountDao.findAccountByCode(expectedAccount.getCode());
+               
+               assertNotNull(account);
+               assertEquals(expectedAccount.getCode(), account.getCode());
+               assertEquals(expectedAccount.getName(), account.getName());
+               assertEquals(expectedAccount.getSurname(), account.getSurname());
+               assertEquals(expectedAccount.getPassword(), account.getPassword());
+               assertEquals(expectedAccount.getRole(), account.getRole());
+               
+               accountDao.deleteAccountByCode(expectedAccount.getCode());
+               
+               account = accountDao.findAccountByCode(expectedAccount.getCode());
+               assertNull(account);
+       }
+       
+       private Account doSampleAccount() {
+               final String expectedCode = "PRUEBA";
+               final String expectedName = "Gustavo";
+               final String expectedSurname = "Martin Morcuende";
+               final String expectedPassword = "lame";
+               final String expectedAppRoleCode = "ROLE_APP_ADMIN";
+               
+               return new Account(expectedCode, expectedName, expectedSurname, expectedPassword, expectedAppRoleCode);
+       }
+       
+       private Account doOtherSampleAccount() {
+               final String expectedCode = "OTRAPRUEBA";
+               final String expectedName = "Gustavo";
+               final String expectedSurname = "Martin Morcuende";
+               final String expectedPassword = "lame";
+               final String expectedAppRoleCode = "ROLE_APP_ADMIN";
+               
+               return new Account(expectedCode, expectedName, expectedSurname, expectedPassword, expectedAppRoleCode);
+       }
+}