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
This commit is contained in:
parent
de8582c9be
commit
3650407264
|
@ -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<SerializableErrorEntity> {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param entity
|
|
||||||
*/
|
|
||||||
protected CodeEntity(SerializableErrorEntity entity) {
|
|
||||||
super(entity);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -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 <E extends Enum<E> & 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);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -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();
|
|
||||||
|
|
||||||
}
|
|
|
@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -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 <E extends Exception> 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());
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -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;
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -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 extends Exception> 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());
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -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 extends Exception> 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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -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;
|
|
|
@ -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;
|
|
|
@ -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());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -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()));
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
Loading…
Reference in New Issue