update verigraph
[parser.git] / verigraph / src / it / polito / verigraph / deserializer / PathsMessageBodyReader.java
1 /*******************************************************************************
2  * Copyright (c) 2017 Politecnico di Torino and others.
3  *
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Apache License, Version 2.0
6  * which accompanies this distribution, and is available at
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *******************************************************************************/
9 package it.polito.verigraph.deserializer;
10
11 import java.io.IOException;
12 import java.io.InputStream;
13 import java.lang.annotation.Annotation;
14 import java.lang.reflect.Type;
15 import javax.ws.rs.Consumes;
16 import javax.ws.rs.ProcessingException;
17 import javax.ws.rs.WebApplicationException;
18 import javax.ws.rs.core.MediaType;
19 import javax.ws.rs.core.MultivaluedMap;
20 import javax.ws.rs.ext.MessageBodyReader;
21 import javax.ws.rs.ext.Provider;
22 import javax.xml.bind.JAXBContext;
23 import javax.xml.bind.JAXBException;
24
25 import it.polito.neo4j.jaxb.Paths;
26
27 @Provider
28 @Consumes(MediaType.APPLICATION_XML)
29 public class PathsMessageBodyReader implements MessageBodyReader<Paths>{
30
31     @Override
32     public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
33         return type == Paths.class;
34     }
35
36     @Override
37     public Paths readFrom(Class<Paths> type, Type genericType, Annotation[] annotations, MediaType mediaType,
38             MultivaluedMap<String, String> httpHeaders, InputStream entityStream)
39                     throws IOException, WebApplicationException {
40         try {
41             JAXBContext jaxbContext = JAXBContext.newInstance(Paths.class);
42             Paths paths = (Paths) jaxbContext.createUnmarshaller()
43                     .unmarshal(entityStream);
44             return paths;
45         } catch (JAXBException jaxbException) {
46             throw new ProcessingException("Error deserializing a Paths object.",
47                     jaxbException);
48         }
49     }
50
51 }