View Javadoc
1   package ejava.examples.ejbwar.inventory.rs;
2   
3   import java.io.IOException;
4   
5   import java.io.OutputStream;
6   import java.lang.annotation.Annotation;
7   import java.lang.reflect.Type;
8   
9   import javax.ws.rs.Produces;
10  import javax.ws.rs.WebApplicationException;
11  import javax.ws.rs.core.Context;
12  import javax.ws.rs.core.MediaType;
13  import javax.ws.rs.core.MultivaluedMap;
14  import javax.ws.rs.ext.ContextResolver;
15  import javax.ws.rs.ext.MessageBodyWriter;
16  import javax.ws.rs.ext.Provider;
17  import javax.ws.rs.ext.Providers;
18  import javax.xml.bind.JAXBContext;
19  import javax.xml.bind.JAXBException;
20  import javax.xml.bind.Marshaller;
21  import javax.xml.bind.annotation.XmlRootElement;
22  
23  /**
24   * Put this class in your JAX-RS application to have XML output formatted
25   * so that it is easier to read.
26   */
27  @Provider
28  @Produces(MediaType.APPLICATION_XML)
29  public class PrettyPrinter implements MessageBodyWriter<Object> {
30  
31      @Context protected Providers providers;
32  
33      public long getSize(Object object, Class<?> type, Type genericType,
34              Annotation[] methodAnnotations, MediaType mediaType) {
35          return -1; //says we don't know
36      }
37  
38      /**
39       * Return true for any JAXB class
40       */
41      public boolean isWriteable(Class<?> type, Type genericType, 
42              Annotation[] methodAnnotations, MediaType mediaType) {
43          for(Class<?> clazz = type;clazz != null; clazz=clazz.getSuperclass()) {
44              if (clazz.isAnnotationPresent(XmlRootElement.class)) {
45                  return true;
46              }
47          }
48          return false;
49      }
50  
51      /**
52       * Write the provided JAXB object using formatted output.
53       */
54      public void writeTo(Object object, Class<?> type, Type genericType,
55              Annotation[] methodAnnotations, MediaType mediaType,
56              MultivaluedMap<String, Object> httpHeaders, OutputStream os)
57              throws IOException, WebApplicationException {
58  
59      	//get a JAXBContext that can marshall the JAXBObject
60          JAXBContext ctx = null;
61          ContextResolver<JAXBContext> resolver = 
62              providers.getContextResolver(JAXBContext.class, MediaType.APPLICATION_XML_TYPE);
63          if (resolver != null) {
64              ctx = resolver.getContext(type);
65          }
66          
67          //if none is found, try a default one
68          if (ctx == null) {
69              try { ctx = JAXBContext.newInstance(type); } 
70              catch (JAXBException ex) {throw new RuntimeException("error creating ad-hoc jaxbContext", ex);}
71          }
72  
73          //marshall the object to the provided stream
74          try {
75              Marshaller marshaller = ctx.createMarshaller();
76              marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
77              marshaller.marshal(object, os);
78              os.flush();
79          } catch (JAXBException ex) {
80              throw new RuntimeException("error marshalling XML", ex);
81          } finally {}
82      }
83  }