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.ResourceMap;
37 import org.collectionspace.services.common.security.UnauthorizedException;
38 import org.dom4j.DocumentException;
39 import org.dom4j.Element;
40 //import org.jboss.resteasy.plugins.providers.multipart.InputPart;
41 //import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
42 //import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
43 //import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
48 * MultipartServiceContextImpl takes Multipart Input/Output
50 * $LastChangedRevision: $
53 public class MultipartServiceContextImpl
54 extends RemoteServiceContextImpl<PoxPayloadIn, PoxPayloadOut>
55 implements MultipartServiceContext {
58 final Logger logger = LoggerFactory.getLogger(MultipartServiceContextImpl.class);
59 private String repositoryWorkspaceName;
62 * Instantiates a new multipart service context impl.
64 * @param serviceName the service name
66 * @throws UnauthorizedException the unauthorized exception
68 protected MultipartServiceContextImpl(String serviceName)
69 throws DocumentException, UnauthorizedException {
71 setOutput(new PoxPayloadOut(serviceName));
75 * Instantiates a new multipart service context impl.
77 * @param serviceName the service name
79 * @throws UnauthorizedException the unauthorized exception
81 protected MultipartServiceContextImpl(String serviceName, PoxPayloadIn theInput)
82 throws DocumentException, UnauthorizedException {
83 super(serviceName, theInput);
84 setOutput(new PoxPayloadOut(serviceName));
88 * Instantiates a new multipart service context impl.
90 * @param serviceName the service name
91 * @param queryParams the query params
93 * @throws UnauthorizedException the unauthorized exception
95 protected MultipartServiceContextImpl(
97 PoxPayloadIn theInput,
98 ResourceMap resourceMap,
99 MultivaluedMap<String, String> queryParams)
100 throws DocumentException, UnauthorizedException {
101 super(serviceName, theInput, resourceMap, queryParams);
102 setOutput(new PoxPayloadOut(serviceName));
106 * @see org.collectionspace.services.common.context.MultipartServiceContext#getInputPart(java.lang.String, java.lang.Class)
110 public Object getInputPart(String label, Class clazz) throws IOException {
111 return getInputPart(label);
115 * @see org.collectionspace.services.common.context.MultipartServiceContext#getInputPart(java.lang.String, java.lang.Class)
118 public Object getInputPart(String label) throws IOException {
119 Object result = null;
120 PayloadInputPart payloadInputPart = getInput().getPart(label);
121 if (payloadInputPart != null) {
122 result = payloadInputPart.getBody();
128 * @see org.collectionspace.services.common.context.MultipartServiceContext#getInputPartAsString(java.lang.String)
131 public String getInputPartAsString(String label) throws IOException {
132 String result = null;
133 PayloadInputPart part = getInput().getPart(label);
135 Element element = part.asElement();
136 if (element != null) {
137 result = element.asXML();
144 * @see org.collectionspace.services.common.context.MultipartServiceContext#getInputPartAsStream(java.lang.String)
147 public InputStream getInputPartAsStream(String label) throws IOException {
148 InputStream result = null;
149 String part = getInputPartAsString(label);
151 result = new ByteArrayInputStream(part.getBytes());
157 * @see org.collectionspace.services.common.context.MultipartServiceContext#addOutputPart(java.lang.String, org.w3c.dom.Document, java.lang.String)
160 public void addOutputPart(String label, Element element, String contentType) throws Exception {
161 PayloadOutputPart part = getOutput().addPart(label, element);
162 if (logger.isTraceEnabled() == true) {
163 logger.trace("Adding part:" + label +
164 " to " + getOutput().getName() + " document.");
169 public void addOutputPart(PayloadOutputPart outputPart) throws Exception {
170 PayloadOutputPart part = getOutput().addPart(outputPart);
171 if (logger.isTraceEnabled() == true) {
172 logger.trace("Adding part:" + part.getLabel() +
173 " to " + getOutput().getName() + " document.");
178 * @see org.collectionspace.services.common.context.RemoteServiceContextImpl#getLocalContext(java.lang.String)
181 public ServiceContext getLocalContext(String localContextClassName) throws Exception {
182 ClassLoader cloader = Thread.currentThread().getContextClassLoader();
183 Class ctxClass = cloader.loadClass(localContextClassName);
184 if (!ServiceContext.class.isAssignableFrom(ctxClass)) {
185 throw new IllegalArgumentException("getLocalContext requires "
186 + " implementation of " + ServiceContext.class.getName());
189 Constructor ctor = ctxClass.getConstructor(java.lang.String.class);
190 ServiceContext ctx = (ServiceContext) ctor.newInstance(getServiceName());
195 * @see org.collectionspace.services.common.context.ServiceContext#getRepositoryWorkspaceName()
198 public String getRepositoryWorkspaceName() {
199 String result = repositoryWorkspaceName;
200 //service name is workspace name by convention
201 if (result == null) {
202 result = serviceBinding.getName();
208 public void setRespositoryWorkspaceName(String workspaceName) {
209 repositoryWorkspaceName = workspaceName;