TSK-281 Remove NotAuthorizedException from BaseQuery. Replace it by NotAuthorizedToQueryWorkbasketException (RuntimeException)

This commit is contained in:
BerndBreier 2018-02-15 09:22:25 +01:00 committed by Marcel Lengl
parent 0a904ac220
commit c64ecd77ba
12 changed files with 81 additions and 67 deletions

View File

@ -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<T> {
/**
* 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<T> list() throws NotAuthorizedException;
List<T> 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<T> list(int offset, int limit) throws NotAuthorizedException;
List<T> list(int offset, int limit);
/**
* This method will return all results for page X with a size of Y of the current query.<br>
* 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<T> listPage(int pageNumber, int pageSize) throws NotAuthorizedException {
default List<T> 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.

View File

@ -903,9 +903,11 @@ public interface TaskQuery extends BaseQuery<TaskSummary> {
*/
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);

View File

@ -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;
}

View File

@ -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 {

View File

@ -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.");

View File

@ -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 {

View File

@ -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<TaskSummary> list() throws NotAuthorizedException {
public List<TaskSummary> list() {
List<TaskSummary> 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<TaskSummary> list(int offset, int limit) throws NotAuthorizedException {
public List<TaskSummary> list(int offset, int limit) {
LOGGER.debug("entry to list(offset = {}, limit = {}), this = {}", offset, limit, this);
List<TaskSummary> result = new ArrayList<>();
try {
@ -666,16 +669,16 @@ public class TaskQueryImpl implements TaskQuery {
List<TaskSummaryImpl> 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());
}
}

View File

@ -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<WorkbasketAccessItem> list() throws NotAuthorizedException {
public List<WorkbasketAccessItem> list() {
LOGGER.debug("entry to list(), this = {}", this);
List<WorkbasketAccessItem> result = new ArrayList<>();
try {
@ -81,7 +80,7 @@ public class WorkbasketAccessItemQueryImpl implements WorkbasketAccessItemQuery
}
@Override
public List<WorkbasketAccessItem> list(int offset, int limit) throws NotAuthorizedException {
public List<WorkbasketAccessItem> list(int offset, int limit) {
LOGGER.debug("entry to list(offset = {}, limit = {}), this = {}", offset, limit, this);
List<WorkbasketAccessItem> 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 {

View File

@ -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 {

View File

@ -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();

View File

@ -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
}

View File

@ -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<ClassificationSummary> list() throws NotAuthorizedException {
public List<ClassificationSummary> list() {
List<ClassificationSummary> returnedClassifications = new ArrayList<>();
returnedClassifications.addAll(classifications);
return returnedClassifications;
}
@Override
public List<ClassificationSummary> list(int offset, int limit) throws NotAuthorizedException {
public List<ClassificationSummary> 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;
}
}