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