Revision 6269
Added by ben leinfelder almost 13 years ago
CNResourceHandler.java | ||
---|---|---|
31 | 31 |
import java.io.PrintWriter; |
32 | 32 |
import java.util.Date; |
33 | 33 |
import java.util.Hashtable; |
34 |
import java.util.Iterator; |
|
35 |
import java.util.List; |
|
36 | 34 |
import java.util.Map; |
37 | 35 |
import java.util.Timer; |
38 | 36 |
|
... | ... | |
40 | 38 |
import javax.servlet.http.HttpServletRequest; |
41 | 39 |
import javax.servlet.http.HttpServletResponse; |
42 | 40 |
|
41 |
import org.apache.commons.fileupload.FileUploadException; |
|
43 | 42 |
import org.apache.commons.io.IOUtils; |
44 | 43 |
import org.apache.log4j.Logger; |
45 | 44 |
import org.dataone.client.ObjectFormatCache; |
46 | 45 |
import org.dataone.client.auth.CertificateManager; |
47 |
import org.dataone.mimemultipart.MultipartRequest; |
|
48 |
import org.dataone.mimemultipart.MultipartRequestResolver; |
|
49 | 46 |
import org.dataone.service.exceptions.BaseException; |
50 | 47 |
import org.dataone.service.exceptions.IdentifierNotUnique; |
51 | 48 |
import org.dataone.service.exceptions.InsufficientResources; |
... | ... | |
69 | 66 |
import org.dataone.service.types.Permission; |
70 | 67 |
import org.dataone.service.types.Subject; |
71 | 68 |
import org.dataone.service.types.SystemMetadata; |
72 |
import org.jibx.runtime.BindingDirectory; |
|
73 |
import org.jibx.runtime.IBindingFactory; |
|
74 |
import org.jibx.runtime.IUnmarshallingContext; |
|
75 | 69 |
import org.jibx.runtime.JiBXException; |
76 | 70 |
|
77 |
import edu.ucsb.nceas.metacat.IdentifierManager; |
|
78 | 71 |
import edu.ucsb.nceas.metacat.MetacatHandler; |
79 | 72 |
import edu.ucsb.nceas.metacat.dataone.CNodeService; |
80 | 73 |
|
... | ... | |
585 | 578 |
* |
586 | 579 |
* @param guid - ID of data object to be inserted or updated. If action is update, the pid |
587 | 580 |
* is the existing pid. If insert, the pid is the new one |
588 |
* @throws IOException |
|
581 |
* @throws InvalidRequest |
|
582 |
* @throws ServiceFailure |
|
583 |
* @throws IdentifierNotUnique |
|
584 |
* @throws JiBXException |
|
585 |
* @throws NotImplemented |
|
586 |
* @throws InvalidSystemMetadata |
|
587 |
* @throws InsufficientResources |
|
588 |
* @throws UnsupportedType |
|
589 |
* @throws NotAuthorized |
|
590 |
* @throws InvalidToken |
|
591 |
* @throws IOException |
|
589 | 592 |
*/ |
590 |
protected void putObject(String pid, String action) { |
|
591 |
logMetacat.debug("putObject with pid " + pid); |
|
593 |
protected void putObject(String pid, String action) throws ServiceFailure, InvalidRequest, IdentifierNotUnique, JiBXException, InvalidToken, NotAuthorized, UnsupportedType, InsufficientResources, InvalidSystemMetadata, NotImplemented, IOException { |
|
592 | 594 |
logMetacat.debug("Entering putObject: " + pid + "/" + action); |
593 |
OutputStream out = null; |
|
594 |
try { |
|
595 |
out = response.getOutputStream(); |
|
596 |
response.setStatus(200); |
|
597 |
response.setContentType("text/xml"); |
|
598 |
} catch (IOException e1) { |
|
599 |
logMetacat.error("Could not get the output stream for writing in putObject"); |
|
600 |
} |
|
601 |
try { |
|
602 |
|
|
603 |
// Read the incoming data from its Mime Multipart encoding |
|
604 |
logMetacat.debug("Disassembling MIME multipart form"); |
|
605 |
InputStream object = null; |
|
606 |
InputStream sysmeta = null; |
|
607 |
Map<String, List<String>> multipartparams; |
|
608 |
|
|
609 |
try |
|
610 |
{ |
|
611 |
//String req = IOUtils.toString(request.getInputStream()); |
|
612 |
//logMetacat.debug("request: " + req); |
|
613 |
//InputStream reqStr = IOUtils.toInputStream(req); |
|
614 |
InputStream reqStr = request.getInputStream(); |
|
615 |
|
|
616 |
//handle MMP inputs |
|
617 |
File tmpDir = getTempDirectory(); |
|
618 |
File tmpSMFile = new File(tmpDir + |
|
619 |
".sysmeta." + new Date().getTime() + ".tmp"); |
|
620 |
logMetacat.debug("temp dir: " + tmpDir.getAbsolutePath()); |
|
621 |
MultipartRequestResolver mrr = new MultipartRequestResolver( |
|
622 |
tmpDir.getAbsolutePath(), 1000000000, 0); |
|
623 |
MultipartRequest mr = mrr.resolveMultipart(request); |
|
624 |
logMetacat.debug("resolved multipart request"); |
|
625 |
Map<String, File> files = mr.getMultipartFiles(); |
|
626 |
if(files == null) |
|
627 |
{ |
|
628 |
throw new ServiceFailure("1202", "create/update must have multipart files with names 'object' and 'sysmeta'"); |
|
629 |
} |
|
630 |
logMetacat.debug("got multipart files"); |
|
631 |
|
|
632 |
if(files.keySet() == null) |
|
633 |
{ |
|
634 |
logMetacat.error("No file keys in MMP request."); |
|
635 |
throw new ServiceFailure("1202", "No file keys found in MMP. " + |
|
636 |
"create/update must have multipart files with names 'object' and 'sysmeta'"); |
|
637 |
} |
|
595 |
|
|
596 |
// Read the incoming data from its Mime Multipart encoding |
|
597 |
Map<String, File> files = collectMultipartFiles(); |
|
598 |
InputStream object = null; |
|
599 |
InputStream sysmeta = null; |
|
638 | 600 |
|
639 |
// for logging purposes, dump out the key-value pairs that constitute the request |
|
640 |
// 3 types exist: request params, multipart params, and multipart files |
|
641 |
Iterator it = files.keySet().iterator(); |
|
642 |
logMetacat.debug("iterating through request parts: " + it); |
|
643 |
while(it.hasNext()) |
|
644 |
{ |
|
645 |
String key = (String)it.next(); |
|
646 |
logMetacat.debug("files key: " + key); |
|
647 |
logMetacat.debug("files value: " + files.get(key)); |
|
648 |
} |
|
649 |
|
|
650 |
multipartparams = mr.getMultipartParameters(); |
|
651 |
it = multipartparams.keySet().iterator(); |
|
652 |
while(it.hasNext()) |
|
653 |
{ |
|
654 |
String key = (String)it.next(); |
|
655 |
logMetacat.debug("multipartparams key: " + key); |
|
656 |
logMetacat.debug("multipartparams value: " + multipartparams.get(key)); |
|
657 |
} |
|
658 |
|
|
659 |
it = params.keySet().iterator(); |
|
660 |
while(it.hasNext()) |
|
661 |
{ |
|
662 |
String key = (String)it.next(); |
|
663 |
logMetacat.debug("param key: " + key); |
|
664 |
logMetacat.debug("param value: " + params.get(key)); |
|
665 |
} |
|
666 |
logMetacat.debug("done iterating the request..."); |
|
601 |
File smFile = files.get("sysmeta"); |
|
602 |
sysmeta = new FileInputStream(smFile); |
|
603 |
File objFile = files.get("object"); |
|
604 |
object = new FileInputStream(objFile); |
|
605 |
|
|
606 |
if ( action.equals(FUNCTION_NAME_INSERT)) { //handle inserts |
|
667 | 607 |
|
668 |
File smFile = files.get("sysmeta"); |
|
669 |
if (smFile == null) |
|
670 |
throw new InvalidRequest("1102", "Missing the required file-part 'sysmeta' from the multipart request."); |
|
671 |
logMetacat.debug("smFile: " + smFile.getAbsolutePath()); |
|
672 |
sysmeta = new FileInputStream(smFile); |
|
673 |
File objFile = files.get("object"); |
|
674 |
if (objFile == null) |
|
675 |
throw new InvalidRequest("1102", "Missing the required file-part 'object' from the multipart request."); |
|
608 |
logMetacat.debug("Commence creation..."); |
|
609 |
SystemMetadata smd = (SystemMetadata) deserializeServiceType(SystemMetadata.class, sysmeta); |
|
676 | 610 |
|
677 |
logMetacat.debug("objectfile: " + objFile.getAbsolutePath()); |
|
678 |
object = new FileInputStream(objFile); |
|
679 |
|
|
680 |
/*String obj = IOUtils.toString(object); |
|
681 |
String sm = IOUtils.toString(sysmeta); |
|
682 |
logMetacat.debug("object: " + obj); |
|
683 |
logMetacat.debug("sm: " + sm); |
|
684 |
object = IOUtils.toInputStream(obj); |
|
685 |
sysmeta = IOUtils.toInputStream(sm);*/ |
|
686 |
|
|
687 |
} |
|
688 |
catch(org.apache.commons.fileupload.FileUploadException fue) |
|
689 |
{ |
|
690 |
throw new ServiceFailure("1202", "Could not upload MMP files: " + fue.getMessage()); |
|
691 |
} |
|
692 |
catch(IOException ioe) |
|
693 |
{ |
|
694 |
throw new ServiceFailure("1202", |
|
695 |
"IOException when processing Mime Multipart: " + ioe.getMessage()); |
|
696 |
} |
|
697 |
catch(Exception e) |
|
698 |
{ |
|
699 |
throw new ServiceFailure("1202", "Error handling MMP upload: " + e.getClass() + ": " + e.getMessage()); |
|
700 |
} |
|
611 |
Identifier id = new Identifier(); |
|
612 |
id.setValue(pid); |
|
613 |
logMetacat.debug("creating object with pid " + id.getValue()); |
|
614 |
Identifier rId = CNodeService.getInstance().create(session, id, object, smd); |
|
701 | 615 |
|
702 |
if ( action.equals(FUNCTION_NAME_INSERT)) { //handle inserts |
|
703 |
|
|
704 |
// Check if the objectId exists |
|
705 |
IdentifierManager im = IdentifierManager.getInstance(); |
|
706 |
if (im.identifierExists(pid)) { |
|
707 |
throw new IdentifierNotUnique("1000", "Identifier is already in use: " + pid); |
|
708 |
} |
|
709 |
|
|
710 |
logMetacat.debug("Commence creation..."); |
|
711 |
IBindingFactory bfact = |
|
712 |
BindingDirectory.getFactory(SystemMetadata.class); |
|
713 |
IUnmarshallingContext uctx = bfact.createUnmarshallingContext(); |
|
714 |
SystemMetadata smd = (SystemMetadata) uctx.unmarshalDocument(sysmeta, null); |
|
715 |
|
|
716 |
Identifier id = new Identifier(); |
|
717 |
id.setValue(pid); |
|
718 |
logMetacat.debug("creating object with pid " + id.getValue()); |
|
719 |
Identifier rId = CNodeService.getInstance().create(session, id, object, smd); |
|
720 |
serializeServiceType(Identifier.class, rId, out); |
|
721 |
|
|
722 |
} else { |
|
723 |
throw new InvalidRequest("1000", "Operation must be create or update."); |
|
724 |
} |
|
616 |
OutputStream out = response.getOutputStream(); |
|
617 |
response.setStatus(200); |
|
618 |
response.setContentType("text/xml"); |
|
725 | 619 |
|
726 |
//clean up the MMP files |
|
727 |
//parts.get("systemmetadata").delete(); |
|
728 |
//parts.get("object").delete(); |
|
729 |
} catch (NotAuthorized e) { |
|
730 |
response.setStatus(500); |
|
731 |
serializeException(e, out); |
|
732 |
} catch (InvalidToken e) { |
|
733 |
response.setStatus(500); |
|
734 |
serializeException(e, out); |
|
735 |
} catch (ServiceFailure e) { |
|
736 |
response.setStatus(500); |
|
737 |
serializeException(e, out); |
|
738 |
} catch (IdentifierNotUnique e) { |
|
739 |
response.setStatus(500); |
|
740 |
serializeException(e, out); |
|
741 |
} catch (UnsupportedType e) { |
|
742 |
response.setStatus(500); |
|
743 |
serializeException(e, out); |
|
744 |
} catch (InsufficientResources e) { |
|
745 |
response.setStatus(500); |
|
746 |
serializeException(e, out); |
|
747 |
} catch (InvalidSystemMetadata e) { |
|
748 |
response.setStatus(500); |
|
749 |
serializeException(e, out); |
|
750 |
} catch (NotImplemented e) { |
|
751 |
response.setStatus(500); |
|
752 |
serializeException(e, out); |
|
753 |
} catch (InvalidRequest e) { |
|
754 |
response.setStatus(500); |
|
755 |
serializeException(e, out); |
|
756 |
} /*catch (MessagingException e) { |
|
757 |
ServiceFailure sf = new ServiceFailure("1000", e.getMessage()); |
|
758 |
serializeException(sf, out); |
|
759 |
} catch (IOException e) { |
|
760 |
response.setStatus(500); |
|
761 |
ServiceFailure sf = new ServiceFailure("1000", e.getMessage()); |
|
762 |
serializeException(sf, out); |
|
763 |
}*/ catch (JiBXException e) { |
|
764 |
response.setStatus(500); |
|
765 |
e.printStackTrace(System.out); |
|
766 |
InvalidSystemMetadata ism = new InvalidSystemMetadata("1080", e.getMessage()); |
|
767 |
serializeException(ism, out); |
|
620 |
serializeServiceType(Identifier.class, rId, out); |
|
621 |
|
|
622 |
} else { |
|
623 |
throw new InvalidRequest("1000", "Operation must be create."); |
|
768 | 624 |
} |
769 | 625 |
} |
770 | 626 |
|
... | ... | |
974 | 830 |
/** |
975 | 831 |
* Register System Metadata without data or metadata object |
976 | 832 |
* @param pid identifier for System Metadata entry |
833 |
* @throws JiBXException |
|
834 |
* @throws FileUploadException |
|
835 |
* @throws IOException |
|
836 |
* @throws InvalidRequest |
|
837 |
* @throws ServiceFailure |
|
838 |
* @throws InvalidSystemMetadata |
|
839 |
* @throws NotAuthorized |
|
840 |
* @throws NotImplemented |
|
977 | 841 |
*/ |
978 |
protected void registerSystemMetadata(String pid) {
|
|
842 |
protected boolean registerSystemMetadata(String pid) throws ServiceFailure, InvalidRequest, IOException, FileUploadException, JiBXException, NotImplemented, NotAuthorized, InvalidSystemMetadata {
|
|
979 | 843 |
logMetacat.debug("Entering registerSystemMetadata: " + pid); |
980 |
OutputStream out = null; |
|
981 |
try { |
|
982 |
out = response.getOutputStream(); |
|
983 |
response.setStatus(200); |
|
984 |
response.setContentType("text/xml"); |
|
985 |
} catch (IOException e1) { |
|
986 |
logMetacat.error("Could not get the output stream for writing in putObject"); |
|
987 |
} |
|
988 |
try { |
|
989 | 844 |
|
990 |
// Read the incoming data from its Mime Multipart encoding |
|
991 |
logMetacat.debug("Disassembling MIME multipart form"); |
|
992 |
InputStream sysmeta = null; |
|
993 |
Map<String, List<String>> multipartparams; |
|
845 |
// get the system metadata from the request |
|
846 |
SystemMetadata systemMetadata = collectSystemMetadata(); |
|
994 | 847 |
|
995 |
try { |
|
996 |
|
|
997 |
// handle MMP inputs |
|
998 |
File tmpDir = getTempDirectory(); |
|
999 |
|
|
1000 |
logMetacat.debug("temp dir: " + tmpDir.getAbsolutePath()); |
|
1001 |
MultipartRequestResolver mrr = new MultipartRequestResolver( |
|
1002 |
tmpDir.getAbsolutePath(), 1000000000, 0); |
|
1003 |
MultipartRequest mr = mrr.resolveMultipart(request); |
|
1004 |
logMetacat.debug("resolved multipart request"); |
|
1005 |
Map<String, File> files = mr.getMultipartFiles(); |
|
1006 |
if (files == null) { |
|
1007 |
throw new ServiceFailure("1202", |
|
1008 |
"register meta must have multipart file with name 'sysmeta'"); |
|
1009 |
} |
|
1010 |
logMetacat.debug("got multipart files"); |
|
1011 |
|
|
1012 |
if (files.keySet() == null) { |
|
1013 |
logMetacat.error("No file keys in MMP request."); |
|
1014 |
throw new ServiceFailure( |
|
1015 |
"1202", |
|
1016 |
"No file keys found in MMP. " |
|
1017 |
+ "register meta must have multipart file with name 'sysmeta'"); |
|
1018 |
} |
|
1019 |
|
|
1020 |
// for logging purposes, dump out the key-value pairs that |
|
1021 |
// constitute the request |
|
1022 |
// 3 types exist: request params, multipart params, and |
|
1023 |
// multipart files |
|
1024 |
Iterator it = files.keySet().iterator(); |
|
1025 |
logMetacat.debug("iterating through request parts: " + it); |
|
1026 |
while (it.hasNext()) { |
|
1027 |
String key = (String) it.next(); |
|
1028 |
logMetacat.debug("files key: " + key); |
|
1029 |
logMetacat.debug("files value: " + files.get(key)); |
|
1030 |
} |
|
1031 |
|
|
1032 |
multipartparams = mr.getMultipartParameters(); |
|
1033 |
it = multipartparams.keySet().iterator(); |
|
1034 |
while (it.hasNext()) { |
|
1035 |
String key = (String) it.next(); |
|
1036 |
logMetacat.debug("multipartparams key: " + key); |
|
1037 |
logMetacat.debug("multipartparams value: " |
|
1038 |
+ multipartparams.get(key)); |
|
1039 |
} |
|
1040 |
|
|
1041 |
it = params.keySet().iterator(); |
|
1042 |
while (it.hasNext()) { |
|
1043 |
String key = (String) it.next(); |
|
1044 |
logMetacat.debug("param key: " + key); |
|
1045 |
logMetacat.debug("param value: " + params.get(key)); |
|
1046 |
} |
|
1047 |
logMetacat.debug("done iterating the request..."); |
|
1048 |
|
|
1049 |
File smFile = files.get("sysmeta"); |
|
1050 |
if (smFile == null) { |
|
1051 |
throw new InvalidRequest("1102", |
|
1052 |
"Missing the required file-part 'sysmeta' from the multipart request."); |
|
1053 |
} |
|
1054 |
logMetacat.debug("smFile: " + smFile.getAbsolutePath()); |
|
1055 |
sysmeta = new FileInputStream(smFile); |
|
1056 |
|
|
1057 |
} catch (org.apache.commons.fileupload.FileUploadException fue) { |
|
1058 |
throw new ServiceFailure("1202", "Could not upload MMP files: " |
|
1059 |
+ fue.getMessage()); |
|
1060 |
} catch (IOException ioe) { |
|
1061 |
throw new ServiceFailure("1202", |
|
1062 |
"IOException when processing Mime Multipart: " |
|
1063 |
+ ioe.getMessage()); |
|
1064 |
} catch (Exception e) { |
|
1065 |
throw new ServiceFailure("1202", "Error handling MMP upload: " |
|
1066 |
+ e.getClass() + ": " + e.getMessage()); |
|
1067 |
} |
|
1068 |
|
|
1069 |
// Check if the objectId exists |
|
1070 |
IdentifierManager im = IdentifierManager.getInstance(); |
|
1071 |
if (im.identifierExists(pid)) { |
|
1072 |
throw new IdentifierNotUnique("1000", |
|
1073 |
"Identifier is already in use: " + pid); |
|
1074 |
} |
|
1075 |
|
|
1076 |
logMetacat.debug("Commence creation..."); |
|
1077 |
IBindingFactory bfact = BindingDirectory |
|
1078 |
.getFactory(SystemMetadata.class); |
|
1079 |
IUnmarshallingContext uctx = bfact.createUnmarshallingContext(); |
|
1080 |
SystemMetadata systemMetadata = (SystemMetadata) uctx |
|
1081 |
.unmarshalDocument(sysmeta, null); |
|
1082 |
|
|
1083 |
Identifier guid = new Identifier(); |
|
1084 |
guid.setValue(pid); |
|
1085 |
logMetacat.debug("registering system metadata with pid " |
|
1086 |
+ guid.getValue()); |
|
1087 |
boolean result = CNodeService.getInstance().registerSystemMetadata( |
|
1088 |
session, guid, systemMetadata); |
|
1089 |
serializeServiceType(Boolean.class, result, out); |
|
1090 |
|
|
1091 |
} catch (NotAuthorized e) { |
|
1092 |
response.setStatus(500); |
|
1093 |
serializeException(e, out); |
|
1094 |
} catch (ServiceFailure e) { |
|
1095 |
response.setStatus(500); |
|
1096 |
serializeException(e, out); |
|
1097 |
} catch (IdentifierNotUnique e) { |
|
1098 |
response.setStatus(500); |
|
1099 |
serializeException(e, out); |
|
1100 |
} catch (NotImplemented e) { |
|
1101 |
response.setStatus(500); |
|
1102 |
serializeException(e, out); |
|
1103 |
} catch (InvalidRequest e) { |
|
1104 |
response.setStatus(500); |
|
1105 |
serializeException(e, out); |
|
1106 |
} catch (JiBXException e) { |
|
1107 |
response.setStatus(500); |
|
1108 |
e.printStackTrace(System.out); |
|
1109 |
InvalidSystemMetadata ism = new InvalidSystemMetadata("1080", e |
|
1110 |
.getMessage()); |
|
1111 |
serializeException(ism, out); |
|
1112 |
} catch (InvalidSystemMetadata e) { |
|
1113 |
serializeException(e, out); |
|
1114 |
} |
|
848 |
Identifier guid = new Identifier(); |
|
849 |
guid.setValue(pid); |
|
850 |
logMetacat.debug("registering system metadata with pid " + guid.getValue()); |
|
851 |
boolean result = CNodeService.getInstance().registerSystemMetadata(session, guid, systemMetadata); |
|
852 |
|
|
853 |
response.setStatus(200); |
|
854 |
response.setContentType("text/xml"); |
|
855 |
return result; |
|
856 |
|
|
1115 | 857 |
} |
1116 | 858 |
|
1117 | 859 |
/** |
Also available in: Unified diff
consolidate multi part handling in the super class - subclasses need only call the appropriate helper to get access to the needed resources. superclass does some validation to make sure the files are in place in the request