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 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  }