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