Added exception marshalling/umarshalling via json using jackson
git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/resource-registry-api@144208 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
faf7af0177
commit
9f71894ca8
|
@ -0,0 +1,5 @@
|
|||
package org.gcube.informationsystem.resourceregistry.api.exceptions;
|
||||
|
||||
public interface AlreadyPresent {
|
||||
|
||||
}
|
|
@ -0,0 +1,147 @@
|
|||
package org.gcube.informationsystem.resourceregistry.api.exceptions;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.Reader;
|
||||
import java.io.Writer;
|
||||
import java.util.List;
|
||||
|
||||
import org.gcube.informationsystem.impl.utils.discovery.ReflectionUtility;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonGenerationException;
|
||||
import com.fasterxml.jackson.core.JsonParseException;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.JsonMappingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
/**
|
||||
* @author Luca Frosini (ISTI - CNR)
|
||||
*/
|
||||
public abstract class ExceptionMapper {
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger(ExceptionMapper.class);
|
||||
|
||||
protected static final ObjectMapper mapper;
|
||||
|
||||
/**
|
||||
* @return the ObjectMapper
|
||||
*/
|
||||
public static ObjectMapper getObjectMapper() {
|
||||
return mapper;
|
||||
}
|
||||
|
||||
static {
|
||||
|
||||
mapper = new ObjectMapper();
|
||||
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||
|
||||
Package p = ResourceRegistryException.class.getPackage();
|
||||
try {
|
||||
List<Class<?>> classes = ReflectionUtility.getClassesForPackage(p);
|
||||
for (Class<?> clz : classes) {
|
||||
logger.trace("Analyzing {}", clz);
|
||||
|
||||
if (ResourceRegistryException.class.isAssignableFrom(clz)) {
|
||||
mapper.registerSubtypes(clz);
|
||||
}
|
||||
|
||||
}
|
||||
} catch (ClassNotFoundException e) {
|
||||
logger.error("Error discovering classes inside package {}",
|
||||
p.getName(), e);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Write the serialization of a given resource to a given
|
||||
* {@link OutputStream} .
|
||||
*
|
||||
* @param resource the resource
|
||||
* @param stream the stream in input
|
||||
* @throws IOException
|
||||
* @throws JsonMappingException
|
||||
* @throws JsonGenerationException
|
||||
*/
|
||||
public static <T extends OutputStream, RRE extends ResourceRegistryException> T marshal(RRE object, T stream)
|
||||
throws JsonGenerationException, JsonMappingException, IOException {
|
||||
mapper.writeValue(stream, object);
|
||||
return stream;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write the serialization of a given resource to a given {@link Writer} .
|
||||
* @param resource the resource
|
||||
* @param writer the writer in input
|
||||
* @throws IOException
|
||||
* @throws JsonMappingException
|
||||
* @throws JsonGenerationException
|
||||
*/
|
||||
public static <T extends Writer, RRE extends ResourceRegistryException> T marshal(RRE object, T writer)
|
||||
throws JsonGenerationException, JsonMappingException, IOException {
|
||||
mapper.writeValue(writer, object);
|
||||
return writer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the String serialization of a given resource
|
||||
* @param object the resource
|
||||
* @return the String serialization of a given resource
|
||||
* @throws JsonProcessingException
|
||||
*/
|
||||
public static <RRE extends ResourceRegistryException> String marshal(RRE object) throws JsonProcessingException {
|
||||
return mapper.writeValueAsString(object);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a resource of given class from its serialization in a given
|
||||
* {@link Reader}.
|
||||
* @param clz the class of the resource
|
||||
* @param reader the reader
|
||||
* @return the resource
|
||||
* @throws JsonParseException
|
||||
* @throws JsonMappingException
|
||||
* @throws IOException
|
||||
*/
|
||||
public static <RRE extends ResourceRegistryException> RRE unmarshal(Class<RRE> clz, Reader reader)
|
||||
throws JsonParseException, JsonMappingException, IOException {
|
||||
return mapper.readValue(reader, clz);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a resource of given class from its serialization in a given
|
||||
* {@link InputStream}.
|
||||
* @param clz the class of the resource
|
||||
* @param stream the stream
|
||||
* @return the resource
|
||||
* @throws IOException
|
||||
* @throws JsonMappingException
|
||||
* @throws JsonParseException
|
||||
*/
|
||||
public static <RRE extends ResourceRegistryException> RRE unmarshal(Class<RRE> clz, InputStream stream)
|
||||
throws JsonParseException, JsonMappingException, IOException {
|
||||
return mapper.readValue(stream, clz);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a resource of given class from its serialization in a given String
|
||||
* @param clz the class of the resource
|
||||
* @param string
|
||||
* @return the resource
|
||||
* @throws JsonParseException
|
||||
* @throws JsonMappingException
|
||||
* @throws IOException
|
||||
*/
|
||||
public static <RRE extends ResourceRegistryException> RRE unmarshal(Class<RRE> clz, String string)
|
||||
throws JsonParseException, JsonMappingException, IOException {
|
||||
return mapper.readValue(string, clz);
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
package org.gcube.informationsystem.resourceregistry.api.exceptions;
|
||||
|
||||
public interface NotFound {
|
||||
|
||||
}
|
|
@ -3,10 +3,14 @@
|
|||
*/
|
||||
package org.gcube.informationsystem.resourceregistry.api.exceptions;
|
||||
|
||||
import org.gcube.informationsystem.model.ISManageable;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonTypeInfo;
|
||||
|
||||
/**
|
||||
* @author Luca Frosini (ISTI - CNR)
|
||||
*
|
||||
*/
|
||||
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = ISManageable.CLASS_PROPERTY)
|
||||
public class ResourceRegistryException extends Exception {
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,11 +1,12 @@
|
|||
package org.gcube.informationsystem.resourceregistry.api.exceptions.context;
|
||||
|
||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFound;
|
||||
|
||||
/**
|
||||
* @author Luca Frosini (ISTI - CNR)
|
||||
*
|
||||
*/
|
||||
public class ContextNotFoundException extends ContextException {
|
||||
public class ContextNotFoundException extends ContextException implements NotFound {
|
||||
|
||||
/**
|
||||
* Generated Serial Version UID
|
||||
|
|
|
@ -1,12 +1,13 @@
|
|||
package org.gcube.informationsystem.resourceregistry.api.exceptions.entity;
|
||||
|
||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.AlreadyPresent;
|
||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.er.ERAlreadyPresentException;
|
||||
|
||||
/**
|
||||
* @author Luca Frosini (ISTI - CNR)
|
||||
*
|
||||
*/
|
||||
public class EntityAlreadyPresentException extends ERAlreadyPresentException {
|
||||
public class EntityAlreadyPresentException extends ERAlreadyPresentException implements AlreadyPresent {
|
||||
|
||||
/**
|
||||
* Generated Serial Version UID
|
||||
|
|
|
@ -1,12 +1,13 @@
|
|||
package org.gcube.informationsystem.resourceregistry.api.exceptions.entity;
|
||||
|
||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFound;
|
||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.er.ERNotFoundException;
|
||||
|
||||
/**
|
||||
* @author Luca Frosini (ISTI - CNR)
|
||||
*
|
||||
*/
|
||||
public class EntityNotFoundException extends ERNotFoundException {
|
||||
public class EntityNotFoundException extends ERNotFoundException implements NotFound {
|
||||
|
||||
/**
|
||||
* Generated Serial Version UID
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package org.gcube.informationsystem.resourceregistry.api.exceptions.entity.facet;
|
||||
|
||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.AlreadyPresent;
|
||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.EntityAlreadyPresentException;
|
||||
|
||||
|
||||
|
@ -7,7 +8,7 @@ import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.Entity
|
|||
* @author Luca Frosini (ISTI - CNR)
|
||||
*
|
||||
*/
|
||||
public class FacetAlreadyPresentException extends EntityAlreadyPresentException {
|
||||
public class FacetAlreadyPresentException extends EntityAlreadyPresentException implements AlreadyPresent {
|
||||
|
||||
/**
|
||||
* Generated Serial Version UID
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package org.gcube.informationsystem.resourceregistry.api.exceptions.entity.facet;
|
||||
|
||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFound;
|
||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.EntityNotFoundException;
|
||||
|
||||
|
||||
|
@ -7,7 +8,7 @@ import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.Entity
|
|||
* @author Luca Frosini (ISTI - CNR)
|
||||
*
|
||||
*/
|
||||
public class FacetNotFoundException extends EntityNotFoundException {
|
||||
public class FacetNotFoundException extends EntityNotFoundException implements NotFound {
|
||||
|
||||
/**
|
||||
* Generated Serial Version UID
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resource;
|
||||
|
||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.AlreadyPresent;
|
||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.EntityAlreadyPresentException;
|
||||
|
||||
|
||||
|
@ -7,7 +8,7 @@ import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.Entity
|
|||
* @author Luca Frosini (ISTI - CNR)
|
||||
*
|
||||
*/
|
||||
public class ResourceAlreadyPresentException extends EntityAlreadyPresentException {
|
||||
public class ResourceAlreadyPresentException extends EntityAlreadyPresentException implements AlreadyPresent {
|
||||
|
||||
/**
|
||||
* Generated Serial Version UID
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resource;
|
||||
|
||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFound;
|
||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.EntityNotFoundException;
|
||||
|
||||
|
||||
|
@ -7,7 +8,7 @@ import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.Entity
|
|||
* @author Luca Frosini (ISTI - CNR)
|
||||
*
|
||||
*/
|
||||
public class ResourceNotFoundException extends EntityNotFoundException {
|
||||
public class ResourceNotFoundException extends EntityNotFoundException implements NotFound {
|
||||
|
||||
/**
|
||||
* Generated Serial Version UID
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
package org.gcube.informationsystem.resourceregistry.api.exceptions.er;
|
||||
|
||||
|
||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.AlreadyPresent;
|
||||
|
||||
/**
|
||||
* @author Luca Frosini (ISTI - CNR)
|
||||
*
|
||||
*/
|
||||
public class ERAlreadyPresentException extends ERException {
|
||||
public class ERAlreadyPresentException extends ERException implements AlreadyPresent {
|
||||
|
||||
/**
|
||||
* Generated Serial Version UID
|
||||
|
|
|
@ -1,11 +1,12 @@
|
|||
package org.gcube.informationsystem.resourceregistry.api.exceptions.er;
|
||||
|
||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFound;
|
||||
|
||||
/**
|
||||
* @author Luca Frosini (ISTI - CNR)
|
||||
*
|
||||
*/
|
||||
public class ERNotFoundException extends ERException {
|
||||
public class ERNotFoundException extends ERException implements NotFound {
|
||||
|
||||
/**
|
||||
* Generated Serial Version UID
|
||||
|
|
|
@ -5,7 +5,6 @@ import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegis
|
|||
|
||||
/**
|
||||
* @author Luca Frosini (ISTI - CNR)
|
||||
*
|
||||
*/
|
||||
public class InvalidQueryException extends ResourceRegistryException {
|
||||
|
||||
|
|
|
@ -1,12 +1,13 @@
|
|||
package org.gcube.informationsystem.resourceregistry.api.exceptions.relation;
|
||||
|
||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.AlreadyPresent;
|
||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.er.ERAlreadyPresentException;
|
||||
|
||||
/**
|
||||
* @author Luca Frosini (ISTI - CNR)
|
||||
*
|
||||
*/
|
||||
public class RelationAlreadyPresentException extends ERAlreadyPresentException {
|
||||
public class RelationAlreadyPresentException extends ERAlreadyPresentException implements AlreadyPresent {
|
||||
|
||||
/**
|
||||
* Generated Serial Version UID
|
||||
|
|
|
@ -1,12 +1,13 @@
|
|||
package org.gcube.informationsystem.resourceregistry.api.exceptions.relation;
|
||||
|
||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFound;
|
||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.er.ERNotFoundException;
|
||||
|
||||
/**
|
||||
* @author Luca Frosini (ISTI - CNR)
|
||||
*
|
||||
*/
|
||||
public class RelationNotFoundException extends ERNotFoundException {
|
||||
public class RelationNotFoundException extends ERNotFoundException implements NotFound {
|
||||
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,10 +1,12 @@
|
|||
package org.gcube.informationsystem.resourceregistry.api.exceptions.schema;
|
||||
|
||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFound;
|
||||
|
||||
/**
|
||||
* @author Luca Frosini (ISTI - CNR)
|
||||
*
|
||||
*/
|
||||
public class SchemaNotFoundException extends SchemaException {
|
||||
public class SchemaNotFoundException extends SchemaException implements NotFound {
|
||||
|
||||
/**
|
||||
* Generated Serial Version UID
|
||||
|
|
|
@ -0,0 +1,49 @@
|
|||
package org.gcube.informationsystem.resourceregistry.api.exceptions;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.util.List;
|
||||
|
||||
import org.gcube.informationsystem.impl.utils.discovery.ReflectionUtility;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class ExceptionSerialization {
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger(ExceptionSerialization.class);
|
||||
|
||||
public static final String MESSAGE = "Error Message to test ";
|
||||
|
||||
@Test
|
||||
public void testAll() throws Exception{
|
||||
Package p = ResourceRegistryException.class.getPackage();
|
||||
try {
|
||||
List<Class<?>> classes = ReflectionUtility.getClassesForPackage(p);
|
||||
for (Class<?> clz : classes) {
|
||||
|
||||
if (ResourceRegistryException.class.isAssignableFrom(clz)) {
|
||||
logger.debug("Testing {}", clz);
|
||||
|
||||
Constructor<?> constructor = clz.getConstructor(String.class);
|
||||
ResourceRegistryException rre = (ResourceRegistryException) constructor.newInstance(MESSAGE + clz.getSimpleName());
|
||||
|
||||
String jsonString = ExceptionMapper.marshal(rre);
|
||||
|
||||
ResourceRegistryException rreUnmashalled = ExceptionMapper.unmarshal(ResourceRegistryException.class, jsonString);
|
||||
|
||||
Assert.assertTrue(rre.getClass() == rreUnmashalled.getClass());
|
||||
Assert.assertTrue(rre.getMessage().compareTo(rreUnmashalled.getMessage())==0);
|
||||
|
||||
logger.debug("{} successfully tested", clz);
|
||||
}
|
||||
|
||||
}
|
||||
} catch (ClassNotFoundException e) {
|
||||
logger.error("Error discovering classes inside package {}",
|
||||
p.getName(), e);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE xml>
|
||||
<configuration>
|
||||
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{0}: %msg%n</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
|
||||
<logger name="org.gcube" level="INFO" />
|
||||
<logger name="org.gcube.informationsystem" level="DEBUG" />
|
||||
|
||||
<root level="WARN">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
|
||||
</configuration>
|
Loading…
Reference in New Issue