All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.gdcc.xoai.serviceprovider.ServiceProvider Maven / Gradle / Ivy

Go to download

OAI-PMH service provider implementation. Use it as a harvesting client to read remote repositories.

The newest version!
/*
 * The contents of this file are subject to the license and copyright
 * detailed in the LICENSE and NOTICE files at the root of the source
 * tree and available online at
 *
 * http://www.dspace.org/license/
 */

package io.gdcc.xoai.serviceprovider;

import io.gdcc.xoai.model.oaipmh.results.MetadataFormat;
import io.gdcc.xoai.model.oaipmh.results.Record;
import io.gdcc.xoai.model.oaipmh.results.Set;
import io.gdcc.xoai.model.oaipmh.results.record.Header;
import io.gdcc.xoai.model.oaipmh.verbs.Identify;
import io.gdcc.xoai.serviceprovider.exceptions.BadArgumentException;
import io.gdcc.xoai.serviceprovider.exceptions.CannotDisseminateFormatException;
import io.gdcc.xoai.serviceprovider.exceptions.EncapsulatedKnownException;
import io.gdcc.xoai.serviceprovider.exceptions.IdDoesNotExistException;
import io.gdcc.xoai.serviceprovider.exceptions.NoSetHierarchyException;
import io.gdcc.xoai.serviceprovider.handler.GetRecordHandler;
import io.gdcc.xoai.serviceprovider.handler.IdentifyHandler;
import io.gdcc.xoai.serviceprovider.handler.ListIdentifierHandler;
import io.gdcc.xoai.serviceprovider.handler.ListMetadataFormatsHandler;
import io.gdcc.xoai.serviceprovider.handler.ListRecordHandler;
import io.gdcc.xoai.serviceprovider.handler.ListSetsHandler;
import io.gdcc.xoai.serviceprovider.lazy.ItemIterator;
import io.gdcc.xoai.serviceprovider.model.Context;
import io.gdcc.xoai.serviceprovider.parameters.GetRecordParameters;
import io.gdcc.xoai.serviceprovider.parameters.ListIdentifiersParameters;
import io.gdcc.xoai.serviceprovider.parameters.ListMetadataParameters;
import io.gdcc.xoai.serviceprovider.parameters.ListRecordsParameters;
import java.util.Iterator;

public class ServiceProvider {
    private final Context context;
    private final ListMetadataFormatsHandler listMetadataFormatsHandler;
    private final IdentifyHandler identifyHandler;
    private final GetRecordHandler getRecordHandler;

    public ServiceProvider(Context context) {
        this.context = context;
        identifyHandler = new IdentifyHandler(context);
        listMetadataFormatsHandler = new ListMetadataFormatsHandler(context);
        getRecordHandler = new GetRecordHandler(context);
    }

    public Identify identify() {
        return identifyHandler.handle();
    }

    public Iterator listMetadataFormats() throws IdDoesNotExistException {
        return listMetadataFormatsHandler.handle(ListMetadataParameters.request()).iterator();
    }

    public Iterator listMetadataFormats(ListMetadataParameters parameters)
            throws IdDoesNotExistException {
        return listMetadataFormatsHandler.handle(parameters).iterator();
    }

    public Record getRecord(GetRecordParameters parameters)
            throws BadArgumentException, IdDoesNotExistException, CannotDisseminateFormatException {
        if (!parameters.areValid())
            throw new BadArgumentException(
                    "GetRecord verb requires identifier and metadataPrefix parameters");
        return getRecordHandler.handle(parameters);
    }

    public Iterator listRecords(ListRecordsParameters parameters)
            throws BadArgumentException {
        if (!parameters.areValid())
            throw new BadArgumentException("ListRecords verb requires the metadataPrefix");
        return new ItemIterator<>(new ListRecordHandler(context, parameters));
    }

    public Iterator
listIdentifiers(ListIdentifiersParameters parameters) throws BadArgumentException { if (!parameters.areValid()) throw new BadArgumentException("ListIdentifiers verb requires the metadataPrefix"); return new ItemIterator<>(new ListIdentifierHandler(context, parameters)); } public Iterator listSets() throws NoSetHierarchyException { try { return new ItemIterator<>(new ListSetsHandler(context)); } catch (EncapsulatedKnownException ex) { // send the root cause when the set hierarchy could not be found if (ex.getCause() instanceof NoSetHierarchyException) { throw (NoSetHierarchyException) ex.getCause(); } else { throw ex; } } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy