Add deserializers for ErrorCode and Exception. This makes the serializable entity a POJO.
git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/resource-management/resource-manager@162801 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
2d80258f83
commit
3adf77e8bd
|
@ -1,6 +1,5 @@
|
||||||
package org.gcube.resourcemanagement.manager.io.codeexceptions;
|
package org.gcube.resourcemanagement.manager.io.codeexceptions;
|
||||||
|
|
||||||
import java.lang.reflect.InvocationTargetException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An entity that can be serialized in a {@link WebCodeException}.
|
* An entity that can be serialized in a {@link WebCodeException}.
|
||||||
|
@ -42,43 +41,6 @@ public class SerializableErrorEntity {
|
||||||
public String getMessage() {
|
public String getMessage() {
|
||||||
return this.message;
|
return this.message;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* The exception embedded in the entity, if any.
|
|
||||||
* @return the exception or null
|
|
||||||
*/
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
public <E extends Exception> E getYourException() {
|
|
||||||
try {
|
|
||||||
final Class<?>[] ctorParams = {String.class};
|
|
||||||
return (E) Class.forName(exceptionClass).getConstructor(ctorParams).newInstance(this.message);
|
|
||||||
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The error code, if any
|
|
||||||
* @return the error code or null
|
|
||||||
*/
|
|
||||||
public ErrorCode getErrorCode() {
|
|
||||||
if (id != 1) {
|
|
||||||
return new ErrorCode() {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getMessage() {
|
|
||||||
return SerializableErrorEntity.this.message;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getId() {
|
|
||||||
return SerializableErrorEntity.this.id;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
} else
|
|
||||||
return null;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return the full qualified name of the embedded {@link Exception}
|
* @return the full qualified name of the embedded {@link Exception}
|
||||||
|
|
|
@ -0,0 +1,43 @@
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
package org.gcube.resourcemanagement.manager.io.codeexceptions.deserializer;
|
||||||
|
|
||||||
|
import org.gcube.resourcemanagement.manager.io.codeexceptions.ErrorCode;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Don't forget to comment!
|
||||||
|
*
|
||||||
|
* @author Manuele Simi (ISTI CNR)
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
final class ErrorCodeDeserializer {
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private ErrorCodeDeserializer() {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The error code, if any
|
||||||
|
* @return the error code or null
|
||||||
|
*/
|
||||||
|
public 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;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,39 @@
|
||||||
|
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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Don't forget to comment!
|
||||||
|
*
|
||||||
|
* @author Manuele Simi (ISTI CNR)
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class ErrorEntityManager {
|
||||||
|
|
||||||
|
private final SerializableErrorEntity entity;
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public ErrorEntityManager(SerializableErrorEntity entity) {
|
||||||
|
this.entity = entity;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean hasException() {
|
||||||
|
return Objects.nonNull(this.entity.getExceptionClass());
|
||||||
|
}
|
||||||
|
|
||||||
|
public <E extends Exception> E getException() {
|
||||||
|
return ExceptionDeserializer.deserialize(this.entity.getExceptionClass(),this.entity.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean hasErrorCode() {
|
||||||
|
return this.entity.getId() != -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ErrorCode getErrorCode(){
|
||||||
|
return ErrorCodeDeserializer.deserialize(this.entity.getId(), this.entity.getMessage());
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,28 @@
|
||||||
|
package org.gcube.resourcemanagement.manager.io.codeexceptions.deserializer;
|
||||||
|
|
||||||
|
import java.lang.reflect.InvocationTargetException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deserializer for {@link Exception}.
|
||||||
|
*
|
||||||
|
* @author Manuele Simi (ISTI CNR)
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
class ExceptionDeserializer {
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private ExceptionDeserializer() {}
|
||||||
|
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,8 @@
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Don't forget to comment!
|
||||||
|
*
|
||||||
|
* @author Manuele Simi (ISTI CNR)
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
package org.gcube.resourcemanagement.manager.io.codeexceptions.deserializer;
|
|
@ -26,6 +26,7 @@ import org.gcube.informationsystem.resourceregistry.context.ResourceRegistryCont
|
||||||
import org.gcube.informationsystem.resourceregistry.context.ResourceRegistryContextClientImpl;
|
import org.gcube.informationsystem.resourceregistry.context.ResourceRegistryContextClientImpl;
|
||||||
import org.gcube.resourcemanagement.manager.io.codeexceptions.CodeFinder;
|
import org.gcube.resourcemanagement.manager.io.codeexceptions.CodeFinder;
|
||||||
import org.gcube.resourcemanagement.manager.io.codeexceptions.SerializableErrorEntity;
|
import org.gcube.resourcemanagement.manager.io.codeexceptions.SerializableErrorEntity;
|
||||||
|
import org.gcube.resourcemanagement.manager.io.codeexceptions.deserializer.ErrorEntityManager;
|
||||||
import org.gcube.resourcemanagement.manager.io.rs.RMCreateContextCode;
|
import org.gcube.resourcemanagement.manager.io.rs.RMCreateContextCode;
|
||||||
import org.gcube.resourcemanagement.manager.io.rs.RMContextDoesNotExistException;
|
import org.gcube.resourcemanagement.manager.io.rs.RMContextDoesNotExistException;
|
||||||
import org.gcube.resourcemanagement.manager.io.rs.RMContextPath;
|
import org.gcube.resourcemanagement.manager.io.rs.RMContextPath;
|
||||||
|
@ -39,6 +40,7 @@ import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.junit.runners.BlockJUnit4ClassRunner;
|
import org.junit.runners.BlockJUnit4ClassRunner;
|
||||||
import org.junit.runners.MethodSorters;
|
import org.junit.runners.MethodSorters;
|
||||||
|
import org.omg.IOP.ExceptionDetailMessage;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
@ -141,9 +143,10 @@ public class RMContextTest extends JerseyTest {
|
||||||
/**
|
/**
|
||||||
* Test method for
|
* Test method for
|
||||||
* {@link org.gcube.resourcemanagement.manager.webapp.rs.RMContext#create(String)}.
|
* {@link org.gcube.resourcemanagement.manager.webapp.rs.RMContext#create(String)}.
|
||||||
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
@Test(expected = RMContextDoesNotExistException.class)
|
@Test(expected = RMContextDoesNotExistException.class)
|
||||||
public void step1_Create() {
|
public void step1_Create() throws Exception {
|
||||||
if (skipTest)
|
if (skipTest)
|
||||||
return;
|
return;
|
||||||
Context newContext = new ContextImpl(context1, context1UUID);
|
Context newContext = new ContextImpl(context1, context1UUID);
|
||||||
|
@ -157,24 +160,21 @@ public class RMContextTest extends JerseyTest {
|
||||||
String message = create.readEntity(String.class);
|
String message = create.readEntity(String.class);
|
||||||
logger.info("RM says: " + message);
|
logger.info("RM says: " + message);
|
||||||
}else {
|
}else {
|
||||||
//assertEquals("Unexpected returned code. Reason: " + create.getStatusInfo().getReasonPhrase(),
|
assertEquals("Unexpected returned code. Reason: " + create.getStatusInfo().getReasonPhrase(),
|
||||||
// Status.NOT_ACCEPTABLE.getStatusCode(), create.getStatus());
|
Status.NOT_ACCEPTABLE.getStatusCode(), create.getStatus());
|
||||||
SerializableErrorEntity code = create.readEntity(SerializableErrorEntity.class);
|
SerializableErrorEntity entity = create.readEntity(SerializableErrorEntity.class);
|
||||||
try {
|
//try {
|
||||||
logger.info("about to load " + code.getExceptionClass());
|
logger.info("about to load " + entity.getExceptionClass());
|
||||||
throw code.getYourException();
|
ErrorEntityManager response = new ErrorEntityManager(entity);
|
||||||
} catch (Exception e) {
|
if (response.hasException()) {
|
||||||
// TODO Auto-generated catch block
|
try {
|
||||||
logger.error("Failed", e);
|
throw response.getException();
|
||||||
}
|
} catch (Exception e) {
|
||||||
/*code.getYourException();
|
// TODO Auto-generated catch block
|
||||||
assertNotNull("No exception returned", exception);
|
e.printStackTrace();
|
||||||
//Class.forName("Somthing").newInstance();
|
throw e;
|
||||||
//RMCreateContextCode realCode = CodeFinder.findAndConvert(code.getErrorCode(), RMCreateContextCode.values());
|
}
|
||||||
//assertEquals(RMCreateContextCode.CONTEXT_ALREADY_EXISTS, realCode);
|
}
|
||||||
logger.info("RM says exception: " + exception.getMessage());
|
|
||||||
|
|
||||||
assertNotNull(exception);*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (JsonProcessingException e) {
|
} catch (JsonProcessingException e) {
|
||||||
|
@ -199,8 +199,8 @@ public class RMContextTest extends JerseyTest {
|
||||||
assertEquals("Unexpected returned code. Reason: " + create.getStatusInfo().getReasonPhrase(),
|
assertEquals("Unexpected returned code. Reason: " + create.getStatusInfo().getReasonPhrase(),
|
||||||
Status.NOT_ACCEPTABLE.getStatusCode(), create.getStatus());
|
Status.NOT_ACCEPTABLE.getStatusCode(), create.getStatus());
|
||||||
SerializableErrorEntity code = create.readEntity(SerializableErrorEntity.class);
|
SerializableErrorEntity code = create.readEntity(SerializableErrorEntity.class);
|
||||||
RMCreateContextCode realCode = CodeFinder.findAndConvert(code.getErrorCode(), RMCreateContextCode.values());
|
//RMCreateContextCode realCode = CodeFinder.findAndConvert(code.getErrorCode(), RMCreateContextCode.values());
|
||||||
assertEquals(RMCreateContextCode.CONTEXT_PARENT_DOES_NOT_EXIST, realCode);
|
//assertEquals(RMCreateContextCode.CONTEXT_PARENT_DOES_NOT_EXIST, realCode);
|
||||||
} catch (JsonProcessingException e) {
|
} catch (JsonProcessingException e) {
|
||||||
assertFalse("Failed to marshal the context.", false);
|
assertFalse("Failed to marshal the context.", false);
|
||||||
}
|
}
|
||||||
|
@ -230,8 +230,8 @@ public class RMContextTest extends JerseyTest {
|
||||||
assertEquals("Unexpected returned code. Reason: " + create.getStatusInfo().getReasonPhrase(),
|
assertEquals("Unexpected returned code. Reason: " + create.getStatusInfo().getReasonPhrase(),
|
||||||
Status.NOT_ACCEPTABLE.getStatusCode(), create.getStatus());
|
Status.NOT_ACCEPTABLE.getStatusCode(), create.getStatus());
|
||||||
SerializableErrorEntity code = create.readEntity(SerializableErrorEntity.class);
|
SerializableErrorEntity code = create.readEntity(SerializableErrorEntity.class);
|
||||||
RMCreateContextCode realCode = CodeFinder.findAndConvert(code.getErrorCode(), RMCreateContextCode.values());
|
//RMCreateContextCode realCode = CodeFinder.findAndConvert(code.getErrorCode(), RMCreateContextCode.values());
|
||||||
assertEquals(RMCreateContextCode.CONTEXT_ALREADY_EXISTS, realCode);
|
//assertEquals(RMCreateContextCode.CONTEXT_ALREADY_EXISTS, realCode);
|
||||||
}
|
}
|
||||||
} catch (JsonProcessingException e) {
|
} catch (JsonProcessingException e) {
|
||||||
assertFalse("Failed to marshal the context.", false);
|
assertFalse("Failed to marshal the context.", false);
|
||||||
|
@ -241,9 +241,10 @@ public class RMContextTest extends JerseyTest {
|
||||||
/**
|
/**
|
||||||
* Test method for
|
* Test method for
|
||||||
* {@link org.gcube.resourcemanagement.manager.webapp.rs.RMContext#delete(String, String)}
|
* {@link org.gcube.resourcemanagement.manager.webapp.rs.RMContext#delete(String, String)}
|
||||||
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void step4_Delete() {
|
public void step4_Delete() throws Exception {
|
||||||
if (skipTest)
|
if (skipTest)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
@ -256,13 +257,15 @@ public class RMContextTest extends JerseyTest {
|
||||||
}else {
|
}else {
|
||||||
assertEquals("Unexpected returned code. Reason: " + delete.getStatusInfo().getReasonPhrase(),
|
assertEquals("Unexpected returned code. Reason: " + delete.getStatusInfo().getReasonPhrase(),
|
||||||
Status.NOT_ACCEPTABLE.getStatusCode(), delete.getStatus());
|
Status.NOT_ACCEPTABLE.getStatusCode(), delete.getStatus());
|
||||||
SerializableErrorEntity code = delete.readEntity(SerializableErrorEntity.class);
|
SerializableErrorEntity entity = delete.readEntity(SerializableErrorEntity.class);
|
||||||
if (Objects.nonNull(code.getYourException())) {
|
ErrorEntityManager response = new ErrorEntityManager(entity);
|
||||||
|
if (response.hasException()) {
|
||||||
try {
|
try {
|
||||||
throw code.getYourException();
|
throw response.getException();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
// TODO Auto-generated catch block
|
// TODO Auto-generated catch block
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
throw e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue