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.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;
41 * MultipartServiceContextImpl takes Multipart Input/Output
43 * $LastChangedRevision: $
46 public class MultipartServiceContextImpl
47 extends RemoteServiceContextImpl<MultipartInput, MultipartOutput>
48 implements MultipartServiceContext {
50 final Logger logger = LoggerFactory.getLogger(MultipartServiceContextImpl.class);
52 public MultipartServiceContextImpl(String serviceName) throws UnauthorizedException {
54 setOutput(new MultipartOutput());
59 public Object getInputPart(String label, Class clazz) throws IOException {
61 if(getInput() != null){
62 MultipartInput fdip = getInput();
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());
71 obj = part.getBody(clazz, null);
80 public void addOutputPart(String label, Document doc, String contentType) throws Exception {
81 ByteArrayOutputStream baos = new ByteArrayOutputStream();
83 DocumentUtils.writeDocument(doc, baos);
85 OutputPart part = getOutput().addPart(new String(baos.toByteArray()),
86 MediaType.valueOf(contentType));
87 part.getHeaders().add("label", label);
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());
107 Constructor ctor = ctxClass.getConstructor(java.lang.String.class);
108 ServiceContext ctx = (ServiceContext)ctor.newInstance(getServiceName());