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;
31 import javax.ws.rs.core.UriInfo;
33 import org.collectionspace.services.client.PayloadInputPart;
34 import org.collectionspace.services.client.PayloadOutputPart;
35 import org.collectionspace.services.client.PoxPayloadIn;
36 import org.collectionspace.services.client.PoxPayloadOut;
37 import org.collectionspace.services.common.ResourceMap;
38 import org.collectionspace.services.common.security.UnauthorizedException;
39 import org.dom4j.DocumentException;
40 import org.dom4j.Element;
41 //import org.jboss.resteasy.plugins.providers.multipart.InputPart;
42 //import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
43 //import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
44 //import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
49 * MultipartServiceContextImpl takes Multipart Input/Output
51 * $LastChangedRevision: $
54 public class MultipartServiceContextImpl
55 extends RemoteServiceContextImpl<PoxPayloadIn, PoxPayloadOut>
56 implements MultipartServiceContext {
59 final Logger logger = LoggerFactory.getLogger(MultipartServiceContextImpl.class);
60 private String repositoryWorkspaceName;
63 * Instantiates a new multipart service context impl.
65 * @param serviceName the service name
67 * @throws UnauthorizedException the unauthorized exception
69 protected MultipartServiceContextImpl(String serviceName)
70 throws DocumentException, UnauthorizedException {
72 setOutput(new PoxPayloadOut(serviceName));
76 * Instantiates a new multipart service context impl.
78 * @param serviceName the service name
80 * @throws UnauthorizedException the unauthorized exception
82 protected MultipartServiceContextImpl(String serviceName, PoxPayloadIn theInput)
83 throws DocumentException, UnauthorizedException {
84 super(serviceName, theInput);
85 setOutput(new PoxPayloadOut(serviceName));
89 * Instantiates a new multipart service context impl.
91 * @param serviceName the service name
92 * @param queryParams the query params
94 * @throws UnauthorizedException the unauthorized exception
96 protected MultipartServiceContextImpl(
98 PoxPayloadIn theInput,
99 ResourceMap resourceMap,
101 throws DocumentException, UnauthorizedException {
102 super(serviceName, theInput, resourceMap, uriInfo);
103 setOutput(new PoxPayloadOut(serviceName));
107 * @see org.collectionspace.services.common.context.MultipartServiceContext#getInputPart(java.lang.String, java.lang.Class)
111 public Object getInputPart(String label, Class clazz) throws IOException {
112 return getInputPart(label);
116 * @see org.collectionspace.services.common.context.MultipartServiceContext#getInputPart(java.lang.String, java.lang.Class)
119 public Object getInputPart(String label) throws IOException {
120 Object result = null;
121 PayloadInputPart payloadInputPart = getInput().getPart(label);
122 if (payloadInputPart != null) {
123 result = payloadInputPart.getBody();
129 * @see org.collectionspace.services.common.context.MultipartServiceContext#getInputPartAsString(java.lang.String)
132 public String getInputPartAsString(String label) throws IOException {
133 String result = null;
134 PayloadInputPart part = getInput().getPart(label);
136 Element element = part.asElement();
137 if (element != null) {
138 result = element.asXML();
145 * @see org.collectionspace.services.common.context.MultipartServiceContext#getInputPartAsStream(java.lang.String)
148 public InputStream getInputPartAsStream(String label) throws IOException {
149 InputStream result = null;
150 String part = getInputPartAsString(label);
152 result = new ByteArrayInputStream(part.getBytes());
158 * @see org.collectionspace.services.common.context.MultipartServiceContext#addOutputPart(java.lang.String, org.w3c.dom.Document, java.lang.String)
161 public void addOutputPart(String label, Element element, String contentType) throws Exception {
162 PayloadOutputPart part = getOutput().addPart(label, element);
163 if (logger.isTraceEnabled() == true) {
164 logger.trace("Adding part:" + label +
165 " to " + getOutput().getName() + " document.");
170 public void addOutputPart(PayloadOutputPart outputPart) throws Exception {
171 PayloadOutputPart part = getOutput().addPart(outputPart);
172 if (logger.isTraceEnabled() == true) {
173 logger.trace("Adding part:" + part.getLabel() +
174 " to " + getOutput().getName() + " document.");
179 * @see org.collectionspace.services.common.context.RemoteServiceContextImpl#getLocalContext(java.lang.String)
182 public ServiceContext getLocalContext(String localContextClassName) throws Exception {
183 ClassLoader cloader = Thread.currentThread().getContextClassLoader();
184 Class ctxClass = cloader.loadClass(localContextClassName);
185 if (!ServiceContext.class.isAssignableFrom(ctxClass)) {
186 throw new IllegalArgumentException("getLocalContext requires "
187 + " implementation of " + ServiceContext.class.getName());
190 Constructor ctor = ctxClass.getConstructor(java.lang.String.class);
191 ServiceContext ctx = (ServiceContext) ctor.newInstance(getServiceName());
196 * @see org.collectionspace.services.common.context.ServiceContext#getRepositoryWorkspaceName()
199 public String getRepositoryWorkspaceName() {
200 String result = repositoryWorkspaceName;
201 //service name is workspace name by convention
202 if (result == null) {
203 result = serviceBinding.getName();
209 public void setRespositoryWorkspaceName(String workspaceName) {
210 repositoryWorkspaceName = workspaceName;