Add verigraph code base
[parser.git] / verigraph / src / main / java / it / polito / escape / verify / 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
10 package it.polito.escape.verify.deserializer;
11
12 import java.io.IOException;
13 import java.io.InputStream;
14 import java.lang.annotation.Annotation;
15 import java.lang.reflect.Type;
16 import javax.ws.rs.Consumes;
17 import javax.ws.rs.ProcessingException;
18 import javax.ws.rs.WebApplicationException;
19 import javax.ws.rs.core.MediaType;
20 import javax.ws.rs.core.MultivaluedMap;
21 import javax.ws.rs.ext.MessageBodyReader;
22 import javax.ws.rs.ext.Provider;
23 import javax.xml.bind.JAXBContext;
24 import javax.xml.bind.JAXBException;
25
26 import it.polito.nffg.neo4j.jaxb.Paths;
27
28 @Provider
29 @Consumes(MediaType.APPLICATION_XML)
30 public class PathsMessageBodyReader implements MessageBodyReader<Paths>{
31
32         @Override
33         public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
34                 return type == Paths.class;
35         }
36
37         @Override
38         public Paths readFrom(Class<Paths> type, Type genericType, Annotation[] annotations, MediaType mediaType,
39                         MultivaluedMap<String, String> httpHeaders, InputStream entityStream)
40                                         throws IOException, WebApplicationException {
41                 try {
42                 JAXBContext jaxbContext = JAXBContext.newInstance(Paths.class);
43                 Paths paths = (Paths) jaxbContext.createUnmarshaller()
44                     .unmarshal(entityStream);
45                 return paths;
46             } catch (JAXBException jaxbException) {
47                 throw new ProcessingException("Error deserializing a Paths object.",
48                     jaxbException);
49             }
50         }
51
52 }