]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
af6f5b49296cbc49f24bc0d63cf6b5a77d2ade9c
[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.repository.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 RemoteServiceContextImpl
46         extends AbstractServiceContext<MultipartInput, MultipartOutput>
47         implements RemoteServiceContext {
48
49     final Logger logger = LoggerFactory.getLogger(RemoteServiceContextImpl.class);
50     //input stores original content as received over the wire
51     private MultipartInput input;
52     private MultipartOutput output;
53
54     public RemoteServiceContextImpl(String serviceName) {
55         super(serviceName);
56         output = new MultipartOutput();
57     }
58
59     @Override
60     public MultipartInput getInput() {
61         return input;
62     }
63
64     @Override
65     public void setInput(MultipartInput input) throws IOException {
66         this.input = input;
67     }
68
69     @Override
70     public MultipartOutput getOutput() {
71         return output;
72     }
73
74     @Override
75     public void setOutput(MultipartOutput output) throws Exception {
76         this.output = output;
77     }
78
79     @Override
80     public Object getInputPart(String label, Class clazz) throws IOException {
81         Object obj = null;
82         if(getInput() != null){
83             MultipartInput fdip = getInput();
84
85             for(InputPart part : fdip.getParts()){
86                 String partLabel = part.getHeaders().getFirst("label");
87                 if(label.equalsIgnoreCase(partLabel)){
88                     if(logger.isDebugEnabled()){
89                         logger.debug("received part label=" + partLabel +
90                                 "\npayload=" + part.getBodyAsString());
91                     }
92                     obj = part.getBody(clazz, null);
93                     break;
94                 }
95             }
96         }
97         return obj;
98     }
99
100     @Override
101     public void addOutputPart(String label, Document doc, String contentType) throws Exception {
102         ByteArrayOutputStream baos = new ByteArrayOutputStream();
103         try{
104             DocumentUtils.writeDocument(doc, baos);
105             baos.close();
106             OutputPart part = output.addPart(new String(baos.toByteArray()),
107                     MediaType.valueOf(contentType));
108             part.getHeaders().add("label", label);
109         }finally{
110             if(baos != null){
111                 try{
112                     baos.close();
113                 }catch(Exception e){
114                 }
115             }
116         }
117     }
118
119     @Override
120     public ServiceContext getLocalContext(String localContextClassName) throws Exception {
121         ClassLoader cloader = Thread.currentThread().getContextClassLoader();
122         Class ctxClass = cloader.loadClass(localContextClassName);
123         if(!ServiceContext.class.isAssignableFrom(ctxClass)) {
124             throw new IllegalArgumentException("getLocalContext requires " +
125                     " implementation of " + ServiceContext.class.getName());
126         }
127         
128         Constructor ctor = ctxClass.getConstructor(java.lang.String.class);
129         ServiceContext ctx = (ServiceContext)ctor.newInstance(getServiceName());
130         return ctx;
131     }
132 }