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;
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.CollectionSpaceResource;
38 import org.collectionspace.services.common.ResourceMap;
39 import org.collectionspace.services.common.security.UnauthorizedException;
40 import org.dom4j.DocumentException;
41 import org.dom4j.Element;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
46 * MultipartServiceContextImpl takes Multipart Input/Output
48 * $LastChangedRevision: $
51 public class MultipartServiceContextImpl
52 extends RemoteServiceContextImpl<PoxPayloadIn, PoxPayloadOut>
53 implements MultipartServiceContext {
56 final Logger logger = LoggerFactory.getLogger(MultipartServiceContextImpl.class);
57 private String repositoryWorkspaceName;
60 public MultipartServiceContextImpl(String serviceName)
61 throws DocumentException, UnauthorizedException {
62 super(serviceName, null);
63 setOutput(new PoxPayloadOut(serviceName));
67 * Instantiates a new multipart service context impl.
69 * @param serviceName the service name
71 * @throws UnauthorizedException the unauthorized exception
73 protected MultipartServiceContextImpl(String serviceName,
75 throws DocumentException, UnauthorizedException {
76 super(serviceName, uriInfo);
77 setOutput(new PoxPayloadOut(serviceName));
81 * Instantiates a new multipart service context impl.
83 * @param serviceName the service name
85 * @throws UnauthorizedException the unauthorized exception
87 protected MultipartServiceContextImpl(String serviceName, PoxPayloadIn theInput, UriInfo uriInfo)
88 throws DocumentException, UnauthorizedException {
89 super(serviceName, theInput, uriInfo);
90 setOutput(new PoxPayloadOut(serviceName));
94 * Instantiates a new multipart service context impl.
96 * @param serviceName the service name
97 * @param queryParams the query params
99 * @throws UnauthorizedException the unauthorized exception
101 protected MultipartServiceContextImpl(
103 PoxPayloadIn theInput,
104 ResourceMap resourceMap,
106 throws DocumentException, UnauthorizedException {
107 super(serviceName, theInput, resourceMap, uriInfo);
108 setOutput(new PoxPayloadOut(serviceName));
112 * @see org.collectionspace.services.common.context.MultipartServiceContext#getInputPart(java.lang.String, java.lang.Class)
116 public Object getInputPart(String label, Class clazz) throws IOException {
117 return getInputPart(label);
121 * @see org.collectionspace.services.common.context.MultipartServiceContext#getInputPart(java.lang.String, java.lang.Class)
124 public Object getInputPart(String label) throws IOException {
125 Object result = null;
126 PayloadInputPart payloadInputPart = getInput().getPart(label);
127 if (payloadInputPart != null) {
128 result = payloadInputPart.getBody();
134 * @see org.collectionspace.services.common.context.MultipartServiceContext#getInputPartAsString(java.lang.String)
137 public String getInputPartAsString(String label) throws IOException {
138 String result = null;
139 PayloadInputPart part = getInput().getPart(label);
141 Element element = part.asElement();
142 if (element != null) {
143 result = element.asXML();
150 * @see org.collectionspace.services.common.context.MultipartServiceContext#getInputPartAsStream(java.lang.String)
153 public InputStream getInputPartAsStream(String label) throws IOException {
154 InputStream result = null;
155 String part = getInputPartAsString(label);
157 result = new ByteArrayInputStream(part.getBytes());
163 * @see org.collectionspace.services.common.context.MultipartServiceContext#addOutputPart(java.lang.String, org.w3c.dom.Document, java.lang.String)
166 public void addOutputPart(String label, Element element, String contentType) throws Exception {
167 PayloadOutputPart part = getOutput().addPart(label, element);
168 if (logger.isTraceEnabled() == true) {
169 logger.trace("Adding part:" + label +
170 " to " + getOutput().getName() + " document.");
175 public void addOutputPart(PayloadOutputPart outputPart) throws Exception {
176 PayloadOutputPart part = getOutput().addPart(outputPart);
177 if (logger.isTraceEnabled() == true) {
178 logger.trace("Adding part:" + part.getLabel() +
179 " to " + getOutput().getName() + " document.");
184 public CollectionSpaceResource<PoxPayloadIn, PoxPayloadOut> getResource() throws Exception {
185 CollectionSpaceResource<PoxPayloadIn, PoxPayloadOut> result = null;
187 ResourceMap resourceMap = getResourceMap();
188 String resourceName = getClient().getServiceName();
189 result = (CollectionSpaceResource<PoxPayloadIn, PoxPayloadOut>) resourceMap.get(resourceName);
195 public CollectionSpaceResource<PoxPayloadIn, PoxPayloadOut> getResource(String serviceName) throws Exception {
196 CollectionSpaceResource<PoxPayloadIn, PoxPayloadOut> result = null;
198 ResourceMap resourceMap = getResourceMap();
199 String resourceName = serviceName;
200 result = (CollectionSpaceResource<PoxPayloadIn, PoxPayloadOut>) resourceMap.get(resourceName);
206 * @see org.collectionspace.services.common.context.RemoteServiceContextImpl#getLocalContext(java.lang.String)
209 public ServiceContext getLocalContext(String localContextClassName) throws Exception {
210 ClassLoader cloader = Thread.currentThread().getContextClassLoader();
211 Class ctxClass = cloader.loadClass(localContextClassName);
212 if (!ServiceContext.class.isAssignableFrom(ctxClass)) {
213 throw new IllegalArgumentException("getLocalContext requires "
214 + " implementation of " + ServiceContext.class.getName());
217 Constructor ctor = ctxClass.getConstructor(java.lang.String.class);
218 ServiceContext ctx = (ServiceContext) ctor.newInstance(getServiceName());
223 * @see org.collectionspace.services.common.context.ServiceContext#getRepositoryWorkspaceName()
226 public String getRepositoryWorkspaceName() {
227 String result = repositoryWorkspaceName;
228 //service name is workspace name by convention
229 if (result == null) {
230 result = serviceBinding.getName();
236 public void setRespositoryWorkspaceName(String workspaceName) {
237 repositoryWorkspaceName = workspaceName;