]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
1faa6097c4c8fff7460f813d5716bbb9cc2c2f21
[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.MultivaluedMap;
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 //import org.jboss.resteasy.plugins.providers.multipart.InputPart;
41 //import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
42 //import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
43 //import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47 /**
48  * MultipartServiceContextImpl takes Multipart Input/Output
49  *
50  * $LastChangedRevision: $
51  * $LastChangedDate: $
52  */
53 public class MultipartServiceContextImpl
54         extends RemoteServiceContextImpl<PoxPayloadIn, PoxPayloadOut>
55         implements MultipartServiceContext {
56
57     /** The logger. */
58     final Logger logger = LoggerFactory.getLogger(MultipartServiceContextImpl.class);
59         private String repositoryWorkspaceName;
60
61     /**
62      * Instantiates a new multipart service context impl.
63      * 
64      * @param serviceName the service name
65      * 
66      * @throws UnauthorizedException the unauthorized exception
67      */
68     protected MultipartServiceContextImpl(String serviceName)
69                 throws DocumentException, UnauthorizedException {
70         super(serviceName);
71         setOutput(new PoxPayloadOut(serviceName));
72     }
73     
74     /**
75      * Instantiates a new multipart service context impl.
76      * 
77      * @param serviceName the service name
78      * 
79      * @throws UnauthorizedException the unauthorized exception
80      */
81     protected MultipartServiceContextImpl(String serviceName, PoxPayloadIn theInput)
82                 throws DocumentException, UnauthorizedException {
83         super(serviceName, theInput);
84         setOutput(new PoxPayloadOut(serviceName));
85     }
86
87     /**
88      * Instantiates a new multipart service context impl.
89      * 
90      * @param serviceName the service name
91      * @param queryParams the query params
92      * 
93      * @throws UnauthorizedException the unauthorized exception
94      */
95     protected MultipartServiceContextImpl(
96                 String serviceName,
97                 PoxPayloadIn theInput,
98                 ResourceMap resourceMap,
99                 MultivaluedMap<String, String> queryParams) 
100                         throws DocumentException, UnauthorizedException {
101         super(serviceName, theInput, resourceMap, queryParams);
102         setOutput(new PoxPayloadOut(serviceName));
103     }
104
105     /* (non-Javadoc)
106      * @see org.collectionspace.services.common.context.MultipartServiceContext#getInputPart(java.lang.String, java.lang.Class)
107      */
108     @Override
109     @Deprecated
110     public Object getInputPart(String label, Class clazz) throws IOException {
111         return getInputPart(label);
112                     }
113     
114     /* (non-Javadoc)
115      * @see org.collectionspace.services.common.context.MultipartServiceContext#getInputPart(java.lang.String, java.lang.Class)
116      */
117     @Override
118     public Object getInputPart(String label) throws IOException {
119         Object result = null;
120         PayloadInputPart payloadInputPart = getInput().getPart(label);
121         if (payloadInputPart != null) {
122                 result = payloadInputPart.getBody();
123         }
124         return result;
125     }
126     
127     /* (non-Javadoc)
128      * @see org.collectionspace.services.common.context.MultipartServiceContext#getInputPartAsString(java.lang.String)
129      */
130     @Override
131     public String getInputPartAsString(String label) throws IOException {
132         String result = null;
133         PayloadInputPart part = getInput().getPart(label);
134         if (part != null) {
135                 Element element = part.asElement();
136                 if (element != null) {
137                         result = element.asXML();
138         }
139     }
140         return result;
141     }
142
143     /* (non-Javadoc)
144      * @see org.collectionspace.services.common.context.MultipartServiceContext#getInputPartAsStream(java.lang.String)
145      */
146     @Override
147     public InputStream getInputPartAsStream(String label) throws IOException {
148         InputStream result = null;
149         String part = getInputPartAsString(label);
150         if (part != null) {
151                 result = new ByteArrayInputStream(part.getBytes());
152         }
153         return result;
154     }
155
156     /* (non-Javadoc)
157      * @see org.collectionspace.services.common.context.MultipartServiceContext#addOutputPart(java.lang.String, org.w3c.dom.Document, java.lang.String)
158      */
159     @Override
160     public void addOutputPart(String label, Element element, String contentType) throws Exception {
161             PayloadOutputPart part = getOutput().addPart(label, element);
162             if (logger.isTraceEnabled() == true) {
163                 logger.trace("Adding part:" + label +
164                                 " to " + getOutput().getName() + " document.");
165             }
166     }
167
168     @Override
169     public void addOutputPart(PayloadOutputPart outputPart) throws Exception {
170             PayloadOutputPart part = getOutput().addPart(outputPart);
171             if (logger.isTraceEnabled() == true) {
172                 logger.trace("Adding part:" + part.getLabel() +
173                                 " to " + getOutput().getName() + " document.");
174             }
175     }
176
177     /* (non-Javadoc)
178      * @see org.collectionspace.services.common.context.RemoteServiceContextImpl#getLocalContext(java.lang.String)
179      */
180     @Override
181     public ServiceContext getLocalContext(String localContextClassName) throws Exception {
182         ClassLoader cloader = Thread.currentThread().getContextClassLoader();
183         Class ctxClass = cloader.loadClass(localContextClassName);
184         if (!ServiceContext.class.isAssignableFrom(ctxClass)) {
185             throw new IllegalArgumentException("getLocalContext requires "
186                     + " implementation of " + ServiceContext.class.getName());
187         }
188
189         Constructor ctor = ctxClass.getConstructor(java.lang.String.class);
190         ServiceContext ctx = (ServiceContext) ctor.newInstance(getServiceName());
191         return ctx;
192     }
193     
194     /* (non-Javadoc)
195      * @see org.collectionspace.services.common.context.ServiceContext#getRepositoryWorkspaceName()
196      */
197     @Override
198     public String getRepositoryWorkspaceName() {
199         String result = repositoryWorkspaceName;
200         //service name is workspace name by convention
201         if (result == null) {
202                 result = serviceBinding.getName();
203         }
204         return result;
205     }
206     
207     @Override
208     public void setRespositoryWorkspaceName(String workspaceName) {
209         repositoryWorkspaceName = workspaceName;
210     }
211 }