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.IOException;
28 import java.io.InputStream;
29 import java.lang.reflect.Constructor;
30 import javax.ws.rs.core.MultivaluedMap;
32 import org.collectionspace.services.client.PayloadInputPart;
33 import org.collectionspace.services.client.PayloadOutputPart;
34 import org.collectionspace.services.client.PoxPayloadIn;
35 import org.collectionspace.services.client.PoxPayloadOut;
36 import org.collectionspace.services.common.security.UnauthorizedException;
37 import org.dom4j.DocumentException;
38 import org.dom4j.Element;
39 //import org.jboss.resteasy.plugins.providers.multipart.InputPart;
40 //import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
41 //import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
42 //import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
47 * MultipartServiceContextImpl takes Multipart Input/Output
49 * $LastChangedRevision: $
52 public class MultipartServiceContextImpl
53 extends RemoteServiceContextImpl<PoxPayloadIn, PoxPayloadOut>
54 implements MultipartServiceContext {
57 final Logger logger = LoggerFactory.getLogger(MultipartServiceContextImpl.class);
60 * Instantiates a new multipart service context impl.
62 * @param serviceName the service name
64 * @throws UnauthorizedException the unauthorized exception
66 protected MultipartServiceContextImpl(String serviceName)
67 throws DocumentException, UnauthorizedException {
69 setOutput(new PoxPayloadOut(serviceName));
73 * Instantiates a new multipart service context impl.
75 * @param serviceName the service name
77 * @throws UnauthorizedException the unauthorized exception
79 protected MultipartServiceContextImpl(String serviceName, PoxPayloadIn theInput)
80 throws DocumentException, UnauthorizedException {
81 super(serviceName, theInput);
82 setOutput(new PoxPayloadOut(serviceName));
86 * Instantiates a new multipart service context impl.
88 * @param serviceName the service name
89 * @param queryParams the query params
91 * @throws UnauthorizedException the unauthorized exception
93 protected MultipartServiceContextImpl(String serviceName,
94 PoxPayloadIn theInput,
95 MultivaluedMap<String, String> queryParams)
96 throws DocumentException, UnauthorizedException {
97 super(serviceName, theInput, queryParams);
98 setOutput(new PoxPayloadOut(serviceName));
102 * @see org.collectionspace.services.common.context.MultipartServiceContext#getInputPart(java.lang.String, java.lang.Class)
106 public Object getInputPart(String label, Class clazz) throws IOException {
107 return getInputPart(label);
111 * @see org.collectionspace.services.common.context.MultipartServiceContext#getInputPart(java.lang.String, java.lang.Class)
114 public Object getInputPart(String label) throws IOException {
115 Object result = null;
116 PayloadInputPart payloadInputPart = getInput().getPart(label);
117 if (payloadInputPart != null) {
118 result = payloadInputPart.getBody();
124 * @see org.collectionspace.services.common.context.MultipartServiceContext#getInputPartAsString(java.lang.String)
127 public String getInputPartAsString(String label) throws IOException {
128 String result = null;
129 PayloadInputPart part = getInput().getPart(label);
131 Element element = part.asElement();
132 if (element != null) {
133 result = element.asXML();
140 * @see org.collectionspace.services.common.context.MultipartServiceContext#getInputPartAsStream(java.lang.String)
143 public InputStream getInputPartAsStream(String label) throws IOException {
144 InputStream result = null;
145 String part = getInputPartAsString(label);
147 result = new ByteArrayInputStream(part.getBytes());
153 * @see org.collectionspace.services.common.context.MultipartServiceContext#addOutputPart(java.lang.String, org.w3c.dom.Document, java.lang.String)
156 public void addOutputPart(String label, Element element, String contentType) throws Exception {
157 PayloadOutputPart part = getOutput().addPart(label, element);
158 if (logger.isTraceEnabled() == true) {
159 logger.trace("Adding part:" + label +
160 " to " + getOutput().getName() + " document.");
165 * @see org.collectionspace.services.common.context.RemoteServiceContextImpl#getLocalContext(java.lang.String)
168 public ServiceContext getLocalContext(String localContextClassName) throws Exception {
169 ClassLoader cloader = Thread.currentThread().getContextClassLoader();
170 Class ctxClass = cloader.loadClass(localContextClassName);
171 if (!ServiceContext.class.isAssignableFrom(ctxClass)) {
172 throw new IllegalArgumentException("getLocalContext requires "
173 + " implementation of " + ServiceContext.class.getName());
176 Constructor ctor = ctxClass.getConstructor(java.lang.String.class);
177 ServiceContext ctx = (ServiceContext) ctor.newInstance(getServiceName());