Add JAX-RS MessageBodyWriter/Reader responsible for converting SerializableErrorEntity to/from a stream.
git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/Common/gxREST@178787 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
6eb3f608db
commit
ac04855b00
|
@ -9,7 +9,8 @@ import org.gcube.common.gxrest.response.outbound.ErrorCode;
|
|||
*
|
||||
*/
|
||||
public class SerializableErrorEntity {
|
||||
|
||||
|
||||
protected final static char ENTITY_CHAR_SEPARATOR = '@';
|
||||
private int id = -1;
|
||||
private String message;
|
||||
private String exceptionClass;
|
||||
|
@ -75,4 +76,29 @@ public class SerializableErrorEntity {
|
|||
public boolean hasStackTrace() {
|
||||
return !this.encodedTrace.isEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the message.
|
||||
* @param message
|
||||
*/
|
||||
protected void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the exception class.
|
||||
* @param exceptionClass
|
||||
*/
|
||||
protected void setExceptionClass(String exceptionClass) {
|
||||
this.exceptionClass = exceptionClass;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the enconded trace.
|
||||
* @param encodedTrace
|
||||
*/
|
||||
protected void setEncodedTrace(String encodedTrace) {
|
||||
this.encodedTrace = encodedTrace;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,121 @@
|
|||
package org.gcube.common.gxrest.response.entity;
|
||||
|
||||
import javax.ws.rs.BadRequestException;
|
||||
import javax.ws.rs.Consumes;
|
||||
import javax.ws.rs.WebApplicationException;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.MultivaluedMap;
|
||||
import javax.ws.rs.core.NoContentException;
|
||||
import javax.ws.rs.ext.ExceptionMapper;
|
||||
import javax.ws.rs.ext.MessageBodyReader;
|
||||
import javax.ws.rs.ext.Provider;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* Text reader for {@link SerializableErrorEntity}
|
||||
* @author Manuele Simi (ISTI-CNR)
|
||||
*/
|
||||
@Provider
|
||||
@Consumes(MediaType.TEXT_PLAIN)
|
||||
public class SerializableErrorEntityTextReader implements MessageBodyReader<SerializableErrorEntity> {
|
||||
|
||||
/**
|
||||
* Ascertain if the MessageBodyReader can produce an instance of a
|
||||
* particular type. The {@code type} parameter gives the
|
||||
* class of the instance that should be produced, the {@code genericType} parameter
|
||||
* gives the {@link Type java.lang.reflect.Type} of the instance
|
||||
* that should be produced.
|
||||
* E.g. if the instance to be produced is {@code List<String>}, the {@code type} parameter
|
||||
* will be {@code java.util.List} and the {@code genericType} parameter will be
|
||||
* {@link ParameterizedType java.lang.reflect.ParameterizedType}.
|
||||
*
|
||||
* @param type the class of instance to be produced.
|
||||
* @param genericType the type of instance to be produced. E.g. if the
|
||||
* message body is to be converted into a method parameter, this will be
|
||||
* the formal type of the method parameter as returned by
|
||||
* {@code Method.getGenericParameterTypes}.
|
||||
* @param annotations an array of the annotations on the declaration of the
|
||||
* artifact that will be initialized with the produced instance. E.g. if the
|
||||
* message body is to be converted into a method parameter, this will be
|
||||
* the annotations on that parameter returned by
|
||||
* {@code Method.getParameterAnnotations}.
|
||||
* @param mediaType the media type of the HTTP entity, if one is not
|
||||
* specified in the request then {@code application/octet-stream} is
|
||||
* used.
|
||||
* @return {@code true} if the type is supported, otherwise {@code false}.
|
||||
*/
|
||||
@Override
|
||||
public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
|
||||
return type == SerializableErrorEntity.class;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read a type from the {@link InputStream}.
|
||||
* <p>
|
||||
* In case the entity input stream is empty, the reader is expected to either return a
|
||||
* Java representation of a zero-length entity or throw a {@link NoContentException}
|
||||
* in case no zero-length entity representation is defined for the supported Java type.
|
||||
* A {@code NoContentException}, if thrown by a message body reader while reading a server
|
||||
* request entity, is automatically translated by JAX-RS server runtime into a {@link BadRequestException}
|
||||
* wrapping the original {@code NoContentException} and rethrown for a standard processing by
|
||||
* the registered {@link ExceptionMapper exception mappers}.
|
||||
* </p>
|
||||
*
|
||||
* @param type the type that is to be read from the entity stream.
|
||||
* @param genericType the type of instance to be produced. E.g. if the
|
||||
* message body is to be converted into a method parameter, this will be
|
||||
* the formal type of the method parameter as returned by
|
||||
* {@code Method.getGenericParameterTypes}.
|
||||
* @param annotations an array of the annotations on the declaration of the
|
||||
* artifact that will be initialized with the produced instance. E.g.
|
||||
* if the message body is to be converted into a method parameter, this
|
||||
* will be the annotations on that parameter returned by
|
||||
* {@code Method.getParameterAnnotations}.
|
||||
* @param mediaType the media type of the HTTP entity.
|
||||
* @param httpHeaders the read-only HTTP headers associated with HTTP entity.
|
||||
* @param entityStream the {@link InputStream} of the HTTP entity. The
|
||||
* caller is responsible for ensuring that the input stream ends when the
|
||||
* entity has been consumed. The implementation should not close the input
|
||||
* stream.
|
||||
* @return the type that was read from the stream. In case the entity input stream is empty, the reader
|
||||
* is expected to either return an instance representing a zero-length entity or throw
|
||||
* a {@link NoContentException} in case no zero-length entity representation is
|
||||
* defined for the supported Java type.
|
||||
* @throws IOException if an IO error arises. In case the entity input stream is empty
|
||||
* and the reader is not able to produce a Java representation for
|
||||
* a zero-length entity, {@code NoContentException} is expected to
|
||||
* be thrown.
|
||||
* @throws WebApplicationException if a specific HTTP error response needs to be produced.
|
||||
* Only effective if thrown prior to the response being committed.
|
||||
*/
|
||||
@Override
|
||||
public SerializableErrorEntity readFrom(Class<SerializableErrorEntity> type, Type genericType,
|
||||
Annotation[] annotations, MediaType mediaType, MultivaluedMap<String,
|
||||
String> httpHeaders, InputStream entityStream)
|
||||
throws IOException, WebApplicationException {
|
||||
String stringEntity = convertStreamToString(entityStream);
|
||||
String[] tokens = stringEntity.split(Character.toString(SerializableErrorEntity.ENTITY_CHAR_SEPARATOR));
|
||||
if (Objects.isNull(tokens) || tokens.length != 3)
|
||||
throw new IOException("Unable to decode SerializableErrorEntity from the response.");
|
||||
SerializableErrorEntity entity = new SerializableErrorEntity();
|
||||
entity.setExceptionClass(tokens[0]);
|
||||
entity.setEncodedTrace(tokens[1]);
|
||||
entity.setMessage(tokens[2]);
|
||||
return entity;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads and converts the content of the input stream.
|
||||
* @param is the input stream to read from
|
||||
* @return the string read from the stream
|
||||
*/
|
||||
private static String convertStreamToString(InputStream is) {
|
||||
java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
|
||||
return s.hasNext() ? s.next() : "";
|
||||
}
|
||||
}
|
|
@ -0,0 +1,102 @@
|
|||
package org.gcube.common.gxrest.response.entity;
|
||||
|
||||
import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.WebApplicationException;
|
||||
import javax.ws.rs.core.GenericEntity;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.MultivaluedMap;
|
||||
import javax.ws.rs.ext.MessageBodyWriter;
|
||||
import javax.ws.rs.ext.Provider;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.io.PrintWriter;
|
||||
import java.io.Writer;
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
/**
|
||||
* Text writer for {@link SerializableErrorEntity}
|
||||
* @author Lucio Lelii (ISTI-CNR)
|
||||
* @author Manuele Simi (ISTI-CNR)
|
||||
*/
|
||||
@Provider
|
||||
@Produces(MediaType.TEXT_PLAIN)
|
||||
public class SerializableErrorEntityTextWriter implements MessageBodyWriter<SerializableErrorEntity> {
|
||||
|
||||
/**
|
||||
* Ascertain if the MessageBodyWriter supports a particular type.
|
||||
*
|
||||
* @param type the class of instance that is to be written.
|
||||
* @param genericType the type of instance to be written, obtained either
|
||||
* by reflection of a resource method return type or via inspection
|
||||
* of the returned instance. {@link GenericEntity}
|
||||
* provides a way to specify this information at runtime.
|
||||
* @param annotations an array of the annotations attached to the message entity instance.
|
||||
* @param mediaType the media type of the HTTP entity.
|
||||
* @return {@code true} if the type is supported, otherwise {@code false}.
|
||||
*/
|
||||
@Override
|
||||
public boolean isWriteable(Class<?> type, Type genericType,
|
||||
Annotation[] annotations, MediaType mediaType) {
|
||||
return type == SerializableErrorEntity.class;
|
||||
}
|
||||
|
||||
/**
|
||||
* Originally, the method has been called before {@code writeTo} to ascertain the length in bytes of
|
||||
* the serialized form of {@code t}. A non-negative return value has been used in a HTTP
|
||||
* {@code Content-Length} header.
|
||||
* <p>
|
||||
* As of JAX-RS 2.0, the method has been deprecated and the value returned by the method is ignored
|
||||
* by a JAX-RS runtime. All {@code MessageBodyWriter} implementations are advised to return {@code -1}
|
||||
* from the method. Responsibility to compute the actual {@code Content-Length} header value has been
|
||||
* delegated to JAX-RS runtime.
|
||||
* </p>
|
||||
*
|
||||
* @param errorEntity the instance to write
|
||||
* @param type the class of instance that is to be written.
|
||||
* @param genericType the type of instance to be written. {@link GenericEntity}
|
||||
* provides a way to specify this information at runtime.
|
||||
* @param annotations an array of the annotations attached to the message entity instance.
|
||||
* @param mediaType the media type of the HTTP entity.
|
||||
* @return length in bytes or -1 if the length cannot be determined in advance.
|
||||
*/
|
||||
@Override
|
||||
public long getSize(SerializableErrorEntity errorEntity, Class<?> type, Type genericType,
|
||||
Annotation[] annotations, MediaType mediaType) {
|
||||
// deprecated by JAX-RS 2.0 and ignored by Jersey runtime
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write a type to an HTTP message. The message header map is mutable
|
||||
* but any changes must be made before writing to the output stream since
|
||||
* the headers will be flushed prior to writing the message body.
|
||||
*
|
||||
* @param errorEntity the instance to write.
|
||||
* @param type the class of instance that is to be written.
|
||||
* @param genericType the type of instance to be written. {@link GenericEntity}
|
||||
* provides a way to specify this information at runtime.
|
||||
* @param annotations an array of the annotations attached to the message entity instance.
|
||||
* @param mediaType the media type of the HTTP entity.
|
||||
* @param httpHeaders a mutable map of the HTTP message headers.
|
||||
* @param out the {@link OutputStream} for the HTTP entity. The
|
||||
* implementation should not close the output stream.
|
||||
* @throws IOException if an IO error arises.
|
||||
* @throws WebApplicationException if a specific HTTP error response needs to be produced.
|
||||
* Only effective if thrown prior to the message being committed.
|
||||
*/
|
||||
@Override
|
||||
public void writeTo(SerializableErrorEntity errorEntity, Class<?> type, Type genericType, Annotation[] annotations,
|
||||
MediaType mediaType, MultivaluedMap<String, Object> httpHeaders,
|
||||
OutputStream out) throws IOException, WebApplicationException {
|
||||
|
||||
Writer writer = new PrintWriter(out);
|
||||
writer.append(errorEntity.getExceptionClass());
|
||||
writer.append(SerializableErrorEntity.ENTITY_CHAR_SEPARATOR);
|
||||
writer.append(errorEntity.getEncodedTrace());
|
||||
writer.append(SerializableErrorEntity.ENTITY_CHAR_SEPARATOR);
|
||||
writer.append(errorEntity.getMessage());
|
||||
writer.flush();
|
||||
writer.close();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue