Revision 5621
Added by berkley about 14 years ago
test/edu/ucsb/nceas/metacat/dataone/CrudServiceTest.java | ||
---|---|---|
105 | 105 |
suite.addTest(new CrudServiceTest("testChecksumError")); |
106 | 106 |
suite.addTest(new CrudServiceTest("testPublicAccess")); |
107 | 107 |
suite.addTest(new CrudServiceTest("testFailedCreate")); |
108 |
suite.addTest(new CrudServiceTest("testChecksum")); |
|
108 | 109 |
return suite; |
109 | 110 |
} |
110 | 111 |
|
111 | 112 |
/** |
113 |
* test the checksum creation |
|
114 |
*/ |
|
115 |
public void testChecksum() |
|
116 |
{ |
|
117 |
try |
|
118 |
{ |
|
119 |
CrudService cs = CrudService.getInstance(); |
|
120 |
AuthToken token = getToken(); |
|
121 |
System.out.println("token: " + token.getToken()); |
|
122 |
String doc = getTestDoc(); |
|
123 |
Identifier id = createDoc(token, doc); |
|
124 |
System.out.println("ID: " + id.getValue()); |
|
125 |
makeDocPublic(token, id, true); |
|
126 |
String doc2 = getDoc(token, id); |
|
127 |
|
|
128 |
String checksum = checksum(doc); |
|
129 |
Checksum checksum2 = cs.getChecksum(token, id, "MD5"); |
|
130 |
System.out.println("doc: \"" + doc + "\""); |
|
131 |
System.out.println("doc2: \"" + doc2 + "\""); |
|
132 |
System.out.println("checksum1: " + checksum); |
|
133 |
System.out.println("checksum2: " + checksum2.getValue()); |
|
134 |
assertTrue(checksum.equals(checksum2.getValue())); |
|
135 |
|
|
136 |
} |
|
137 |
catch(Exception e) |
|
138 |
{ |
|
139 |
fail("Unexpected error in testChecksum: " + e.getMessage()); |
|
140 |
} |
|
141 |
|
|
142 |
} |
|
143 |
|
|
144 |
/** |
|
112 | 145 |
* insert an invalid document and make sure create fails and does not |
113 | 146 |
* leave any artifacts behind |
114 | 147 |
*/ |
... | ... | |
664 | 697 |
throws Exception |
665 | 698 |
{ |
666 | 699 |
CrudService cs = CrudService.getInstance(); |
667 |
//try to get the same doc then compare them |
|
668 | 700 |
InputStream gotDocStream = cs.get(token, id); |
669 |
StringBuffer sb = new StringBuffer(); |
|
670 |
byte[] b = new byte[1024]; |
|
671 |
int numread = gotDocStream.read(b, 0, 1024); |
|
672 |
while(numread != -1) |
|
673 |
{ |
|
674 |
sb.append(new String(b, 0, numread)); |
|
675 |
numread = gotDocStream.read(b, 0, 1024); |
|
676 |
} |
|
677 |
return sb.toString(); |
|
701 |
return IOUtils.toString(gotDocStream); |
|
678 | 702 |
} |
679 | 703 |
|
680 | 704 |
/** |
src/edu/ucsb/nceas/metacat/DocumentImpl.java | ||
---|---|---|
28 | 28 |
|
29 | 29 |
import java.io.BufferedInputStream; |
30 | 30 |
import java.io.BufferedOutputStream; |
31 |
import java.io.ByteArrayInputStream; |
|
32 |
import java.io.ByteArrayOutputStream; |
|
31 | 33 |
import java.io.File; |
32 | 34 |
import java.io.FileOutputStream; |
33 | 35 |
import java.io.FileWriter; |
... | ... | |
73 | 75 |
import edu.ucsb.nceas.utilities.PropertyNotFoundException; |
74 | 76 |
import edu.ucsb.nceas.utilities.UtilException; |
75 | 77 |
|
78 |
import org.apache.commons.io.IOUtils; |
|
76 | 79 |
import org.apache.log4j.Logger; |
77 | 80 |
import org.xml.sax.ContentHandler; |
78 | 81 |
import org.xml.sax.DTDHandler; |
... | ... | |
1411 | 1414 |
* @param accNumber |
1412 | 1415 |
* the document id which is used to name the output file |
1413 | 1416 |
*/ |
1414 |
private static void writeToFileSystem(Reader xml, String accNumber) throws McdbException {
|
|
1417 |
private static void writeToFileSystem(InputStream xml, String accNumber) throws McdbException {
|
|
1415 | 1418 |
|
1416 | 1419 |
// write the document to disk |
1417 | 1420 |
// FileWriter fileWriter = null; |
... | ... | |
1439 | 1442 |
&& (FileUtil.getFileStatus(documentPath) == FileUtil.DOES_NOT_EXIST |
1440 | 1443 |
|| FileUtil.getFileSize(documentPath) == 0)) { |
1441 | 1444 |
|
1442 |
FileUtil.writeFile(documentPath, xml); |
|
1443 |
// fileWriter = new FileWriter(documentPath); |
|
1444 |
// fileWriter.write(xml); |
|
1445 |
// fileWriter.close(); |
|
1445 |
//FileUtil.writeFile(documentPath, xml); |
|
1446 |
try |
|
1447 |
{ |
|
1448 |
String s = IOUtils.toString(xml); |
|
1449 |
//TODO: the doc gets put into memory here. I can't get |
|
1450 |
//it to write to the filesystem correctly without doing |
|
1451 |
//this. Need to revisit at some point. |
|
1452 |
if(s.endsWith("\n")) |
|
1453 |
{ |
|
1454 |
s += "\n"; |
|
1455 |
} |
|
1456 |
//HACK: For some reason, FileOutputStream is cutting off the last |
|
1457 |
//byte of the file if there is a newline present. The only |
|
1458 |
//way I could find to fix this was to add another newline. |
|
1459 |
|
|
1460 |
FileOutputStream fos = new FileOutputStream(documentPath); |
|
1461 |
IOUtils.write(s.getBytes(), fos); |
|
1462 |
byte[] b = new byte[1024]; |
|
1463 |
int numread = xml.read(b, 0, 1024); |
|
1464 |
/*while(numread != -1) |
|
1465 |
{ |
|
1466 |
System.out.println("numread: " + numread + " s: \"" + new String(b, 0, numread) + "\""); |
|
1467 |
System.out.println("last byte: " + b[numread] + " one less: " + b[numread - 1] + " one more: " + b[numread + 1]); |
|
1468 |
fos.write(b, 0, numread); |
|
1469 |
numread = xml.read(b, 0, 1024); |
|
1470 |
}*/ |
|
1471 |
fos.flush(); |
|
1472 |
fos.close(); |
|
1473 |
} |
|
1474 |
catch(IOException ioe) |
|
1475 |
{ |
|
1476 |
throw new McdbException("Could not write file: " + documentPath + " : " |
|
1477 |
+ ioe.getMessage()); |
|
1478 |
} |
|
1446 | 1479 |
} |
1447 | 1480 |
|
1448 | 1481 |
} catch (PropertyNotFoundException pnfe) { |
1449 | 1482 |
throw new McdbException("Could not write file: " + documentPath + " : " |
1450 | 1483 |
+ pnfe.getMessage()); |
1451 |
} catch (UtilException ue) { |
|
1484 |
} /*catch (UtilException ue) {
|
|
1452 | 1485 |
throw new McdbException("Could not write file: " + documentPath + " : " |
1453 | 1486 |
+ ue.getMessage()); |
1454 |
} |
|
1487 |
}*/
|
|
1455 | 1488 |
} |
1456 | 1489 |
|
1457 | 1490 |
/** |
... | ... | |
2626 | 2659 |
|
2627 | 2660 |
//write the file to disk |
2628 | 2661 |
logMetacat.debug("DocumentImpl.write - Writing xml to file system"); |
2629 |
StringReader xmlReaderForFile = new StringReader(xmlString); |
|
2630 |
writeToFileSystem(xmlReaderForFile, accnum); |
|
2662 |
|
|
2663 |
ByteArrayInputStream xmlBAOS = new ByteArrayInputStream(xmlString.getBytes()); |
|
2664 |
writeToFileSystem(xmlBAOS, accnum); |
|
2631 | 2665 |
|
2632 | 2666 |
// write to xml_node complete. start the indexing thread. |
2633 | 2667 |
addDocidToIndexingQueue(docid, rev); |
... | ... | |
2707 | 2741 |
conn.setAutoCommit(true); |
2708 | 2742 |
|
2709 | 2743 |
//write the file to disk |
2710 |
StringReader xmlReaderForFile = new StringReader(xmlString);
|
|
2711 |
writeToFileSystem(xmlReaderForFile, accnum);
|
|
2744 |
ByteArrayInputStream xmlBAOS = new ByteArrayInputStream(xmlString.getBytes());
|
|
2745 |
writeToFileSystem(xmlBAOS, accnum);
|
|
2712 | 2746 |
|
2713 | 2747 |
addDocidToIndexingQueue(docid, rev); |
2714 | 2748 |
} catch (Exception e) { |
... | ... | |
2886 | 2920 |
conn.setAutoCommit(true); |
2887 | 2921 |
|
2888 | 2922 |
// Write the file to disk |
2889 |
StringReader xmlReaderForFile = new StringReader(xmlString);
|
|
2890 |
writeToFileSystem(xmlReaderForFile, accnum);
|
|
2923 |
ByteArrayInputStream xmlBAOS = new ByteArrayInputStream(xmlString.getBytes());
|
|
2924 |
writeToFileSystem(xmlBAOS, accnum);
|
|
2891 | 2925 |
|
2892 | 2926 |
// write to xml_node complete. start the indexing thread. |
2893 | 2927 |
// this only for xml_documents |
src/edu/ucsb/nceas/metacat/dataone/CrudService.java | ||
---|---|---|
980 | 980 |
|
981 | 981 |
} catch (McdbDocNotFoundException e) { |
982 | 982 |
throw new NotFound("1020", e.getMessage()); |
983 |
} |
|
983 |
}
|
|
984 | 984 |
} |
985 | 985 |
|
986 | 986 |
/** |
987 |
* get the checksum for a document. NOT IMPLEMENTED
|
|
987 |
* get the checksum for a document. defaults to MD5.
|
|
988 | 988 |
*/ |
989 | 989 |
public Checksum getChecksum(AuthToken token, Identifier guid) |
990 | 990 |
throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, |
991 |
InvalidRequest, NotImplemented { |
|
991 |
InvalidRequest, NotImplemented |
|
992 |
{ |
|
992 | 993 |
logCrud.info("getChecksum"); |
993 |
//TODO: this could now easily be implemented |
|
994 |
throw new NotImplemented("1000", "This method not yet implemented."); |
|
994 |
return getChecksum(token, guid, "MD5"); |
|
995 | 995 |
} |
996 | 996 |
|
997 | 997 |
/** |
998 |
* get the checksum for a document. NOT IMPLEMENTED
|
|
998 |
* get the checksum for a document with the given algorithm
|
|
999 | 999 |
*/ |
1000 | 1000 |
public Checksum getChecksum(AuthToken token, Identifier guid, |
1001 | 1001 |
String checksumAlgorithm) throws InvalidToken, ServiceFailure, |
1002 |
NotAuthorized, NotFound, InvalidRequest, NotImplemented { |
|
1002 |
NotAuthorized, NotFound, InvalidRequest, NotImplemented |
|
1003 |
{ |
|
1003 | 1004 |
logCrud.info("getChecksum"); |
1004 |
//TODO: this could now easily be implemented |
|
1005 |
throw new NotImplemented("1000", "This method not yet implemented."); |
|
1005 |
InputStream docStream = get(token, guid); |
|
1006 |
String checksum; |
|
1007 |
try |
|
1008 |
{ |
|
1009 |
checksum = checksum(docStream, checksumAlgorithm); |
|
1010 |
} |
|
1011 |
catch(Exception e) |
|
1012 |
{ |
|
1013 |
throw new ServiceFailure("1000", "Error getting checksum: " + e.getMessage()); |
|
1014 |
} |
|
1015 |
Checksum c = new Checksum(); |
|
1016 |
c.setAlgorithm(ChecksumAlgorithm.convert(checksumAlgorithm)); |
|
1017 |
c.setValue(checksum); |
|
1018 |
return c; |
|
1006 | 1019 |
} |
1007 | 1020 |
|
1008 | 1021 |
/** |
... | ... | |
1718 | 1731 |
} |
1719 | 1732 |
|
1720 | 1733 |
/** |
1721 |
* produce an md5 checksum for item |
|
1734 |
* return an MD5 checksum for the stream |
|
1735 |
* @param is |
|
1736 |
* @return |
|
1722 | 1737 |
*/ |
1723 | 1738 |
private String checksum(InputStream is) |
1739 |
throws Exception |
|
1740 |
{ |
|
1741 |
return checksum(is, "MD5"); |
|
1742 |
} |
|
1743 |
|
|
1744 |
/** |
|
1745 |
* produce a checksum for item using the given algorithm |
|
1746 |
*/ |
|
1747 |
private String checksum(InputStream is, String algorithm) |
|
1724 | 1748 |
throws Exception |
1725 | 1749 |
{ |
1726 | 1750 |
byte[] buffer = new byte[1024]; |
1727 |
MessageDigest complete = MessageDigest.getInstance("MD5");
|
|
1751 |
MessageDigest complete = MessageDigest.getInstance(algorithm);
|
|
1728 | 1752 |
int numRead; |
1729 | 1753 |
|
1730 | 1754 |
do |
Also available in: Unified diff
fixed a bunch of small errors, did some reformatting, and fixed a bug that I thought was fixed last week