]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
97322e405ad729a0b7251ecb77e012e241afdd30
[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 import javax.ws.rs.core.UriInfo;
31
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;
40
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 /**
45  * MultipartServiceContextImpl takes Multipart Input/Output
46  *
47  * $LastChangedRevision: $
48  * $LastChangedDate: $
49  */
50 public class MultipartServiceContextImpl
51         extends RemoteServiceContextImpl<PoxPayloadIn, PoxPayloadOut>
52         implements MultipartServiceContext {
53
54     /** The logger. */
55     final Logger logger = LoggerFactory.getLogger(MultipartServiceContextImpl.class);
56         private String repositoryWorkspaceName;
57
58         
59     public MultipartServiceContextImpl(String serviceName)
60                 throws DocumentException, UnauthorizedException {
61         super(serviceName, null);
62         setOutput(new PoxPayloadOut(serviceName));
63     }
64
65     /**
66      * Instantiates a new multipart service context impl.
67      * 
68      * @param serviceName the service name
69      * 
70      * @throws UnauthorizedException the unauthorized exception
71      */
72     protected MultipartServiceContextImpl(String serviceName,
73                 UriInfo uriInfo)
74                 throws DocumentException, UnauthorizedException {
75         super(serviceName, uriInfo);
76         setOutput(new PoxPayloadOut(serviceName));
77     }
78     
79     /**
80      * Instantiates a new multipart service context impl.
81      * 
82      * @param serviceName the service name
83      * 
84      * @throws UnauthorizedException the unauthorized exception
85      */
86     protected MultipartServiceContextImpl(String serviceName, PoxPayloadIn theInput, UriInfo uriInfo)
87                 throws DocumentException, UnauthorizedException {
88         super(serviceName, theInput, uriInfo);
89         setOutput(new PoxPayloadOut(serviceName));
90     }
91
92     /**
93      * Instantiates a new multipart service context impl.
94      * 
95      * @param serviceName the service name
96      * @param queryParams the query params
97      * 
98      * @throws UnauthorizedException the unauthorized exception
99      */
100     protected MultipartServiceContextImpl(
101                 String serviceName,
102                 PoxPayloadIn theInput,
103                 ResourceMap resourceMap,
104                 UriInfo uriInfo) 
105                         throws DocumentException, UnauthorizedException {
106         super(serviceName, theInput, resourceMap, uriInfo);
107         setOutput(new PoxPayloadOut(serviceName));
108     }
109
110     /* (non-Javadoc)
111      * @see org.collectionspace.services.common.context.MultipartServiceContext#getInputPart(java.lang.String, java.lang.Class)
112      */
113     @Override
114     @Deprecated
115     public Object getInputPart(String label, Class clazz) throws IOException {
116         return getInputPart(label);
117                     }
118     
119     /* (non-Javadoc)
120      * @see org.collectionspace.services.common.context.MultipartServiceContext#getInputPart(java.lang.String, java.lang.Class)
121      */
122     @Override
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();
128         }
129         return result;
130     }
131     
132     /* (non-Javadoc)
133      * @see org.collectionspace.services.common.context.MultipartServiceContext#getInputPartAsString(java.lang.String)
134      */
135     @Override
136     public String getInputPartAsString(String label) throws IOException {
137         String result = null;
138         PayloadInputPart part = getInput().getPart(label);
139         if (part != null) {
140                 Element element = part.asElement();
141                 if (element != null) {
142                         result = element.asXML();
143         }
144     }
145         return result;
146     }
147
148     /* (non-Javadoc)
149      * @see org.collectionspace.services.common.context.MultipartServiceContext#getInputPartAsStream(java.lang.String)
150      */
151     @Override
152     public InputStream getInputPartAsStream(String label) throws IOException {
153         InputStream result = null;
154         String part = getInputPartAsString(label);
155         if (part != null) {
156                 result = new ByteArrayInputStream(part.getBytes());
157         }
158         return result;
159     }
160
161     /* (non-Javadoc)
162      * @see org.collectionspace.services.common.context.MultipartServiceContext#addOutputPart(java.lang.String, org.w3c.dom.Document, java.lang.String)
163      */
164     @Override
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.");
170             }
171     }
172
173     @Override
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.");
179             }
180     }
181
182     /* (non-Javadoc)
183      * @see org.collectionspace.services.common.context.RemoteServiceContextImpl#getLocalContext(java.lang.String)
184      */
185     @Override
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());
192         }
193
194         Constructor ctor = ctxClass.getConstructor(java.lang.String.class);
195         ServiceContext ctx = (ServiceContext) ctor.newInstance(getServiceName());
196         return ctx;
197     }
198     
199     /* (non-Javadoc)
200      * @see org.collectionspace.services.common.context.ServiceContext#getRepositoryWorkspaceName()
201      */
202     @Override
203     public String getRepositoryWorkspaceName() {
204         String result = repositoryWorkspaceName;
205         //service name is workspace name by convention
206         if (result == null) {
207                 result = serviceBinding.getName();
208         }
209         return result;
210     }
211     
212     @Override
213     public void setRespositoryWorkspaceName(String workspaceName) {
214         repositoryWorkspaceName = workspaceName;
215     }
216 }