From d82923366201a770525c33fc5302e1816f934669 Mon Sep 17 00:00:00 2001 From: anhefti Date: Tue, 4 Dec 2018 13:34:23 +0100 Subject: [PATCH] fixed bugs --- .../ch/ethz/seb/sebserver/gbl/util/Utils.java | 7 +++++ .../webservice/datalayer/APIMessage.java | 27 ++++++++++++++----- .../AuthorizationGrantService.java | 10 ++++--- .../servicelayer/dao/impl/UserDaoImpl.java | 2 +- 4 files changed, 34 insertions(+), 12 deletions(-) diff --git a/src/main/java/ch/ethz/seb/sebserver/gbl/util/Utils.java b/src/main/java/ch/ethz/seb/sebserver/gbl/util/Utils.java index 5a07a1ef..0d75fc0a 100644 --- a/src/main/java/ch/ethz/seb/sebserver/gbl/util/Utils.java +++ b/src/main/java/ch/ethz/seb/sebserver/gbl/util/Utils.java @@ -12,6 +12,7 @@ import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.LinkedHashSet; +import java.util.List; import java.util.Set; public final class Utils { @@ -57,4 +58,10 @@ public final class Utils { return Collections.unmodifiableSet(new LinkedHashSet<>(Arrays.asList(items))); } + public static List asImmutableList(final T[] array) { + return (array != null) + ? Collections.unmodifiableList(Arrays.asList(array)) + : Collections.emptyList(); + } + } diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/datalayer/APIMessage.java b/src/main/java/ch/ethz/seb/sebserver/webservice/datalayer/APIMessage.java index 03d39e59..ff6a9b98 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/datalayer/APIMessage.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/datalayer/APIMessage.java @@ -8,10 +8,18 @@ package ch.ethz.seb.sebserver.webservice.datalayer; +import java.io.Serializable; +import java.util.Collections; +import java.util.List; + import org.apache.commons.lang3.StringUtils; import org.springframework.validation.FieldError; -public class APIMessage { +import ch.ethz.seb.sebserver.gbl.util.Utils; + +public class APIMessage implements Serializable { + + private static final long serialVersionUID = -6858683658311637361L; public enum ErrorMessage { UNEXPECTED("1000", "Unexpected intenral server-side error"), @@ -37,7 +45,11 @@ public class APIMessage { } public APIMessage of(final String detail, final String... attributes) { - return new APIMessage(this.messageCode, this.systemMessage, detail, attributes); + return new APIMessage( + this.messageCode, + this.systemMessage, + detail, + Utils.asImmutableList(attributes)); } public APIMessage of(final Throwable error) { @@ -48,18 +60,20 @@ public class APIMessage { public final String messageCode; public final String systemMessage; public final String details; - public final String[] attributes; + public final List attributes; public APIMessage( final String messageCode, final String systemMessage, final String details, - final String[] attributes) { + final List attributes) { this.messageCode = messageCode; this.systemMessage = systemMessage; this.details = details; - this.attributes = attributes; + this.attributes = (attributes != null) + ? Collections.unmodifiableList(attributes) + : Collections.emptyList(); } public APIMessage(final String messageCode, final String systemMessage, final String details) { @@ -82,7 +96,7 @@ public class APIMessage { return this.details; } - public String[] getAttributes() { + public List getAttributes() { return this.attributes; } @@ -110,7 +124,6 @@ public class APIMessage { public APIMessage getAPIMessage() { return this.apiMessage; } - } } diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/authorization/AuthorizationGrantService.java b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/authorization/AuthorizationGrantService.java index 4c0cf704..fbd5ade8 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/authorization/AuthorizationGrantService.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/authorization/AuthorizationGrantService.java @@ -188,7 +188,9 @@ public class AuthorizationGrantService { } private AuthorizationGrantRule getGrantRule(final EntityType type) { - return this.exceptionalRules.computeIfAbsent(type, entityType -> new BaseTypeGrantRule(entityType)); + return this.exceptionalRules.computeIfAbsent( + type, + entityType -> new BaseTypeGrantRule(entityType, this)); } private GrantRuleBuilder addGrant(final EntityType entityType) { @@ -207,17 +209,17 @@ public class AuthorizationGrantService { * entity-instance for the given grant type. * if true return true * if false return false */ - private final class BaseTypeGrantRule implements AuthorizationGrantRule { + private static class BaseTypeGrantRule implements AuthorizationGrantRule { private final EntityType type; private final Map grants; - public BaseTypeGrantRule(final EntityType type) { + public BaseTypeGrantRule(final EntityType type, final AuthorizationGrantService service) { this.type = type; this.grants = new EnumMap<>(UserRole.class); for (final UserRole role : UserRole.values()) { this.grants.put(role, - AuthorizationGrantService.this.grants.get(new RoleTypeKey(type, role))); + service.grants.get(new RoleTypeKey(type, role))); } } diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/dao/impl/UserDaoImpl.java b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/dao/impl/UserDaoImpl.java index 30be5155..b4fbe2b2 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/dao/impl/UserDaoImpl.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/dao/impl/UserDaoImpl.java @@ -300,7 +300,7 @@ public class UserDaoImpl implements UserDAO { private Result toDomainModel(final String nameId, final UserRecord record) { if (record == null) { - Result.ofError(new ResourceNotFoundException( + return Result.ofError(new ResourceNotFoundException( Domain.USER.TYPE_NAME, String.valueOf(nameId))); }