View Javadoc

1   /*
2    * Copyright 2015 Data Archiving and Networked Services (an institute of
3    * Koninklijke Nederlandse Akademie van Wetenschappen), King's College London,
4    * Georg-August-Universitaet Goettingen Stiftung Oeffentlichen Rechts
5    *
6    * Licensed under the EUPL, Version 1.1 or – as soon they will be approved by
7    * the European Commission - subsequent versions of the EUPL (the "Licence");
8    * You may not use this work except in compliance with the Licence.
9    * You may obtain a copy of the Licence at:
10   *
11   * https://joinup.ec.europa.eu/software/page/eupl
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the Licence is distributed on an "AS IS" basis,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the Licence for the specific language governing
17   * permissions and limitations under the Licence.
18   */
19  
20  package eu.ehri.extension.providers;
21  
22  import com.fasterxml.jackson.core.JsonProcessingException;
23  import eu.ehri.project.graphql.GraphQLQuery;
24  import eu.ehri.extension.errors.InvalidJsonError;
25  
26  import javax.ws.rs.Consumes;
27  import javax.ws.rs.Produces;
28  import javax.ws.rs.WebApplicationException;
29  import javax.ws.rs.core.MediaType;
30  import javax.ws.rs.core.MultivaluedMap;
31  import javax.ws.rs.ext.MessageBodyReader;
32  import javax.ws.rs.ext.MessageBodyWriter;
33  import javax.ws.rs.ext.Provider;
34  import java.io.IOException;
35  import java.io.InputStream;
36  import java.io.OutputStream;
37  import java.lang.annotation.Annotation;
38  import java.lang.reflect.Type;
39  
40  @Provider
41  @Consumes(MediaType.APPLICATION_JSON)
42  @Produces(MediaType.APPLICATION_JSON)
43  public class GraphQLQueryProvider implements MessageBodyReader<GraphQLQuery>,
44          MessageBodyWriter<GraphQLQuery>, JsonMessageBodyHandler {
45  
46      @Override
47      public boolean isReadable(Class<?> aClass, Type type, Annotation[] annotations, MediaType mediaType) {
48          return GraphQLQuery.class.isAssignableFrom(aClass);
49      }
50  
51      @Override
52      public GraphQLQuery readFrom(Class<GraphQLQuery> aClass, Type type, Annotation[] annotations,
53              MediaType mediaType, MultivaluedMap<String, String> multivaluedMap,
54              InputStream inputStream) throws IOException, WebApplicationException {
55          try {
56              return mapper.readValue(inputStream, GraphQLQuery.class);
57          } catch (JsonProcessingException e) {
58              throw new InvalidJsonError(e);
59          }
60      }
61  
62      @Override
63      public boolean isWriteable(Class<?> aClass, Type type, Annotation[] annotations, MediaType mediaType) {
64          return GraphQLQuery.class.isAssignableFrom(aClass);
65      }
66  
67      @Override
68      public long getSize(GraphQLQuery graphQLQuery, Class<?> aClass, Type type, Annotation[] annotations, MediaType mediaType) {
69          return -1;
70      }
71  
72      @Override
73      public void writeTo(GraphQLQuery graphQLQuery, Class<?> aClass, Type type, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> multivaluedMap, OutputStream outputStream) throws IOException, WebApplicationException {
74          mapper.writeValue(outputStream, graphQLQuery);
75      }
76  }