]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
cc068a2832c5970b83036e72be5ec3aec25bbd2c
[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
59     /**
60      * Instantiates a new multipart service context impl.
61      * 
62      * @param serviceName the service name
63      * 
64      * @throws UnauthorizedException the unauthorized exception
65      */
66     protected MultipartServiceContextImpl(String serviceName)
67                 throws DocumentException, UnauthorizedException {
68         super(serviceName);
69         setOutput(new PoxPayloadOut(serviceName));
70     }
71     
72     /**
73      * Instantiates a new multipart service context impl.
74      * 
75      * @param serviceName the service name
76      * 
77      * @throws UnauthorizedException the unauthorized exception
78      */
79     protected MultipartServiceContextImpl(String serviceName, PoxPayloadIn theInput)
80                 throws DocumentException, UnauthorizedException {
81         super(serviceName, theInput);
82         setOutput(new PoxPayloadOut(serviceName));
83     }
84
85     /**
86      * Instantiates a new multipart service context impl.
87      * 
88      * @param serviceName the service name
89      * @param queryParams the query params
90      * 
91      * @throws UnauthorizedException the unauthorized exception
92      */
93     protected MultipartServiceContextImpl(String serviceName,
94                 PoxPayloadIn theInput,
95                 MultivaluedMap<String, String> queryParams) 
96                         throws DocumentException, UnauthorizedException {
97         super(serviceName, theInput, queryParams);
98         setOutput(new PoxPayloadOut(serviceName));
99     }
100
101     /* (non-Javadoc)
102      * @see org.collectionspace.services.common.context.MultipartServiceContext#getInputPart(java.lang.String, java.lang.Class)
103      */
104     @Override
105     @Deprecated
106     public Object getInputPart(String label, Class clazz) throws IOException {
107         return getInputPart(label);
108                     }
109     
110     /* (non-Javadoc)
111      * @see org.collectionspace.services.common.context.MultipartServiceContext#getInputPart(java.lang.String, java.lang.Class)
112      */
113     @Override
114     public Object getInputPart(String label) throws IOException {
115         Object result = null;
116         PayloadInputPart payloadInputPart = getInput().getPart(label);
117         if (payloadInputPart != null) {
118                 result = payloadInputPart.getBody();
119         }
120         return result;
121     }
122     
123     /* (non-Javadoc)
124      * @see org.collectionspace.services.common.context.MultipartServiceContext#getInputPartAsString(java.lang.String)
125      */
126     @Override
127     public String getInputPartAsString(String label) throws IOException {
128         String result = null;
129         PayloadInputPart part = getInput().getPart(label);
130         if (part != null) {
131                 Element element = part.asElement();
132                 if (element != null) {
133                         result = element.asXML();
134         }
135     }
136         return result;
137     }
138
139     /* (non-Javadoc)
140      * @see org.collectionspace.services.common.context.MultipartServiceContext#getInputPartAsStream(java.lang.String)
141      */
142     @Override
143     public InputStream getInputPartAsStream(String label) throws IOException {
144         InputStream result = null;
145         String part = getInputPartAsString(label);
146         if (part != null) {
147                 result = new ByteArrayInputStream(part.getBytes());
148         }
149         return result;
150     }
151
152     /* (non-Javadoc)
153      * @see org.collectionspace.services.common.context.MultipartServiceContext#addOutputPart(java.lang.String, org.w3c.dom.Document, java.lang.String)
154      */
155     @Override
156     public void addOutputPart(String label, Element element, String contentType) throws Exception {
157             PayloadOutputPart part = getOutput().addPart(label, element);
158             if (logger.isTraceEnabled() == true) {
159                 logger.trace("Adding part:" + label +
160                                 " to " + getOutput().getName() + " document.");
161                 }
162             }
163
164     /* (non-Javadoc)
165      * @see org.collectionspace.services.common.context.RemoteServiceContextImpl#getLocalContext(java.lang.String)
166      */
167     @Override
168     public ServiceContext getLocalContext(String localContextClassName) throws Exception {
169         ClassLoader cloader = Thread.currentThread().getContextClassLoader();
170         Class ctxClass = cloader.loadClass(localContextClassName);
171         if (!ServiceContext.class.isAssignableFrom(ctxClass)) {
172             throw new IllegalArgumentException("getLocalContext requires "
173                     + " implementation of " + ServiceContext.class.getName());
174         }
175
176         Constructor ctor = ctxClass.getConstructor(java.lang.String.class);
177         ServiceContext ctx = (ServiceContext) ctor.newInstance(getServiceName());
178         return ctx;
179     }
180 }