View Javadoc

1   package com.atlassian.plugins.rest.common.error.jersey;
2   
3   import java.io.IOException;
4   import java.io.OutputStream;
5   import java.lang.annotation.Annotation;
6   import java.lang.reflect.Type;
7   
8   import javax.ws.rs.Produces;
9   import javax.ws.rs.WebApplicationException;
10  import javax.ws.rs.core.MediaType;
11  import javax.ws.rs.core.MultivaluedMap;
12  import javax.ws.rs.ext.MessageBodyWriter;
13  import javax.ws.rs.ext.Provider;
14  
15  /**
16   * Writes {@link UncaughtExceptionEntity}s out as plain text.
17   */
18  @Provider
19  @Produces(MediaType.TEXT_PLAIN)
20  public class UncaughtExceptionEntityWriter implements MessageBodyWriter<UncaughtExceptionEntity>
21  {
22      public long getSize(UncaughtExceptionEntity t, Class<?> type, Type genericType, Annotation[] annotations,
23              MediaType mediaType)
24      {
25          return -1;
26      }
27  
28      public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType)
29      {
30          return UncaughtExceptionEntity.class.isAssignableFrom(type);
31      }
32  
33      public void writeTo(UncaughtExceptionEntity t, Class<?> type, Type genericType, Annotation[] annotations,
34              MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream)
35              throws IOException, WebApplicationException
36      {
37          entityStream.write(t.getStackTrace().getBytes("utf-8"));
38      }
39  }