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.UriInfo;
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;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
45 * MultipartServiceContextImpl takes Multipart Input/Output
47 * $LastChangedRevision: $
50 public class MultipartServiceContextImpl
51 extends RemoteServiceContextImpl<PoxPayloadIn, PoxPayloadOut>
52 implements MultipartServiceContext {
55 final Logger logger = LoggerFactory.getLogger(MultipartServiceContextImpl.class);
56 private String repositoryWorkspaceName;
59 * Instantiates a new multipart service context impl.
61 * @param serviceName the service name
63 * @throws UnauthorizedException the unauthorized exception
65 protected MultipartServiceContextImpl(String serviceName,
67 throws DocumentException, UnauthorizedException {
68 super(serviceName, uriInfo);
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, UriInfo uriInfo)
80 throws DocumentException, UnauthorizedException {
81 super(serviceName, theInput, uriInfo);
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(
95 PoxPayloadIn theInput,
96 ResourceMap resourceMap,
98 throws DocumentException, UnauthorizedException {
99 super(serviceName, theInput, resourceMap, uriInfo);
100 setOutput(new PoxPayloadOut(serviceName));
104 * @see org.collectionspace.services.common.context.MultipartServiceContext#getInputPart(java.lang.String, java.lang.Class)
108 public Object getInputPart(String label, Class clazz) throws IOException {
109 return getInputPart(label);
113 * @see org.collectionspace.services.common.context.MultipartServiceContext#getInputPart(java.lang.String, java.lang.Class)
116 public Object getInputPart(String label) throws IOException {
117 Object result = null;
118 PayloadInputPart payloadInputPart = getInput().getPart(label);
119 if (payloadInputPart != null) {
120 result = payloadInputPart.getBody();
126 * @see org.collectionspace.services.common.context.MultipartServiceContext#getInputPartAsString(java.lang.String)
129 public String getInputPartAsString(String label) throws IOException {
130 String result = null;
131 PayloadInputPart part = getInput().getPart(label);
133 Element element = part.asElement();
134 if (element != null) {
135 result = element.asXML();
142 * @see org.collectionspace.services.common.context.MultipartServiceContext#getInputPartAsStream(java.lang.String)
145 public InputStream getInputPartAsStream(String label) throws IOException {
146 InputStream result = null;
147 String part = getInputPartAsString(label);
149 result = new ByteArrayInputStream(part.getBytes());
155 * @see org.collectionspace.services.common.context.MultipartServiceContext#addOutputPart(java.lang.String, org.w3c.dom.Document, java.lang.String)
158 public void addOutputPart(String label, Element element, String contentType) throws Exception {
159 PayloadOutputPart part = getOutput().addPart(label, element);
160 if (logger.isTraceEnabled() == true) {
161 logger.trace("Adding part:" + label +
162 " to " + getOutput().getName() + " document.");
167 public void addOutputPart(PayloadOutputPart outputPart) throws Exception {
168 PayloadOutputPart part = getOutput().addPart(outputPart);
169 if (logger.isTraceEnabled() == true) {
170 logger.trace("Adding part:" + part.getLabel() +
171 " to " + getOutput().getName() + " document.");
176 * @see org.collectionspace.services.common.context.RemoteServiceContextImpl#getLocalContext(java.lang.String)
179 public ServiceContext getLocalContext(String localContextClassName) throws Exception {
180 ClassLoader cloader = Thread.currentThread().getContextClassLoader();
181 Class ctxClass = cloader.loadClass(localContextClassName);
182 if (!ServiceContext.class.isAssignableFrom(ctxClass)) {
183 throw new IllegalArgumentException("getLocalContext requires "
184 + " implementation of " + ServiceContext.class.getName());
187 Constructor ctor = ctxClass.getConstructor(java.lang.String.class);
188 ServiceContext ctx = (ServiceContext) ctor.newInstance(getServiceName());
193 * @see org.collectionspace.services.common.context.ServiceContext#getRepositoryWorkspaceName()
196 public String getRepositoryWorkspaceName() {
197 String result = repositoryWorkspaceName;
198 //service name is workspace name by convention
199 if (result == null) {
200 result = serviceBinding.getName();
206 public void setRespositoryWorkspaceName(String workspaceName) {
207 repositoryWorkspaceName = workspaceName;