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:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright 2009 University of California at Berkeley
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
16 * https://source.collectionspace.org/collection-space/LICENSE.txt
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.
24 package org.collectionspace.services.common.context;
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;
40 * RemoteServiceContextImpl
42 * $LastChangedRevision: $
45 public class RemoteServiceContextImpl
46 extends AbstractServiceContext<MultipartInput, MultipartOutput>
47 implements RemoteServiceContext {
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;
54 public RemoteServiceContextImpl(String serviceName) {
56 output = new MultipartOutput();
60 public MultipartInput getInput() {
65 public void setInput(MultipartInput input) throws IOException {
70 public MultipartOutput getOutput() {
75 public void setOutput(MultipartOutput output) throws Exception {
80 public Object getInputPart(String label, Class clazz) throws IOException {
82 if(getInput() != null){
83 MultipartInput fdip = getInput();
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());
92 obj = part.getBody(clazz, null);
101 public void addOutputPart(String label, Document doc, String contentType) throws Exception {
102 ByteArrayOutputStream baos = new ByteArrayOutputStream();
104 DocumentUtils.writeDocument(doc, baos);
106 OutputPart part = output.addPart(new String(baos.toByteArray()),
107 MediaType.valueOf(contentType));
108 part.getHeaders().add("label", label);
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());
128 Constructor ctor = ctxClass.getConstructor(java.lang.String.class);
129 ServiceContext ctx = (ServiceContext)ctor.newInstance(getServiceName());