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.databind.ObjectWriter;
23 import com.google.common.collect.ImmutableMap;
24 import eu.ehri.project.persistence.Bundle;
25 import graphql.ExecutionResult;
26
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.MessageBodyWriter;
32 import javax.ws.rs.ext.Provider;
33 import java.io.IOException;
34 import java.io.OutputStream;
35 import java.lang.annotation.Annotation;
36 import java.lang.reflect.Type;
37
38 @Provider
39 @Produces(MediaType.APPLICATION_JSON)
40 public class ExecutionResultProvider implements MessageBodyWriter<ExecutionResult>, JsonMessageBodyHandler {
41
42 private static final ObjectWriter writer = mapper.writerWithDefaultPrettyPrinter();
43
44 @Override
45 public boolean isWriteable(Class<?> aClass, Type type, Annotation[] annotations, MediaType mediaType) {
46 return ExecutionResult.class.isAssignableFrom(aClass);
47 }
48
49 @Override
50 public long getSize(ExecutionResult executionResult, Class<?> aClass, Type type, Annotation[] annotations,
51 MediaType mediaType) {
52 return -1L;
53 }
54
55 @Override
56 public void writeTo(ExecutionResult executionResult,
57 Class<?> aClass, Type type, Annotation[] annotations, MediaType mediaType,
58 MultivaluedMap<String, Object> headers, OutputStream outputStream)
59 throws IOException, WebApplicationException {
60 if (executionResult.getData() != null) {
61 writer.writeValue(outputStream, ImmutableMap.of(
62 Bundle.DATA_KEY, executionResult.getData()));
63 }
64 }
65 }