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