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);
58 private String repositoryWorkspaceName;
61 * Instantiates a new multipart service context impl.
63 * @param serviceName the service name
65 * @throws UnauthorizedException the unauthorized exception
67 protected MultipartServiceContextImpl(String serviceName)
68 throws DocumentException, UnauthorizedException {
70 setOutput(new PoxPayloadOut(serviceName));
74 * Instantiates a new multipart service context impl.
76 * @param serviceName the service name
78 * @throws UnauthorizedException the unauthorized exception
80 protected MultipartServiceContextImpl(String serviceName, PoxPayloadIn theInput)
81 throws DocumentException, UnauthorizedException {
82 super(serviceName, theInput);
83 setOutput(new PoxPayloadOut(serviceName));
87 * Instantiates a new multipart service context impl.
89 * @param serviceName the service name
90 * @param queryParams the query params
92 * @throws UnauthorizedException the unauthorized exception
94 protected MultipartServiceContextImpl(String serviceName,
95 PoxPayloadIn theInput,
96 MultivaluedMap<String, String> queryParams)
97 throws DocumentException, UnauthorizedException {
98 super(serviceName, theInput, queryParams);
99 setOutput(new PoxPayloadOut(serviceName));
103 * @see org.collectionspace.services.common.context.MultipartServiceContext#getInputPart(java.lang.String, java.lang.Class)
107 public Object getInputPart(String label, Class clazz) throws IOException {
108 return getInputPart(label);
112 * @see org.collectionspace.services.common.context.MultipartServiceContext#getInputPart(java.lang.String, java.lang.Class)
115 public Object getInputPart(String label) throws IOException {
116 Object result = null;
117 PayloadInputPart payloadInputPart = getInput().getPart(label);
118 if (payloadInputPart != null) {
119 result = payloadInputPart.getBody();
125 * @see org.collectionspace.services.common.context.MultipartServiceContext#getInputPartAsString(java.lang.String)
128 public String getInputPartAsString(String label) throws IOException {
129 String result = null;
130 PayloadInputPart part = getInput().getPart(label);
132 Element element = part.asElement();
133 if (element != null) {
134 result = element.asXML();
141 * @see org.collectionspace.services.common.context.MultipartServiceContext#getInputPartAsStream(java.lang.String)
144 public InputStream getInputPartAsStream(String label) throws IOException {
145 InputStream result = null;
146 String part = getInputPartAsString(label);
148 result = new ByteArrayInputStream(part.getBytes());
154 * @see org.collectionspace.services.common.context.MultipartServiceContext#addOutputPart(java.lang.String, org.w3c.dom.Document, java.lang.String)
157 public void addOutputPart(String label, Element element, String contentType) throws Exception {
158 PayloadOutputPart part = getOutput().addPart(label, element);
159 if (logger.isTraceEnabled() == true) {
160 logger.trace("Adding part:" + label +
161 " to " + getOutput().getName() + " document.");
166 public void addOutputPart(PayloadOutputPart outputPart) throws Exception {
167 PayloadOutputPart part = getOutput().addPart(outputPart);
168 if (logger.isTraceEnabled() == true) {
169 logger.trace("Adding part:" + part.getLabel() +
170 " to " + getOutput().getName() + " document.");
175 * @see org.collectionspace.services.common.context.RemoteServiceContextImpl#getLocalContext(java.lang.String)
178 public ServiceContext getLocalContext(String localContextClassName) throws Exception {
179 ClassLoader cloader = Thread.currentThread().getContextClassLoader();
180 Class ctxClass = cloader.loadClass(localContextClassName);
181 if (!ServiceContext.class.isAssignableFrom(ctxClass)) {
182 throw new IllegalArgumentException("getLocalContext requires "
183 + " implementation of " + ServiceContext.class.getName());
186 Constructor ctor = ctxClass.getConstructor(java.lang.String.class);
187 ServiceContext ctx = (ServiceContext) ctor.newInstance(getServiceName());
192 * @see org.collectionspace.services.common.context.ServiceContext#getRepositoryWorkspaceName()
195 public String getRepositoryWorkspaceName() {
196 String result = repositoryWorkspaceName;
197 //service name is workspace name by convention
198 if (result == null) {
199 result = serviceBinding.getName();
205 public void setRespositoryWorkspaceName(String workspaceName) {
206 repositoryWorkspaceName = workspaceName;