]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
d760ecbf6a7787990a5376263f7b370dbabdde07
[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.ByteArrayOutputStream;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.lang.reflect.Constructor;
31 import javax.ws.rs.core.MediaType;
32 import javax.ws.rs.core.MultivaluedMap;
33
34 import org.collectionspace.services.common.document.DocumentUtils;
35 import org.collectionspace.services.common.security.UnauthorizedException;
36 import org.jboss.resteasy.plugins.providers.multipart.InputPart;
37 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
38 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
39 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.w3c.dom.Document;
43
44 /**
45  * MultipartServiceContextImpl takes Multipart Input/Output
46  *
47  * $LastChangedRevision: $
48  * $LastChangedDate: $
49  */
50 public class MultipartServiceContextImpl
51         extends RemoteServiceContextImpl<MultipartInput, MultipartOutput>
52         implements MultipartServiceContext {
53
54     /** The logger. */
55     final Logger logger = LoggerFactory.getLogger(MultipartServiceContextImpl.class);
56
57     /**
58      * Instantiates a new multipart service context impl.
59      * 
60      * @param serviceName the service name
61      * 
62      * @throws UnauthorizedException the unauthorized exception
63      */
64     protected MultipartServiceContextImpl(String serviceName)
65                         throws UnauthorizedException {
66         super(serviceName);
67         setOutput(new MultipartOutput());
68     }
69     
70     /**
71      * Instantiates a new multipart service context impl.
72      * 
73      * @param serviceName the service name
74      * 
75      * @throws UnauthorizedException the unauthorized exception
76      */
77     protected MultipartServiceContextImpl(String serviceName, MultipartInput theInput)
78                 throws UnauthorizedException {
79         super(serviceName, theInput);
80         setOutput(new MultipartOutput());
81     }
82
83     /**
84      * Instantiates a new multipart service context impl.
85      * 
86      * @param serviceName the service name
87      * @param queryParams the query params
88      * 
89      * @throws UnauthorizedException the unauthorized exception
90      */
91     protected MultipartServiceContextImpl(String serviceName,
92                 MultipartInput theInput,
93                 MultivaluedMap<String, String> queryParams) throws UnauthorizedException {
94         super(serviceName, theInput, queryParams);
95         setOutput(new MultipartOutput());
96     }
97
98     /**
99      * Gets the input part.
100      * 
101      * @param label the label
102      * 
103      * @return the input part
104      * 
105      * @throws IOException Signals that an I/O exception has occurred.
106      */
107     private InputPart getInputPart(String label) throws IOException {
108         if (getInput() != null) {
109             MultipartInput fdip = getInput();
110
111             for (InputPart part : fdip.getParts()) {
112                 String partLabel = part.getHeaders().getFirst("label");
113                 if (label.equalsIgnoreCase(partLabel)) {
114                     if (logger.isTraceEnabled()) {
115                         logger.trace("getInputPart found part with label=" + partLabel
116                                 + "\npayload=" + part.getBodyAsString());
117                     }
118                     return part;
119                 }
120             }
121         }
122         return null;
123     }
124
125     /* (non-Javadoc)
126      * @see org.collectionspace.services.common.context.MultipartServiceContext#getInputPart(java.lang.String, java.lang.Class)
127      */
128     @Override
129     public Object getInputPart(String label, Class clazz) throws IOException {
130         Object obj = null;
131         InputPart part = getInputPart(label);
132         if (part != null) {
133             obj = part.getBody(clazz, null);
134         }
135         return obj;
136     }
137
138     /* (non-Javadoc)
139      * @see org.collectionspace.services.common.context.MultipartServiceContext#getInputPartAsString(java.lang.String)
140      */
141     @Override
142     public String getInputPartAsString(String label) throws IOException {
143         InputPart part = getInputPart(label);
144         if (part != null) {
145             return part.getBodyAsString();
146         }
147         return null;
148     }
149
150     /* (non-Javadoc)
151      * @see org.collectionspace.services.common.context.MultipartServiceContext#getInputPartAsStream(java.lang.String)
152      */
153     @Override
154     public InputStream getInputPartAsStream(String label) throws IOException {
155         InputPart part = getInputPart(label);
156         if (part != null) {
157             return new ByteArrayInputStream(part.getBodyAsString().getBytes());
158         }
159         return null;
160     }
161
162     /* (non-Javadoc)
163      * @see org.collectionspace.services.common.context.MultipartServiceContext#addOutputPart(java.lang.String, org.w3c.dom.Document, java.lang.String)
164      */
165     @Override
166     public void addOutputPart(String label, Document doc, String contentType) throws Exception {
167         ByteArrayOutputStream baos = new ByteArrayOutputStream();
168         try {
169             DocumentUtils.writeDocument(doc, baos);
170             baos.close();
171             OutputPart part = getOutput().addPart(new String(baos.toByteArray()),
172                     MediaType.valueOf(contentType));
173             part.getHeaders().add("label", label);
174         } finally {
175             if (baos != null) {
176                 try {
177                     baos.close();
178                 } catch (Exception e) {
179                 }
180             }
181         }
182     }
183
184     /* (non-Javadoc)
185      * @see org.collectionspace.services.common.context.RemoteServiceContextImpl#getLocalContext(java.lang.String)
186      */
187     @Override
188     public ServiceContext getLocalContext(String localContextClassName) throws Exception {
189         ClassLoader cloader = Thread.currentThread().getContextClassLoader();
190         Class ctxClass = cloader.loadClass(localContextClassName);
191         if (!ServiceContext.class.isAssignableFrom(ctxClass)) {
192             throw new IllegalArgumentException("getLocalContext requires "
193                     + " implementation of " + ServiceContext.class.getName());
194         }
195
196         Constructor ctor = ctxClass.getConstructor(java.lang.String.class);
197         ServiceContext ctx = (ServiceContext) ctor.newInstance(getServiceName());
198         return ctx;
199     }
200 }