Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.yammer.dropwizard.jersey;
  
  
 
 // TODO: 10/12/11 <coda> -- write tests for JacksonMessageBodyProvider
 // TODO: 10/12/11 <coda> -- write docs for JacksonMessageBodyProvider
 
 @Produces("application/json")
 @Consumes("application/json")
 public class JacksonMessageBodyProvider implements MessageBodyReader<Object>,
                                                    MessageBodyWriter<Object> {
     private static final Log LOG = Log.forClass(JacksonMessageBodyProvider.class);
     private static final Validator VALIDATOR = new Validator();
     private static final Response.StatusType UNPROCESSABLE_ENTITY = new Response.StatusType() {
         @Override
         public int getStatusCode() {
             return 422;
         }
 
         @Override
         public Response.Status.Family getFamily() {
             return ...;
         }
 
         @Override
         public String getReasonPhrase() {
             return "Unprocessable Entity";
         }
     };
 
     private final Json json;
 
     public JacksonMessageBodyProvider(Json json) {
         this. = json;
     }
 
     @Override
     public boolean isReadable(Class<?> type,
                               Type genericType,
                               Annotation[] annotations,
                               MediaType mediaType) {
         return .canDeserialize(type);
     }
 
     @Override
     public Object readFrom(Class<Objecttype,
                            Type genericType,
                            Annotation[] annotations,
                            MediaType mediaType,
                            MultivaluedMap<StringStringhttpHeaders,
                            InputStream entityStreamthrows IOExceptionWebApplicationException {
         boolean validating = false;
         for (Annotation annotation : annotations) {
             validating = validating || (annotation.annotationType() == Valid.class);
         }
 
         final Object value = .readValue(entityStreamgenericType);
         if (validating) {
             final ImmutableList<Stringerrors = .validate(value);
             if (!errors.isEmpty()) {
                 final StringBuilder msg = new StringBuilder("The request entity had the following errors:\n");
                 for (String error : errors) {
                     msg.append("  * ").append(error).append('\n');
                 }
                 throw new WebApplicationException(Response.status()
                                                           .entity(msg.toString())
                                                           .type(.)
                                                           .build());
             }
         }
         return value;
     }
 
     @Override
     public boolean isWriteable(Class<?> type,
                                Type genericType,
                                Annotation[] annotations,
                               MediaType mediaType) {
        return .canSerialize(type);
    }
    @Override
    public long getSize(Object t,
                        Class<?> type,
                        Type genericType,
                        Annotation[] annotations,
                        MediaType mediaType) {
        return -1;
    }
    @Override
    public void writeTo(Object t,
                        Class<?> type,
                        Type genericType,
                        Annotation[] annotations,
                        MediaType mediaType,
                        MultivaluedMap<StringObjecthttpHeaders,
                        OutputStream entityStreamthrows IOExceptionWebApplicationException {
        try {
            .writeValue(entityStreamt);
        } catch (EofException ignored) {
            // we don't care about these
        } catch (IOException e) {
            .error(e"Error writing response");
        }
    }
New to GrepCode? Check out our FAQ X