📄 dspaceoaicatalog.java
字号:
/* * DSpaceOAICatalog.java * * Version: $Revision: 1.18 $ * * Date: $Date: 2005/11/09 18:58: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.app.oai;import java.sql.SQLException;import java.util.HashMap;import java.util.Iterator;import java.util.LinkedList;import java.util.List;import java.util.Map;import java.util.NoSuchElementException;import java.util.Properties;import java.util.StringTokenizer;import java.util.Vector;import org.apache.log4j.Logger;import org.dspace.content.Collection;import org.dspace.content.DSpaceObject;import org.dspace.core.ConfigurationManager;import org.dspace.core.Context;import org.dspace.core.LogManager;import org.dspace.core.Utils;import org.dspace.handle.HandleManager;import org.dspace.search.Harvest;import org.dspace.search.HarvestedItemInfo;import ORG.oclc.oai.server.catalog.AbstractCatalog;import ORG.oclc.oai.server.verb.BadArgumentException;import ORG.oclc.oai.server.verb.BadResumptionTokenException;import ORG.oclc.oai.server.verb.CannotDisseminateFormatException;import ORG.oclc.oai.server.verb.IdDoesNotExistException;import ORG.oclc.oai.server.verb.NoItemsMatchException;import ORG.oclc.oai.server.verb.NoMetadataFormatsException;import ORG.oclc.oai.server.verb.NoSetHierarchyException;import ORG.oclc.oai.server.verb.OAIInternalServerError;/** * This is class extends OAICat's AbstractCatalog base class to allow metadata * harvesting of the metadata in DSpace via OAI-PMH 2.0. * * FIXME: Some CNRI Handle-specific stuff in here. Anyone wanting to use * something else will need to update this code too. Sorry about that. * * @author Robert Tansley * @version $Revision: 1.18 $ */public class DSpaceOAICatalog extends AbstractCatalog{ /** log4j logger */ private static Logger log = Logger.getLogger(DSpaceOAICatalog.class); /** Prefix that all our OAI identifiers have */ public final static String OAI_ID_PREFIX = "oai:" + ConfigurationManager.getProperty("dspace.hostname") + ":"; /** Maximum number of records returned by one request */ private final int MAX_RECORDS = 100; public DSpaceOAICatalog(Properties properties) { // Don't need to do anything } /** * Retrieve a list of schemaLocation values associated with the specified * identifier. * * @param identifier * the OAI identifier * @return a Vector containing schemaLocation Strings * @exception OAIInternalServerError * signals an http status code 500 problem * @exception IdDoesNotExistException * the specified identifier can't be found * @exception NoMetadataFormatsException * the specified identifier was found but the item is flagged * as deleted and thus no schemaLocations (i.e. * metadataFormats) can be produced. */ public Vector getSchemaLocations(String identifier) throws OAIInternalServerError, IdDoesNotExistException, NoMetadataFormatsException { log.info(LogManager.getHeader(null, "oai_request", "verb=getSchemaLocations,identifier=" + ((identifier == null) ? "null" : identifier))); HarvestedItemInfo itemInfo = null; Context context = null; // Get the item from the DB try { context = new Context(); // Valid identifiers all have prefix "oai:hostname:" if (identifier.startsWith(OAI_ID_PREFIX)) { itemInfo = Harvest.getSingle(context, identifier .substring(OAI_ID_PREFIX.length()), // Strip prefix to // get raw handle false); } } catch (SQLException se) { // Log the error log.warn(LogManager.getHeader(context, "database_error", ""), se); throw new OAIInternalServerError(se.toString()); } finally { if (context != null) { context.abort(); } } if (itemInfo == null) { throw new IdDoesNotExistException(identifier); } else { if (itemInfo.withdrawn) { throw new NoMetadataFormatsException(); } else { return getRecordFactory().getSchemaLocations(itemInfo); } } } /** * Retrieve a list of identifiers that satisfy the specified criteria * * @param from * beginning date using the proper granularity * @param until * ending date using the proper granularity * @param set * the set name or null if no such limit is requested * @param metadataPrefix * the OAI metadataPrefix or null if no such limit is requested * @return a Map object containing entries for "headers" and "identifiers" * Iterators (both containing Strings) as well as an optional * "resumptionMap" Map. It may seem strange for the map to include * both "headers" and "identifiers" since the identifiers can be * obtained from the headers. This may be true, but * AbstractCatalog.listRecords() can operate quicker if it doesn't * need to parse identifiers from the XML headers itself. Better * still, do like I do below and override * AbstractCatalog.listRecords(). AbstractCatalog.listRecords() is * relatively inefficient because given the list of identifiers, it * must call getRecord() individually for each as it constructs its * response. It's much more efficient to construct the entire * response in one fell swoop by overriding listRecords() as I've * done here. * @exception OAIInternalServerError * signals an http status code 500 problem * @exception NoSetHierarchyException * the repository doesn't support sets. * @exception CannotDisseminateFormatException * the metadata format specified is not supported by your * repository. */ public Map listIdentifiers(String from, String until, String set, String metadataPrefix) throws OAIInternalServerError, NoSetHierarchyException, NoItemsMatchException, CannotDisseminateFormatException, BadArgumentException { log .info(LogManager.getHeader(null, "oai_request", "verb=listIdentifiers,from=" + ((from == null) ? "null" : from) + ",until=" + ((until == null) ? "null" : until) + ",set=" + ((set == null) ? "null" : set) + ",metadataPrefix=" + ((metadataPrefix == null) ? "null" : metadataPrefix))); // We can produce oai_dc and simple DC for all items, so just return IDs Context context = null; // Lists to put results in List headers = new LinkedList(); List identifiers = new LinkedList(); try { context = new Context(); // Get the relevant OAIItemInfo objects to make headers Collection scope = resolveSet(context, set); List itemInfos = Harvest.harvest(context, scope, from, until, 0, 0, // Everything // for // now false, true, true); // No Item objects, but we need to know collections they're in and // withdrawn items if (itemInfos.size() == 0) { log.info(LogManager.getHeader(null, "oai_error", "no_items_match")); throw new NoItemsMatchException(); } // Build up lists of headers and identifiers Iterator i = itemInfos.iterator(); while (i.hasNext()) { HarvestedItemInfo itemInfo = (HarvestedItemInfo) i.next(); String[] header = getRecordFactory().createHeader(itemInfo); headers.add(header[0]); identifiers.add(header[1]); } } catch (SQLException se) { // Log the error log.warn(LogManager.getHeader(context, "database_error", ""), se); throw new OAIInternalServerError(se.toString()); } finally { if (context != null) { context.abort(); } } // Put results in form needed to return Map results = new HashMap(); results.put("headers", headers.iterator()); results.put("identifiers", identifiers.iterator()); return results; } /** * Retrieve the next set of identifiers associated with the resumptionToken * * @param resumptionToken * implementation-dependent format taken from the previous * listIdentifiers() Map result. * @return a Map object containing entries for "headers" and "identifiers" * Iterators (both containing Strings) as well as an optional * "resumptionMap" Map. * @exception BadResumptionTokenException * the value of the resumptionToken is invalid or expired. * @exception OAIInternalServerError * signals an http status code 500 problem
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -