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 MultipartServiceContextImpl
46 extends RemoteServiceContextImpl<MultipartInput, MultipartOutput>
47 implements MultipartServiceContext {
49 final Logger logger = LoggerFactory.getLogger(MultipartServiceContextImpl.class);
51 public MultipartServiceContextImpl(String serviceName) {
53 setOutput(new MultipartOutput());
58 public Object getInputPart(String label, Class clazz) throws IOException {
60 if(getInput() != null){
61 MultipartInput fdip = getInput();
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());
70 obj = part.getBody(clazz, null);
79 public void addOutputPart(String label, Document doc, String contentType) throws Exception {
80 ByteArrayOutputStream baos = new ByteArrayOutputStream();
82 DocumentUtils.writeDocument(doc, baos);
84 OutputPart part = getOutput().addPart(new String(baos.toByteArray()),
85 MediaType.valueOf(contentType));
86 part.getHeaders().add("label", label);
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());
106 Constructor ctor = ctxClass.getConstructor(java.lang.String.class);
107 ServiceContext ctx = (ServiceContext)ctor.newInstance(getServiceName());