Revision 8810
Added by ben leinfelder almost 10 years ago
D1ResourceHandler.java | ||
---|---|---|
23 | 23 |
package edu.ucsb.nceas.metacat.restservice; |
24 | 24 |
|
25 | 25 |
import java.io.File; |
26 |
import java.io.FileInputStream; |
|
27 |
import java.io.FileNotFoundException; |
|
28 | 26 |
import java.io.IOException; |
29 | 27 |
import java.io.InputStream; |
30 | 28 |
import java.io.OutputStream; |
... | ... | |
41 | 39 |
import javax.servlet.ServletContext; |
42 | 40 |
import javax.servlet.http.HttpServletRequest; |
43 | 41 |
import javax.servlet.http.HttpServletResponse; |
44 |
import javax.xml.parsers.ParserConfigurationException; |
|
45 | 42 |
|
46 | 43 |
import org.apache.commons.fileupload.FileUploadException; |
47 | 44 |
import org.apache.commons.io.IOUtils; |
... | ... | |
53 | 50 |
import org.dataone.service.exceptions.BaseException; |
54 | 51 |
import org.dataone.service.exceptions.InvalidRequest; |
55 | 52 |
import org.dataone.service.exceptions.ServiceFailure; |
56 |
import org.dataone.service.types.v1.AccessPolicy; |
|
57 | 53 |
import org.dataone.service.types.v1.Group; |
58 | 54 |
import org.dataone.service.types.v1.Person; |
59 |
import org.dataone.service.types.v1.Replica; |
|
60 |
import org.dataone.service.types.v1.ReplicationPolicy; |
|
61 | 55 |
import org.dataone.service.types.v1.Session; |
62 | 56 |
import org.dataone.service.types.v1.Subject; |
63 | 57 |
import org.dataone.service.types.v1.SubjectInfo; |
64 |
import org.dataone.service.types.v1.SystemMetadata; |
|
65 |
import org.dataone.service.util.ExceptionHandler; |
|
66 |
import org.dataone.service.util.TypeMarshaller; |
|
67 |
import org.jibx.runtime.JiBXException; |
|
68 |
import org.xml.sax.SAXException; |
|
69 | 58 |
|
70 | 59 |
import edu.ucsb.nceas.metacat.MetacatHandler; |
71 | 60 |
import edu.ucsb.nceas.metacat.properties.PropertyService; |
... | ... | |
249 | 238 |
} |
250 | 239 |
|
251 | 240 |
/** |
252 |
* Parse the BaseException information for replication status failures if any |
|
253 |
* |
|
254 |
* @return failure the BaseException failure, one of it's subclasses, or null |
|
255 |
* @throws ServiceFailure |
|
256 |
* @throws InvalidRequest |
|
257 |
* @throws JiBXException |
|
258 |
* @throws IllegalAccessException |
|
259 |
* @throws InstantiationException |
|
260 |
* @throws IOException |
|
261 |
*/ |
|
262 |
protected BaseException collectReplicationStatus() |
|
263 |
throws ServiceFailure, InvalidRequest, IOException, |
|
264 |
InstantiationException, IllegalAccessException, JiBXException { |
|
265 |
|
|
266 |
BaseException failure = null; |
|
267 |
File tmpDir = getTempDirectory(); |
|
268 |
MultipartRequest mr = null; |
|
269 |
Map<String, File> mmFileParts = null; |
|
270 |
File exceptionFile = null; |
|
271 |
InputStream exceptionFileStream = null; |
|
272 |
|
|
273 |
// Read the incoming data from its Mime Multipart encoding |
|
274 |
logMetacat.debug("Parsing BaseException from the mime multipart entity"); |
|
275 |
|
|
276 |
// handle MMP inputs |
|
277 |
MultipartRequestResolver mrr = |
|
278 |
new MultipartRequestResolver(tmpDir.getAbsolutePath(),1000000000, 0); |
|
279 |
|
|
280 |
try { |
|
281 |
mr = mrr.resolveMultipart(request); |
|
282 |
logMetacat.debug("Resolved the replication status BaseException multipart request."); |
|
283 |
|
|
284 |
} catch (IOException e) { |
|
285 |
throw new ServiceFailure("4700", "Couldn't resolve the multipart request: " + |
|
286 |
e.getMessage()); |
|
287 |
|
|
288 |
} catch (FileUploadException e) { |
|
289 |
throw new ServiceFailure("4700", "Couldn't resolve the multipart request: " + |
|
290 |
e.getMessage()); |
|
291 |
|
|
292 |
} catch (Exception e) { |
|
293 |
throw new ServiceFailure("4700", "Couldn't resolve the multipart request: " + |
|
294 |
e.getMessage()); |
|
295 |
|
|
296 |
} |
|
297 |
|
|
298 |
// get the map of file parts |
|
299 |
mmFileParts = mr.getMultipartFiles(); |
|
300 |
|
|
301 |
if ( mmFileParts == null || mmFileParts.keySet() == null) { |
|
302 |
logMetacat.debug("BaseException for setReplicationStatus is null"); |
|
303 |
} |
|
304 |
|
|
305 |
multipartparams = mr.getMultipartParameters(); |
|
306 |
exceptionFile = mmFileParts.get("failure"); |
|
307 |
|
|
308 |
if ( exceptionFile != null && exceptionFile.length() > 0 ) { |
|
309 |
|
|
310 |
// deserialize the BaseException subclass |
|
311 |
exceptionFileStream = new FileInputStream(exceptionFile); |
|
312 |
try { |
|
313 |
failure = ExceptionHandler.deserializeXml(exceptionFileStream, |
|
314 |
"Replication failed for an unknown reason."); |
|
315 |
|
|
316 |
} catch (ParserConfigurationException e) { |
|
317 |
throw new ServiceFailure("4700", "Couldn't parse the replication failure exception: " + |
|
318 |
e.getMessage()); |
|
319 |
|
|
320 |
} catch (SAXException e) { |
|
321 |
throw new ServiceFailure("4700", "Couldn't traverse the replication failure exception: " + |
|
322 |
e.getMessage()); |
|
323 |
|
|
324 |
} |
|
325 |
|
|
326 |
} |
|
327 |
|
|
328 |
|
|
329 |
return failure; |
|
330 |
|
|
331 |
} |
|
332 |
|
|
333 |
/** |
|
334 | 241 |
* Parse string parameters from the mime multipart entity of the request. |
335 | 242 |
* Populates the multipartparams map |
336 | 243 |
* |
... | ... | |
359 | 266 |
|
360 | 267 |
} |
361 | 268 |
|
362 |
|
|
363 | 269 |
/** |
364 |
* Parse the replication policy document out of the mime-multipart form data |
|
365 |
* |
|
366 |
* @return policy the encoded policy |
|
367 |
* @throws ServiceFailure |
|
368 |
* @throws InvalidRequest |
|
369 |
* @throws IOException |
|
370 |
* @throws InstantiationException |
|
371 |
* @throws IllegalAccessException |
|
372 |
* @throws JiBXException |
|
373 |
*/ |
|
374 |
protected ReplicationPolicy collectReplicationPolicy() |
|
375 |
throws ServiceFailure, InvalidRequest, IOException, InstantiationException, |
|
376 |
IllegalAccessException, JiBXException { |
|
377 |
|
|
378 |
ReplicationPolicy policy = null; |
|
379 |
File tmpDir = getTempDirectory(); |
|
380 |
MultipartRequest mr = null; |
|
381 |
Map<String, File> mmFileParts = null; |
|
382 |
File replPolicyFile = null; |
|
383 |
InputStream replPolicyStream = null; |
|
384 |
|
|
385 |
// Read the incoming data from its Mime Multipart encoding |
|
386 |
logMetacat.debug("Parsing ReplicationPolicy from the mime multipart entity"); |
|
387 |
|
|
388 |
// handle MMP inputs |
|
389 |
MultipartRequestResolver mrr = |
|
390 |
new MultipartRequestResolver(tmpDir.getAbsolutePath(),1000000000, 0); |
|
391 |
|
|
392 |
try { |
|
393 |
mr = mrr.resolveMultipart(request); |
|
394 |
logMetacat.debug("Resolved the ReplicationPolicy multipart request."); |
|
395 |
|
|
396 |
} catch (IOException e) { |
|
397 |
throw new ServiceFailure("4882", "Couldn't resolve the multipart request: " + |
|
398 |
e.getMessage()); |
|
399 |
|
|
400 |
} catch (FileUploadException e) { |
|
401 |
throw new ServiceFailure("4882", "Couldn't resolve the multipart request: " + |
|
402 |
e.getMessage()); |
|
403 |
|
|
404 |
} catch (Exception e) { |
|
405 |
throw new ServiceFailure("4882", "Couldn't resolve the multipart request: " + |
|
406 |
e.getMessage()); |
|
407 |
|
|
408 |
} |
|
409 |
|
|
410 |
// get the map of file parts |
|
411 |
mmFileParts = mr.getMultipartFiles(); |
|
412 |
|
|
413 |
if ( mmFileParts == null || mmFileParts.keySet() == null) { |
|
414 |
throw new InvalidRequest("4883", "The multipart request must include " + |
|
415 |
"a file with the name 'policy'."); |
|
416 |
|
|
417 |
} |
|
418 |
|
|
419 |
multipartparams = mr.getMultipartParameters(); |
|
420 |
replPolicyFile = mmFileParts.get("policy"); |
|
421 |
|
|
422 |
if ( replPolicyFile == null ) { |
|
423 |
throw new InvalidRequest("4883", "The multipart request must include " + |
|
424 |
"a file with the name 'policy'."); |
|
425 |
|
|
426 |
} |
|
427 |
|
|
428 |
|
|
429 |
// deserialize the ReplicationPolicy |
|
430 |
replPolicyStream = new FileInputStream(replPolicyFile); |
|
431 |
policy = TypeMarshaller.unmarshalTypeFromStream(ReplicationPolicy.class, replPolicyStream); |
|
432 |
|
|
433 |
return policy; |
|
434 |
|
|
435 |
} |
|
436 |
|
|
437 |
/** |
|
438 |
* Parse the replica metadata document out of the mime-multipart form data |
|
439 |
* |
|
440 |
* @return replica the encoded replica |
|
441 |
* @throws ServiceFailure |
|
442 |
* @throws InvalidRequest |
|
443 |
* @throws IOException |
|
444 |
* @throws InstantiationException |
|
445 |
* @throws IllegalAccessException |
|
446 |
* @throws JiBXException |
|
447 |
*/ |
|
448 |
protected Replica collectReplicaMetadata() |
|
449 |
throws ServiceFailure, InvalidRequest { |
|
450 |
|
|
451 |
Replica replica = null; |
|
452 |
File tmpDir = getTempDirectory(); |
|
453 |
MultipartRequest mr = null; |
|
454 |
Map<String, File> mmFileParts = null; |
|
455 |
File replicaFile = null; |
|
456 |
InputStream replicaStream = null; |
|
457 |
|
|
458 |
// Read the incoming data from its Mime Multipart encoding |
|
459 |
logMetacat.debug("Parsing Replica from the mime multipart entity"); |
|
460 |
|
|
461 |
// handle MMP inputs |
|
462 |
MultipartRequestResolver mrr = |
|
463 |
new MultipartRequestResolver(tmpDir.getAbsolutePath(),1000000000, 0); |
|
464 |
|
|
465 |
try { |
|
466 |
mr = mrr.resolveMultipart(request); |
|
467 |
logMetacat.debug("Resolved the Replica multipart request."); |
|
468 |
|
|
469 |
} catch (IOException e) { |
|
470 |
throw new ServiceFailure("4852", "Couldn't resolve the multipart request: " + |
|
471 |
e.getMessage()); |
|
472 |
|
|
473 |
} catch (FileUploadException e) { |
|
474 |
throw new ServiceFailure("4852", "Couldn't resolve the multipart request: " + |
|
475 |
e.getMessage()); |
|
476 |
|
|
477 |
} catch (Exception e) { |
|
478 |
throw new ServiceFailure("4852", "Couldn't resolve the multipart request: " + |
|
479 |
e.getMessage()); |
|
480 |
|
|
481 |
} |
|
482 |
|
|
483 |
// get the map of file parts |
|
484 |
mmFileParts = mr.getMultipartFiles(); |
|
485 |
|
|
486 |
if ( mmFileParts == null || mmFileParts.keySet() == null) { |
|
487 |
throw new InvalidRequest("4853", "The multipart request must include " + |
|
488 |
"a file with the name 'replicaMetadata'."); |
|
489 |
|
|
490 |
} |
|
491 |
|
|
492 |
multipartparams = mr.getMultipartParameters(); |
|
493 |
replicaFile = mmFileParts.get("replicaMetadata"); |
|
494 |
|
|
495 |
if ( replicaFile == null ) { |
|
496 |
throw new InvalidRequest("4853", "The multipart request must include " + |
|
497 |
"a file with the name 'replicaMetadata'."); |
|
498 |
|
|
499 |
} |
|
500 |
|
|
501 |
|
|
502 |
// deserialize the ReplicationPolicy |
|
503 |
try { |
|
504 |
replicaStream = new FileInputStream(replicaFile); |
|
505 |
} catch (FileNotFoundException e) { |
|
506 |
throw new ServiceFailure("4852", "Couldn't find the multipart file: " + |
|
507 |
e.getMessage()); |
|
508 |
|
|
509 |
} |
|
510 |
|
|
511 |
try { |
|
512 |
replica = TypeMarshaller.unmarshalTypeFromStream(Replica.class, replicaStream); |
|
513 |
} catch (IOException e) { |
|
514 |
throw new ServiceFailure("4852", "Couldn't deserialize the replica document: " + |
|
515 |
e.getMessage()); |
|
516 |
|
|
517 |
} catch (InstantiationException e) { |
|
518 |
throw new ServiceFailure("4852", "Couldn't deserialize the replica document: " + |
|
519 |
e.getMessage()); |
|
520 |
|
|
521 |
} catch (IllegalAccessException e) { |
|
522 |
throw new ServiceFailure("4852", "Couldn't deserialize the replica document: " + |
|
523 |
e.getMessage()); |
|
524 |
|
|
525 |
} catch (JiBXException e) { |
|
526 |
throw new ServiceFailure("4852", "Couldn't deserialize the replica document: " + |
|
527 |
e.getMessage()); |
|
528 |
|
|
529 |
} |
|
530 |
|
|
531 |
return replica; |
|
532 |
|
|
533 |
} |
|
534 |
|
|
535 |
protected AccessPolicy collectAccessPolicy() |
|
536 |
throws IOException, ServiceFailure, InvalidRequest, JiBXException, |
|
537 |
InstantiationException, IllegalAccessException, ParserConfigurationException, |
|
538 |
SAXException { |
|
539 |
|
|
540 |
// Read the incoming data from its Mime Multipart encoding |
|
541 |
logMetacat.debug("Disassembling MIME multipart form"); |
|
542 |
InputStream ap = null; |
|
543 |
|
|
544 |
// handle MMP inputs |
|
545 |
File tmpDir = getTempDirectory(); |
|
546 |
logMetacat.debug("temp dir: " + tmpDir.getAbsolutePath()); |
|
547 |
MultipartRequestResolver mrr = |
|
548 |
new MultipartRequestResolver(tmpDir.getAbsolutePath(), 1000000000, 0); |
|
549 |
MultipartRequest mr = null; |
|
550 |
try { |
|
551 |
mr = mrr.resolveMultipart(request); |
|
552 |
} catch (Exception e) { |
|
553 |
throw new ServiceFailure("2161", |
|
554 |
"Could not resolve multipart: " + e.getMessage()); |
|
555 |
} |
|
556 |
logMetacat.debug("resolved multipart request"); |
|
557 |
Map<String, File> files = mr.getMultipartFiles(); |
|
558 |
if (files == null || files.keySet() == null) { |
|
559 |
throw new InvalidRequest("2163", |
|
560 |
"must have multipart file with name 'accessPolicy'"); |
|
561 |
} |
|
562 |
logMetacat.debug("got multipart files"); |
|
563 |
|
|
564 |
multipartparams = mr.getMultipartParameters(); |
|
565 |
|
|
566 |
File apFile = files.get("accessPolicy"); |
|
567 |
if (apFile == null) { |
|
568 |
throw new InvalidRequest("2163", |
|
569 |
"Missing the required file-part 'accessPolicy' from the multipart request."); |
|
570 |
} |
|
571 |
logMetacat.debug("apFile: " + apFile.getAbsolutePath()); |
|
572 |
ap = new FileInputStream(apFile); |
|
573 |
|
|
574 |
AccessPolicy accessPolicy = TypeMarshaller.unmarshalTypeFromStream(AccessPolicy.class, ap); |
|
575 |
return accessPolicy; |
|
576 |
} |
|
577 |
|
|
578 |
protected SystemMetadata collectSystemMetadata() |
|
579 |
throws IOException, FileUploadException, ServiceFailure, InvalidRequest, |
|
580 |
JiBXException, InstantiationException, IllegalAccessException { |
|
581 |
|
|
582 |
// Read the incoming data from its Mime Multipart encoding |
|
583 |
logMetacat.debug("Disassembling MIME multipart form"); |
|
584 |
InputStream sysmeta = null; |
|
585 |
|
|
586 |
// handle MMP inputs |
|
587 |
File tmpDir = getTempDirectory(); |
|
588 |
logMetacat.debug("temp dir: " + tmpDir.getAbsolutePath()); |
|
589 |
MultipartRequestResolver mrr = |
|
590 |
new MultipartRequestResolver(tmpDir.getAbsolutePath(), 1000000000, 0); |
|
591 |
MultipartRequest mr = null; |
|
592 |
try { |
|
593 |
mr = mrr.resolveMultipart(request); |
|
594 |
|
|
595 |
} catch (Exception e) { |
|
596 |
if ( logMetacat.isDebugEnabled() ) { |
|
597 |
e.printStackTrace(); |
|
598 |
|
|
599 |
} |
|
600 |
throw new ServiceFailure("1202", |
|
601 |
"Could not resolve multipart: " + e.getMessage()); |
|
602 |
|
|
603 |
} |
|
604 |
logMetacat.debug("resolved multipart request"); |
|
605 |
Map<String, File> files = mr.getMultipartFiles(); |
|
606 |
if (files == null) { |
|
607 |
throw new ServiceFailure("1202", |
|
608 |
"register meta must have multipart file with name 'sysmeta'"); |
|
609 |
} |
|
610 |
logMetacat.debug("got multipart files"); |
|
611 |
|
|
612 |
if (files.keySet() == null) { |
|
613 |
logMetacat.error("No file keys in MMP request."); |
|
614 |
throw new ServiceFailure( |
|
615 |
"1202", |
|
616 |
"No file keys found in MMP. " |
|
617 |
+ "register meta must have multipart file with name 'sysmeta'"); |
|
618 |
} |
|
619 |
|
|
620 |
// for logging purposes, dump out the key-value pairs that |
|
621 |
// constitute the request |
|
622 |
// 3 types exist: request params, multipart params, and |
|
623 |
// multipart files |
|
624 |
Iterator it = files.keySet().iterator(); |
|
625 |
logMetacat.debug("iterating through request parts: " + it); |
|
626 |
while (it.hasNext()) { |
|
627 |
String key = (String) it.next(); |
|
628 |
logMetacat.debug("files key: " + key); |
|
629 |
logMetacat.debug("files value: " + files.get(key)); |
|
630 |
} |
|
631 |
|
|
632 |
multipartparams = mr.getMultipartParameters(); |
|
633 |
it = multipartparams.keySet().iterator(); |
|
634 |
while (it.hasNext()) { |
|
635 |
String key = (String) it.next(); |
|
636 |
logMetacat.debug("multipartparams key: " + key); |
|
637 |
logMetacat.debug("multipartparams value: " + multipartparams.get(key)); |
|
638 |
} |
|
639 |
|
|
640 |
it = params.keySet().iterator(); |
|
641 |
while (it.hasNext()) { |
|
642 |
String key = (String) it.next(); |
|
643 |
logMetacat.debug("param key: " + key); |
|
644 |
logMetacat.debug("param value: " + params.get(key)); |
|
645 |
} |
|
646 |
logMetacat.debug("done iterating the request..."); |
|
647 |
|
|
648 |
File smFile = files.get("sysmeta"); |
|
649 |
if (smFile == null) { |
|
650 |
throw new InvalidRequest("1102", |
|
651 |
"Missing the required file-part 'sysmeta' from the multipart request."); |
|
652 |
} |
|
653 |
logMetacat.debug("smFile: " + smFile.getAbsolutePath()); |
|
654 |
sysmeta = new FileInputStream(smFile); |
|
655 |
|
|
656 |
logMetacat.debug("Commence creation..."); |
|
657 |
SystemMetadata systemMetadata = TypeMarshaller.unmarshalTypeFromStream(SystemMetadata.class, sysmeta); |
|
658 |
return systemMetadata; |
|
659 |
} |
|
660 |
|
|
661 |
/** |
|
662 | 270 |
* Process the MMP request that includes files for each param |
663 | 271 |
* @return map of param key and the temp file that contains the encoded information |
664 | 272 |
* @throws ServiceFailure |
Also available in: Unified diff
add support for v2 DataONE API.