source: VirtualCollectionRegistry/trunk/VirtualCollectionRegistry/src/main/java/eu/clarin/cmdi/virtualcollectionregistry/rest/VirtualCollectionCMDIBodyWriter.java @ 5510

Last change on this file since 5510 was 5510, checked in by Twan Goosen, 10 years ago

Created message body writers for virtual collection; CMDI, XML and JSON are now returned via a single method in the resource and CMDI is default.
Refs #604

File size: 1.9 KB
Line 
1package eu.clarin.cmdi.virtualcollectionregistry.rest;
2
3import com.sun.jersey.api.core.InjectParam;
4import eu.clarin.cmdi.virtualcollectionregistry.model.VirtualCollection;
5import eu.clarin.cmdi.virtualcollectionregistry.service.VirtualCollectionMarshaller;
6import java.io.IOException;
7import java.io.OutputStream;
8import java.lang.annotation.Annotation;
9import java.lang.reflect.Type;
10import javax.ws.rs.Produces;
11import javax.ws.rs.WebApplicationException;
12import javax.ws.rs.core.MediaType;
13import javax.ws.rs.core.MultivaluedMap;
14import javax.ws.rs.core.Response;
15import javax.ws.rs.core.Response.Status;
16import javax.ws.rs.ext.MessageBodyWriter;
17import javax.ws.rs.ext.Provider;
18
19/**
20 * Body writer that outputs a CMDI representation of a virtual collection
21 *
22 * @author twagoo
23 */
24@Provider
25@Produces(VirtualCollectionResource.MediaTypes.CMDI)
26public class VirtualCollectionCMDIBodyWriter implements MessageBodyWriter<VirtualCollection> {
27
28    @InjectParam
29    private VirtualCollectionMarshaller marshaller;
30
31    @Override
32    public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
33        return type.equals(VirtualCollection.class);
34    }
35
36    @Override
37    public long getSize(VirtualCollection t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
38        return -1;
39    }
40
41    @Override
42    public void writeTo(VirtualCollection vc, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream stream) throws IOException, WebApplicationException {
43        if (!vc.isPublic() || (vc.getPersistentIdentifier() == null)) {
44            throw new WebApplicationException(Response.status(Status.NOT_ACCEPTABLE).entity("CMDI not available for unpublished profiles. Please request XML or JSON").build());
45        }
46        marshaller.marshalAsCMDI(stream, VirtualCollectionMarshaller.Format.XML, vc);
47    }
48
49}
Note: See TracBrowser for help on using the repository browser.