1 | package eu.clarin.cmdi.virtualcollectionregistry.rest; |
---|
2 | |
---|
3 | import com.sun.jersey.api.core.InjectParam; |
---|
4 | import eu.clarin.cmdi.virtualcollectionregistry.model.VirtualCollection; |
---|
5 | import eu.clarin.cmdi.virtualcollectionregistry.service.VirtualCollectionMarshaller; |
---|
6 | import java.io.IOException; |
---|
7 | import java.io.OutputStream; |
---|
8 | import java.lang.annotation.Annotation; |
---|
9 | import java.lang.reflect.Type; |
---|
10 | import java.util.Collections; |
---|
11 | import javax.ws.rs.Produces; |
---|
12 | import javax.ws.rs.WebApplicationException; |
---|
13 | import javax.ws.rs.core.MediaType; |
---|
14 | import javax.ws.rs.core.MultivaluedMap; |
---|
15 | import javax.ws.rs.ext.MessageBodyWriter; |
---|
16 | import 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}) |
---|
28 | public 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 | } |
---|