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.ByteArrayInputStream;
27 import java.io.ByteArrayOutputStream;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.lang.reflect.Constructor;
31 import javax.ws.rs.core.MediaType;
32 import javax.ws.rs.core.MultivaluedMap;
34 import org.collectionspace.services.common.document.DocumentUtils;
35 import org.collectionspace.services.common.security.UnauthorizedException;
36 import org.jboss.resteasy.plugins.providers.multipart.InputPart;
37 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
38 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
39 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.w3c.dom.Document;
45 * MultipartServiceContextImpl takes Multipart Input/Output
47 * $LastChangedRevision: $
50 public class MultipartServiceContextImpl
51 extends RemoteServiceContextImpl<MultipartInput, MultipartOutput>
52 implements MultipartServiceContext {
55 final Logger logger = LoggerFactory.getLogger(MultipartServiceContextImpl.class);
58 * Instantiates a new multipart service context impl.
60 * @param serviceName the service name
62 * @throws UnauthorizedException the unauthorized exception
64 protected MultipartServiceContextImpl(String serviceName)
65 throws UnauthorizedException {
67 setOutput(new MultipartOutput());
71 * Instantiates a new multipart service context impl.
73 * @param serviceName the service name
75 * @throws UnauthorizedException the unauthorized exception
77 protected MultipartServiceContextImpl(String serviceName, MultipartInput theInput)
78 throws UnauthorizedException {
79 super(serviceName, theInput);
80 setOutput(new MultipartOutput());
84 * Instantiates a new multipart service context impl.
86 * @param serviceName the service name
87 * @param queryParams the query params
89 * @throws UnauthorizedException the unauthorized exception
91 protected MultipartServiceContextImpl(String serviceName,
92 MultipartInput theInput,
93 MultivaluedMap<String, String> queryParams) throws UnauthorizedException {
94 super(serviceName, theInput, queryParams);
95 setOutput(new MultipartOutput());
99 * Gets the input part.
101 * @param label the label
103 * @return the input part
105 * @throws IOException Signals that an I/O exception has occurred.
107 private InputPart getInputPart(String label) throws IOException {
108 if (getInput() != null) {
109 MultipartInput fdip = getInput();
111 for (InputPart part : fdip.getParts()) {
112 String partLabel = part.getHeaders().getFirst("label");
113 if (label.equalsIgnoreCase(partLabel)) {
114 if (logger.isTraceEnabled()) {
115 logger.trace("getInputPart found part with label=" + partLabel
116 + "\npayload=" + part.getBodyAsString());
126 * @see org.collectionspace.services.common.context.MultipartServiceContext#getInputPart(java.lang.String, java.lang.Class)
129 public Object getInputPart(String label, Class clazz) throws IOException {
131 InputPart part = getInputPart(label);
133 obj = part.getBody(clazz, null);
139 * @see org.collectionspace.services.common.context.MultipartServiceContext#getInputPartAsString(java.lang.String)
142 public String getInputPartAsString(String label) throws IOException {
143 InputPart part = getInputPart(label);
145 return part.getBodyAsString();
151 * @see org.collectionspace.services.common.context.MultipartServiceContext#getInputPartAsStream(java.lang.String)
154 public InputStream getInputPartAsStream(String label) throws IOException {
155 InputPart part = getInputPart(label);
157 return new ByteArrayInputStream(part.getBodyAsString().getBytes());
163 * @see org.collectionspace.services.common.context.MultipartServiceContext#addOutputPart(java.lang.String, org.w3c.dom.Document, java.lang.String)
166 public void addOutputPart(String label, Document doc, String contentType) throws Exception {
167 ByteArrayOutputStream baos = new ByteArrayOutputStream();
169 DocumentUtils.writeDocument(doc, baos);
171 OutputPart part = getOutput().addPart(new String(baos.toByteArray()),
172 MediaType.valueOf(contentType));
173 part.getHeaders().add("label", label);
178 } catch (Exception e) {
185 * @see org.collectionspace.services.common.context.RemoteServiceContextImpl#getLocalContext(java.lang.String)
188 public ServiceContext getLocalContext(String localContextClassName) throws Exception {
189 ClassLoader cloader = Thread.currentThread().getContextClassLoader();
190 Class ctxClass = cloader.loadClass(localContextClassName);
191 if (!ServiceContext.class.isAssignableFrom(ctxClass)) {
192 throw new IllegalArgumentException("getLocalContext requires "
193 + " implementation of " + ServiceContext.class.getName());
196 Constructor ctor = ctxClass.getConstructor(java.lang.String.class);
197 ServiceContext ctx = (ServiceContext) ctor.newInstance(getServiceName());