]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
7fd703394558bb265b5ab8fb6ecf55969afec9d9
[tmp/jakarta-migration.git] /
1 /**
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:
5
6  *  http://www.collectionspace.org
7  *  http://wiki.collectionspace.org
8
9  *  Copyright 2009 University of California at Berkeley
10
11  *  Licensed under the Educational Community License (ECL), Version 2.0.
12  *  You may not use this file except in compliance with this License.
13
14  *  You may obtain a copy of the ECL 2.0 License at
15
16  *  https://source.collectionspace.org/collection-space/LICENSE.txt
17
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.
23  */
24 package org.collectionspace.services.common.context;
25
26 import java.io.ByteArrayInputStream;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.lang.reflect.Constructor;
30
31 import javax.ws.rs.core.UriInfo;
32
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;
44
45 /**
46  * MultipartServiceContextImpl takes Multipart Input/Output
47  *
48  * $LastChangedRevision: $
49  * $LastChangedDate: $
50  */
51 public class MultipartServiceContextImpl
52         extends RemoteServiceContextImpl<PoxPayloadIn, PoxPayloadOut>
53         implements MultipartServiceContext {
54
55     /** The logger. */
56     final Logger logger = LoggerFactory.getLogger(MultipartServiceContextImpl.class);
57         private String repositoryWorkspaceName;
58
59         
60     public MultipartServiceContextImpl(String serviceName)
61                 throws DocumentException, UnauthorizedException {
62         super(serviceName, null);
63         setOutput(new PoxPayloadOut(serviceName));
64     }
65
66     /**
67      * Instantiates a new multipart service context impl.
68      * 
69      * @param serviceName the service name
70      * 
71      * @throws UnauthorizedException the unauthorized exception
72      */
73     protected MultipartServiceContextImpl(String serviceName,
74                 UriInfo uriInfo)
75                 throws DocumentException, UnauthorizedException {
76         super(serviceName, uriInfo);
77         setOutput(new PoxPayloadOut(serviceName));
78     }
79     
80     /**
81      * Instantiates a new multipart service context impl.
82      * 
83      * @param serviceName the service name
84      * 
85      * @throws UnauthorizedException the unauthorized exception
86      */
87     protected MultipartServiceContextImpl(String serviceName, PoxPayloadIn theInput, UriInfo uriInfo)
88                 throws DocumentException, UnauthorizedException {
89         super(serviceName, theInput, uriInfo);
90         setOutput(new PoxPayloadOut(serviceName));
91     }
92
93     /**
94      * Instantiates a new multipart service context impl.
95      * 
96      * @param serviceName the service name
97      * @param queryParams the query params
98      * 
99      * @throws UnauthorizedException the unauthorized exception
100      */
101     protected MultipartServiceContextImpl(
102                 String serviceName,
103                 PoxPayloadIn theInput,
104                 ResourceMap resourceMap,
105                 UriInfo uriInfo) 
106                         throws DocumentException, UnauthorizedException {
107         super(serviceName, theInput, resourceMap, uriInfo);
108         setOutput(new PoxPayloadOut(serviceName));
109     }
110
111     /* (non-Javadoc)
112      * @see org.collectionspace.services.common.context.MultipartServiceContext#getInputPart(java.lang.String, java.lang.Class)
113      */
114     @Override
115     @Deprecated
116     public Object getInputPart(String label, Class clazz) throws IOException {
117         return getInputPart(label);
118                     }
119     
120     /* (non-Javadoc)
121      * @see org.collectionspace.services.common.context.MultipartServiceContext#getInputPart(java.lang.String, java.lang.Class)
122      */
123     @Override
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();
129         }
130         return result;
131     }
132     
133     /* (non-Javadoc)
134      * @see org.collectionspace.services.common.context.MultipartServiceContext#getInputPartAsString(java.lang.String)
135      */
136     @Override
137     public String getInputPartAsString(String label) throws IOException {
138         String result = null;
139         PayloadInputPart part = getInput().getPart(label);
140         if (part != null) {
141                 Element element = part.asElement();
142                 if (element != null) {
143                         result = element.asXML();
144         }
145     }
146         return result;
147     }
148
149     /* (non-Javadoc)
150      * @see org.collectionspace.services.common.context.MultipartServiceContext#getInputPartAsStream(java.lang.String)
151      */
152     @Override
153     public InputStream getInputPartAsStream(String label) throws IOException {
154         InputStream result = null;
155         String part = getInputPartAsString(label);
156         if (part != null) {
157                 result = new ByteArrayInputStream(part.getBytes());
158         }
159         return result;
160     }
161
162     /* (non-Javadoc)
163      * @see org.collectionspace.services.common.context.MultipartServiceContext#addOutputPart(java.lang.String, org.w3c.dom.Document, java.lang.String)
164      */
165     @Override
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.");
171             }
172     }
173
174     @Override
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.");
180             }
181     }
182
183     @Override
184     public CollectionSpaceResource<PoxPayloadIn, PoxPayloadOut> getResource() throws Exception {
185         CollectionSpaceResource<PoxPayloadIn, PoxPayloadOut> result = null;
186         
187         ResourceMap resourceMap = getResourceMap();
188         String resourceName = getClient().getServiceName();
189         result = (CollectionSpaceResource<PoxPayloadIn, PoxPayloadOut>) resourceMap.get(resourceName);
190         
191         return result;
192     }
193     
194     @Override
195     public CollectionSpaceResource<PoxPayloadIn, PoxPayloadOut> getResource(String serviceName) throws Exception {
196         CollectionSpaceResource<PoxPayloadIn, PoxPayloadOut> result = null;
197         
198         ResourceMap resourceMap = getResourceMap();
199         String resourceName = serviceName;
200         result = (CollectionSpaceResource<PoxPayloadIn, PoxPayloadOut>) resourceMap.get(resourceName);
201         
202         return result;
203     }    
204
205     /* (non-Javadoc)
206      * @see org.collectionspace.services.common.context.RemoteServiceContextImpl#getLocalContext(java.lang.String)
207      */
208     @Override
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());
215         }
216
217         Constructor ctor = ctxClass.getConstructor(java.lang.String.class);
218         ServiceContext ctx = (ServiceContext) ctor.newInstance(getServiceName());
219         return ctx;
220     }
221     
222     /* (non-Javadoc)
223      * @see org.collectionspace.services.common.context.ServiceContext#getRepositoryWorkspaceName()
224      */
225     @Override
226     public String getRepositoryWorkspaceName() {
227         String result = repositoryWorkspaceName;
228         //service name is workspace name by convention
229         if (result == null) {
230                 result = serviceBinding.getName();
231         }
232         return result;
233     }
234     
235     @Override
236     public void setRespositoryWorkspaceName(String workspaceName) {
237         repositoryWorkspaceName = workspaceName;
238     }
239 }