source: VirtualCollectionRegistry/trunk/VirtualCollectionRegistry/src/main/java/eu/clarin/cmdi/virtualcollectionregistry/rest/VirtualCollectionXMLBodyWriter.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.8 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 java.util.Collections;
11import javax.ws.rs.Produces;
12import javax.ws.rs.WebApplicationException;
13import javax.ws.rs.core.MediaType;
14import javax.ws.rs.core.MultivaluedMap;
15import javax.ws.rs.ext.MessageBodyWriter;
16import javax.ws.rs.ext.Provider;
17
18/**
19 * Body writer that outputs an XML representation of a virtual collection in the
20 * internal representation
21 *
22 * @author twagoo
23 */
24@Provider
25@Produces({MediaType.TEXT_XML,
26    MediaType.APPLICATION_XML,
27    MediaType.APPLICATION_JSON})
28public class VirtualCollectionXMLBodyWriter implements MessageBodyWriter<VirtualCollection> {
29
30    @InjectParam
31    private VirtualCollectionMarshaller marshaller;
32
33    @Override
34    public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
35        return type.equals(VirtualCollection.class);
36    }
37
38    @Override
39    public long getSize(VirtualCollection t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
40        return -1;
41    }
42
43    @Override
44    public void writeTo(VirtualCollection vc, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream stream) throws IOException, WebApplicationException {
45        final VirtualCollectionMarshaller.Format format = RestUtils.getOutputFormat(Collections.singletonList(mediaType));
46        marshaller.marshal(stream, format, vc);
47    }
48
49}
Note: See TracBrowser for help on using the repository browser.