|
1 |
/**
|
|
2 |
* '$RCSfile$'
|
|
3 |
* Copyright: 2011 Regents of the University of California and the
|
|
4 |
* National Center for Ecological Analysis and Synthesis
|
|
5 |
*
|
|
6 |
* '$Author$'
|
|
7 |
* '$Date$'
|
|
8 |
*
|
|
9 |
* This program is free software; you can redistribute it and/or modify
|
|
10 |
* it under the terms of the GNU General Public License as published by
|
|
11 |
* the Free Software Foundation; either version 2 of the License, or
|
|
12 |
* (at your option) any later version.
|
|
13 |
*
|
|
14 |
* This program is distributed in the hope that it will be useful,
|
|
15 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
17 |
* GNU General Public License for more details.
|
|
18 |
*
|
|
19 |
* You should have received a copy of the GNU General Public License
|
|
20 |
* along with this program; if not, write to the Free Software
|
|
21 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
22 |
*/
|
|
23 |
package edu.ucsb.nceas.metacat.restservice;
|
|
24 |
|
|
25 |
import java.io.ByteArrayInputStream;
|
|
26 |
import java.io.File;
|
|
27 |
import java.io.FileInputStream;
|
|
28 |
import java.io.IOException;
|
|
29 |
import java.io.InputStream;
|
|
30 |
import java.io.OutputStream;
|
|
31 |
import java.util.Date;
|
|
32 |
import java.util.Hashtable;
|
|
33 |
import java.util.Iterator;
|
|
34 |
import java.util.List;
|
|
35 |
import java.util.Map;
|
|
36 |
import java.util.Timer;
|
|
37 |
|
|
38 |
import javax.servlet.ServletContext;
|
|
39 |
import javax.servlet.http.HttpServletRequest;
|
|
40 |
import javax.servlet.http.HttpServletResponse;
|
|
41 |
|
|
42 |
import org.apache.commons.io.IOUtils;
|
|
43 |
import org.apache.log4j.Logger;
|
|
44 |
import org.dataone.client.ObjectFormatCache;
|
|
45 |
import org.dataone.client.auth.CertificateManager;
|
|
46 |
import org.dataone.mimemultipart.MultipartRequest;
|
|
47 |
import org.dataone.mimemultipart.MultipartRequestResolver;
|
|
48 |
import org.dataone.service.exceptions.BaseException;
|
|
49 |
import org.dataone.service.exceptions.IdentifierNotUnique;
|
|
50 |
import org.dataone.service.exceptions.InsufficientResources;
|
|
51 |
import org.dataone.service.exceptions.InvalidRequest;
|
|
52 |
import org.dataone.service.exceptions.InvalidSystemMetadata;
|
|
53 |
import org.dataone.service.exceptions.InvalidToken;
|
|
54 |
import org.dataone.service.exceptions.NotAuthorized;
|
|
55 |
import org.dataone.service.exceptions.NotFound;
|
|
56 |
import org.dataone.service.exceptions.NotImplemented;
|
|
57 |
import org.dataone.service.exceptions.ServiceFailure;
|
|
58 |
import org.dataone.service.exceptions.UnsupportedType;
|
|
59 |
import org.dataone.service.types.Checksum;
|
|
60 |
import org.dataone.service.types.Event;
|
|
61 |
import org.dataone.service.types.Identifier;
|
|
62 |
import org.dataone.service.types.Log;
|
|
63 |
import org.dataone.service.types.ObjectFormat;
|
|
64 |
import org.dataone.service.types.ObjectFormatIdentifier;
|
|
65 |
import org.dataone.service.types.ObjectFormatList;
|
|
66 |
import org.dataone.service.types.ObjectLocationList;
|
|
67 |
import org.dataone.service.types.Permission;
|
|
68 |
import org.dataone.service.types.Subject;
|
|
69 |
import org.dataone.service.types.SystemMetadata;
|
|
70 |
import org.jibx.runtime.BindingDirectory;
|
|
71 |
import org.jibx.runtime.IBindingFactory;
|
|
72 |
import org.jibx.runtime.IUnmarshallingContext;
|
|
73 |
import org.jibx.runtime.JiBXException;
|
|
74 |
|
|
75 |
import edu.ucsb.nceas.metacat.IdentifierManager;
|
|
76 |
import edu.ucsb.nceas.metacat.MetacatHandler;
|
|
77 |
import edu.ucsb.nceas.metacat.dataone.CNodeService;
|
|
78 |
|
|
79 |
/**
|
|
80 |
* CN REST service implementation handler
|
|
81 |
*
|
|
82 |
* ******************
|
|
83 |
CNCore -- DONE
|
|
84 |
create() - POST /d1/cn/object/PID
|
|
85 |
listFormats() - GET /d1/cn/formats
|
|
86 |
getFormat() - GET /d1/cn/formats/FMTID
|
|
87 |
getLogRecords - GET /d1/cn/log
|
|
88 |
reserveIdentifier() - POST /d1/cn/reserve
|
|
89 |
listNodes() - Not implemented
|
|
90 |
registerSystemMetadata() - POST /d1/meta/PID
|
|
91 |
|
|
92 |
CNRead -- DONE
|
|
93 |
get() - GET /d1/cn/object/PID
|
|
94 |
getSystemMetadata() - GET /d1/cn/meta/PID
|
|
95 |
resolve() - GET /d1/cn/resolve/PID
|
|
96 |
assertRelation() - GET /d1/cn/assertRelation/PID
|
|
97 |
getChecksum() - GET /d1/cn/checksum
|
|
98 |
search() - Not implemented in Metacat
|
|
99 |
|
|
100 |
CNAuthorization
|
|
101 |
setOwner() - PUT /d1/cn/owner/PID
|
|
102 |
isAuthorized() - GET /d1/cn/isAuthorized/PID
|
|
103 |
setAccessPolicy() - POST /d1/cn/accessRules
|
|
104 |
|
|
105 |
CNIdentity - not implemented at all on Metacat
|
|
106 |
|
|
107 |
CNReplication
|
|
108 |
setReplicationStatus() - Not exposed
|
|
109 |
updateReplicationMetadata() - New method?
|
|
110 |
setReplicationPolicy() - Not exposed
|
|
111 |
|
|
112 |
CNRegister -- not implemented at all in Metacat
|
|
113 |
* ******************
|
|
114 |
* @author leinfelder
|
|
115 |
*
|
|
116 |
*/
|
|
117 |
public class CNResourceHandler extends ResourceHandler {
|
|
118 |
|
|
119 |
/** CN-specific operations **/
|
|
120 |
protected static final String RESOURCE_RESERVE = "reserve";
|
|
121 |
protected static final String RESOURCE_FORMATS = "formats"; //remove from super
|
|
122 |
protected static final String RESOURCE_RESOLVE = "resolve";
|
|
123 |
protected static final String RESOURCE_ASSERT_RELATION = "assertRelation";
|
|
124 |
protected static final String RESOURCE_OWNER = "owner";
|
|
125 |
protected static final String RESOURCE_NOTIFY = "notify";
|
|
126 |
protected static final String RESOURCE_META_REPLICATION = "meta/replication";
|
|
127 |
protected static final String RESOURCE_META_POLICY = "meta/policy";
|
|
128 |
|
|
129 |
public CNResourceHandler(ServletContext servletContext,
|
|
130 |
HttpServletRequest request, HttpServletResponse response) {
|
|
131 |
super(servletContext, request, response);
|
|
132 |
logMetacat = Logger.getLogger(CNResourceHandler.class);
|
|
133 |
}
|
|
134 |
|
|
135 |
/**
|
|
136 |
* This function is called from REST APU servlet and handles each request to the servlet
|
|
137 |
*
|
|
138 |
* @param httpVerb (GET, POST, PUT or DELETE)
|
|
139 |
*/
|
|
140 |
public void handle(byte httpVerb) {
|
|
141 |
try {
|
|
142 |
//substring off the "d1/cn/" part of the url
|
|
143 |
String resourcePrefix = RESOURCE_BASE_URL + "/cn/";
|
|
144 |
String resource = request.getServletPath();
|
|
145 |
resource = resource.substring(resource.indexOf(resourcePrefix) + resourcePrefix.length());
|
|
146 |
resource = resource.trim();
|
|
147 |
|
|
148 |
logMetacat.debug("handling verb " + httpVerb + " request with resource '" + resource + "'");
|
|
149 |
boolean status = false;
|
|
150 |
|
|
151 |
// load session from certificate in request
|
|
152 |
// TODO: move to superclass
|
|
153 |
session = CertificateManager.getInstance().getSession(request);
|
|
154 |
|
|
155 |
if (resource != null) {
|
|
156 |
|
|
157 |
params = new Hashtable<String, String[]>();
|
|
158 |
initParams();
|
|
159 |
|
|
160 |
Timer timer = new Timer();
|
|
161 |
handler = new MetacatHandler(timer);
|
|
162 |
|
|
163 |
if (resource.equals(RESOURCE_ACCESS_RULES) && httpVerb == PUT) {
|
|
164 |
logMetacat.debug("Setting access policy");
|
|
165 |
setaccess();
|
|
166 |
status = true;
|
|
167 |
logMetacat.debug("done setting access");
|
|
168 |
|
|
169 |
} else if (resource.equals(RESOURCE_META)) {
|
|
170 |
logMetacat.debug("Using resource: " + RESOURCE_META);
|
|
171 |
String objectId = request.getPathInfo();
|
|
172 |
if (objectId != null && objectId.length() > 1) {
|
|
173 |
objectId = request.getPathInfo().substring(1);
|
|
174 |
}
|
|
175 |
// get
|
|
176 |
if (httpVerb == GET) {
|
|
177 |
getSystemMetadataObject(objectId);
|
|
178 |
status = true;
|
|
179 |
}
|
|
180 |
// post to register system metadata
|
|
181 |
if (httpVerb == POST) {
|
|
182 |
registerSystemMetadata(objectId);
|
|
183 |
status = true;
|
|
184 |
}
|
|
185 |
|
|
186 |
} else if (resource.equals(RESOURCE_RESERVE)) {
|
|
187 |
// reserve the ID (in params)
|
|
188 |
if (httpVerb == POST) {
|
|
189 |
reserve();
|
|
190 |
status = true;
|
|
191 |
}
|
|
192 |
} else if (resource.equals(RESOURCE_ASSERT_RELATION)) {
|
|
193 |
String objectId = request.getPathInfo();
|
|
194 |
if (objectId != null && objectId.length() > 1) {
|
|
195 |
objectId = request.getPathInfo().substring(1);
|
|
196 |
}
|
|
197 |
// reserve the ID (in params)
|
|
198 |
if (httpVerb == GET) {
|
|
199 |
asserRelation(objectId);
|
|
200 |
status = true;
|
|
201 |
}
|
|
202 |
} else if (resource.equals(RESOURCE_RESOLVE)) {
|
|
203 |
String objectId = request.getPathInfo();
|
|
204 |
if (objectId != null && objectId.length() > 1) {
|
|
205 |
objectId = request.getPathInfo().substring(1);
|
|
206 |
}
|
|
207 |
// resolve the object location
|
|
208 |
if (httpVerb == GET) {
|
|
209 |
resolve(objectId);
|
|
210 |
status = true;
|
|
211 |
}
|
|
212 |
} else if (resource.equals(RESOURCE_OWNER)) {
|
|
213 |
String objectId = request.getPathInfo();
|
|
214 |
if (objectId != null && objectId.length() > 1) {
|
|
215 |
objectId = request.getPathInfo().substring(1);
|
|
216 |
}
|
|
217 |
// set the owner
|
|
218 |
if (httpVerb == PUT) {
|
|
219 |
owner(objectId);
|
|
220 |
status = true;
|
|
221 |
}
|
|
222 |
} else if (resource.equals(RESOURCE_OWNER)) {
|
|
223 |
String objectId = request.getPathInfo();
|
|
224 |
if (objectId != null && objectId.length() > 1) {
|
|
225 |
objectId = request.getPathInfo().substring(1);
|
|
226 |
}
|
|
227 |
// set the owner
|
|
228 |
if (httpVerb == PUT) {
|
|
229 |
owner(objectId);
|
|
230 |
status = true;
|
|
231 |
}
|
|
232 |
} else if (resource.equals(RESOURCE_IS_AUTHORIZED)) {
|
|
233 |
String objectId = request.getPathInfo();
|
|
234 |
if (objectId != null && objectId.length() > 1) {
|
|
235 |
objectId = request.getPathInfo().substring(1);
|
|
236 |
}
|
|
237 |
// authorized?
|
|
238 |
if (httpVerb == GET) {
|
|
239 |
isAuthorized(objectId);
|
|
240 |
status = true;
|
|
241 |
}
|
|
242 |
} else if (resource.equals(RESOURCE_OBJECTS)) {
|
|
243 |
logMetacat.debug("Using resource 'object'");
|
|
244 |
logMetacat.debug("D1 Rest: Starting resource processing...");
|
|
245 |
|
|
246 |
String objectId = request.getPathInfo();
|
|
247 |
if (objectId != null && objectId.length() > 1) {
|
|
248 |
objectId = request.getPathInfo().substring(1);
|
|
249 |
}
|
|
250 |
else {
|
|
251 |
objectId = null;
|
|
252 |
}
|
|
253 |
|
|
254 |
logMetacat.debug("objectId: " + objectId);
|
|
255 |
logMetacat.debug("verb:" + httpVerb);
|
|
256 |
|
|
257 |
if (httpVerb == GET) {
|
|
258 |
getObject(objectId);
|
|
259 |
status = true;
|
|
260 |
} else if (httpVerb == POST) {
|
|
261 |
putObject(objectId, FUNCTION_NAME_INSERT);
|
|
262 |
status = true;
|
|
263 |
} else if (httpVerb == PUT) {
|
|
264 |
// TODO: this is not applicable for CN
|
|
265 |
putObject(objectId, FUNCTION_NAME_UPDATE);
|
|
266 |
status = true;
|
|
267 |
}
|
|
268 |
|
|
269 |
} else if (resource.equals(RESOURCE_FORMATS)) {
|
|
270 |
logMetacat.debug("Using resource: " + RESOURCE_FORMATS);
|
|
271 |
|
|
272 |
// check for a format identifier
|
|
273 |
String fmtid = request.getPathInfo();
|
|
274 |
|
|
275 |
if (fmtid != null && fmtid.length() > 1 ) {
|
|
276 |
fmtid = request.getPathInfo().substring(1);
|
|
277 |
} else {
|
|
278 |
fmtid = null;
|
|
279 |
}
|
|
280 |
|
|
281 |
// handle each verb
|
|
282 |
if (httpVerb == GET) {
|
|
283 |
if (fmtid == null) {
|
|
284 |
// list the formats collection
|
|
285 |
listFormats();
|
|
286 |
} else {
|
|
287 |
// get the specified format
|
|
288 |
getFormat(fmtid);
|
|
289 |
}
|
|
290 |
status = true;
|
|
291 |
}
|
|
292 |
|
|
293 |
} else if (resource.equals(RESOURCE_LOG)) {
|
|
294 |
logMetacat.debug("Using resource: " + RESOURCE_LOG);
|
|
295 |
//handle log events
|
|
296 |
if (httpVerb == GET) {
|
|
297 |
getLog();
|
|
298 |
status = true;
|
|
299 |
}
|
|
300 |
|
|
301 |
} else if (resource.equals(RESOURCE_CHECKSUM)) {
|
|
302 |
logMetacat.debug("Using resource: " + RESOURCE_CHECKSUM);
|
|
303 |
//handle checksum requests
|
|
304 |
if (httpVerb == GET) {
|
|
305 |
|
|
306 |
String guid = request.getPathInfo();
|
|
307 |
if (guid != null && guid.length() > 1)
|
|
308 |
guid = request.getPathInfo().substring(1); //trim the slash
|
|
309 |
|
|
310 |
Identifier guidid = new Identifier();
|
|
311 |
guidid.setValue(guid);
|
|
312 |
logMetacat.debug("getting checksum for object " + guid);
|
|
313 |
try {
|
|
314 |
Checksum c = CNodeService.getInstance().getChecksum(session, guidid);
|
|
315 |
logMetacat.debug("got checksum " + c.getValue());
|
|
316 |
response.setStatus(200);
|
|
317 |
logMetacat.debug("serializing response");
|
|
318 |
serializeServiceType(Checksum.class, c, response.getOutputStream());
|
|
319 |
logMetacat.debug("done serializing response.");
|
|
320 |
}
|
|
321 |
catch(NotAuthorized na)
|
|
322 |
{
|
|
323 |
na.setDetail_code("1400");
|
|
324 |
serializeException(na, response.getOutputStream());
|
|
325 |
}
|
|
326 |
catch(NotFound nf)
|
|
327 |
{
|
|
328 |
nf.setDetail_code("1420");
|
|
329 |
serializeException(nf, response.getOutputStream());
|
|
330 |
}
|
|
331 |
catch(InvalidRequest ir)
|
|
332 |
{
|
|
333 |
ir.setDetail_code("1402");
|
|
334 |
serializeException(ir, response.getOutputStream());
|
|
335 |
}
|
|
336 |
catch(ServiceFailure sf)
|
|
337 |
{
|
|
338 |
sf.setDetail_code("1410");
|
|
339 |
serializeException(sf, response.getOutputStream());
|
|
340 |
}
|
|
341 |
catch(InvalidToken it)
|
|
342 |
{
|
|
343 |
it.setDetail_code("1430");
|
|
344 |
serializeException(it, response.getOutputStream());
|
|
345 |
}
|
|
346 |
status = true;
|
|
347 |
}
|
|
348 |
}
|
|
349 |
|
|
350 |
if (!status) {
|
|
351 |
response.setStatus(400);
|
|
352 |
printError("Incorrect parameters!", response);
|
|
353 |
}
|
|
354 |
} else {
|
|
355 |
response.setStatus(400);
|
|
356 |
printError("Incorrect resource!", response);
|
|
357 |
}
|
|
358 |
} catch (BaseException be) {
|
|
359 |
// report Exceptions as clearly as possible
|
|
360 |
OutputStream out = null;
|
|
361 |
try {
|
|
362 |
out = response.getOutputStream();
|
|
363 |
} catch (IOException e) {
|
|
364 |
logMetacat.error("Could not get output stream from response", e);
|
|
365 |
}
|
|
366 |
serializeException(be, out);
|
|
367 |
} catch (Exception e) {
|
|
368 |
logMetacat.error(e.getClass() + ": " + e.getMessage(), e);
|
|
369 |
}
|
|
370 |
}
|
|
371 |
|
|
372 |
/**
|
|
373 |
* get the logs from the CrudService based on passed params. Available
|
|
374 |
* params are token, fromDate, toDate, event. See
|
|
375 |
* http://mule1.dataone.org/ArchitectureDocs/mn_api_crud.html#MN_crud.getLogRecords
|
|
376 |
* for more info
|
|
377 |
*/
|
|
378 |
private void getLog()
|
|
379 |
{
|
|
380 |
OutputStream out = null;
|
|
381 |
try
|
|
382 |
{
|
|
383 |
out = response.getOutputStream();
|
|
384 |
response.setStatus(200);
|
|
385 |
response.setContentType("text/xml");
|
|
386 |
String fromDateS = params.get("fromDate")[0];
|
|
387 |
logMetacat.debug("param fromDateS: " + fromDateS);
|
|
388 |
Date fromDate = null;
|
|
389 |
String toDateS = params.get("toDate")[0];
|
|
390 |
logMetacat.debug("param toDateS: " + toDateS);
|
|
391 |
Date toDate = null;
|
|
392 |
String eventS = params.get("event")[0];
|
|
393 |
Event event = null;
|
|
394 |
if(fromDateS != null)
|
|
395 |
{
|
|
396 |
//fromDate = dateFormat.parse(fromDateS);
|
|
397 |
fromDate = parseDateAndConvertToGMT(fromDateS);
|
|
398 |
}
|
|
399 |
if(toDateS != null)
|
|
400 |
{
|
|
401 |
//toDate = dateFormat.parse(toDateS);
|
|
402 |
toDate = parseDateAndConvertToGMT(toDateS);
|
|
403 |
}
|
|
404 |
if(eventS != null)
|
|
405 |
{
|
|
406 |
event = Event.convert(eventS);
|
|
407 |
}
|
|
408 |
logMetacat.debug("fromDate: " + fromDate + " toDate: " + toDate);
|
|
409 |
|
|
410 |
Integer start = null;
|
|
411 |
Integer count = null;
|
|
412 |
try {
|
|
413 |
start = Integer.parseInt(params.get("start")[0]);
|
|
414 |
} catch (Exception e) {
|
|
415 |
logMetacat.warn("Could not parse start: " + e.getMessage());
|
|
416 |
}
|
|
417 |
try {
|
|
418 |
count = Integer.parseInt(params.get("count")[0]);
|
|
419 |
} catch (Exception e) {
|
|
420 |
logMetacat.warn("Could not count start: " + e.getMessage());
|
|
421 |
}
|
|
422 |
|
|
423 |
logMetacat.debug("calling crudservice.getLogRecords");
|
|
424 |
Log log = CNodeService.getInstance().getLogRecords(session, fromDate, toDate, event, start, count);
|
|
425 |
serializeServiceType(Log.class, log, out);
|
|
426 |
}
|
|
427 |
catch(Exception e)
|
|
428 |
{
|
|
429 |
String msg = "Could not get logs from CrudService: " + e.getClass() + ": " + e.getMessage();
|
|
430 |
response.setStatus(500);
|
|
431 |
ServiceFailure sf = new ServiceFailure("1490", msg);
|
|
432 |
logMetacat.error(msg);
|
|
433 |
e.printStackTrace();
|
|
434 |
serializeException(sf, out);
|
|
435 |
}
|
|
436 |
}
|
|
437 |
|
|
438 |
/**
|
|
439 |
* Implements REST version of DataONE CRUD API --> get
|
|
440 |
* @param guid ID of data object to be read
|
|
441 |
*/
|
|
442 |
protected void getObject(String guid) {
|
|
443 |
OutputStream out = null;
|
|
444 |
try {
|
|
445 |
out = response.getOutputStream();
|
|
446 |
response.setStatus(200);
|
|
447 |
|
|
448 |
if(guid != null)
|
|
449 |
{ //get a specific document
|
|
450 |
Identifier id = new Identifier();
|
|
451 |
id.setValue(guid);
|
|
452 |
try
|
|
453 |
{
|
|
454 |
|
|
455 |
SystemMetadata sm = CNodeService.getInstance().getSystemMetadata(session, id);
|
|
456 |
|
|
457 |
//set the content type
|
|
458 |
if(sm.getObjectFormat().getFmtid().getValue().trim().equals(
|
|
459 |
ObjectFormatCache.getInstance().getFormat("text/csv").getFmtid().getValue()))
|
|
460 |
{
|
|
461 |
response.setContentType("text/csv");
|
|
462 |
response.setHeader("Content-Disposition", "inline; filename=" + id.getValue() + ".csv");
|
|
463 |
}
|
|
464 |
else if(sm.getObjectFormat().getFmtid().getValue().trim().equals(
|
|
465 |
ObjectFormatCache.getInstance().getFormat("text/plain").getFmtid().getValue()))
|
|
466 |
{
|
|
467 |
response.setContentType("text/plain");
|
|
468 |
response.setHeader("Content-Disposition", "inline; filename=" + id.getValue() + ".txt");
|
|
469 |
}
|
|
470 |
else if(sm.getObjectFormat().getFmtid().getValue().trim().equals(
|
|
471 |
ObjectFormatCache.getInstance().getFormat("application/octet-stream").getFmtid().getValue()))
|
|
472 |
{
|
|
473 |
response.setContentType("application/octet-stream");
|
|
474 |
}
|
|
475 |
else
|
|
476 |
{
|
|
477 |
response.setContentType("text/xml");
|
|
478 |
response.setHeader("Content-Disposition", "inline; filename=" + id.getValue() + ".xml");
|
|
479 |
}
|
|
480 |
|
|
481 |
InputStream data = CNodeService.getInstance().get(session, id);
|
|
482 |
IOUtils.copyLarge(data, response.getOutputStream());
|
|
483 |
}
|
|
484 |
catch(InvalidToken it)
|
|
485 |
{
|
|
486 |
response.setStatus(500);
|
|
487 |
serializeException(it, out);
|
|
488 |
}
|
|
489 |
catch(ServiceFailure sf)
|
|
490 |
{
|
|
491 |
response.setStatus(500);
|
|
492 |
serializeException(sf, out);
|
|
493 |
}
|
|
494 |
catch(NotAuthorized na)
|
|
495 |
{
|
|
496 |
response.setStatus(500);
|
|
497 |
serializeException(na, out);
|
|
498 |
}
|
|
499 |
catch(NotFound nf)
|
|
500 |
{
|
|
501 |
response.setStatus(500);
|
|
502 |
serializeException(nf, out);
|
|
503 |
}
|
|
504 |
catch(NotImplemented ni)
|
|
505 |
{
|
|
506 |
response.setStatus(500);
|
|
507 |
serializeException(ni, out);
|
|
508 |
}
|
|
509 |
catch(Exception e)
|
|
510 |
{
|
|
511 |
response.setStatus(500);
|
|
512 |
logMetacat.error("Error with Crud.get(). " +
|
|
513 |
"If this is an 'Exception producing data' error, " +
|
|
514 |
"go to CrudService.get() for better debugging. " +
|
|
515 |
"Here's the error: " + e.getClass() + ": " + e.getMessage());
|
|
516 |
e.printStackTrace();
|
|
517 |
ServiceFailure sf = new ServiceFailure("1030",
|
|
518 |
"IO Error in ResourceHandler.getObject: " + e.getClass() + ": " + e.getMessage());
|
|
519 |
serializeException(sf, out);
|
|
520 |
}
|
|
521 |
}
|
|
522 |
|
|
523 |
} catch (IOException e) {
|
|
524 |
e.printStackTrace();
|
|
525 |
response.setStatus(500);
|
|
526 |
ServiceFailure sf = new ServiceFailure("1030",
|
|
527 |
"IO Error in ResourceHandler.getObject: " + e.getMessage());
|
|
528 |
serializeException(sf, out);
|
|
529 |
} catch(NumberFormatException ne) {
|
|
530 |
response.setStatus(500);
|
|
531 |
InvalidRequest ir = new InvalidRequest("1030", "Invalid format for parameter: " + ne.getMessage());
|
|
532 |
serializeException(ir, out);
|
|
533 |
} catch (Exception e) {
|
|
534 |
e.printStackTrace();
|
|
535 |
response.setStatus(500);
|
|
536 |
ServiceFailure sf = new ServiceFailure("1030",
|
|
537 |
"Exception " + e.getClass().getName() + " raised while handling listObjects request: " +
|
|
538 |
e.getMessage());
|
|
539 |
serializeException(sf, out);
|
|
540 |
}
|
|
541 |
}
|
|
542 |
|
|
543 |
|
|
544 |
/**
|
|
545 |
* Implements REST version of DataONE CRUD API --> getSystemMetadata
|
|
546 |
* @param guid ID of data object to be read
|
|
547 |
*/
|
|
548 |
private void getSystemMetadataObject(String guid) {
|
|
549 |
OutputStream out = null;
|
|
550 |
try {
|
|
551 |
response.setContentType("text/xml");
|
|
552 |
response.setStatus(200);
|
|
553 |
out = response.getOutputStream();
|
|
554 |
Identifier id = new Identifier();
|
|
555 |
id.setValue(guid);
|
|
556 |
SystemMetadata sysmeta = CNodeService.getInstance().getSystemMetadata(session, id);
|
|
557 |
|
|
558 |
// Serialize and write it to the output stream
|
|
559 |
try {
|
|
560 |
//TODO: look at the efficiency of this method. The system metadata
|
|
561 |
//is read from metacat (in CrudService) as xml, then serialized
|
|
562 |
//to a SystemMetadat object, then returned here, then serizlized
|
|
563 |
//back to XML to be sent to the response.
|
|
564 |
serializeServiceType(SystemMetadata.class, sysmeta, out);
|
|
565 |
} catch (JiBXException e) {
|
|
566 |
throw new ServiceFailure("1190", "Failed to serialize SystemMetadata: " + e.getMessage());
|
|
567 |
}
|
|
568 |
} catch (BaseException e) {
|
|
569 |
response.setStatus(500);
|
|
570 |
serializeException(e, out);
|
|
571 |
} catch (IOException e) {
|
|
572 |
response.setStatus(500);
|
|
573 |
ServiceFailure sf = new ServiceFailure("1030",
|
|
574 |
"IO Error in ResourceHandler.getSystemMetadataObject: " + e.getMessage());
|
|
575 |
serializeException(sf, out);
|
|
576 |
} finally {
|
|
577 |
IOUtils.closeQuietly(out);
|
|
578 |
}
|
|
579 |
}
|
|
580 |
|
|
581 |
/**
|
|
582 |
* Earthgrid API > Put Service >Put Function : calls MetacatHandler > handleInsertOrUpdateAction
|
|
583 |
*
|
|
584 |
* @param guid - ID of data object to be inserted or updated. If action is update, the pid
|
|
585 |
* is the existing pid. If insert, the pid is the new one
|
|
586 |
* @throws IOException
|
|
587 |
*/
|
|
588 |
protected void putObject(String pid, String action) {
|
|
589 |
logMetacat.debug("ResourceHandler: putObject with pid " + pid);
|
|
590 |
logMetacat.debug("Entering putObject: " + pid + "/" + action);
|
|
591 |
OutputStream out = null;
|
|
592 |
try {
|
|
593 |
out = response.getOutputStream();
|
|
594 |
response.setStatus(200);
|
|
595 |
response.setContentType("text/xml");
|
|
596 |
} catch (IOException e1) {
|
|
597 |
logMetacat.error("Could not get the output stream for writing in putObject");
|
|
598 |
}
|
|
599 |
try {
|
|
600 |
|
|
601 |
// Read the incoming data from its Mime Multipart encoding
|
|
602 |
logMetacat.debug("Disassembling MIME multipart form");
|
|
603 |
InputStream object = null;
|
|
604 |
InputStream sysmeta = null;
|
|
605 |
Map<String, List<String>> multipartparams;
|
|
606 |
|
|
607 |
try
|
|
608 |
{
|
|
609 |
//String req = IOUtils.toString(request.getInputStream());
|
|
610 |
//logMetacat.debug("request: " + req);
|
|
611 |
//InputStream reqStr = IOUtils.toInputStream(req);
|
|
612 |
InputStream reqStr = request.getInputStream();
|
|
613 |
|
|
614 |
//handle MMP inputs
|
|
615 |
File tmpDir = getTempDirectory();
|
|
616 |
File tmpSMFile = new File(tmpDir +
|
|
617 |
".sysmeta." + new Date().getTime() + ".tmp");
|
|
618 |
logMetacat.debug("temp dir: " + tmpDir.getAbsolutePath());
|
|
619 |
MultipartRequestResolver mrr = new MultipartRequestResolver(
|
|
620 |
tmpDir.getAbsolutePath(), 1000000000, 0);
|
|
621 |
MultipartRequest mr = mrr.resolveMultipart(request);
|
|
622 |
logMetacat.debug("resolved multipart request");
|
|
623 |
Map<String, File> files = mr.getMultipartFiles();
|
|
624 |
if(files == null)
|
|
625 |
{
|
|
626 |
throw new ServiceFailure("1202", "create/update must have multipart files with names 'object' and 'sysmeta'");
|
|
627 |
}
|
|
628 |
logMetacat.debug("got multipart files");
|
|
629 |
|
|
630 |
if(files.keySet() == null)
|
|
631 |
{
|
|
632 |
logMetacat.error("No file keys in MMP request.");
|
|
633 |
throw new ServiceFailure("1202", "No file keys found in MMP. " +
|
|
634 |
"create/update must have multipart files with names 'object' and 'sysmeta'");
|
|
635 |
}
|
|
636 |
|
|
637 |
// for logging purposes, dump out the key-value pairs that constitute the request
|
|
638 |
// 3 types exist: request params, multipart params, and multipart files
|
|
639 |
Iterator it = files.keySet().iterator();
|
|
640 |
logMetacat.debug("iterating through request parts: " + it);
|
|
641 |
while(it.hasNext())
|
|
642 |
{
|
|
643 |
String key = (String)it.next();
|
|
644 |
logMetacat.debug("files key: " + key);
|
|
645 |
logMetacat.debug("files value: " + files.get(key));
|
|
646 |
}
|
|
647 |
|
|
648 |
multipartparams = mr.getMultipartParameters();
|
|
649 |
it = multipartparams.keySet().iterator();
|
|
650 |
while(it.hasNext())
|
|
651 |
{
|
|
652 |
String key = (String)it.next();
|
|
653 |
logMetacat.debug("multipartparams key: " + key);
|
|
654 |
logMetacat.debug("multipartparams value: " + multipartparams.get(key));
|
|
655 |
}
|
|
656 |
|
|
657 |
it = params.keySet().iterator();
|
|
658 |
while(it.hasNext())
|
|
659 |
{
|
|
660 |
String key = (String)it.next();
|
|
661 |
logMetacat.debug("param key: " + key);
|
|
662 |
logMetacat.debug("param value: " + params.get(key));
|
|
663 |
}
|
|
664 |
logMetacat.debug("done iterating the request...");
|
|
665 |
|
|
666 |
File smFile = files.get("sysmeta");
|
|
667 |
if (smFile == null)
|
|
668 |
throw new InvalidRequest("1102", "Missing the required file-part 'sysmeta' from the multipart request.");
|
|
669 |
logMetacat.debug("smFile: " + smFile.getAbsolutePath());
|
|
670 |
sysmeta = new FileInputStream(smFile);
|
|
671 |
File objFile = files.get("object");
|
|
672 |
if (objFile == null)
|
|
673 |
throw new InvalidRequest("1102", "Missing the required file-part 'object' from the multipart request.");
|
|
674 |
|
|
675 |
logMetacat.debug("objectfile: " + objFile.getAbsolutePath());
|
|
676 |
object = new FileInputStream(objFile);
|
|
677 |
|
|
678 |
/*String obj = IOUtils.toString(object);
|
|
679 |
String sm = IOUtils.toString(sysmeta);
|
|
680 |
logMetacat.debug("object: " + obj);
|
|
681 |
logMetacat.debug("sm: " + sm);
|
|
682 |
object = IOUtils.toInputStream(obj);
|
|
683 |
sysmeta = IOUtils.toInputStream(sm);*/
|
|
684 |
|
|
685 |
}
|
|
686 |
catch(org.apache.commons.fileupload.FileUploadException fue)
|
|
687 |
{
|
|
688 |
throw new ServiceFailure("1202", "Could not upload MMP files: " + fue.getMessage());
|
|
689 |
}
|
|
690 |
catch(IOException ioe)
|
|
691 |
{
|
|
692 |
throw new ServiceFailure("1202",
|
|
693 |
"IOException when processing Mime Multipart: " + ioe.getMessage());
|
|
694 |
}
|
|
695 |
catch(Exception e)
|
|
696 |
{
|
|
697 |
throw new ServiceFailure("1202", "Error handling MMP upload: " + e.getClass() + ": " + e.getMessage());
|
|
698 |
}
|
|
699 |
|
|
700 |
if ( action.equals(FUNCTION_NAME_INSERT)) { //handle inserts
|
|
701 |
|
|
702 |
// Check if the objectId exists
|
|
703 |
IdentifierManager im = IdentifierManager.getInstance();
|
|
704 |
if (im.identifierExists(pid)) {
|
|
705 |
throw new IdentifierNotUnique("1000", "Identifier is already in use: " + pid);
|
|
706 |
}
|
|
707 |
|
|
708 |
logMetacat.debug("Commence creation...");
|
|
709 |
IBindingFactory bfact =
|
|
710 |
BindingDirectory.getFactory(SystemMetadata.class);
|
|
711 |
IUnmarshallingContext uctx = bfact.createUnmarshallingContext();
|
|
712 |
SystemMetadata smd = (SystemMetadata) uctx.unmarshalDocument(sysmeta, null);
|
|
713 |
|
|
714 |
Identifier id = new Identifier();
|
|
715 |
id.setValue(pid);
|
|
716 |
logMetacat.debug("creating object with pid " + id.getValue());
|
|
717 |
Identifier rId = CNodeService.getInstance().create(session, id, object, smd);
|
|
718 |
serializeServiceType(Identifier.class, rId, out);
|
|
719 |
|
|
720 |
} else {
|
|
721 |
throw new InvalidRequest("1000", "Operation must be create or update.");
|
|
722 |
}
|
|
723 |
|
|
724 |
//clean up the MMP files
|
|
725 |
//parts.get("systemmetadata").delete();
|
|
726 |
//parts.get("object").delete();
|
|
727 |
} catch (NotAuthorized e) {
|
|
728 |
response.setStatus(500);
|
|
729 |
serializeException(e, out);
|
|
730 |
} catch (InvalidToken e) {
|
|
731 |
response.setStatus(500);
|
|
732 |
serializeException(e, out);
|
|
733 |
} catch (ServiceFailure e) {
|
|
734 |
response.setStatus(500);
|
|
735 |
serializeException(e, out);
|
|
736 |
} catch (IdentifierNotUnique e) {
|
|
737 |
response.setStatus(500);
|
|
738 |
serializeException(e, out);
|
|
739 |
} catch (UnsupportedType e) {
|
|
740 |
response.setStatus(500);
|
|
741 |
serializeException(e, out);
|
|
742 |
} catch (InsufficientResources e) {
|
|
743 |
response.setStatus(500);
|
|
744 |
serializeException(e, out);
|
|
745 |
} catch (InvalidSystemMetadata e) {
|
|
746 |
response.setStatus(500);
|
|
747 |
serializeException(e, out);
|
|
748 |
} catch (NotImplemented e) {
|
|
749 |
response.setStatus(500);
|
|
750 |
serializeException(e, out);
|
|
751 |
} catch (InvalidRequest e) {
|
|
752 |
response.setStatus(500);
|
|
753 |
serializeException(e, out);
|
|
754 |
} /*catch (MessagingException e) {
|
|
755 |
ServiceFailure sf = new ServiceFailure("1000", e.getMessage());
|
|
756 |
serializeException(sf, out);
|
|
757 |
} catch (IOException e) {
|
|
758 |
response.setStatus(500);
|
|
759 |
ServiceFailure sf = new ServiceFailure("1000", e.getMessage());
|
|
760 |
serializeException(sf, out);
|
|
761 |
}*/ catch (JiBXException e) {
|
|
762 |
response.setStatus(500);
|
|
763 |
e.printStackTrace(System.out);
|
|
764 |
InvalidSystemMetadata ism = new InvalidSystemMetadata("1080", e.getMessage());
|
|
765 |
serializeException(ism, out);
|
|
766 |
}
|
|
767 |
}
|
|
768 |
|
|
769 |
/**
|
|
770 |
* List the object formats registered with the system
|
|
771 |
*/
|
|
772 |
private void listFormats() {
|
|
773 |
logMetacat.debug("Entering listFormats()");
|
|
774 |
// get the response output stream
|
|
775 |
OutputStream out = null;
|
|
776 |
|
|
777 |
try {
|
|
778 |
out = response.getOutputStream();
|
|
779 |
response.setStatus(200);
|
|
780 |
response.setContentType("text/xml");
|
|
781 |
|
|
782 |
} catch (IOException ioe) {
|
|
783 |
logMetacat.error("Could not get the output stream for writing" +
|
|
784 |
"in ResourceHandler.listFormats()");
|
|
785 |
|
|
786 |
}
|
|
787 |
|
|
788 |
// get the object format list
|
|
789 |
try {
|
|
790 |
ObjectFormatList objectFormatList = CNodeService.getInstance().listFormats();
|
|
791 |
serializeServiceType(ObjectFormatList.class, objectFormatList, out);
|
|
792 |
|
|
793 |
} catch (InvalidRequest e) {
|
|
794 |
response.setStatus(200);
|
|
795 |
serializeException(e, out);
|
|
796 |
|
|
797 |
} catch (ServiceFailure e) {
|
|
798 |
response.setStatus(501);
|
|
799 |
serializeException(e, out);
|
|
800 |
|
|
801 |
} catch (NotFound e) {
|
|
802 |
response.setStatus(200);
|
|
803 |
serializeException(e, out);
|
|
804 |
|
|
805 |
} catch (InsufficientResources e) {
|
|
806 |
response.setStatus(200);
|
|
807 |
serializeException(e, out);
|
|
808 |
|
|
809 |
} catch (NotImplemented e) {
|
|
810 |
response.setStatus(200);
|
|
811 |
serializeException(e, out);
|
|
812 |
|
|
813 |
} catch (JiBXException jibxe) {
|
|
814 |
response.setStatus(501);
|
|
815 |
ServiceFailure e =
|
|
816 |
new ServiceFailure("4841", "Unexpected exception from the service - " +
|
|
817 |
jibxe.getClass() + ": " + jibxe.getMessage());
|
|
818 |
serializeException(e, out);
|
|
819 |
|
|
820 |
}
|
|
821 |
|
|
822 |
}
|
|
823 |
|
|
824 |
/**
|
|
825 |
* Return the requested object format
|
|
826 |
*
|
|
827 |
* @param fmtidStr the requested format identifier as a string
|
|
828 |
*/
|
|
829 |
private void getFormat(String fmtidStr) {
|
|
830 |
logMetacat.debug("Entering listFormats()");
|
|
831 |
|
|
832 |
// get the response output stream
|
|
833 |
OutputStream out = null;
|
|
834 |
try {
|
|
835 |
out = response.getOutputStream();
|
|
836 |
response.setStatus(200);
|
|
837 |
response.setContentType("text/xml");
|
|
838 |
|
|
839 |
} catch (IOException ioe) {
|
|
840 |
logMetacat.error("Could not get the output stream for writing" +
|
|
841 |
"in ResourceHandler.listFormats()");
|
|
842 |
|
|
843 |
}
|
|
844 |
|
|
845 |
ObjectFormatIdentifier fmtid = new ObjectFormatIdentifier();
|
|
846 |
fmtid.setValue(fmtidStr);
|
|
847 |
|
|
848 |
try {
|
|
849 |
// get the specified object format
|
|
850 |
ObjectFormat objectFormat = CNodeService.getInstance().getFormat(fmtid);
|
|
851 |
serializeServiceType(ObjectFormat.class, objectFormat, out);
|
|
852 |
|
|
853 |
} catch (InvalidRequest e) {
|
|
854 |
response.setStatus(200);
|
|
855 |
serializeException(e, out);
|
|
856 |
|
|
857 |
} catch (ServiceFailure e) {
|
|
858 |
response.setStatus(501);
|
|
859 |
serializeException(e, out);
|
|
860 |
|
|
861 |
} catch (NotFound e) {
|
|
862 |
response.setStatus(200);
|
|
863 |
serializeException(e, out);
|
|
864 |
|
|
865 |
} catch (InsufficientResources e) {
|
|
866 |
response.setStatus(200);
|
|
867 |
serializeException(e, out);
|
|
868 |
|
|
869 |
} catch (NotImplemented e) {
|
|
870 |
response.setStatus(200);
|
|
871 |
serializeException(e, out);
|
|
872 |
|
|
873 |
} catch (JiBXException jibxe) {
|
|
874 |
ServiceFailure e =
|
|
875 |
new ServiceFailure("4841", "Unexpected exception from the service - " +
|
|
876 |
jibxe.getClass() + ": " + jibxe.getMessage());
|
|
877 |
serializeException(e, out);
|
|
878 |
|
|
879 |
}
|
|
880 |
|
|
881 |
}
|
|
882 |
|
|
883 |
/**
|
|
884 |
* Reserve the given Identifier
|
|
885 |
* @throws InvalidToken
|
|
886 |
* @throws ServiceFailure
|
|
887 |
* @throws NotAuthorized
|
|
888 |
* @throws IdentifierNotUnique
|
|
889 |
* @throws NotImplemented
|
|
890 |
* @throws InvalidRequest
|
|
891 |
* @throws IOException
|
|
892 |
*/
|
|
893 |
private void reserve() throws InvalidToken, ServiceFailure, NotAuthorized, IdentifierNotUnique, NotImplemented, InvalidRequest, IOException {
|
|
894 |
String id = params.get("pid")[0];
|
|
895 |
String format = params.get("format")[0];
|
|
896 |
String scope = params.get("scope")[0];
|
|
897 |
Identifier pid = new Identifier();
|
|
898 |
pid.setValue(id);
|
|
899 |
Identifier retPid = CNodeService.getInstance().reserveIdentifier(session, pid, scope, format);
|
|
900 |
OutputStream out = response.getOutputStream();
|
|
901 |
response.setStatus(200);
|
|
902 |
response.setContentType("text/xml");
|
|
903 |
try {
|
|
904 |
serializeServiceType(Identifier.class, retPid, out);
|
|
905 |
} catch (JiBXException e) {
|
|
906 |
throw new ServiceFailure("4210",
|
|
907 |
"Unexpected exception from the service - " +
|
|
908 |
e.getClass() + ": " + e.getMessage());
|
|
909 |
}
|
|
910 |
|
|
911 |
}
|
|
912 |
|
|
913 |
private void resolve(String id) throws InvalidRequest, InvalidToken, ServiceFailure, NotAuthorized, NotFound, NotImplemented, IOException {
|
|
914 |
Identifier pid = new Identifier();
|
|
915 |
pid.setValue(id);
|
|
916 |
ObjectLocationList locationList = CNodeService.getInstance().resolve(session, pid);
|
|
917 |
OutputStream out = response.getOutputStream();
|
|
918 |
response.setStatus(200);
|
|
919 |
response.setContentType("text/xml");
|
|
920 |
try {
|
|
921 |
serializeServiceType(ObjectLocationList.class, locationList, out);
|
|
922 |
} catch (JiBXException e) {
|
|
923 |
throw new ServiceFailure("4150",
|
|
924 |
"Unexpected exception from the service - " +
|
|
925 |
e.getClass() + ": " + e.getMessage());
|
|
926 |
}
|
|
927 |
}
|
|
928 |
|
|
929 |
private boolean asserRelation(String id) throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, InvalidRequest, NotImplemented {
|
|
930 |
Identifier pidOfSubject = new Identifier();
|
|
931 |
pidOfSubject.setValue(id);
|
|
932 |
Identifier pidOfObject = new Identifier();
|
|
933 |
String relationship;
|
|
934 |
relationship = params.get("relationship")[0];
|
|
935 |
String objPid = params.get("pidOfObject")[0];
|
|
936 |
pidOfObject.setValue(objPid);
|
|
937 |
boolean result = CNodeService.getInstance().assertRelation(session, pidOfSubject, relationship, pidOfObject);
|
|
938 |
response.setStatus(200);
|
|
939 |
response.setContentType("text/xml");
|
|
940 |
return result;
|
|
941 |
}
|
|
942 |
|
|
943 |
private void owner(String id) throws JiBXException, InvalidToken, ServiceFailure, NotFound, NotAuthorized, NotImplemented, InvalidRequest, IOException {
|
|
944 |
Identifier pid = new Identifier();
|
|
945 |
pid.setValue(id);
|
|
946 |
String subjectStr = params.get("subject")[0];
|
|
947 |
Subject subject = (Subject) deserializeServiceType(Subject.class, new ByteArrayInputStream(subjectStr.getBytes("UTF-8")));
|
|
948 |
Identifier retPid = CNodeService.getInstance().setOwner(session, pid, subject);
|
|
949 |
OutputStream out = response.getOutputStream();
|
|
950 |
response.setStatus(200);
|
|
951 |
response.setContentType("text/xml");
|
|
952 |
try {
|
|
953 |
serializeServiceType(Identifier.class, retPid, out);
|
|
954 |
} catch (JiBXException e) {
|
|
955 |
throw new ServiceFailure("4490",
|
|
956 |
"Unexpected exception from the service - " +
|
|
957 |
e.getClass() + ": " + e.getMessage());
|
|
958 |
}
|
|
959 |
|
|
960 |
}
|
|
961 |
|
|
962 |
private boolean isAuthorized(String id) throws ServiceFailure, InvalidToken, NotFound, NotAuthorized, NotImplemented, InvalidRequest {
|
|
963 |
Identifier pid = new Identifier();
|
|
964 |
pid.setValue(id);
|
|
965 |
String permission = params.get("permission")[0];
|
|
966 |
boolean result = CNodeService.getInstance().isAuthorized(session, pid, Permission.valueOf(permission));
|
|
967 |
response.setStatus(200);
|
|
968 |
response.setContentType("text/xml");
|
|
969 |
return result;
|
|
970 |
}
|
|
971 |
|
|
972 |
/**
|
|
973 |
* Register System Metadata without data or metadata object
|
|
974 |
* @param pid identifier for System Metadata entry
|
|
975 |
*/
|
|
976 |
protected void registerSystemMetadata(String pid) {
|
|
977 |
logMetacat.debug("Entering registerSystemMetadata: " + pid);
|
|
978 |
OutputStream out = null;
|
|
979 |
try {
|
|
980 |
out = response.getOutputStream();
|
|
981 |
response.setStatus(200);
|
|
982 |
response.setContentType("text/xml");
|
|
983 |
} catch (IOException e1) {
|
|
984 |
logMetacat.error("Could not get the output stream for writing in putObject");
|
|
985 |
}
|
|
986 |
try {
|
|
987 |
|
|
988 |
// Read the incoming data from its Mime Multipart encoding
|
|
989 |
logMetacat.debug("Disassembling MIME multipart form");
|
|
990 |
InputStream sysmeta = null;
|
|
991 |
Map<String, List<String>> multipartparams;
|
|
992 |
|
|
993 |
try {
|
|
994 |
|
|
995 |
// handle MMP inputs
|
|
996 |
File tmpDir = getTempDirectory();
|
|
997 |
|
|
998 |
logMetacat.debug("temp dir: " + tmpDir.getAbsolutePath());
|
|
999 |
MultipartRequestResolver mrr = new MultipartRequestResolver(
|
|
1000 |
tmpDir.getAbsolutePath(), 1000000000, 0);
|
|
1001 |
MultipartRequest mr = mrr.resolveMultipart(request);
|
|
1002 |
logMetacat.debug("resolved multipart request");
|
|
1003 |
Map<String, File> files = mr.getMultipartFiles();
|
|
1004 |
if (files == null) {
|
|
1005 |
throw new ServiceFailure("1202",
|
|
1006 |
"register meta must have multipart file with name 'sysmeta'");
|
|
1007 |
}
|
|
1008 |
logMetacat.debug("got multipart files");
|
|
1009 |
|
|
1010 |
if (files.keySet() == null) {
|
|
1011 |
logMetacat.error("No file keys in MMP request.");
|
|
1012 |
throw new ServiceFailure(
|
|
1013 |
"1202",
|
|
1014 |
"No file keys found in MMP. "
|
|
1015 |
+ "register meta must have multipart file with name 'sysmeta'");
|
|
1016 |
}
|
|
1017 |
|
|
1018 |
// for logging purposes, dump out the key-value pairs that
|
|
1019 |
// constitute the request
|
|
1020 |
// 3 types exist: request params, multipart params, and
|
|
1021 |
// multipart files
|
|
1022 |
Iterator it = files.keySet().iterator();
|
|
1023 |
logMetacat.debug("iterating through request parts: " + it);
|
|
1024 |
while (it.hasNext()) {
|
|
1025 |
String key = (String) it.next();
|
|
1026 |
logMetacat.debug("files key: " + key);
|
|
1027 |
logMetacat.debug("files value: " + files.get(key));
|
|
1028 |
}
|
|
1029 |
|
|
1030 |
multipartparams = mr.getMultipartParameters();
|
|
1031 |
it = multipartparams.keySet().iterator();
|
|
1032 |
while (it.hasNext()) {
|
|
1033 |
String key = (String) it.next();
|
|
1034 |
logMetacat.debug("multipartparams key: " + key);
|
|
1035 |
logMetacat.debug("multipartparams value: "
|
|
1036 |
+ multipartparams.get(key));
|
|
1037 |
}
|
|
1038 |
|
|
1039 |
it = params.keySet().iterator();
|
|
1040 |
while (it.hasNext()) {
|
|
1041 |
String key = (String) it.next();
|
|
1042 |
logMetacat.debug("param key: " + key);
|
|
1043 |
logMetacat.debug("param value: " + params.get(key));
|
|
1044 |
}
|
|
1045 |
logMetacat.debug("done iterating the request...");
|
|
1046 |
|
|
1047 |
File smFile = files.get("sysmeta");
|
|
1048 |
if (smFile == null) {
|
|
1049 |
throw new InvalidRequest("1102",
|
|
1050 |
"Missing the required file-part 'sysmeta' from the multipart request.");
|
|
1051 |
}
|
|
1052 |
logMetacat.debug("smFile: " + smFile.getAbsolutePath());
|
|
1053 |
sysmeta = new FileInputStream(smFile);
|
|
1054 |
|
|
1055 |
} catch (org.apache.commons.fileupload.FileUploadException fue) {
|
|
1056 |
throw new ServiceFailure("1202", "Could not upload MMP files: "
|
|
1057 |
+ fue.getMessage());
|
|
1058 |
} catch (IOException ioe) {
|
|
1059 |
throw new ServiceFailure("1202",
|
|
1060 |
"IOException when processing Mime Multipart: "
|
|
1061 |
+ ioe.getMessage());
|
|
1062 |
} catch (Exception e) {
|
|
1063 |
throw new ServiceFailure("1202", "Error handling MMP upload: "
|
|
1064 |
+ e.getClass() + ": " + e.getMessage());
|
|
1065 |
}
|
|
1066 |
|
|
1067 |
// Check if the objectId exists
|
|
1068 |
IdentifierManager im = IdentifierManager.getInstance();
|
|
1069 |
if (im.identifierExists(pid)) {
|
|
1070 |
throw new IdentifierNotUnique("1000",
|
|
1071 |
"Identifier is already in use: " + pid);
|
|
1072 |
}
|
|
1073 |
|
|
1074 |
logMetacat.debug("Commence creation...");
|
|
1075 |
IBindingFactory bfact = BindingDirectory
|
|
1076 |
.getFactory(SystemMetadata.class);
|
|
1077 |
IUnmarshallingContext uctx = bfact.createUnmarshallingContext();
|
|
1078 |
SystemMetadata systemMetadata = (SystemMetadata) uctx
|
|
1079 |
.unmarshalDocument(sysmeta, null);
|
|
1080 |
|
|
1081 |
Identifier guid = new Identifier();
|
|
1082 |
guid.setValue(pid);
|
|
1083 |
logMetacat.debug("registering system metadata with pid "
|
|
1084 |
+ guid.getValue());
|
|
1085 |
boolean result = CNodeService.getInstance().registerSystemMetadata(
|
|
1086 |
session, guid, systemMetadata);
|
|
1087 |
serializeServiceType(Boolean.class, result, out);
|
|
1088 |
|
|
1089 |
} catch (NotAuthorized e) {
|
|
1090 |
response.setStatus(500);
|
|
1091 |
serializeException(e, out);
|
|
1092 |
} catch (ServiceFailure e) {
|
|
1093 |
response.setStatus(500);
|
|
1094 |
serializeException(e, out);
|
|
1095 |
} catch (IdentifierNotUnique e) {
|
|
1096 |
response.setStatus(500);
|
|
1097 |
serializeException(e, out);
|
|
1098 |
} catch (NotImplemented e) {
|
|
1099 |
response.setStatus(500);
|
|
1100 |
serializeException(e, out);
|
|
1101 |
} catch (InvalidRequest e) {
|
|
1102 |
response.setStatus(500);
|
|
1103 |
serializeException(e, out);
|
|
1104 |
} catch (JiBXException e) {
|
|
1105 |
response.setStatus(500);
|
|
1106 |
e.printStackTrace(System.out);
|
|
1107 |
InvalidSystemMetadata ism = new InvalidSystemMetadata("1080", e
|
|
1108 |
.getMessage());
|
|
1109 |
serializeException(ism, out);
|
|
1110 |
} catch (InvalidSystemMetadata e) {
|
|
1111 |
serializeException(e, out);
|
|
1112 |
}
|
|
1113 |
}
|
|
1114 |
|
|
1115 |
}
|
0 |
1116 |
|
add CNRestServlet and mappings for urls in the form:
/d1/cn/*
This extends existing code for CN-specific handling. MN handling will be analogous and is forthcoming