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 public MultipartServiceContextImpl(String serviceName)
60 throws DocumentException, UnauthorizedException {
61 super(serviceName, null);
62 setOutput(new PoxPayloadOut(serviceName));
66 * Instantiates a new multipart service context impl.
68 * @param serviceName the service name
70 * @throws UnauthorizedException the unauthorized exception
72 protected MultipartServiceContextImpl(String serviceName,
74 throws DocumentException, UnauthorizedException {
75 super(serviceName, uriInfo);
76 setOutput(new PoxPayloadOut(serviceName));
80 * Instantiates a new multipart service context impl.
82 * @param serviceName the service name
84 * @throws UnauthorizedException the unauthorized exception
86 protected MultipartServiceContextImpl(String serviceName, PoxPayloadIn theInput, UriInfo uriInfo)
87 throws DocumentException, UnauthorizedException {
88 super(serviceName, theInput, uriInfo);
89 setOutput(new PoxPayloadOut(serviceName));
93 * Instantiates a new multipart service context impl.
95 * @param serviceName the service name
96 * @param queryParams the query params
98 * @throws UnauthorizedException the unauthorized exception
100 protected MultipartServiceContextImpl(
102 PoxPayloadIn theInput,
103 ResourceMap resourceMap,
105 throws DocumentException, UnauthorizedException {
106 super(serviceName, theInput, resourceMap, uriInfo);
107 setOutput(new PoxPayloadOut(serviceName));
111 * @see org.collectionspace.services.common.context.MultipartServiceContext#getInputPart(java.lang.String, java.lang.Class)
115 public Object getInputPart(String label, Class clazz) throws IOException {
116 return getInputPart(label);
120 * @see org.collectionspace.services.common.context.MultipartServiceContext#getInputPart(java.lang.String, java.lang.Class)
123 public Object getInputPart(String label) throws IOException {
124 Object result = null;
125 PayloadInputPart payloadInputPart = getInput().getPart(label);
126 if (payloadInputPart != null) {
127 result = payloadInputPart.getBody();
133 * @see org.collectionspace.services.common.context.MultipartServiceContext#getInputPartAsString(java.lang.String)
136 public String getInputPartAsString(String label) throws IOException {
137 String result = null;
138 PayloadInputPart part = getInput().getPart(label);
140 Element element = part.asElement();
141 if (element != null) {
142 result = element.asXML();
149 * @see org.collectionspace.services.common.context.MultipartServiceContext#getInputPartAsStream(java.lang.String)
152 public InputStream getInputPartAsStream(String label) throws IOException {
153 InputStream result = null;
154 String part = getInputPartAsString(label);
156 result = new ByteArrayInputStream(part.getBytes());
162 * @see org.collectionspace.services.common.context.MultipartServiceContext#addOutputPart(java.lang.String, org.w3c.dom.Document, java.lang.String)
165 public void addOutputPart(String label, Element element, String contentType) throws Exception {
166 PayloadOutputPart part = getOutput().addPart(label, element);
167 if (logger.isTraceEnabled() == true) {
168 logger.trace("Adding part:" + label +
169 " to " + getOutput().getName() + " document.");
174 public void addOutputPart(PayloadOutputPart outputPart) throws Exception {
175 PayloadOutputPart part = getOutput().addPart(outputPart);
176 if (logger.isTraceEnabled() == true) {
177 logger.trace("Adding part:" + part.getLabel() +
178 " to " + getOutput().getName() + " document.");
183 * @see org.collectionspace.services.common.context.RemoteServiceContextImpl#getLocalContext(java.lang.String)
186 public ServiceContext getLocalContext(String localContextClassName) throws Exception {
187 ClassLoader cloader = Thread.currentThread().getContextClassLoader();
188 Class ctxClass = cloader.loadClass(localContextClassName);
189 if (!ServiceContext.class.isAssignableFrom(ctxClass)) {
190 throw new IllegalArgumentException("getLocalContext requires "
191 + " implementation of " + ServiceContext.class.getName());
194 Constructor ctor = ctxClass.getConstructor(java.lang.String.class);
195 ServiceContext ctx = (ServiceContext) ctor.newInstance(getServiceName());
200 * @see org.collectionspace.services.common.context.ServiceContext#getRepositoryWorkspaceName()
203 public String getRepositoryWorkspaceName() {
204 String result = repositoryWorkspaceName;
205 //service name is workspace name by convention
206 if (result == null) {
207 result = serviceBinding.getName();
213 public void setRespositoryWorkspaceName(String workspaceName) {
214 repositoryWorkspaceName = workspaceName;