From 480424db998900492bd206bbac64151b55714488 Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Tue, 4 Oct 2016 14:37:25 +0200 Subject: [PATCH] ApplicationResourResource domain object --- .../persistence/dao/ApplicationResourceDao.java | 21 ++++++++++++++---- .../domain/ApplicationResourceResource.java | 25 ++++++++++++++++++++++ .../services/impl/AuthorizationServicesImpl.java | 10 ++++----- 3 files changed, 47 insertions(+), 9 deletions(-) create mode 100644 src/main/java/com/prueba/persistence/domain/ApplicationResourceResource.java diff --git a/src/main/java/com/prueba/persistence/dao/ApplicationResourceDao.java b/src/main/java/com/prueba/persistence/dao/ApplicationResourceDao.java index 0df20bb..fc352ab 100644 --- a/src/main/java/com/prueba/persistence/dao/ApplicationResourceDao.java +++ b/src/main/java/com/prueba/persistence/dao/ApplicationResourceDao.java @@ -9,16 +9,17 @@ import javax.sql.DataSource; import com.prueba.core.context.integration.database.impl.DataBaseAccessImpl; import com.prueba.core.context.web.application.ApplicationWebContext; +import com.prueba.persistence.domain.ApplicationResourceResource; public class ApplicationResourceDao { - public static final String URL_PATTERN = "URL_PATTERN"; - public static final String HTTP_METHOD = "HTTP_METHOD"; + private static final String URL_PATTERN = "URL_PATTERN"; + private static final String HTTP_METHOD = "HTTP_METHOD"; - public List> findURLsByUserName(String userName) { + public List findURLsByUserName(String userName) { final DataSource dataSource = ApplicationWebContext.getInstance().getDataSource(); final DataBaseAccessImpl dataBaseAccess = new DataBaseAccessImpl(dataSource); - return dataBaseAccess.executeQuery("" + final List> results = dataBaseAccess.executeQuery("" + "SELECT APP_RES.URL_PATTERN, APP_RES.HTTP_METHOD FROM APPLICATION_ROLE APP_ROLE " + "INNER JOIN APPLICATION_RESOURCE_APPLICATION_ROLE APP_RES_APP_ROLE ON APP_ROLE.CODE = APP_RES_APP_ROLE.APPLICATION_ROLE_CODE " + "INNER JOIN APPLICATION_RESOURCE APP_RES ON APP_RES.URL_PATTERN = APP_RES_APP_ROLE.APPLICATION_RESOURCE_URL_PATTERN " @@ -41,6 +42,18 @@ public class ApplicationResourceDao { preparedStatement -> { preparedStatement.setString(1, userName); }); + + final List applicationResources = new ArrayList<>(); + if (!results.isEmpty()) { + results.forEach(row -> + { + ApplicationResourceResource resource = + new ApplicationResourceResource(row.get(URL_PATTERN), row.get(HTTP_METHOD)); + applicationResources.add(resource); + }); + } + + return applicationResources; } } diff --git a/src/main/java/com/prueba/persistence/domain/ApplicationResourceResource.java b/src/main/java/com/prueba/persistence/domain/ApplicationResourceResource.java new file mode 100644 index 0000000..7d528c2 --- /dev/null +++ b/src/main/java/com/prueba/persistence/domain/ApplicationResourceResource.java @@ -0,0 +1,25 @@ +package com.prueba.persistence.domain; + +public class ApplicationResourceResource { + private final String urlPattern; + private final String httpMethod; + + public ApplicationResourceResource() { + this.urlPattern = null; + this.httpMethod = null; + } + + public ApplicationResourceResource(String urlPattern, String httpMethod) { + this.urlPattern = urlPattern; + this.httpMethod = httpMethod; + } + + public String getUrlPattern() { + return urlPattern; + } + + public String getHttpMethod() { + return httpMethod; + } + +} diff --git a/src/main/java/com/prueba/services/impl/AuthorizationServicesImpl.java b/src/main/java/com/prueba/services/impl/AuthorizationServicesImpl.java index 77e9f22..d30f61d 100644 --- a/src/main/java/com/prueba/services/impl/AuthorizationServicesImpl.java +++ b/src/main/java/com/prueba/services/impl/AuthorizationServicesImpl.java @@ -8,10 +8,10 @@ import org.slf4j.LoggerFactory; import com.prueba.core.context.util.AntPathMatcher; import com.prueba.persistence.dao.ApplicationResourceDao; +import com.prueba.persistence.domain.ApplicationResourceResource; public class AuthorizationServicesImpl { private static final Logger LOGGER = LoggerFactory.getLogger(AuthorizationServicesImpl.class); - private static final String USER_NAME_PARAM = "username"; private static final String API_URL_PATTERN = "/app/api/users/{" + USER_NAME_PARAM + "}"; @@ -20,14 +20,14 @@ public class AuthorizationServicesImpl { final ApplicationResourceDao dao = new ApplicationResourceDao(); - final List> urls = dao.findURLsByUserName(userName); + final List urls = dao.findURLsByUserName(userName); - return urls.stream().anyMatch(urlMap -> + return urls.stream().anyMatch(url -> { - final String urlPatternValue = urlMap.get(ApplicationResourceDao.URL_PATTERN); + final String urlPatternValue = url.getUrlPattern(); final String urlReplacedPatternValue = urlPatternValue.replace("{" + USER_NAME_PARAM + "}", userNameParam); - final String httpMethodValue = urlMap.get(ApplicationResourceDao.HTTP_METHOD); + final String httpMethodValue = url.getHttpMethod(); return urlReplacedPatternValue.equals(uri) && httpMethodValue.equals(httpMethod); }); -- 2.1.4