]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
5e99ab85033123d2010351a021149a35239264d6
[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.context;
25
26 import java.io.ByteArrayOutputStream;
27 import java.io.IOException;
28 import java.lang.reflect.Constructor;
29 import javax.ws.rs.core.MediaType;
30 import org.collectionspace.services.common.document.DocumentUtils;
31 import org.collectionspace.services.common.security.UnauthorizedException;
32 import org.jboss.resteasy.plugins.providers.multipart.InputPart;
33 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
34 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
35 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.w3c.dom.Document;
39
40 /**
41  * MultipartServiceContextImpl takes Multipart Input/Output
42  *
43  * $LastChangedRevision: $
44  * $LastChangedDate: $
45  */
46 public class MultipartServiceContextImpl
47         extends RemoteServiceContextImpl<MultipartInput, MultipartOutput>
48         implements MultipartServiceContext {
49
50     final Logger logger = LoggerFactory.getLogger(MultipartServiceContextImpl.class);
51
52     public MultipartServiceContextImpl(String serviceName) throws UnauthorizedException {
53         super(serviceName);
54         setOutput(new MultipartOutput());
55     }
56
57
58     @Override
59     public Object getInputPart(String label, Class clazz) throws IOException {
60         Object obj = null;
61         if(getInput() != null){
62             MultipartInput fdip = getInput();
63
64             for(InputPart part : fdip.getParts()){
65                 String partLabel = part.getHeaders().getFirst("label");
66                 if(label.equalsIgnoreCase(partLabel)){
67                     if(logger.isDebugEnabled()){
68                         logger.debug("received part label=" + partLabel +
69                                 "\npayload=" + part.getBodyAsString());
70                     }
71                     obj = part.getBody(clazz, null);
72                     break;
73                 }
74             }
75         }
76         return obj;
77     }
78
79     @Override
80     public void addOutputPart(String label, Document doc, String contentType) throws Exception {
81         ByteArrayOutputStream baos = new ByteArrayOutputStream();
82         try{
83             DocumentUtils.writeDocument(doc, baos);
84             baos.close();
85             OutputPart part = getOutput().addPart(new String(baos.toByteArray()),
86                     MediaType.valueOf(contentType));
87             part.getHeaders().add("label", label);
88         }finally{
89             if(baos != null){
90                 try{
91                     baos.close();
92                 }catch(Exception e){
93                 }
94             }
95         }
96     }
97
98     @Override
99     public ServiceContext getLocalContext(String localContextClassName) throws Exception {
100         ClassLoader cloader = Thread.currentThread().getContextClassLoader();
101         Class ctxClass = cloader.loadClass(localContextClassName);
102         if(!ServiceContext.class.isAssignableFrom(ctxClass)) {
103             throw new IllegalArgumentException("getLocalContext requires " +
104                     " implementation of " + ServiceContext.class.getName());
105         }
106         
107         Constructor ctor = ctxClass.getConstructor(java.lang.String.class);
108         ServiceContext ctx = (ServiceContext)ctor.newInstance(getServiceName());
109         return ctx;
110     }
111 }