View Javadoc

1   /* This file is part of COPAL (COntext Provisioning for All).
2    *
3    * COPAL is a part of SM4All (Smart hoMes for All) project.
4    *
5    * COPAL is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU Lesser General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * COPAL is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU Lesser General Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser General Public License
16   * along with COPAL. If not, see <http://www.gnu.org/licenses/>.
17   */
18  package at.ac.tuwien.infosys.sm4all.copal.rest.provider;
19  
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.lang.annotation.Annotation;
23  import java.lang.reflect.Type;
24  import javax.ws.rs.Consumes;
25  import javax.ws.rs.WebApplicationException;
26  import javax.ws.rs.core.MediaType;
27  import javax.ws.rs.core.MultivaluedMap;
28  import javax.ws.rs.core.Response.Status;
29  import javax.ws.rs.ext.MessageBodyReader;
30  import javax.ws.rs.ext.Provider;
31  import javax.xml.parsers.DocumentBuilder;
32  import javax.xml.parsers.DocumentBuilderFactory;
33  import javax.xml.parsers.ParserConfigurationException;
34  import org.w3c.dom.Element;
35  import org.xml.sax.SAXException;
36  import at.ac.tuwien.infosys.sm4all.copal.api.event.xml.ParsingException;
37  import at.ac.tuwien.infosys.sm4all.copal.api.event.xml.publisher.Event;
38  import at.ac.tuwien.infosys.sm4all.copal.api.event.xml.publisher.SourceID;
39  import at.ac.tuwien.infosys.sm4all.copal.rest.model.RESTPublisher;
40  
41  /**
42   * A {@link MessageBodyReader} that transforms an <code>application/xml</code>
43   * {@link InputStream} into {@link RESTPublisher}.
44   * 
45   * @author sanjin
46   */
47  @Provider
48  @Consumes("application/xml")
49  public class XMLRESTPublisherReader implements MessageBodyReader<RESTPublisher> {
50  
51      private final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
52  
53      /**
54       * Creates an instance of <code>application/xml</code> {@link RESTPublisher}
55       * {@link MessageBodyReader}.
56       */
57      public XMLRESTPublisherReader() {
58          super();
59  
60          this.documentBuilderFactory.setNamespaceAware(true);
61  
62      }
63  
64      @Override
65      public boolean isReadable(final Class<?> type, final Type genericType,
66              final Annotation[] annotations, final MediaType mediaType) {
67          return RESTPublisher.class.isAssignableFrom(type);
68      }
69  
70      @Override
71      public RESTPublisher readFrom(final Class<RESTPublisher> type,
72              final Type genericType, final Annotation[] annotations,
73              final MediaType mediaType,
74              final MultivaluedMap<String, String> httpHeaders,
75              final InputStream entityStream) throws IOException,
76              WebApplicationException {
77          final RESTPublisher result;
78  
79          try {
80              final DocumentBuilder builder = this.documentBuilderFactory.newDocumentBuilder();
81              final Element element = builder.parse(entityStream).getDocumentElement();
82  
83              result = new RESTPublisher(new SourceID(element).unmarshal(),
84                      new Event(element).unmarshal());
85          } catch (final ParsingException ex) {
86              throw new WebApplicationException(ex, Status.BAD_REQUEST);
87          } catch (final ParserConfigurationException ex) {
88              throw new WebApplicationException(ex, Status.INTERNAL_SERVER_ERROR);
89          } catch (final SAXException ex) {
90              throw new WebApplicationException(ex, Status.BAD_REQUEST);
91          }
92  
93          return result;
94      }
95  }