1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 }