Revision 7850
Added by ben leinfelder almost 11 years ago
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 |
} |
Also available in: Unified diff
First pass at MN.getPackage() implementation using Bagit library from LOC. https://projects.ecoinformatics.org/ecoinfo/issues/6026