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 eu.ehri.project.importers.ead.SyncLog;
23
24 import javax.ws.rs.Consumes;
25 import javax.ws.rs.Produces;
26 import javax.ws.rs.WebApplicationException;
27 import javax.ws.rs.core.MediaType;
28 import javax.ws.rs.core.MultivaluedMap;
29 import javax.ws.rs.ext.MessageBodyReader;
30 import javax.ws.rs.ext.MessageBodyWriter;
31 import javax.ws.rs.ext.Provider;
32 import java.io.IOException;
33 import java.io.InputStream;
34 import java.io.OutputStream;
35 import java.io.PrintStream;
36 import java.lang.annotation.Annotation;
37 import java.lang.reflect.Type;
38
39 @Provider
40 @Consumes(MediaType.APPLICATION_JSON)
41 @Produces({MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN})
42 public class SyncLogProvider implements MessageBodyWriter<SyncLog>,
43 MessageBodyReader<SyncLog>, JsonMessageBodyHandler {
44 @Override
45 public boolean isWriteable(Class<?> aClass, Type type, Annotation[] annotations, MediaType mediaType) {
46 return aClass == SyncLog.class;
47 }
48
49 @Override
50 public long getSize(SyncLog syncLog, Class<?> aClass, Type type, Annotation[] annotations, MediaType mediaType) {
51 return -1L;
52 }
53
54 @Override
55 public void writeTo(SyncLog syncLog, Class<?> aClass, Type type,
56 Annotation[] annotations, MediaType mediaType,
57 MultivaluedMap<String, Object> multivaluedMap, OutputStream outputStream)
58 throws IOException, WebApplicationException {
59 if (mediaType.isCompatible(MediaType.APPLICATION_JSON_TYPE)) {
60 mapper.writeValue(outputStream, syncLog);
61 } else {
62 new PrintStream(outputStream).println(syncLog);
63 }
64 }
65
66 @Override
67 public boolean isReadable(Class<?> aClass, Type type, Annotation[] annotations, MediaType mediaType) {
68 return aClass == SyncLog.class;
69 }
70
71 @Override
72 public SyncLog readFrom(Class<SyncLog> aClass, Type type,
73 Annotation[] annotations, MediaType mediaType,
74 MultivaluedMap<String, String> multivaluedMap, InputStream inputStream)
75 throws IOException, WebApplicationException {
76 return mapper.readValue(inputStream, SyncLog.class);
77 }
78 }