From c64ecd77baa52d0a434169b486f1fba8ff6523af Mon Sep 17 00:00:00 2001 From: BerndBreier <33351391+BerndBreier@users.noreply.github.com> Date: Thu, 15 Feb 2018 09:22:25 +0100 Subject: [PATCH] TSK-281 Remove NotAuthorizedException from BaseQuery. Replace it by NotAuthorizedToQueryWorkbasketException (RuntimeException) --- .../src/main/java/pro/taskana/BaseQuery.java | 37 +++++++---------- .../src/main/java/pro/taskana/TaskQuery.java | 4 +- ...tAuthorizedToQueryWorkbasketException.java | 14 +++++++ .../taskana/impl/ClassificationQueryImpl.java | 3 +- .../impl/ClassificationServiceImpl.java | 3 +- .../impl/ObjectReferenceQueryImpl.java | 3 +- .../java/pro/taskana/impl/TaskQueryImpl.java | 41 +++++++++++-------- .../impl/WorkbasketAccessItemQueryImpl.java | 21 ++++------ .../pro/taskana/impl/WorkbasketQueryImpl.java | 3 +- .../task/QueryTasksByWorkbasketAccTest.java | 5 ++- ...UpdateWorkbasketAuthorizationsAccTest.java | 5 ++- .../taskana/impl/TestClassificationQuery.java | 9 ++-- 12 files changed, 81 insertions(+), 67 deletions(-) create mode 100644 lib/taskana-core/src/main/java/pro/taskana/exceptions/NotAuthorizedToQueryWorkbasketException.java diff --git a/lib/taskana-core/src/main/java/pro/taskana/BaseQuery.java b/lib/taskana-core/src/main/java/pro/taskana/BaseQuery.java index 8a6fe0766..db8b0fff8 100644 --- a/lib/taskana-core/src/main/java/pro/taskana/BaseQuery.java +++ b/lib/taskana-core/src/main/java/pro/taskana/BaseQuery.java @@ -2,8 +2,6 @@ package pro.taskana; import java.util.List; -import pro.taskana.exceptions.NotAuthorizedException; - /** * Main query interface. * @@ -14,62 +12,57 @@ import pro.taskana.exceptions.NotAuthorizedException; public interface BaseQuery { /** - * This method will return a list of defined {@link T} objects. + * This method will return a list of defined {@link T} objects. In case of a TaskQuery, this method can throw a + * NotAuthorizedToQueryWorkbasketException. * * @return List containing elements of type T - * @throws NotAuthorizedException - * if the user is not authorized to perform this query */ - List list() throws NotAuthorizedException; + List list(); /** - * This method will return a list of defined {@link T} objects with specified offset and an limit. + * This method will return a list of defined {@link T} objects with specified offset and an limit. In case of a + * TaskQuery, this method can throw a NotAuthorizedToQueryWorkbasketException. * * @param offset * index of the first element which should be returned. * @param limit * number of elements which should be returned beginning with offset. * @return List containing elements of type T - * @throws NotAuthorizedException - * if the user is not authorized to perform this query */ - List list(int offset, int limit) throws NotAuthorizedException; + List list(int offset, int limit); /** * This method will return all results for page X with a size of Y of the current query.
- * Negative pageNumber/size will be changed to 0 and the last page got maybe less elements. + * Negative pageNumber/size will be changed to 0 and the last page got maybe less elements. In case of a TaskQuery, + * this method can throw a NotAuthorizedToQueryWorkbasketException. * * @param pageNumber * current pagination page starting at 0. * @param pageSize * amount of elements for this page. * @return resulList for the current query starting at X and returning max Y elements. - * @throws NotAuthorizedException - * if the user is not authorized to perform this query */ - default List listPage(int pageNumber, int pageSize) throws NotAuthorizedException { + default List listPage(int pageNumber, int pageSize) { int offset = (pageNumber < 0) ? 0 : (pageNumber * pageSize); int limit = (pageSize < 0) ? 0 : pageSize; return list(offset, limit); } /** - * This method will return a single object of {@link T}. + * This method will return a single object of {@link T}. In case of a TaskQuery, this method can throw a + * NotAuthorizedToQueryWorkbasketException. * * @return T a single object of given Type. - * @throws NotAuthorizedException - * if the user is not authorized to perform this query */ - T single() throws NotAuthorizedException; + T single(); /** - * Counting the amount of rows/results for the current query. This can be used for a pagination afterwards. + * Counting the amount of rows/results for the current query. This can be used for a pagination afterwards. In case + * of a TaskQuery, this method can throw a NotAuthorizedToQueryWorkbasketException. * - * @throws NotAuthorizedException - * when permissions not granted. * @return resultRowCount */ - long count() throws NotAuthorizedException; + long count(); /** * Determines the sort direction. diff --git a/lib/taskana-core/src/main/java/pro/taskana/TaskQuery.java b/lib/taskana-core/src/main/java/pro/taskana/TaskQuery.java index 5f9800453..caaa5c8e1 100644 --- a/lib/taskana-core/src/main/java/pro/taskana/TaskQuery.java +++ b/lib/taskana-core/src/main/java/pro/taskana/TaskQuery.java @@ -903,9 +903,11 @@ public interface TaskQuery extends BaseQuery { */ TaskQuery orderByCustom10(SortDirection sortDirection); - /* + /** * Filter for summaries which are containing one of the given taskIds. + * * @param taskIds + * The ids of the searched-for tasks. * @return the taskQuery */ TaskQuery idIn(String... taskIds); diff --git a/lib/taskana-core/src/main/java/pro/taskana/exceptions/NotAuthorizedToQueryWorkbasketException.java b/lib/taskana-core/src/main/java/pro/taskana/exceptions/NotAuthorizedToQueryWorkbasketException.java new file mode 100644 index 000000000..a6c2411a5 --- /dev/null +++ b/lib/taskana-core/src/main/java/pro/taskana/exceptions/NotAuthorizedToQueryWorkbasketException.java @@ -0,0 +1,14 @@ +package pro.taskana.exceptions; + +/** + * This exception is used to communicate that a user is not authorized to query a Workbasket. + */ +public class NotAuthorizedToQueryWorkbasketException extends TaskanaRuntimeException { + + public NotAuthorizedToQueryWorkbasketException(String msg) { + super(msg); + } + + private static final long serialVersionUID = 1L; + +} diff --git a/lib/taskana-core/src/main/java/pro/taskana/impl/ClassificationQueryImpl.java b/lib/taskana-core/src/main/java/pro/taskana/impl/ClassificationQueryImpl.java index 04f21cce6..704cfb2c8 100644 --- a/lib/taskana-core/src/main/java/pro/taskana/impl/ClassificationQueryImpl.java +++ b/lib/taskana-core/src/main/java/pro/taskana/impl/ClassificationQueryImpl.java @@ -12,7 +12,6 @@ import org.slf4j.LoggerFactory; import pro.taskana.ClassificationQuery; import pro.taskana.ClassificationSummary; import pro.taskana.TaskanaEngine; -import pro.taskana.exceptions.NotAuthorizedException; import pro.taskana.exceptions.TaskanaRuntimeException; import pro.taskana.impl.util.LoggerUtils; @@ -559,7 +558,7 @@ public class ClassificationQueryImpl implements ClassificationQuery { } @Override - public long count() throws NotAuthorizedException { + public long count() { LOGGER.debug("entry to count(), this = {}", this); Long rowCount = null; try { diff --git a/lib/taskana-core/src/main/java/pro/taskana/impl/ClassificationServiceImpl.java b/lib/taskana-core/src/main/java/pro/taskana/impl/ClassificationServiceImpl.java index c98d6dd8a..32bae8c71 100644 --- a/lib/taskana-core/src/main/java/pro/taskana/impl/ClassificationServiceImpl.java +++ b/lib/taskana-core/src/main/java/pro/taskana/impl/ClassificationServiceImpl.java @@ -18,6 +18,7 @@ import pro.taskana.exceptions.ClassificationAlreadyExistException; import pro.taskana.exceptions.ClassificationInUseException; import pro.taskana.exceptions.ClassificationNotFoundException; import pro.taskana.exceptions.NotAuthorizedException; +import pro.taskana.exceptions.NotAuthorizedToQueryWorkbasketException; import pro.taskana.exceptions.SystemException; import pro.taskana.impl.util.IdGenerator; import pro.taskana.impl.util.LoggerUtils; @@ -323,7 +324,7 @@ public class ClassificationServiceImpl implements ClassificationService { throw new ClassificationInUseException("There are " + classificationTasks.size() + " Tasks which belong to this classification or a child classification. Please complete them and try again."); } - } catch (NotAuthorizedException e) { + } catch (NotAuthorizedToQueryWorkbasketException e) { LOGGER.error( "ClassificationQuery unexpectedly returned NotauthorizedException. Throwing SystemException "); throw new SystemException("ClassificationQuery unexpectedly returned NotauthorizedException."); diff --git a/lib/taskana-core/src/main/java/pro/taskana/impl/ObjectReferenceQueryImpl.java b/lib/taskana-core/src/main/java/pro/taskana/impl/ObjectReferenceQueryImpl.java index c34e18ae3..662708bfa 100644 --- a/lib/taskana-core/src/main/java/pro/taskana/impl/ObjectReferenceQueryImpl.java +++ b/lib/taskana-core/src/main/java/pro/taskana/impl/ObjectReferenceQueryImpl.java @@ -10,7 +10,6 @@ import org.slf4j.LoggerFactory; import pro.taskana.ObjectReferenceQuery; import pro.taskana.TaskanaEngine; -import pro.taskana.exceptions.NotAuthorizedException; import pro.taskana.exceptions.TaskanaRuntimeException; import pro.taskana.impl.util.LoggerUtils; import pro.taskana.model.ObjectReference; @@ -169,7 +168,7 @@ public class ObjectReferenceQueryImpl implements ObjectReferenceQuery { } @Override - public long count() throws NotAuthorizedException { + public long count() { LOGGER.debug("entry to count(), this = {}", this); Long rowCount = null; try { diff --git a/lib/taskana-core/src/main/java/pro/taskana/impl/TaskQueryImpl.java b/lib/taskana-core/src/main/java/pro/taskana/impl/TaskQueryImpl.java index 4d97dc190..1c880d04c 100644 --- a/lib/taskana-core/src/main/java/pro/taskana/impl/TaskQueryImpl.java +++ b/lib/taskana-core/src/main/java/pro/taskana/impl/TaskQueryImpl.java @@ -15,6 +15,7 @@ import pro.taskana.TaskSummary; import pro.taskana.TaskanaEngine; import pro.taskana.TimeInterval; import pro.taskana.exceptions.NotAuthorizedException; +import pro.taskana.exceptions.NotAuthorizedToQueryWorkbasketException; import pro.taskana.exceptions.TaskanaRuntimeException; import pro.taskana.impl.util.LoggerUtils; import pro.taskana.model.TaskState; @@ -635,7 +636,7 @@ public class TaskQueryImpl implements TaskQuery { } @Override - public List list() throws NotAuthorizedException { + public List list() { List result = new ArrayList<>(); try { LOGGER.debug("entry to list(), this = {}", this); @@ -645,6 +646,8 @@ public class TaskQueryImpl implements TaskQuery { tasks = taskanaEngineImpl.getSqlSession().selectList(LINK_TO_MAPPER, this); result = taskService.augmentTaskSummariesByContainedSummaries(tasks); return result; + } catch (NotAuthorizedException e) { + throw new NotAuthorizedToQueryWorkbasketException(e.getMessage()); } finally { taskanaEngineImpl.returnConnection(); if (LOGGER.isDebugEnabled()) { @@ -656,7 +659,7 @@ public class TaskQueryImpl implements TaskQuery { } @Override - public List list(int offset, int limit) throws NotAuthorizedException { + public List list(int offset, int limit) { LOGGER.debug("entry to list(offset = {}, limit = {}), this = {}", offset, limit, this); List result = new ArrayList<>(); try { @@ -666,16 +669,16 @@ public class TaskQueryImpl implements TaskQuery { List tasks = taskanaEngineImpl.getSqlSession().selectList(LINK_TO_MAPPER, this, rowBounds); result = taskService.augmentTaskSummariesByContainedSummaries(tasks); return result; - } catch (Exception e) { - if (e instanceof PersistenceException) { - if (e.getMessage().contains("ERRORCODE=-4470")) { - TaskanaRuntimeException ex = new TaskanaRuntimeException( - "The offset beginning was set over the amount of result-rows.", e.getCause()); - ex.setStackTrace(e.getStackTrace()); - throw ex; - } + } catch (PersistenceException e) { + if (e.getMessage().contains("ERRORCODE=-4470")) { + TaskanaRuntimeException ex = new TaskanaRuntimeException( + "The offset beginning was set over the amount of result-rows.", e.getCause()); + ex.setStackTrace(e.getStackTrace()); + throw ex; } throw e; + } catch (NotAuthorizedException e) { + throw new NotAuthorizedToQueryWorkbasketException(e.getMessage()); } finally { taskanaEngineImpl.returnConnection(); if (LOGGER.isDebugEnabled()) { @@ -687,7 +690,7 @@ public class TaskQueryImpl implements TaskQuery { } @Override - public TaskSummary single() throws NotAuthorizedException { + public TaskSummary single() { LOGGER.debug("entry to single(), this = {}", this); TaskSummary result = null; try { @@ -703,6 +706,8 @@ public class TaskQueryImpl implements TaskQuery { result = augmentedList.get(0); return result; + } catch (NotAuthorizedException e) { + throw new NotAuthorizedToQueryWorkbasketException(e.getMessage()); } finally { taskanaEngineImpl.returnConnection(); LOGGER.debug("exit from single(). Returning result {} ", result); @@ -710,7 +715,7 @@ public class TaskQueryImpl implements TaskQuery { } @Override - public long count() throws NotAuthorizedException { + public long count() { LOGGER.debug("entry to count(), this = {}", this); Long rowCount = null; try { @@ -724,11 +729,15 @@ public class TaskQueryImpl implements TaskQuery { } } - private void checkOpenPermissionForWorkbasketKey() throws NotAuthorizedException { - if (this.workbasketKeyIn != null && this.workbasketKeyIn.length > 0) { - for (String wbKey : this.workbasketKeyIn) { - taskanaEngineImpl.getWorkbasketService().checkAuthorization(wbKey, WorkbasketAuthorization.OPEN); + private void checkOpenPermissionForWorkbasketKey() { + try { + if (this.workbasketKeyIn != null && this.workbasketKeyIn.length > 0) { + for (String wbKey : this.workbasketKeyIn) { + taskanaEngineImpl.getWorkbasketService().checkAuthorization(wbKey, WorkbasketAuthorization.OPEN); + } } + } catch (NotAuthorizedException e) { + throw new NotAuthorizedToQueryWorkbasketException(e.getMessage()); } } diff --git a/lib/taskana-core/src/main/java/pro/taskana/impl/WorkbasketAccessItemQueryImpl.java b/lib/taskana-core/src/main/java/pro/taskana/impl/WorkbasketAccessItemQueryImpl.java index 494128b5b..ba56990bc 100644 --- a/lib/taskana-core/src/main/java/pro/taskana/impl/WorkbasketAccessItemQueryImpl.java +++ b/lib/taskana-core/src/main/java/pro/taskana/impl/WorkbasketAccessItemQueryImpl.java @@ -12,7 +12,6 @@ import org.slf4j.LoggerFactory; import pro.taskana.TaskanaEngine; import pro.taskana.WorkbasketAccessItem; import pro.taskana.WorkbasketAccessItemQuery; -import pro.taskana.exceptions.NotAuthorizedException; import pro.taskana.exceptions.TaskanaRuntimeException; import pro.taskana.impl.util.LoggerUtils; @@ -61,7 +60,7 @@ public class WorkbasketAccessItemQueryImpl implements WorkbasketAccessItemQuery } @Override - public List list() throws NotAuthorizedException { + public List list() { LOGGER.debug("entry to list(), this = {}", this); List result = new ArrayList<>(); try { @@ -81,7 +80,7 @@ public class WorkbasketAccessItemQueryImpl implements WorkbasketAccessItemQuery } @Override - public List list(int offset, int limit) throws NotAuthorizedException { + public List list(int offset, int limit) { LOGGER.debug("entry to list(offset = {}, limit = {}), this = {}", offset, limit, this); List result = new ArrayList<>(); try { @@ -91,14 +90,12 @@ public class WorkbasketAccessItemQueryImpl implements WorkbasketAccessItemQuery .selectList(LINK_TO_MAPPER, this, rowBounds); result.addAll(foundAccessItms); return result; - } catch (Exception e) { - if (e instanceof PersistenceException) { - if (e.getMessage().contains("ERRORCODE=-4470")) { - TaskanaRuntimeException ex = new TaskanaRuntimeException( - "The offset beginning was set over the amount of result-rows.", e.getCause()); - ex.setStackTrace(e.getStackTrace()); - throw ex; - } + } catch (PersistenceException e) { + if (e.getMessage().contains("ERRORCODE=-4470")) { + TaskanaRuntimeException ex = new TaskanaRuntimeException( + "The offset beginning was set over the amount of result-rows.", e.getCause()); + ex.setStackTrace(e.getStackTrace()); + throw ex; } throw e; } finally { @@ -112,7 +109,7 @@ public class WorkbasketAccessItemQueryImpl implements WorkbasketAccessItemQuery } @Override - public WorkbasketAccessItem single() throws NotAuthorizedException { + public WorkbasketAccessItem single() { LOGGER.debug("entry to single(), this = {}", this); WorkbasketAccessItem accessItm = null; try { diff --git a/lib/taskana-core/src/main/java/pro/taskana/impl/WorkbasketQueryImpl.java b/lib/taskana-core/src/main/java/pro/taskana/impl/WorkbasketQueryImpl.java index 5c548fbaa..94fa57959 100644 --- a/lib/taskana-core/src/main/java/pro/taskana/impl/WorkbasketQueryImpl.java +++ b/lib/taskana-core/src/main/java/pro/taskana/impl/WorkbasketQueryImpl.java @@ -15,7 +15,6 @@ import pro.taskana.WorkbasketQuery; import pro.taskana.WorkbasketSummary; import pro.taskana.configuration.TaskanaEngineConfiguration; import pro.taskana.exceptions.InvalidArgumentException; -import pro.taskana.exceptions.NotAuthorizedException; import pro.taskana.exceptions.TaskanaRuntimeException; import pro.taskana.impl.util.LoggerUtils; import pro.taskana.model.WorkbasketAuthorization; @@ -337,7 +336,7 @@ public class WorkbasketQueryImpl implements WorkbasketQuery { } @Override - public long count() throws NotAuthorizedException { + public long count() { LOGGER.debug("entry to count(), this = {}", this); Long rowCount = null; try { diff --git a/lib/taskana-core/src/test/java/acceptance/task/QueryTasksByWorkbasketAccTest.java b/lib/taskana-core/src/test/java/acceptance/task/QueryTasksByWorkbasketAccTest.java index 3d22cfcb7..110bf0b46 100644 --- a/lib/taskana-core/src/test/java/acceptance/task/QueryTasksByWorkbasketAccTest.java +++ b/lib/taskana-core/src/test/java/acceptance/task/QueryTasksByWorkbasketAccTest.java @@ -16,6 +16,7 @@ import pro.taskana.TaskService; import pro.taskana.TaskSummary; import pro.taskana.exceptions.InvalidArgumentException; import pro.taskana.exceptions.NotAuthorizedException; +import pro.taskana.exceptions.NotAuthorizedToQueryWorkbasketException; import pro.taskana.security.JAASRunner; import pro.taskana.security.WithAccessId; @@ -32,7 +33,7 @@ public class QueryTasksByWorkbasketAccTest extends AbstractAccTest { @WithAccessId( userName = "user_1_1", groupNames = {"group_1"}) - @Test(expected = NotAuthorizedException.class) + @Test(expected = NotAuthorizedToQueryWorkbasketException.class) public void testThrowsExceptionIfNoOpenerPermissionOnQueriedWorkbasket() throws SQLException, NotAuthorizedException, InvalidArgumentException { TaskService taskService = taskanaEngine.getTaskService(); @@ -44,7 +45,7 @@ public class QueryTasksByWorkbasketAccTest extends AbstractAccTest { @WithAccessId( userName = "user_1_1", groupNames = {"group_1"}) - @Test(expected = NotAuthorizedException.class) + @Test(expected = NotAuthorizedToQueryWorkbasketException.class) public void testThrowsExceptionIfNoOpenerPermissionOnAtLeastOneQueriedWorkbasket() throws SQLException, NotAuthorizedException, InvalidArgumentException { TaskService taskService = taskanaEngine.getTaskService(); diff --git a/lib/taskana-core/src/test/java/acceptance/workbasket/UpdateWorkbasketAuthorizationsAccTest.java b/lib/taskana-core/src/test/java/acceptance/workbasket/UpdateWorkbasketAuthorizationsAccTest.java index 6990c9797..67ef93d41 100644 --- a/lib/taskana-core/src/test/java/acceptance/workbasket/UpdateWorkbasketAuthorizationsAccTest.java +++ b/lib/taskana-core/src/test/java/acceptance/workbasket/UpdateWorkbasketAuthorizationsAccTest.java @@ -21,6 +21,7 @@ import pro.taskana.exceptions.ClassificationNotFoundException; import pro.taskana.exceptions.InvalidArgumentException; import pro.taskana.exceptions.InvalidWorkbasketException; import pro.taskana.exceptions.NotAuthorizedException; +import pro.taskana.exceptions.NotAuthorizedToQueryWorkbasketException; import pro.taskana.exceptions.TaskAlreadyExistException; import pro.taskana.exceptions.WorkbasketNotFoundException; import pro.taskana.impl.WorkbasketAccessItemImpl; @@ -137,8 +138,8 @@ public class UpdateWorkbasketAuthorizationsAccTest extends AbstractAccTest { taskService.createTaskQuery() .workbasketKeyIn(wbKey) .list(); - fail("NotAuthorizedException was expected "); - } catch (NotAuthorizedException ignored) { + fail("NotAuthorizedToQueryWorkbasketException was expected "); + } catch (NotAuthorizedToQueryWorkbasketException ignored) { // nothing to do } diff --git a/lib/taskana-core/src/test/java/pro/taskana/impl/TestClassificationQuery.java b/lib/taskana-core/src/test/java/pro/taskana/impl/TestClassificationQuery.java index 6fc20b55b..9de652d20 100644 --- a/lib/taskana-core/src/test/java/pro/taskana/impl/TestClassificationQuery.java +++ b/lib/taskana-core/src/test/java/pro/taskana/impl/TestClassificationQuery.java @@ -6,7 +6,6 @@ import java.util.List; import pro.taskana.ClassificationQuery; import pro.taskana.ClassificationSummary; -import pro.taskana.exceptions.NotAuthorizedException; /** * Created by BV on 26.10.2017. @@ -178,24 +177,24 @@ public class TestClassificationQuery implements ClassificationQuery { } @Override - public List list() throws NotAuthorizedException { + public List list() { List returnedClassifications = new ArrayList<>(); returnedClassifications.addAll(classifications); return returnedClassifications; } @Override - public List list(int offset, int limit) throws NotAuthorizedException { + public List list(int offset, int limit) { return null; } @Override - public ClassificationSummary single() throws NotAuthorizedException { + public ClassificationSummary single() { return null; } @Override - public long count() throws NotAuthorizedException { + public long count() { return 0; } }