Revision 7824
Added by ben leinfelder almost 11 years ago
MetacatHandler.java | ||
---|---|---|
30 | 30 |
import java.io.File; |
31 | 31 |
import java.io.FileInputStream; |
32 | 32 |
import java.io.FileNotFoundException; |
33 |
import java.io.FileOutputStream; |
|
34 | 33 |
import java.io.IOException; |
35 | 34 |
import java.io.InputStream; |
36 | 35 |
import java.io.InputStreamReader; |
... | ... | |
49 | 48 |
import java.sql.Timestamp; |
50 | 49 |
import java.text.ParseException; |
51 | 50 |
import java.text.SimpleDateFormat; |
52 |
import java.util.Date; |
|
53 | 51 |
import java.util.Enumeration; |
54 | 52 |
import java.util.HashMap; |
55 | 53 |
import java.util.Hashtable; |
... | ... | |
1009 | 1007 |
* @throws ClassNotFoundException |
1010 | 1008 |
* @throws IOException |
1011 | 1009 |
*/ |
1012 |
public static InputStream read(String docid) |
|
1013 |
throws ParseLSIDException, |
|
1014 |
PropertyNotFoundException, McdbException, SQLException, |
|
1015 |
ClassNotFoundException, IOException |
|
1016 |
{ |
|
1017 |
logMetacat.debug("MetacatHandler.read() called."); |
|
1018 |
|
|
1019 |
InputStream fileInputStream = null; |
|
1020 |
File tmpDir = null; |
|
1021 |
|
|
1022 |
// be sure we have a local ID from an LSID |
|
1023 |
if (docid.startsWith("urn:")) { |
|
1024 |
try { |
|
1025 |
docid = LSIDUtil.getDocId(docid, true); |
|
1026 |
|
|
1027 |
} catch ( ParseLSIDException ple ) { |
|
1028 |
logMetacat.debug("There was a problem parsing the LSID. The " + |
|
1029 |
"error message was: " + ple.getMessage()); |
|
1030 |
throw ple; |
|
1031 |
} |
|
1032 |
} |
|
1033 |
|
|
1034 |
// accomodate old clients that send docids without revision numbers |
|
1035 |
docid = DocumentUtil.appendRev(docid); |
|
1036 |
|
|
1037 |
DocumentImpl doc = new DocumentImpl(docid, false); |
|
1038 |
|
|
1039 |
// deal with data or metadata cases |
|
1040 |
if (doc.getRootNodeID() == 0) { |
|
1041 |
|
|
1042 |
// this is a data file |
|
1043 |
|
|
1044 |
// get the path to the file to read |
|
1045 |
try { |
|
1046 |
String filepath = PropertyService.getProperty("application.datafilepath"); |
|
1047 |
|
|
1048 |
// ensure it is a directory path |
|
1049 |
if ( !(filepath.endsWith("/")) ) { |
|
1050 |
filepath += "/"; |
|
1051 |
|
|
1052 |
} |
|
1053 |
String filename = filepath + docid; |
|
1054 |
fileInputStream = (InputStream) readFromFilesystem(filename); |
|
1055 |
|
|
1056 |
} catch ( PropertyNotFoundException pnf ) { |
|
1057 |
logMetacat.debug("There was a problem finding the " + |
|
1058 |
"application.datafilepath property. The error " + |
|
1059 |
"message was: " + pnf.getMessage()); |
|
1060 |
throw pnf; |
|
1061 |
|
|
1062 |
} // end try() |
|
1010 |
public static InputStream read(String docid) throws ParseLSIDException, |
|
1011 |
PropertyNotFoundException, McdbException, SQLException, |
|
1012 |
ClassNotFoundException, IOException { |
|
1013 |
logMetacat.debug("MetacatHandler.read() called."); |
|
1063 | 1014 |
|
1064 |
} else { |
|
1065 |
|
|
1066 |
// this is an metadata document |
|
1067 |
|
|
1068 |
// get a temporary file handle to write to as an OutputStream |
|
1069 |
try { |
|
1070 |
tmpDir = new File(PropertyService.getProperty("application.tempDir")); |
|
1015 |
InputStream inputStream = null; |
|
1071 | 1016 |
|
1072 |
} catch ( PropertyNotFoundException pnfe ) { |
|
1073 |
logMetacat.debug("There was a problem getting the" + |
|
1074 |
"application.tempDir property. Using /tmp " + |
|
1075 |
"instead. The error was: " + pnfe.getMessage()); |
|
1076 |
tmpDir = new File("/tmp"); |
|
1017 |
// be sure we have a local ID from an LSID |
|
1018 |
if (docid.startsWith("urn:")) { |
|
1019 |
try { |
|
1020 |
docid = LSIDUtil.getDocId(docid, true); |
|
1021 |
} catch (ParseLSIDException ple) { |
|
1022 |
logMetacat.debug("There was a problem parsing the LSID. The " |
|
1023 |
+ "error message was: " + ple.getMessage()); |
|
1024 |
throw ple; |
|
1025 |
} |
|
1026 |
} |
|
1077 | 1027 |
|
1078 |
} //end try() |
|
1079 |
|
|
1080 |
final File outputFile = File.createTempFile("metacat.output", null, tmpDir); |
|
1081 |
OutputStream out = new FileOutputStream(outputFile); |
|
1028 |
// accomodate old clients that send docids without revision numbers |
|
1029 |
docid = DocumentUtil.appendRev(docid); |
|
1030 |
DocumentImpl doc = new DocumentImpl(docid, false); |
|
1082 | 1031 |
|
1083 |
// Try to get the metadata file from disk. If it isn't |
|
1084 |
// found, create it from the db and write it to disk then. |
|
1085 |
try { |
|
1086 |
doc.toXml(out, null, null, true); |
|
1087 |
|
|
1088 |
} catch (McdbException e) { |
|
1089 |
// any exceptions in reading the xml from disc, and we go back to the |
|
1090 |
// old way of creating the xml directly. |
|
1091 |
logMetacat.error("MetacatHandler.readFromMetacat() " + |
|
1092 |
"- could not read from document file " + |
|
1093 |
docid + ": " + e.getMessage()); |
|
1094 |
e.printStackTrace(System.out); |
|
1095 |
doc.toXmlFromDb(out, null, null, true); |
|
1096 |
} |
|
1097 |
|
|
1098 |
// set the input stream |
|
1099 |
fileInputStream = new FileInputStream(outputFile); |
|
1100 |
outputFile.deleteOnExit(); |
|
1101 |
|
|
1102 |
} |
|
1103 |
|
|
1104 |
return fileInputStream; |
|
1105 |
} |
|
1032 |
// deal with data or metadata cases |
|
1033 |
if (doc.getRootNodeID() == 0) { |
|
1034 |
|
|
1035 |
// this is a data file |
|
1036 |
// get the path to the file to read |
|
1037 |
try { |
|
1038 |
String filepath = PropertyService.getProperty("application.datafilepath"); |
|
1039 |
// ensure it is a directory path |
|
1040 |
if (!(filepath.endsWith("/"))) { |
|
1041 |
filepath += "/"; |
|
1042 |
} |
|
1043 |
String filename = filepath + docid; |
|
1044 |
inputStream = readFromFilesystem(filename); |
|
1045 |
} catch (PropertyNotFoundException pnf) { |
|
1046 |
logMetacat.debug("There was a problem finding the " |
|
1047 |
+ "application.datafilepath property. The error " |
|
1048 |
+ "message was: " + pnf.getMessage()); |
|
1049 |
throw pnf; |
|
1050 |
} // end try() |
|
1051 |
|
|
1052 |
} else { |
|
1053 |
// this is an metadata document |
|
1054 |
// Get the xml (will try disk then DB) |
|
1055 |
try { |
|
1056 |
// force the InputStream to be returned |
|
1057 |
OutputStream nout = null; |
|
1058 |
inputStream = doc.toXml(nout, null, null, true); |
|
1059 |
} catch (McdbException e) { |
|
1060 |
// report the error |
|
1061 |
logMetacat.error( |
|
1062 |
"MetacatHandler.readFromMetacat() " |
|
1063 |
+ "- could not read document " + docid + ": " |
|
1064 |
+ e.getMessage(), e); |
|
1065 |
} |
|
1066 |
|
|
1067 |
} |
|
1068 |
|
|
1069 |
return inputStream; |
|
1070 |
} |
|
1106 | 1071 |
|
1107 | 1072 |
/** |
1108 | 1073 |
* Read a file from Metacat's configured file system data directory. |
Also available in: Unified diff
do not use tmp file to return an inputstream on read() operations - just read from the file we already have. https://projects.ecoinformatics.org/ecoinfo/issues/6009