]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
f105b0c112c47f6c2fa0273fe5602cde0d1d9e72
[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 import java.lang.reflect.Method;
28
29 import org.collectionspace.authentication.spi.AuthNContext;
30 import org.collectionspace.services.publicitem.PublicitemsCommon;
31 import org.collectionspace.services.client.PublicItemClient;
32 import org.collectionspace.services.client.PoxPayloadIn;
33 import org.collectionspace.services.client.PoxPayloadOut;
34 import org.collectionspace.services.common.NuxeoBasedResource;
35 import org.collectionspace.services.common.ServiceMessages;
36 import org.collectionspace.services.common.blob.BlobOutput;
37 import org.collectionspace.services.common.context.RemoteServiceContext;
38 import org.collectionspace.services.common.imaging.nuxeo.NuxeoBlobUtils;
39 //import org.jboss.resteasy.core.ResourceMethod;
40 import org.jboss.resteasy.spi.HttpRequest;
41 import org.jboss.resteasy.spi.metadata.ResourceMethod;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 import javax.ws.rs.Consumes;
46 import javax.ws.rs.GET;
47 import javax.ws.rs.Path;
48 import javax.ws.rs.PathParam;
49 import javax.ws.rs.Produces;
50 import javax.ws.rs.core.Context;
51 import javax.ws.rs.core.Request;
52 import javax.ws.rs.core.Response;
53 import javax.ws.rs.core.UriInfo;
54
55 @Path(PublicItemClient.SERVICE_PATH)
56 @Consumes("application/xml")
57 @Produces("application/xml")
58 public class PublicItemResource extends NuxeoBasedResource {
59
60     final Logger logger = LoggerFactory.getLogger(PublicItemResource.class);
61
62     @Override
63     protected String getVersionString() {
64         final String lastChangeRevision = "$LastChangedRevision$";
65         return lastChangeRevision;
66     }
67     
68     @Override
69     public String getServiceName() {
70         return PublicItemClient.SERVICE_NAME;
71     }
72
73     @Override
74     public Class<PublicitemsCommon> getCommonPartClass() {
75         return PublicitemsCommon.class;
76     }
77
78         @Override
79         public boolean allowAnonymousAccess(HttpRequest request,
80                         Method method) {
81                 return true;
82         }
83         
84     @GET
85     @Path("/{csid}/{tenantId}/" + PublicItemClient.PUBLICITEMS_CONTENT_SUFFIX) // "content"
86     public Response getPublishedResource(
87             @Context Request request,
88             @Context UriInfo uriInfo,
89             @PathParam("csid") String csid,
90             @PathParam(AuthNContext.TENANT_ID_PATH_PARAM) String tenantId) {
91         Response result = null;
92
93         try {
94                 //
95                 // First, extract the PublicitemsCommon instance.
96                 //
97                         RemoteServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = (RemoteServiceContext<PoxPayloadIn, PoxPayloadOut>) createServiceContext(uriInfo);
98                         PoxPayloadOut poxPayloadOut = get(csid, ctx);
99                         PublicitemsCommon publicitemsCommon = (PublicitemsCommon)poxPayloadOut.getPart(PublicItemClient.SERVICE_COMMON_PART_NAME).getBody();
100                         //
101                         // Get the repository blob ID and retrieve the content as a stream
102                         //
103                         String blobContentCsid = publicitemsCommon.getContentId();
104                         StringBuffer outMimeType = new StringBuffer();
105                         BlobOutput blobOutput = NuxeoBlobUtils.getBlobOutput(ctx, getRepositoryClient(ctx), blobContentCsid, outMimeType);
106                         InputStream contentStream = blobOutput.getBlobInputStream();
107                         //
108                         // Return the content stream in the response
109                         //
110                 Response.ResponseBuilder responseBuilder = Response.ok(contentStream, outMimeType.toString());
111                 responseBuilder = responseBuilder.header("Content-Disposition","inline;filename=\""
112                                 + publicitemsCommon.getContentName() +"\"");
113                 result = responseBuilder.build();
114                 } catch (Exception e) {
115             throw bigReThrow(e, ServiceMessages.READ_FAILED, csid);
116                 }
117
118         return result;
119     }
120 }
121
122
123