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.extension.errors.WebDeserializationError;
23 import eu.ehri.project.exceptions.DeserializationError;
24 import eu.ehri.project.exceptions.SerializationError;
25 import eu.ehri.project.persistence.Bundle;
26
27 import javax.ws.rs.Consumes;
28 import javax.ws.rs.Produces;
29 import javax.ws.rs.WebApplicationException;
30 import javax.ws.rs.core.MediaType;
31 import javax.ws.rs.core.MultivaluedMap;
32 import javax.ws.rs.ext.MessageBodyReader;
33 import javax.ws.rs.ext.MessageBodyWriter;
34 import javax.ws.rs.ext.Provider;
35 import java.io.IOException;
36 import java.io.InputStream;
37 import java.io.OutputStream;
38 import java.lang.annotation.Annotation;
39 import java.lang.reflect.Type;
40
41
42 @Provider
43 @Consumes(MediaType.APPLICATION_JSON)
44 @Produces(MediaType.APPLICATION_JSON)
45 public class BundleProvider implements MessageBodyReader<Bundle>, MessageBodyWriter<Bundle> {
46 @Override
47 public boolean isReadable(Class<?> aClass, Type type, Annotation[] annotations, MediaType mediaType) {
48 return aClass == Bundle.class;
49 }
50
51 @Override
52 public Bundle readFrom(Class<Bundle> bundleClass, Type type, Annotation[] annotations,
53 MediaType mediaType, MultivaluedMap<String,
54 String> headers, InputStream stream) throws IOException, WebApplicationException {
55 try {
56 return Bundle.fromStream(stream);
57 } catch (DeserializationError deserializationError) {
58 throw new WebDeserializationError(deserializationError);
59 }
60 }
61
62 @Override
63 public boolean isWriteable(Class<?> aClass, Type type, Annotation[] annotations, MediaType mediaType) {
64 return aClass == Bundle.class;
65 }
66
67 @Override
68 public long getSize(Bundle bundle, Class<?> aClass, Type type, Annotation[] annotations, MediaType mediaType) {
69 return -1;
70 }
71
72 @Override
73 public void writeTo(Bundle bundle, Class<?> aClass, Type type, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> multivaluedMap, OutputStream outputStream) throws IOException, WebApplicationException {
74 try {
75 Bundle.toStream(bundle, outputStream);
76 } catch (SerializationError serializationError) {
77 throw new WebApplicationException(serializationError);
78 }
79 }
80 }