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.PermissionsResource;
23  import eu.ehri.project.acl.InheritedItemPermissionSet;
24  
25  import javax.ws.rs.Produces;
26  import javax.ws.rs.WebApplicationException;
27  import javax.ws.rs.core.HttpHeaders;
28  import javax.ws.rs.core.MediaType;
29  import javax.ws.rs.core.MultivaluedMap;
30  import javax.ws.rs.ext.MessageBodyWriter;
31  import javax.ws.rs.ext.Provider;
32  import java.io.IOException;
33  import java.io.OutputStream;
34  import java.lang.annotation.Annotation;
35  import java.lang.reflect.Type;
36  
37  @Provider
38  @Produces(MediaType.APPLICATION_JSON)
39  public class InheritedItemPermissionSetProvider
40          implements MessageBodyWriter<InheritedItemPermissionSet>, JsonMessageBodyHandler {
41  
42      @Override
43      public boolean isWriteable(Class<?> aClass, Type type, Annotation[] annotations, MediaType mediaType) {
44          return InheritedItemPermissionSet.class.equals(aClass);
45      }
46  
47      @Override
48      public long getSize(InheritedItemPermissionSet permissionSet, Class<?> aClass, Type type, Annotation[] annotations,
49              MediaType mediaType) {
50          return -1L;
51      }
52  
53      @Override
54      public void writeTo(InheritedItemPermissionSet itemPermissionSet,
55              Class<?> aClass, Type type, Annotation[] annotations,
56              MediaType mediaType, MultivaluedMap<String, Object> headers,
57              OutputStream outputStream) throws IOException, WebApplicationException {
58          headers.putSingle(HttpHeaders.CACHE_CONTROL,
59                  PermissionsResource.getCacheControl().toString());
60          mapper.writeValue(outputStream, itemPermissionSet.serialize());
61      }
62  }