Revision 6269
Added by ben leinfelder almost 13 years ago
MNResourceHandler.java | ||
---|---|---|
25 | 25 |
import java.io.ByteArrayInputStream; |
26 | 26 |
import java.io.File; |
27 | 27 |
import java.io.FileInputStream; |
28 |
import java.io.FileNotFoundException; |
|
28 | 29 |
import java.io.IOException; |
29 | 30 |
import java.io.InputStream; |
30 | 31 |
import java.io.OutputStream; |
... | ... | |
46 | 47 |
import javax.servlet.http.HttpServletRequest; |
47 | 48 |
import javax.servlet.http.HttpServletResponse; |
48 | 49 |
|
49 |
import org.apache.commons.fileupload.FileUploadException; |
|
50 | 50 |
import org.apache.commons.io.IOUtils; |
51 | 51 |
import org.apache.log4j.Logger; |
52 | 52 |
import org.dataone.client.ObjectFormatCache; |
... | ... | |
878 | 878 |
} |
879 | 879 |
} |
880 | 880 |
|
881 |
/** |
|
882 |
* Earthgrid API > Query Service > Query Function : translates ecogrid query document to metacat query |
|
883 |
* then calls DBQuery > createResultDocument function and then again translate resultset to ecogrid resultset |
|
884 |
* |
|
885 |
* NOTE: |
|
886 |
* This is the only method that uses EcoGrid classes for its implementation. |
|
887 |
* It does so because it takes an EcoGrid Query as input, and outputs an |
|
888 |
* EcoGrid ResultSet document. These documents are parsed by the auto-generated |
|
889 |
* EcoGrid classes from axis, and so we link to them here rather than re-inventing them. |
|
890 |
* This creates a circular dependency, because the Metacat classes are needed |
|
891 |
* to build the EcoGrid implementation, and the EcoGrid jars are needed to build this query() |
|
892 |
* method. This circularity could be resolved by moving the EcoGrid classes |
|
893 |
* to Metacat directly. As we transition away from EcoGrid SOAP methods in |
|
894 |
* favor of these REST interfaces, this circular dependency can be eliminated. |
|
895 |
* |
|
896 |
* @throws Exception |
|
897 |
*/ |
|
898 |
private void query() throws Exception { |
|
899 |
/* This block commented out because of the EcoGrid circular dependency. |
|
900 |
* For now, query will not be supported until the circularity can be |
|
901 |
* resolved, probably by moving the ecogrid query syntax transformers |
|
902 |
* directly into the Metacat codebase. MBJ 2010-02-03 |
|
903 |
|
|
904 |
try { |
|
905 |
EcogridQueryParser parser = new EcogridQueryParser(request |
|
906 |
.getReader()); |
|
907 |
parser.parseXML(); |
|
908 |
QueryType queryType = parser.getEcogridQuery(); |
|
909 |
EcogridJavaToMetacatJavaQueryTransformer queryTransformer = |
|
910 |
new EcogridJavaToMetacatJavaQueryTransformer(); |
|
911 |
QuerySpecification metacatQuery = queryTransformer |
|
912 |
.transform(queryType); |
|
913 |
|
|
914 |
DBQuery metacat = new DBQuery(); |
|
915 |
|
|
916 |
boolean useXMLIndex = (new Boolean(PropertyService |
|
917 |
.getProperty("database.usexmlindex"))).booleanValue(); |
|
918 |
String xmlquery = "query"; // we don't care the query in resultset, |
|
919 |
// the query can be anything |
|
920 |
PrintWriter out = null; // we don't want metacat result, so set out null |
|
921 |
|
|
922 |
// parameter: queryspecification, user, group, usingIndexOrNot |
|
923 |
StringBuffer result = metacat.createResultDocument(xmlquery, |
|
924 |
metacatQuery, out, username, groupNames, useXMLIndex); |
|
925 |
|
|
926 |
// create result set transfer |
|
927 |
String saxparser = PropertyService.getProperty("xml.saxparser"); |
|
928 |
MetacatResultsetParser metacatResultsetParser = new MetacatResultsetParser( |
|
929 |
new StringReader(result.toString()), saxparser, queryType |
|
930 |
.getNamespace().get_value()); |
|
931 |
ResultsetType records = metacatResultsetParser.getEcogridResult(); |
|
932 |
|
|
933 |
System.out |
|
934 |
.println(EcogridResultsetTransformer.toXMLString(records)); |
|
935 |
response.setContentType("text/xml"); |
|
936 |
out = response.getWriter(); |
|
937 |
out.print(EcogridResultsetTransformer.toXMLString(records)); |
|
938 |
|
|
939 |
} catch (Exception e) { |
|
940 |
e.printStackTrace(); |
|
941 |
}*/ |
|
942 |
response.setContentType("text/xml"); |
|
943 |
response.setStatus(501); |
|
944 |
PrintWriter out = response.getWriter(); |
|
945 |
out.print("<error>Query operation not yet supported by Metacat.</error>"); |
|
946 |
out.close(); |
|
947 |
} |
|
948 | 881 |
|
949 | 882 |
/** |
950 |
* return a tmp file with a given name |
|
951 |
* @param name |
|
952 |
* @return |
|
953 |
*/ |
|
954 |
private static File getTempFile(String name) |
|
955 |
{ |
|
956 |
File tmpDir = getTempDirectory(); |
|
957 |
File f = new File(tmpDir, name); |
|
958 |
return f; |
|
959 |
} |
|
960 |
|
|
961 |
/** |
|
962 |
* return a temp file with a default name |
|
963 |
* @return |
|
964 |
*/ |
|
965 |
private static File getTempFile() |
|
966 |
{ |
|
967 |
return getTempFile(new Date().getTime() + ".tmp"); |
|
968 |
} |
|
969 |
|
|
970 |
/** |
|
971 | 883 |
* Inserts or updates the object |
972 | 884 |
* |
973 | 885 |
* @param guid - ID of data object to be inserted or updated. If action is update, the pid |
974 | 886 |
* is the existing pid. If insert, the pid is the new one |
887 |
* @throws InvalidRequest |
|
888 |
* @throws ServiceFailure |
|
889 |
* @throws FileNotFoundException |
|
890 |
* @throws JiBXException |
|
891 |
* @throws NotImplemented |
|
892 |
* @throws InvalidSystemMetadata |
|
893 |
* @throws InsufficientResources |
|
894 |
* @throws UnsupportedType |
|
895 |
* @throws IdentifierNotUnique |
|
896 |
* @throws NotAuthorized |
|
897 |
* @throws InvalidToken |
|
898 |
* @throws NotFound |
|
975 | 899 |
*/ |
976 |
protected void putObject(String pid, String action) { |
|
900 |
protected void putObject(String pid, String action) throws ServiceFailure, InvalidRequest, FileNotFoundException, JiBXException, InvalidToken, NotAuthorized, IdentifierNotUnique, UnsupportedType, InsufficientResources, InvalidSystemMetadata, NotImplemented, NotFound {
|
|
977 | 901 |
System.out.println("putObject with pid " + pid); |
978 | 902 |
logMetacat.debug("Entering putObject: " + pid + "/" + action); |
979 | 903 |
OutputStream out = null; |
... | ... | |
984 | 908 |
} catch (IOException e1) { |
985 | 909 |
logMetacat.error("Could not get the output stream for writing in putObject"); |
986 | 910 |
} |
987 |
try { |
|
988 |
|
|
989 |
// Read the incoming data from its Mime Multipart encoding |
|
990 |
logMetacat.debug("Disassembling MIME multipart form"); |
|
991 |
InputStream object = null; |
|
992 |
InputStream sysmeta = null; |
|
993 |
Map<String, List<String>> multipartparams; |
|
994 |
|
|
995 |
try { |
|
911 |
|
|
912 |
// Read the incoming data from its Mime Multipart encoding |
|
913 |
Map<String, File> files = collectMultipartFiles(); |
|
914 |
InputStream object = null; |
|
915 |
InputStream sysmeta = null; |
|
996 | 916 |
|
997 |
// handle MMP inputs |
|
998 |
File tmpDir = getTempDirectory(); |
|
999 |
System.out.println("temp dir: " + tmpDir.getAbsolutePath()); |
|
1000 |
MultipartRequestResolver mrr = |
|
1001 |
new MultipartRequestResolver(tmpDir.getAbsolutePath(), 1000000000, 0); |
|
1002 |
MultipartRequest mr = mrr.resolveMultipart(request); |
|
1003 |
System.out.println("resolved multipart request"); |
|
1004 |
Map<String, File> files = mr.getMultipartFiles(); |
|
1005 |
if (files == null) { |
|
1006 |
throw new ServiceFailure("1202", "create/update must have multipart files with names 'object' and 'sysmeta'"); |
|
1007 |
} |
|
1008 |
System.out.println("got multipart files"); |
|
1009 |
|
|
1010 |
if (files.keySet() == null) { |
|
1011 |
System.out.println("No file keys in MMP request."); |
|
1012 |
throw new ServiceFailure("1202", "No file keys found in MMP. " + |
|
1013 |
"create/update must have multipart files with names 'object' and 'sysmeta'"); |
|
1014 |
} |
|
917 |
File smFile = files.get("sysmeta"); |
|
918 |
sysmeta = new FileInputStream(smFile); |
|
919 |
File objFile = files.get("object"); |
|
920 |
object = new FileInputStream(objFile); |
|
921 |
|
|
922 |
if (action.equals(FUNCTION_NAME_INSERT)) { |
|
923 |
// handle inserts |
|
924 |
logMetacat.debug("Commence creation..."); |
|
925 |
SystemMetadata smd = (SystemMetadata) deserializeServiceType(SystemMetadata.class, sysmeta); |
|
1015 | 926 |
|
1016 |
// for logging purposes, dump out the key-value pairs that constitute the request |
|
1017 |
// 3 types exist: request params, multipart params, and multipart files |
|
1018 |
Iterator it = files.keySet().iterator(); |
|
1019 |
System.out.println("iterating through files"); |
|
1020 |
while (it.hasNext()) { |
|
1021 |
String key = (String)it.next(); |
|
1022 |
System.out.println("files key: " + key); |
|
1023 |
System.out.println("files value: " + files.get(key)); |
|
1024 |
} |
|
1025 |
|
|
1026 |
multipartparams = mr.getMultipartParameters(); |
|
1027 |
it = multipartparams.keySet().iterator(); |
|
1028 |
System.out.println("iterating through multipartparams"); |
|
1029 |
while (it.hasNext()) { |
|
1030 |
String key = (String)it.next(); |
|
1031 |
System.out.println("multipartparams key: " + key); |
|
1032 |
System.out.println("multipartparams value: " + multipartparams.get(key)); |
|
1033 |
} |
|
1034 |
|
|
1035 |
it = params.keySet().iterator(); |
|
1036 |
System.out.println("iterating through params"); |
|
1037 |
while (it.hasNext()) { |
|
1038 |
String key = (String)it.next(); |
|
1039 |
System.out.println("param key: " + key); |
|
1040 |
System.out.println("param value: " + params.get(key)); |
|
1041 |
} |
|
1042 |
System.out.println("done iterating the request..."); |
|
1043 |
|
|
1044 |
File smFile = files.get("sysmeta"); |
|
1045 |
if (smFile == null) { |
|
1046 |
throw new InvalidRequest("1102", "Missing the required file-part 'sysmeta' from the multipart request."); |
|
1047 |
} |
|
1048 |
System.out.println("smFile: " + smFile.getAbsolutePath()); |
|
1049 |
sysmeta = new FileInputStream(smFile); |
|
1050 |
File objFile = files.get("object"); |
|
1051 |
if (objFile == null) { |
|
1052 |
throw new InvalidRequest("1102", "Missing the required file-part 'object' from the multipart request."); |
|
1053 |
} |
|
1054 |
System.out.println("objectfile: " + objFile.getAbsolutePath()); |
|
1055 |
object = new FileInputStream(objFile); |
|
1056 |
|
|
1057 |
} |
|
1058 |
catch (FileUploadException fue) |
|
1059 |
{ |
|
1060 |
throw new ServiceFailure("1202", "Could not upload MMP files: " + fue.getMessage()); |
|
1061 |
} |
|
1062 |
catch (IOException ioe) |
|
1063 |
{ |
|
1064 |
throw new ServiceFailure("1202", |
|
1065 |
"IOException when processing Mime Multipart: " + ioe.getMessage()); |
|
1066 |
} |
|
1067 |
catch (Exception e) |
|
1068 |
{ |
|
1069 |
throw new ServiceFailure("1202", "Error handling MMP upload: " + e.getClass() + ": " + e.getMessage()); |
|
1070 |
} |
|
927 |
Identifier id = new Identifier(); |
|
928 |
id.setValue(pid); |
|
929 |
System.out.println("creating object with pid " + pid); |
|
930 |
Identifier rId = MNodeService.getInstance().create(session, id, object, smd); |
|
931 |
serializeServiceType(Identifier.class, rId, out); |
|
1071 | 932 |
|
1072 |
// handle inserts |
|
1073 |
if (action.equals(FUNCTION_NAME_INSERT)) { |
|
1074 |
|
|
1075 |
logMetacat.debug("Commence creation..."); |
|
1076 |
SystemMetadata smd = (SystemMetadata) deserializeServiceType(SystemMetadata.class, sysmeta); |
|
1077 |
|
|
1078 |
Identifier id = new Identifier(); |
|
1079 |
id.setValue(pid); |
|
1080 |
System.out.println("creating object with pid " + pid); |
|
1081 |
Identifier rId = MNodeService.getInstance().create(session, id, object, smd); |
|
1082 |
serializeServiceType(Identifier.class, rId, out); |
|
1083 |
|
|
1084 |
} else if (action.equals(FUNCTION_NAME_UPDATE)) { |
|
1085 |
|
|
1086 |
// check that the newPid parameter was provided |
|
1087 |
if (multipartparams.get("newPid") == null) { |
|
1088 |
throw new InvalidRequest("1202", "'newPid' must be contained in the request parameters."); |
|
1089 |
} |
|
1090 |
String newPidString = multipartparams.get("newPid").get(0); |
|
1091 |
|
|
1092 |
Identifier newPid = new Identifier(); |
|
1093 |
Identifier obsoletedPid = new Identifier(); |
|
933 |
} else if (action.equals(FUNCTION_NAME_UPDATE)) { |
|
934 |
// handle updates |
|
935 |
|
|
936 |
// construct pids |
|
937 |
Identifier newPid = new Identifier(); |
|
938 |
try { |
|
939 |
String newPidString = multipartparams.get("newPid").get(0); |
|
1094 | 940 |
newPid.setValue(newPidString); |
1095 |
obsoletedPid.setValue(pid); |
|
1096 |
|
|
1097 |
logMetacat.debug("Commence update..."); |
|
1098 |
|
|
1099 |
//get the systemmetadata object |
|
1100 |
SystemMetadata smd = (SystemMetadata) deserializeServiceType(SystemMetadata.class, sysmeta); |
|
941 |
} catch (Exception e) { |
|
942 |
logMetacat.warn("newPid not given"); |
|
943 |
} |
|
944 |
|
|
945 |
Identifier obsoletedPid = new Identifier(); |
|
946 |
obsoletedPid.setValue(pid); |
|
947 |
|
|
948 |
logMetacat.debug("Commence update..."); |
|
949 |
|
|
950 |
// get the systemmetadata object |
|
951 |
SystemMetadata smd = (SystemMetadata) deserializeServiceType(SystemMetadata.class, sysmeta); |
|
1101 | 952 |
|
1102 |
Identifier rId = MNodeService.getInstance().update(session, newPid, object, obsoletedPid, smd);
|
|
1103 |
serializeServiceType(Identifier.class, rId, out);
|
|
1104 |
} else {
|
|
1105 |
throw new InvalidRequest("1000", "Operation must be create or update.");
|
|
1106 |
}
|
|
953 |
Identifier rId = MNodeService.getInstance().update(session, newPid, object, obsoletedPid, smd); |
|
954 |
serializeServiceType(Identifier.class, rId, out); |
|
955 |
} else { |
|
956 |
throw new InvalidRequest("1000", "Operation must be create or update."); |
|
957 |
} |
|
1107 | 958 |
|
1108 |
//clean up the MMP files |
|
1109 |
//parts.get("systemmetadata").delete(); |
|
1110 |
//parts.get("object").delete(); |
|
1111 |
} catch (NotAuthorized e) { |
|
1112 |
response.setStatus(500); |
|
1113 |
serializeException(e, out); |
|
1114 |
} catch (InvalidToken e) { |
|
1115 |
response.setStatus(500); |
|
1116 |
serializeException(e, out); |
|
1117 |
} catch (ServiceFailure e) { |
|
1118 |
response.setStatus(500); |
|
1119 |
serializeException(e, out); |
|
1120 |
} catch (NotFound e) { |
|
1121 |
response.setStatus(500); |
|
1122 |
serializeException(e, out); |
|
1123 |
} catch (IdentifierNotUnique e) { |
|
1124 |
response.setStatus(500); |
|
1125 |
serializeException(e, out); |
|
1126 |
} catch (UnsupportedType e) { |
|
1127 |
response.setStatus(500); |
|
1128 |
serializeException(e, out); |
|
1129 |
} catch (InsufficientResources e) { |
|
1130 |
response.setStatus(500); |
|
1131 |
serializeException(e, out); |
|
1132 |
} catch (InvalidSystemMetadata e) { |
|
1133 |
response.setStatus(500); |
|
1134 |
serializeException(e, out); |
|
1135 |
} catch (NotImplemented e) { |
|
1136 |
response.setStatus(500); |
|
1137 |
serializeException(e, out); |
|
1138 |
} catch (InvalidRequest e) { |
|
1139 |
response.setStatus(500); |
|
1140 |
serializeException(e, out); |
|
1141 |
} catch (JiBXException e) { |
|
1142 |
response.setStatus(500); |
|
1143 |
e.printStackTrace(System.out); |
|
1144 |
InvalidSystemMetadata ism = new InvalidSystemMetadata("1080", e.getMessage()); |
|
1145 |
serializeException(ism, out); |
|
1146 |
} |
|
959 |
|
|
1147 | 960 |
} |
1148 | 961 |
|
1149 | 962 |
/** |
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