📄 abstractmetsdisseminator.java
字号:
/* * AbstractMETSDisseminator.java * * Version: $Revision: 1.1 $ * * Date: $Date: 2006/03/17 00:04:38 $ * * Copyright (c) 2002-2005, Hewlett-Packard Company and Massachusetts * Institute of Technology. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * - Neither the name of the Hewlett-Packard Company nor the name of the * Massachusetts Institute of Technology nor the names of their * contributors may be used to endorse or promote products derived from * this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH * DAMAGE. */package org.dspace.content.packager;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.sql.SQLException;import java.util.ArrayList;import java.util.Date;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.zip.ZipEntry;import java.util.zip.ZipOutputStream;import org.apache.log4j.Logger;import org.dspace.authorize.AuthorizeException;import org.dspace.authorize.AuthorizeManager;import org.dspace.content.Bitstream;import org.dspace.content.Bundle;import org.dspace.content.DSpaceObject;import org.dspace.content.Item;import org.dspace.content.crosswalk.CrosswalkException;import org.dspace.content.crosswalk.DisseminationCrosswalk;import org.dspace.core.ConfigurationManager;import org.dspace.core.Constants;import org.dspace.core.Context;import org.dspace.core.PluginManager;import org.dspace.core.Utils;import org.jdom.Namespace;import org.jdom.output.Format;import org.jdom.output.XMLOutputter;import edu.harvard.hul.ois.mets.Agent;import edu.harvard.hul.ois.mets.AmdSec;import edu.harvard.hul.ois.mets.Checksumtype;import edu.harvard.hul.ois.mets.Div;import edu.harvard.hul.ois.mets.DmdSec;import edu.harvard.hul.ois.mets.FLocat;import edu.harvard.hul.ois.mets.FileGrp;import edu.harvard.hul.ois.mets.FileSec;import edu.harvard.hul.ois.mets.Fptr;import edu.harvard.hul.ois.mets.Loctype;import edu.harvard.hul.ois.mets.MdWrap;import edu.harvard.hul.ois.mets.Mdtype;import edu.harvard.hul.ois.mets.Mets;import edu.harvard.hul.ois.mets.MetsHdr;import edu.harvard.hul.ois.mets.Name;import edu.harvard.hul.ois.mets.Role;import edu.harvard.hul.ois.mets.StructMap;import edu.harvard.hul.ois.mets.TechMD;import edu.harvard.hul.ois.mets.Type;import edu.harvard.hul.ois.mets.XmlData;import edu.harvard.hul.ois.mets.helper.MetsElement;import edu.harvard.hul.ois.mets.helper.MetsException;import edu.harvard.hul.ois.mets.helper.MetsValidator;import edu.harvard.hul.ois.mets.helper.MetsWriter;import edu.harvard.hul.ois.mets.helper.PCData;import edu.harvard.hul.ois.mets.helper.PreformedXML;/** * Base class for disseminator of * METS (Metadata Encoding & Transmission Standard) Package.<br> * See <a href="http://www.loc.gov/standards/mets/">http://www.loc.gov/standards/mets/</a> * <p> * This is a generic packager framework intended to be subclassed to create * packagers for more specific METS "profiles". METS is an * abstract and flexible framework that can encompass many * different kinds of metadata and inner package structures. * <p> * <b>Package Parameters:</b><br> * <code>manifestOnly</code> -- if true, generate a standalone XML * document of the METS manifest instead of a complete package. Any * other metadata (such as licenses) will be encoded inline. * Default is <code>false</code>. * * <code>unauthorized</code> -- this determines what is done when the * packager encounters a Bundle or Bitstream it is not authorized to * read. By default, it just quits with an AuthorizeException. * If this option is present, it must be one of the following values: * <code>skip</code> -- simply exclude unreadable content from package. * <code>zero</code> -- include unreadable bitstreams as 0-length files; * unreadable Bundles will still cause authorize errors. * * @author Larry Stone * @author Robert Tansley * @version $Revision: 1.1 $ */public abstract class AbstractMETSDisseminator implements PackageDisseminator{ /** log4j category */ private static Logger log = Logger.getLogger(AbstractMETSDisseminator.class); /** Filename of manifest, relative to package toplevel. */ public static final String MANIFEST_FILE = "mets.xml"; // JDOM xml output writer - indented format for readability. private static XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat()); // for gensym() private int idCounter = 1; /** * Table of files to add to package, such as mdRef'd metadata. * Key is relative pathname of file, value is <code>InputStream</code> * with contents to put in it. * New map is created by disseminate(). */ protected Map extraFiles = null; /** * Make a new unique ID with specified prefix. * @param prefix the prefix of the identifier, constrained to XML ID schema * @return a new string identifier unique in this session (instance). */ protected String gensym(String prefix) { return prefix + "_" + String.valueOf(idCounter++); } public String getMIMEType(PackageParameters params) { return (params != null && params.getProperty("manifestOnly") != null) ? "text/xml" : "application/zip"; } /** * Export the object (Item, Collection, or Community) to a * package file on the indicated OutputStream. * Gets an exception of the object cannot be packaged or there is * a failure creating the package. * * @param context - DSpace context. * @param dso - DSpace object (item, collection, etc) * @param pkg - output stream on which to write package * @throws PackageException if package cannot be created or there is * a fatal error in creating it. */ public void disseminate(Context context, DSpaceObject dso, PackageParameters params, OutputStream pkg) throws PackageValidationException, CrosswalkException, AuthorizeException, SQLException, IOException { if (dso.getType() == Constants.ITEM) { Item item = (Item)dso; long lmTime = item.getLastModified().getTime(); // how to handle unauthorized bundle/bitstream: String unauth = (params == null) ? null : params.getProperty("unauthorized"); if (params != null && params.getProperty("manifestOnly") != null) { extraFiles = null; writeManifest(context, item, params, pkg); } else { extraFiles = new HashMap(); ZipOutputStream zip = new ZipOutputStream(pkg); zip.setComment("METS archive created by DSpace METSDisseminationCrosswalk"); // write manifest first. ZipEntry me = new ZipEntry(MANIFEST_FILE); me.setTime(lmTime); zip.putNextEntry(me); writeManifest(context, item, params, zip); zip.closeEntry(); // copy extra (meta?) bitstreams into zip Iterator fi = extraFiles.keySet().iterator(); while (fi.hasNext()) { String fname = (String)fi.next(); ZipEntry ze = new ZipEntry(fname); ze.setTime(lmTime); zip.putNextEntry(ze); Utils.copy((InputStream)extraFiles.get(fname), zip); zip.closeEntry(); } // copy all non-meta bitstreams into zip Bundle bundles[] = item.getBundles(); for (int i = 0; i < bundles.length; i++) { if (!PackageUtils.isMetaInfoBundle(bundles[i])) { // unauthorized bundle? if (!AuthorizeManager.authorizeActionBoolean(context, bundles[i], Constants.READ)) { if (unauth != null && (unauth.equalsIgnoreCase("skip"))) { log.warn("Skipping Bundle[\""+bundles[i].getName()+"\"] because you are not authorized to read it."); continue; } else throw new AuthorizeException("Not authorized to read Bundle named \""+bundles[i].getName()+"\""); } Bitstream[] bitstreams = bundles[i].getBitstreams(); for (int k = 0; k < bitstreams.length; k++) { boolean auth = AuthorizeManager.authorizeActionBoolean(context, bitstreams[k], Constants.READ); if (auth || (unauth != null && unauth.equalsIgnoreCase("zero"))) { ZipEntry ze = new ZipEntry( makeBitstreamName(bitstreams[k])); ze.setTime(lmTime); ze.setSize(auth ? bitstreams[k].getSize() : 0); zip.putNextEntry(ze); if (auth) Utils.copy(bitstreams[k].retrieve(), zip); else log.warn("Adding zero-length file for Bitstream, SID="+String.valueOf(bitstreams[k].getSequenceID())+", not authorized for READ."); zip.closeEntry(); } else if (unauth != null && unauth.equalsIgnoreCase("skip"))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -