]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
e0d80e1977f1bb26c17c808eeda023ee0c48c835
[tmp/jakarta-migration.git] /
1 /**
2  *  This document is a part of the source code and related artifacts
3  *  for CollectionSpace, an open source collections management system
4  *  for museums and related institutions:
5
6  *  http://www.collectionspace.org
7  *  http://wiki.collectionspace.org
8
9  *  Copyright 2009 University of California at Berkeley
10
11  *  Licensed under the Educational Community License (ECL), Version 2.0.
12  *  You may not use this file except in compliance with this License.
13
14  *  You may obtain a copy of the ECL 2.0 License at
15
16  *  https://source.collectionspace.org/collection-space/LICENSE.txt
17
18  *  Unless required by applicable law or agreed to in writing, software
19  *  distributed under the License is distributed on an "AS IS" BASIS,
20  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  *  See the License for the specific language governing permissions and
22  *  limitations under the License.
23  */
24 package org.collectionspace.services.common.publicitem;
25
26 import java.io.InputStream;
27
28 import org.collectionspace.authentication.spi.AuthNContext;
29 import org.collectionspace.services.publicitem.PublicitemsCommon;
30 import org.collectionspace.services.client.PublicItemClient;
31 import org.collectionspace.services.client.PoxPayloadIn;
32 import org.collectionspace.services.client.PoxPayloadOut;
33 import org.collectionspace.services.common.ResourceBase;
34 import org.collectionspace.services.common.ServiceMessages;
35 import org.collectionspace.services.common.blob.BlobOutput;
36 import org.collectionspace.services.common.context.RemoteServiceContext;
37 import org.collectionspace.services.common.imaging.nuxeo.NuxeoBlobUtils;
38 import org.jboss.resteasy.core.ResourceMethod;
39 import org.jboss.resteasy.spi.HttpRequest;
40
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 import javax.ws.rs.Consumes;
45 import javax.ws.rs.GET;
46 import javax.ws.rs.Path;
47 import javax.ws.rs.PathParam;
48 import javax.ws.rs.Produces;
49 import javax.ws.rs.core.Context;
50 import javax.ws.rs.core.Request;
51 import javax.ws.rs.core.Response;
52 import javax.ws.rs.core.UriInfo;
53
54 @Path(PublicItemClient.SERVICE_PATH)
55 @Consumes("application/xml")
56 @Produces("application/xml")
57 public class PublicItemResource extends ResourceBase {
58
59     final Logger logger = LoggerFactory.getLogger(PublicItemResource.class);
60
61     @Override
62     protected String getVersionString() {
63         final String lastChangeRevision = "$LastChangedRevision$";
64         return lastChangeRevision;
65     }
66     
67     @Override
68     public String getServiceName() {
69         return PublicItemClient.SERVICE_NAME;
70     }
71
72     @Override
73     public Class<PublicitemsCommon> getCommonPartClass() {
74         return PublicitemsCommon.class;
75     }
76
77         @Override
78         public boolean allowAnonymousAccess(HttpRequest request,
79                         ResourceMethod method) {
80                 return true;
81         }
82         
83     @GET
84     @Path("/{csid}/{tenantId}/" + PublicItemClient.PUBLICITEMS_CONTENT_SUFFIX) // "content"
85     public Response getPublishedResource(
86             @Context Request request,
87             @Context UriInfo uriInfo,
88             @PathParam("csid") String csid,
89             @PathParam(AuthNContext.TENANT_ID_PATH_PARAM) String tenantId) {
90         Response result = null;
91
92         try {
93                 //
94                 // First, extract the PublicitemsCommon instance.
95                 //
96                         RemoteServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = (RemoteServiceContext<PoxPayloadIn, PoxPayloadOut>) createServiceContext(uriInfo);
97                         PoxPayloadOut poxPayloadOut = get(csid, ctx);
98                         PublicitemsCommon publicitemsCommon = (PublicitemsCommon)poxPayloadOut.getPart(PublicItemClient.SERVICE_COMMON_PART_NAME).getBody();
99                         //
100                         // Get the repository blob ID and retrieve the content as a stream
101                         //
102                         String blobContentCsid = publicitemsCommon.getContentId();
103                         StringBuffer outMimeType = new StringBuffer();
104                         BlobOutput blobOutput = NuxeoBlobUtils.getBlobOutput(ctx, getRepositoryClient(ctx), blobContentCsid, outMimeType);
105                         InputStream contentStream = blobOutput.getBlobInputStream();
106                         //
107                         // Return the content stream in the response
108                         //
109                 Response.ResponseBuilder responseBuilder = Response.ok(contentStream, outMimeType.toString());
110                 responseBuilder = responseBuilder.header("Content-Disposition","inline;filename=\""
111                                 + publicitemsCommon.getContentName() +"\"");
112                 result = responseBuilder.build();
113                 } catch (Exception e) {
114             throw bigReThrow(e, ServiceMessages.READ_FAILED, csid);
115                 }
116
117         return result;
118     }
119 }
120
121
122