From 365040726409c5186d8c9c22262a1dc03059bcba Mon Sep 17 00:00:00 2001 From: "manuele.simi" Date: Sun, 4 Feb 2018 04:38:16 +0000 Subject: [PATCH] Clean up code moved to gxRest. git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/resource-management/resource-manager@162886 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../manager/io/codeexceptions/CodeEntity.java | 20 ------- .../manager/io/codeexceptions/CodeFinder.java | 24 -------- .../manager/io/codeexceptions/ErrorCode.java | 22 -------- .../io/codeexceptions/LocalCodeException.java | 29 ---------- .../SerializableErrorEntity.java | 51 ----------------- .../io/codeexceptions/WebCodeException.java | 35 ------------ .../deserializer/ErrorCodeDeserializer.java | 40 -------------- .../deserializer/ErrorEntityManager.java | 55 ------------------- .../deserializer/ExceptionDeserializer.java | 28 ---------- .../deserializer/package-info.java | 8 --- .../io/codeexceptions/package-info.java | 8 --- .../LocalCodeExceptionTest.java | 29 ---------- .../codeexceptions/WebCodeExceptionTest.java | 35 ------------ 13 files changed, 384 deletions(-) delete mode 100644 io/src/main/java/org/gcube/resourcemanagement/manager/io/codeexceptions/CodeEntity.java delete mode 100644 io/src/main/java/org/gcube/resourcemanagement/manager/io/codeexceptions/CodeFinder.java delete mode 100644 io/src/main/java/org/gcube/resourcemanagement/manager/io/codeexceptions/ErrorCode.java delete mode 100644 io/src/main/java/org/gcube/resourcemanagement/manager/io/codeexceptions/LocalCodeException.java delete mode 100644 io/src/main/java/org/gcube/resourcemanagement/manager/io/codeexceptions/SerializableErrorEntity.java delete mode 100644 io/src/main/java/org/gcube/resourcemanagement/manager/io/codeexceptions/WebCodeException.java delete mode 100644 io/src/main/java/org/gcube/resourcemanagement/manager/io/codeexceptions/deserializer/ErrorCodeDeserializer.java delete mode 100644 io/src/main/java/org/gcube/resourcemanagement/manager/io/codeexceptions/deserializer/ErrorEntityManager.java delete mode 100644 io/src/main/java/org/gcube/resourcemanagement/manager/io/codeexceptions/deserializer/ExceptionDeserializer.java delete mode 100644 io/src/main/java/org/gcube/resourcemanagement/manager/io/codeexceptions/deserializer/package-info.java delete mode 100644 io/src/main/java/org/gcube/resourcemanagement/manager/io/codeexceptions/package-info.java delete mode 100644 io/src/test/java/org/gcube/resourcemanagement/manager/io/codeexceptions/LocalCodeExceptionTest.java delete mode 100644 io/src/test/java/org/gcube/resourcemanagement/manager/io/codeexceptions/WebCodeExceptionTest.java diff --git a/io/src/main/java/org/gcube/resourcemanagement/manager/io/codeexceptions/CodeEntity.java b/io/src/main/java/org/gcube/resourcemanagement/manager/io/codeexceptions/CodeEntity.java deleted file mode 100644 index d827e3d..0000000 --- a/io/src/main/java/org/gcube/resourcemanagement/manager/io/codeexceptions/CodeEntity.java +++ /dev/null @@ -1,20 +0,0 @@ -package org.gcube.resourcemanagement.manager.io.codeexceptions; - -import javax.ws.rs.core.GenericEntity; - -/** - * An entity to wrapf {@link SerializableErrorEntity}s into a {@link WebCodeException}. - * - * @author Manuele Simi (ISTI CNR) - * - */ -public class CodeEntity extends GenericEntity { - - /** - * @param entity - */ - protected CodeEntity(SerializableErrorEntity entity) { - super(entity); - } - -} diff --git a/io/src/main/java/org/gcube/resourcemanagement/manager/io/codeexceptions/CodeFinder.java b/io/src/main/java/org/gcube/resourcemanagement/manager/io/codeexceptions/CodeFinder.java deleted file mode 100644 index 572402c..0000000 --- a/io/src/main/java/org/gcube/resourcemanagement/manager/io/codeexceptions/CodeFinder.java +++ /dev/null @@ -1,24 +0,0 @@ -package org.gcube.resourcemanagement.manager.io.codeexceptions; - -import java.util.stream.Stream; - -/** - * Helper methods to find an error code in an enumeration implementing {@link ErrorCode}. - * - * @author Manuele Simi (ISTI CNR) - * - */ -public class CodeFinder { - - /** - * Finds and convert the given code as enum value. - * @param code the code to look for. - * @param codes the enum values. - * @return the code as enum value or null if the code is not found. - */ - public static & ErrorCode> E findAndConvert(ErrorCode code, E[] codes) { - return Stream.of(codes).filter(e -> e.getId() == code.getId() && e.getMessage().equals(code.getMessage())) - .findFirst().orElse(null); - } - -} diff --git a/io/src/main/java/org/gcube/resourcemanagement/manager/io/codeexceptions/ErrorCode.java b/io/src/main/java/org/gcube/resourcemanagement/manager/io/codeexceptions/ErrorCode.java deleted file mode 100644 index b6a954a..0000000 --- a/io/src/main/java/org/gcube/resourcemanagement/manager/io/codeexceptions/ErrorCode.java +++ /dev/null @@ -1,22 +0,0 @@ -package org.gcube.resourcemanagement.manager.io.codeexceptions; - -/** - * Interface for error codes. - * - * @author Manuele Simi (ISTI CNR) - * - */ -public interface ErrorCode { - - /** - * Identifier of the code. - */ - public int getId(); - - /** - * The message associated to the code - * @return the message - */ - public String getMessage(); - -} diff --git a/io/src/main/java/org/gcube/resourcemanagement/manager/io/codeexceptions/LocalCodeException.java b/io/src/main/java/org/gcube/resourcemanagement/manager/io/codeexceptions/LocalCodeException.java deleted file mode 100644 index d418b58..0000000 --- a/io/src/main/java/org/gcube/resourcemanagement/manager/io/codeexceptions/LocalCodeException.java +++ /dev/null @@ -1,29 +0,0 @@ - -package org.gcube.resourcemanagement.manager.io.codeexceptions; - -/** - * A local exception wrapping an {@link ErrorCode}. - * - * @author Manuele Simi (ISTI CNR) - * - */ -public class LocalCodeException extends Exception implements ErrorCode { - - private static final long serialVersionUID = 1872093579811881630L; - private final int id; - private final String message; - - public LocalCodeException(ErrorCode code) { - super(); - this.id = code.getId(); - this.message = code.getMessage(); - } - - public int getId() { - return id; - } - - public String getMessage() { - return message; - } -} diff --git a/io/src/main/java/org/gcube/resourcemanagement/manager/io/codeexceptions/SerializableErrorEntity.java b/io/src/main/java/org/gcube/resourcemanagement/manager/io/codeexceptions/SerializableErrorEntity.java deleted file mode 100644 index 991b344..0000000 --- a/io/src/main/java/org/gcube/resourcemanagement/manager/io/codeexceptions/SerializableErrorEntity.java +++ /dev/null @@ -1,51 +0,0 @@ -package org.gcube.resourcemanagement.manager.io.codeexceptions; - - -/** - * An entity that can be serialized in a {@link WebCodeException}. - * - * @author Manuele Simi (ISTI CNR) - * - */ -public class SerializableErrorEntity { - - private int id = -1; - private String message; - private String exceptionClass; - - public SerializableErrorEntity() {} - - /** - * @param id - * @param message - */ - public SerializableErrorEntity(ErrorCode errorCode) { - this.id = errorCode.getId(); - this.message = errorCode.getMessage(); - } - - /** - * - * @param e - */ - public SerializableErrorEntity(Exception e) { - this.exceptionClass = e.getClass().getCanonicalName(); - this.message = e.getMessage(); - } - - public int getId() { - return this.id; - } - - - public String getMessage() { - return this.message; - } - - /** - * @return the full qualified name of the embedded {@link Exception} - */ - public String getExceptionClass() { - return this.exceptionClass; - } -} diff --git a/io/src/main/java/org/gcube/resourcemanagement/manager/io/codeexceptions/WebCodeException.java b/io/src/main/java/org/gcube/resourcemanagement/manager/io/codeexceptions/WebCodeException.java deleted file mode 100644 index 3c18989..0000000 --- a/io/src/main/java/org/gcube/resourcemanagement/manager/io/codeexceptions/WebCodeException.java +++ /dev/null @@ -1,35 +0,0 @@ -package org.gcube.resourcemanagement.manager.io.codeexceptions; - -import javax.ws.rs.WebApplicationException; -import javax.ws.rs.core.Response; - -/** - * Exception returned by the Resource Manager. - * - * @author Manuele Simi (ISTI CNR) - * - */ -public class WebCodeException extends WebApplicationException { - - private static final long serialVersionUID = 333945715086602250L; - - public WebCodeException() { - super(Response.status(Response.Status.NOT_ACCEPTABLE).build()); - } - public WebCodeException(Response.Status status, ErrorCode code) { - super(Response.status(status).entity(new CodeEntity(new SerializableErrorEntity(code))) - .build()); - } - - public WebCodeException(ErrorCode code) { - super(Response.status(Response.Status.NOT_ACCEPTABLE) - .entity(new CodeEntity(new SerializableErrorEntity(code))).build()); - } - - public WebCodeException(E exception) { - //super(Response.status(Response.Status.NOT_ACCEPTABLE) - // .entity(new CodeEntity(new SerializableErrorEntity(exception))).build()); - super(Response.status(Response.Status.NOT_ACCEPTABLE) - .entity(new CodeEntity(new SerializableErrorEntity(exception))).build()); - } -} diff --git a/io/src/main/java/org/gcube/resourcemanagement/manager/io/codeexceptions/deserializer/ErrorCodeDeserializer.java b/io/src/main/java/org/gcube/resourcemanagement/manager/io/codeexceptions/deserializer/ErrorCodeDeserializer.java deleted file mode 100644 index 4900f75..0000000 --- a/io/src/main/java/org/gcube/resourcemanagement/manager/io/codeexceptions/deserializer/ErrorCodeDeserializer.java +++ /dev/null @@ -1,40 +0,0 @@ -package org.gcube.resourcemanagement.manager.io.codeexceptions.deserializer; - -import org.gcube.resourcemanagement.manager.io.codeexceptions.ErrorCode; - -/** - * Deserializer for {@link ErrorCode}. - * - * @author Manuele Simi (ISTI CNR) - * - */ -final class ErrorCodeDeserializer { - - /** - * - */ - private ErrorCodeDeserializer() {} - - /** - * The error code, if any - * @return the error code or null - */ - protected static ErrorCode deserialize(int id, String message) { - if (id != 1) { - return new ErrorCode() { - - @Override - public String getMessage() { - return message; - } - - @Override - public int getId() { - return id; - } - }; - } else - return null; - - } -} diff --git a/io/src/main/java/org/gcube/resourcemanagement/manager/io/codeexceptions/deserializer/ErrorEntityManager.java b/io/src/main/java/org/gcube/resourcemanagement/manager/io/codeexceptions/deserializer/ErrorEntityManager.java deleted file mode 100644 index 8d81747..0000000 --- a/io/src/main/java/org/gcube/resourcemanagement/manager/io/codeexceptions/deserializer/ErrorEntityManager.java +++ /dev/null @@ -1,55 +0,0 @@ -package org.gcube.resourcemanagement.manager.io.codeexceptions.deserializer; - -import java.util.Objects; - -import org.gcube.resourcemanagement.manager.io.codeexceptions.ErrorCode; -import org.gcube.resourcemanagement.manager.io.codeexceptions.SerializableErrorEntity; - -/** - * Manager for extracting information held inside a {@link SerializableErrorEntity}. - * - * @author Manuele Simi (ISTI CNR) - * - */ -public class ErrorEntityManager { - - private final SerializableErrorEntity entity; - /** - * - */ - public ErrorEntityManager(SerializableErrorEntity entity) { - this.entity = entity; - } - - /** - * Checks if there is an {@link Exception} in the entity. - * @return true if the entity holds an exception, false otherwise - */ - public boolean hasException() { - return Objects.nonNull(this.entity.getExceptionClass()); - } - - /** - * Gets the {@link Exception} inside the entity. - * @return the exception or null - */ - public E getException() { - return ExceptionDeserializer.deserialize(this.entity.getExceptionClass(),this.entity.getMessage()); - } - - /** - * Checks if there is an {@link ErrorCode} in the entity. - * @return true if the entity holds an errorcode, false otherwise - */ - public boolean hasErrorCode() { - return this.entity.getId() != -1; - } - - /** - * Gets the {@link ErrorCode} inside the entity. - * @return the error code or null - */ - public ErrorCode getErrorCode(){ - return ErrorCodeDeserializer.deserialize(this.entity.getId(), this.entity.getMessage()); - } -} diff --git a/io/src/main/java/org/gcube/resourcemanagement/manager/io/codeexceptions/deserializer/ExceptionDeserializer.java b/io/src/main/java/org/gcube/resourcemanagement/manager/io/codeexceptions/deserializer/ExceptionDeserializer.java deleted file mode 100644 index bed4f3b..0000000 --- a/io/src/main/java/org/gcube/resourcemanagement/manager/io/codeexceptions/deserializer/ExceptionDeserializer.java +++ /dev/null @@ -1,28 +0,0 @@ -package org.gcube.resourcemanagement.manager.io.codeexceptions.deserializer; - -import java.lang.reflect.InvocationTargetException; - -/** - * Deserializer for {@link Exception}. - * - * @author Manuele Simi (ISTI CNR) - * - */ -final class ExceptionDeserializer { - - /** - * - */ - private ExceptionDeserializer() {} - - - @SuppressWarnings("unchecked") - protected static E deserialize(String exceptionClass, String message) { - try { - final Class[] ctorParams = {String.class}; - return (E) Class.forName(exceptionClass).getConstructor(ctorParams).newInstance(message); - } catch (InstantiationException | IllegalAccessException | ClassNotFoundException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) { - return null; - } -} -} diff --git a/io/src/main/java/org/gcube/resourcemanagement/manager/io/codeexceptions/deserializer/package-info.java b/io/src/main/java/org/gcube/resourcemanagement/manager/io/codeexceptions/deserializer/package-info.java deleted file mode 100644 index 3151265..0000000 --- a/io/src/main/java/org/gcube/resourcemanagement/manager/io/codeexceptions/deserializer/package-info.java +++ /dev/null @@ -1,8 +0,0 @@ - -/** - * Helper classes to deserialize the information held inside a {@link org.gcube.resourcemanagement.manager.io.codeexceptions.SerializableErrorEntity}. - * - * @author Manuele Simi (ISTI CNR) - * - */ -package org.gcube.resourcemanagement.manager.io.codeexceptions.deserializer; \ No newline at end of file diff --git a/io/src/main/java/org/gcube/resourcemanagement/manager/io/codeexceptions/package-info.java b/io/src/main/java/org/gcube/resourcemanagement/manager/io/codeexceptions/package-info.java deleted file mode 100644 index a405f84..0000000 --- a/io/src/main/java/org/gcube/resourcemanagement/manager/io/codeexceptions/package-info.java +++ /dev/null @@ -1,8 +0,0 @@ -/** - * Code Exception is capable to hold an error code - * and an associated message, wrap them into a REST response and return them to the caller. - * - * @author Manuele Simi (ISTI - CNR) - * - */ -package org.gcube.resourcemanagement.manager.io.codeexceptions; \ No newline at end of file diff --git a/io/src/test/java/org/gcube/resourcemanagement/manager/io/codeexceptions/LocalCodeExceptionTest.java b/io/src/test/java/org/gcube/resourcemanagement/manager/io/codeexceptions/LocalCodeExceptionTest.java deleted file mode 100644 index ea7917c..0000000 --- a/io/src/test/java/org/gcube/resourcemanagement/manager/io/codeexceptions/LocalCodeExceptionTest.java +++ /dev/null @@ -1,29 +0,0 @@ -/** - * - */ -package org.gcube.resourcemanagement.manager.io.codeexceptions; - -import static org.junit.Assert.*; - -import org.gcube.resourcemanagement.manager.io.codeexceptions.LocalCodeException; -import org.gcube.resourcemanagement.manager.io.rs.RMCreateContextCode; -import org.junit.Test; - -/** - * Don't forget to comment! - * - * @author Manuele Simi (ISTI CNR) - * - */ -public class LocalCodeExceptionTest { - - /** - * Test method for {@link org.gcube.resourcemanagement.manager.io.codeexceptions.LocalCodeException#CodeException(org.gcube.resourcemanagement.manager.io.codeexceptions.ErrorCode)}. - */ - @Test - public void testCodeException() { - LocalCodeException exception = new LocalCodeException(RMCreateContextCode.GENERIC_ERROR_FROM_RR); - assertTrue("Unexpected id", exception.getId() == RMCreateContextCode.GENERIC_ERROR_FROM_RR.getId()); - } - -} diff --git a/io/src/test/java/org/gcube/resourcemanagement/manager/io/codeexceptions/WebCodeExceptionTest.java b/io/src/test/java/org/gcube/resourcemanagement/manager/io/codeexceptions/WebCodeExceptionTest.java deleted file mode 100644 index 69ace35..0000000 --- a/io/src/test/java/org/gcube/resourcemanagement/manager/io/codeexceptions/WebCodeExceptionTest.java +++ /dev/null @@ -1,35 +0,0 @@ -package org.gcube.resourcemanagement.manager.io.codeexceptions; - -import static org.junit.Assert.*; - -import java.util.Objects; - -import org.gcube.resourcemanagement.manager.io.codeexceptions.WebCodeException; -import org.gcube.resourcemanagement.manager.io.rs.RMCreateContextCode; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.BlockJUnit4ClassRunner; - -/** - * Tests for {@link WebCodeException}s - * - * @author Manuele Simi (ISTI CNR) - * - */ -@RunWith(BlockJUnit4ClassRunner.class) -public class WebCodeExceptionTest { - - /** - * Test method for - * {@link org.gcube.resourcemanagement.manager.io.codeexceptions.WebCodeException#CodeException(org.gcube.resourcemanagement.manager.io.rs.RMCreateContextCode)}. - */ - @Test - public void testWebCodeException() { - WebCodeException exception = new WebCodeException(RMCreateContextCode.CONTEXT_ALREADY_EXISTS); - // unfortunately, we cannot test the response readEntity method on an - // outbound response - assertTrue("Unexpected code", Objects.nonNull(exception.getResponse())); - - } - -}