source: VirtualCollectionRegistry/trunk/VirtualCollectionRegistry/src/main/java/eu/clarin/cmdi/virtualcollectionregistry/rest/MyVirtualCollectionsResource.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

  • Property svn:eol-style set to native
File size: 3.7 KB
Line 
1package eu.clarin.cmdi.virtualcollectionregistry.rest;
2
3import com.sun.jersey.api.core.InjectParam;
4import eu.clarin.cmdi.virtualcollectionregistry.VirtualCollectionRegistry;
5import eu.clarin.cmdi.virtualcollectionregistry.VirtualCollectionRegistryException;
6import eu.clarin.cmdi.virtualcollectionregistry.model.VirtualCollectionList;
7import eu.clarin.cmdi.virtualcollectionregistry.service.VirtualCollectionMarshaller;
8import java.io.IOException;
9import java.io.OutputStream;
10import java.net.URI;
11import java.security.Principal;
12import javax.ws.rs.DefaultValue;
13import javax.ws.rs.GET;
14import javax.ws.rs.Path;
15import javax.ws.rs.Produces;
16import javax.ws.rs.QueryParam;
17import javax.ws.rs.WebApplicationException;
18import javax.ws.rs.core.Context;
19import javax.ws.rs.core.HttpHeaders;
20import javax.ws.rs.core.MediaType;
21import javax.ws.rs.core.Response;
22import javax.ws.rs.core.SecurityContext;
23import javax.ws.rs.core.StreamingOutput;
24import javax.ws.rs.core.UriInfo;
25
26/**
27 * REST resource representing the collection of the user's virtual collections
28 * (public or private)
29 *
30 * @author twagoo
31 */
32@Path("/my-virtualcollections")
33public class MyVirtualCollectionsResource {
34
35    @InjectParam
36    private VirtualCollectionRegistry registry;
37    @InjectParam
38    private VirtualCollectionMarshaller marshaller;
39    @Context
40    private SecurityContext security;
41    @Context
42    private UriInfo uriInfo;
43    @Context
44    private HttpHeaders headers;
45
46    /**
47     * All virtual collections owned by the authenticated user will be
48     * retrieved; if a query expression is used, only the virtual collections
49     * satisfying the query will be retrieved.
50     *
51     * @param query a Virtual Collection Query Language expression
52     * @param offset
53     * @param count
54     * @return a serialised list of all virtual collections created by the
55     * authenticated user
56     * @throws VirtualCollectionRegistryException if the collections could not
57     * be retrieved
58     * @see
59     * VirtualCollectionRegistry#getVirtualCollections(java.security.Principal,
60     * java.lang.String, int, int)
61     */
62    @GET
63    @Produces({MediaType.TEXT_XML,
64        MediaType.APPLICATION_XML,
65        MediaType.APPLICATION_JSON})
66    public Response getMyVirtualCollections(@QueryParam("q") String query,
67            @DefaultValue("0") @QueryParam("offset") int offset,
68            @DefaultValue("-1") @QueryParam("count") int count)
69            throws VirtualCollectionRegistryException {
70        Principal principal = security.getUserPrincipal();
71        if (principal == null) {
72            String path = uriInfo.getPath();
73            if (path.endsWith("/")) {
74                /*
75                 * fix bad client request and remove tailing slash
76                 */
77                path = path.substring(0, path.length() - 1);
78                URI uri = uriInfo.getBaseUriBuilder().path(path).build();
79                return Response.seeOther(uri).build();
80            }
81            /*
82             * should never happen, because servlet container should supply a
83             * valid principal
84             */
85            throw new AssertionError("principal == null");
86        }
87        final VirtualCollectionList vcs = registry.getVirtualCollections(
88                principal, query, (offset > 0) ? offset : 0, count);
89        StreamingOutput writer = new StreamingOutput() {
90            @Override
91            public void write(OutputStream output) throws IOException,
92                    WebApplicationException {
93                final VirtualCollectionMarshaller.Format format = RestUtils.getOutputFormat(headers.getAcceptableMediaTypes());
94                marshaller.marshal(output, format, vcs);
95                output.close();
96            }
97        };
98        return Response.ok(writer).build();
99    }
100}
Note: See TracBrowser for help on using the repository browser.