Revision 7850
Added by ben leinfelder over 11 years ago
test/edu/ucsb/nceas/metacat/dataone/MNodeServiceTest.java | ||
---|---|---|
26 | 26 |
package edu.ucsb.nceas.metacat.dataone; |
27 | 27 |
|
28 | 28 |
|
29 |
import gov.loc.repository.bagit.Bag; |
|
30 |
import gov.loc.repository.bagit.BagFactory; |
|
31 |
|
|
29 | 32 |
import java.io.ByteArrayInputStream; |
30 | 33 |
import java.io.ByteArrayOutputStream; |
34 |
import java.io.File; |
|
35 |
import java.io.FileOutputStream; |
|
31 | 36 |
import java.io.IOException; |
32 | 37 |
import java.io.InputStream; |
33 | 38 |
import java.io.UnsupportedEncodingException; |
... | ... | |
142 | 147 |
suite.addTest(new MNodeServiceTest("testSetAccessPolicy")); |
143 | 148 |
// MNreplication tests |
144 | 149 |
suite.addTest(new MNodeServiceTest("testReplicate")); |
150 |
// MN packaging tests |
|
151 |
suite.addTest(new MNodeServiceTest("testGetPackage")); |
|
145 | 152 |
|
146 | 153 |
|
147 | 154 |
return suite; |
... | ... | |
1091 | 1098 |
|
1092 | 1099 |
} |
1093 | 1100 |
|
1101 |
/** |
|
1102 |
* Test getting a known object |
|
1103 |
*/ |
|
1104 |
public void testGetPackage() { |
|
1105 |
printTestHeader("testGetPackage"); |
|
1106 |
|
|
1107 |
try { |
|
1108 |
Session session = getTestSession(); |
|
1109 |
Identifier guid = new Identifier(); |
|
1110 |
guid.setValue("testGetPackage." + System.currentTimeMillis()); |
|
1111 |
InputStream object = new ByteArrayInputStream("test".getBytes("UTF-8")); |
|
1112 |
SystemMetadata sysmeta = createSystemMetadata(guid, session.getSubject(), object); |
|
1113 |
Identifier pid = MNodeService.getInstance(request).create(session, guid, object, sysmeta); |
|
1114 |
InputStream bagStream = MNodeService.getInstance(request).getPackage(session, pid); |
|
1115 |
File bagFile = File.createTempFile("bagit", ".zip"); |
|
1116 |
IOUtils.copy(bagStream, new FileOutputStream(bagFile)); |
|
1117 |
BagFactory bagFactory = new BagFactory(); |
|
1118 |
Bag bag = bagFactory.createBag(bagFile); |
|
1119 |
InputStream result = bag.getPayload().iterator().next().newInputStream(); |
|
1120 |
|
|
1121 |
// go back to beginning of original stream |
|
1122 |
object.reset(); |
|
1123 |
// check |
|
1124 |
assertTrue(object.available() > 0); |
|
1125 |
assertTrue(result.available() > 0); |
|
1126 |
assertTrue(IOUtils.contentEquals(result, object)); |
|
1127 |
|
|
1128 |
// clean up |
|
1129 |
bagFile.delete(); |
|
1130 |
|
|
1131 |
} catch (Exception e) { |
|
1132 |
e.printStackTrace(); |
|
1133 |
fail("Unexpected error: " + e.getMessage()); |
|
1134 |
} |
|
1135 |
} |
|
1094 | 1136 |
|
1095 | 1137 |
|
1096 | 1138 |
|
src/edu/ucsb/nceas/metacat/dataone/MNodeService.java | ||
---|---|---|
24 | 24 |
package edu.ucsb.nceas.metacat.dataone; |
25 | 25 |
|
26 | 26 |
import java.io.ByteArrayInputStream; |
27 |
import java.io.File; |
|
28 |
import java.io.FileInputStream; |
|
29 |
import java.io.FileOutputStream; |
|
27 | 30 |
import java.io.IOException; |
28 | 31 |
import java.io.InputStream; |
29 | 32 |
import java.io.UnsupportedEncodingException; |
... | ... | |
49 | 52 |
import org.dataone.client.D1Client; |
50 | 53 |
import org.dataone.client.MNode; |
51 | 54 |
import org.dataone.client.auth.CertificateManager; |
55 |
import org.dataone.client.formats.ObjectFormatInfo; |
|
52 | 56 |
import org.dataone.configuration.Settings; |
53 | 57 |
import org.dataone.ore.ResourceMapFactory; |
54 | 58 |
import org.dataone.service.exceptions.BaseException; |
... | ... | |
123 | 127 |
import edu.ucsb.nceas.metacat.util.DocumentUtil; |
124 | 128 |
import edu.ucsb.nceas.metacat.util.SystemUtil; |
125 | 129 |
import edu.ucsb.nceas.utilities.PropertyNotFoundException; |
130 |
import gov.loc.repository.bagit.Bag; |
|
131 |
import gov.loc.repository.bagit.BagFactory; |
|
132 |
import gov.loc.repository.bagit.writer.Writer; |
|
133 |
import gov.loc.repository.bagit.writer.impl.ZipWriter; |
|
126 | 134 |
|
127 | 135 |
/** |
128 | 136 |
* Represents Metacat's implementation of the DataONE Member Node |
... | ... | |
1623 | 1631 |
|
1624 | 1632 |
return newIdentifier; |
1625 | 1633 |
} |
1634 |
|
|
1635 |
/** |
|
1636 |
* Packages the given package in a Bagit collection for download |
|
1637 |
* @param pid |
|
1638 |
* @throws IOException |
|
1639 |
* @throws NotImplemented |
|
1640 |
* @throws NotFound |
|
1641 |
* @throws NotAuthorized |
|
1642 |
* @throws ServiceFailure |
|
1643 |
* @throws InvalidToken |
|
1644 |
* @throws OREParserException |
|
1645 |
* @throws URISyntaxException |
|
1646 |
* @throws OREException |
|
1647 |
*/ |
|
1648 |
public InputStream getPackage(Session session, Identifier pid) throws IOException, InvalidToken, ServiceFailure, NotAuthorized, NotFound, NotImplemented, OREException, URISyntaxException, OREParserException { |
|
1649 |
InputStream bagInputStream = null; |
|
1650 |
BagFactory bagFactory = new BagFactory(); |
|
1651 |
Bag bag = bagFactory.createBag(); |
|
1652 |
|
|
1653 |
// track the temp files we use so we can delete them when finished |
|
1654 |
List<File> tempFiles = new ArrayList<File>(); |
|
1655 |
|
|
1656 |
// the pids to include in the package |
|
1657 |
List<Identifier> packagePids = new ArrayList<Identifier>(); |
|
1658 |
|
|
1659 |
// find the package contents |
|
1660 |
SystemMetadata sysMeta = this.getSystemMetadata(session, pid); |
|
1661 |
if (ObjectFormatService.getInstance().getFormat(sysMeta.getFormatId()).getFormatType().equals("RESOURCE")) { |
|
1662 |
InputStream oreInputStream = this.get(session, pid); |
|
1663 |
Map<Identifier, Map<Identifier, List<Identifier>>> resourceMapStructure = ResourceMapFactory.getInstance().parseResourceMap(oreInputStream); |
|
1664 |
packagePids.addAll(resourceMapStructure.keySet()); |
|
1665 |
for (Map<Identifier, List<Identifier>> entries: resourceMapStructure.values()) { |
|
1666 |
packagePids.addAll(entries.keySet()); |
|
1667 |
for (List<Identifier> dataPids: entries.values()) { |
|
1668 |
packagePids.addAll(dataPids); |
|
1669 |
} |
|
1670 |
} |
|
1671 |
} else { |
|
1672 |
// just the lone pid in this package |
|
1673 |
packagePids.add(pid); |
|
1674 |
} |
|
1675 |
|
|
1676 |
// loop through the package contents |
|
1677 |
for (Identifier entryPid: packagePids) { |
|
1678 |
SystemMetadata entrySysMeta = this.getSystemMetadata(session, entryPid); |
|
1679 |
String extension = ObjectFormatInfo.instance().getExtension(entrySysMeta.getFormatId().getValue()); |
|
1680 |
String prefix = entryPid.getValue(); |
|
1681 |
File tempFile = File.createTempFile(prefix + "_bagit", extension); |
|
1682 |
tempFiles.add(tempFile); |
|
1683 |
InputStream entryInputStream = this.get(session, entryPid); |
|
1684 |
IOUtils.copy(entryInputStream, new FileOutputStream(tempFile)); |
|
1685 |
bag.addFileToPayload(tempFile); |
|
1686 |
} |
|
1687 |
|
|
1688 |
bag = bag.makeComplete(); |
|
1689 |
File bagFile = File.createTempFile("bagit", ".zip"); |
|
1690 |
// TODO: delete more confidently |
|
1691 |
bagFile.deleteOnExit(); |
|
1692 |
bag.setFile(bagFile); |
|
1693 |
Writer zipWriter = new ZipWriter(bagFactory); |
|
1694 |
bag.write(zipWriter, bagFile); |
|
1695 |
bagFile = bag.getFile(); |
|
1696 |
bagInputStream = new FileInputStream(bagFile); |
|
1697 |
|
|
1698 |
// clean up temp entry files |
|
1699 |
for (File tf: tempFiles) { |
|
1700 |
tf.delete(); |
|
1701 |
} |
|
1702 |
|
|
1703 |
return bagInputStream; |
|
1704 |
|
|
1705 |
} |
|
1626 | 1706 |
|
1627 | 1707 |
} |
pom.xml | ||
---|---|---|
36 | 36 |
</repositories> |
37 | 37 |
<dependencies> |
38 | 38 |
<dependency> |
39 |
<groupId>gov.loc</groupId> |
|
40 |
<artifactId>bagit</artifactId> |
|
41 |
<version>4.4</version> |
|
42 |
<type>jar</type> |
|
43 |
<exclusions> |
|
44 |
<exclusion> |
|
45 |
<groupId>xml-apis</groupId> |
|
46 |
<artifactId>xml-apis</artifactId> |
|
47 |
</exclusion> |
|
48 |
<exclusion> |
|
49 |
<groupId>org.apache.httpcomponents</groupId> |
|
50 |
<artifactId>httpclient</artifactId> |
|
51 |
</exclusion> |
|
52 |
<exclusion> |
|
53 |
<groupId>org.apache.httpcomponents</groupId> |
|
54 |
<artifactId>httpmime</artifactId> |
|
55 |
</exclusion> |
|
56 |
<exclusion> |
|
57 |
<groupId>commons-codec</groupId> |
|
58 |
<artifactId>commons-codec</artifactId> |
|
59 |
</exclusion> |
|
60 |
</exclusions> |
|
61 |
</dependency> |
|
62 |
<dependency> |
|
39 | 63 |
<artifactId>d1_portal</artifactId> |
40 | 64 |
<groupId>org.dataone</groupId> |
41 | 65 |
<version>${d1_portal_version}</version> |
Also available in: Unified diff
First pass at MN.getPackage() implementation using Bagit library from LOC. https://projects.ecoinformatics.org/ecoinfo/issues/6026