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.project.importers.ImportLog;
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 ImportLogProvider implements MessageBodyWriter<ImportLog>,
43          MessageBodyReader<ImportLog>, JsonMessageBodyHandler {
44      @Override
45      public boolean isWriteable(Class<?> aClass, Type type, Annotation[] annotations, MediaType mediaType) {
46          return aClass == ImportLog.class;
47      }
48  
49      @Override
50      public long getSize(ImportLog importLog, Class<?> aClass, Type type, Annotation[] annotations, MediaType mediaType) {
51          return -1L;
52      }
53  
54      @Override
55      public void writeTo(ImportLog importLog, 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, importLog);
61          } else {
62              new PrintStream(outputStream).println(importLog);
63          }
64      }
65  
66      @Override
67      public boolean isReadable(Class<?> aClass, Type type, Annotation[] annotations, MediaType mediaType) {
68          return aClass == ImportLog.class;
69      }
70  
71      @Override
72      public ImportLog readFrom(Class<ImportLog> aClass, Type type,
73              Annotation[] annotations, MediaType mediaType,
74              MultivaluedMap<String, String> multivaluedMap, InputStream inputStream)
75              throws IOException, WebApplicationException {
76          return mapper.readValue(inputStream, ImportLog.class);
77      }
78  }