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