de.fhg.iais.asc.oai.retriever.MetadataFormatsAndSetNamesRetriever.java Source code

Java tutorial

Introduction

Here is the source code for de.fhg.iais.asc.oai.retriever.MetadataFormatsAndSetNamesRetriever.java

Source

package de.fhg.iais.asc.oai.retriever;

/******************************************************************************
 * Copyright 2011 (c) Fraunhofer IAIS Netmedia  http://www.iais.fraunhofer.de *
 * ************************************************************************** *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may    *
 * not use this file except in compliance with the License.                   *
 * You may obtain a copy of the License at                                    *
 * http://www.apache.org/licenses/LICENSE-2.0                                 *
 * Unless required by applicable law or agreed to in writing,                 *
 * software distributed under the License is distributed on an "AS IS" BASIS, *
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   *
 * See the License for the specific language governing permissions and        *
 * limitations under the License.                                             *
 ******************************************************************************/

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.apache.http.HttpHost;
import org.apache.http.client.HttpClient;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.impl.client.DefaultHttpClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import se.kb.oai.OAIException;
import se.kb.oai.pmh.MetadataFormat;
import se.kb.oai.pmh.MetadataFormatsList;
import se.kb.oai.pmh.OaiPmhServer;
import se.kb.oai.pmh.SetsList;
import de.fhg.iais.asc.commons.state.ASCState;
import de.fhg.iais.asc.oai.consumer.set.ISetConsumer;
import de.fhg.iais.asc.oai.consumer.set.SetCollector;
import de.fhg.iais.commons.dbc.DbcException;

public class MetadataFormatsAndSetNamesRetriever {

    private static final Logger LOG = LoggerFactory.getLogger(MetadataFormatsAndSetNamesRetriever.class);

    protected OaiPmhServer server;
    protected ASCState ascstate;

    protected String uri = null;

    //    protected String fromDate = null;
    //    protected String untilDate = null;

    //    protected String oaiSet = null;

    public MetadataFormatsAndSetNamesRetriever(String uri, ASCState ascstate, String proxyhost, Integer proxyport) {
        this.uri = uri;

        this.ascstate = ascstate;

        HttpClient client = new DefaultHttpClient();

        // set some parameters that might help but will not harm
        // see: http://hc.apache.org/httpclient-legacy/preference-api.html

        // the user-agent:
        // tell them who we are (they see that from the IP anyway), thats a good habit,
        // shows that we are professional and not some script kiddies
        // and this is also a little bit of viral marketing :-)
        client.getParams().setParameter("http.useragent", "myCortex Harvester; http://www.iais.fraunhofer.de/");
        // the following option "can result in noticeable performance improvement" (see api docs)
        // it may switch on a keep-alive, may reduce load on server side (if they are smart)
        // and might reduce latency
        client.getParams().setParameter("http.protocol.expect-continue", true);

        // ignore all cookies because some OAI-PMH implementations don't know how to handle cookies
        client.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.IGNORE_COOKIES);

        // setting of the proxy if needed
        if (proxyhost != null && !proxyhost.isEmpty()) {
            HttpHost proxy = new HttpHost(proxyhost, proxyport);
            client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
        }

        this.server = new OaiPmhServer(client, this.uri);
    }

    public String[] listSets() {
        SetCollector consumer = new SetCollector();
        listSets(consumer);
        Set<String> sets = consumer.getSpecs();
        LOG.debug("Sets are " + sets);
        return sets != null ? sets.toArray(new String[consumer.getSpecs().size()]) : null; // better returning a zero length array rather than null, but it is correctly checked, so ==> // NOSONAR
    }

    /**
     * The list for available sets is downloaded from the OAI-PMH repository
     * 
     * @return true if stopped by client
     * @throws DbcException
     */
    private boolean listSets(ISetConsumer setConsumer) {
        if (setConsumer == null) {
            LOG.error("No HeaderConsumer defined!");
        }

        SetsList list = null;
        try {
            list = this.server.listSets();
        } catch (Exception e) {
            LOG.error(e.getLocalizedMessage(), e);
            this.ascstate.reportProblem(e.getMessage());
        }

        boolean more = list != null && list.size() > 0;
        while (more) {
            for (se.kb.oai.pmh.Set set : list.asList()) {
                if (setConsumer.consume(set.getName(), set.getSpec())) {
                    return true;
                }
                if (list.getResumptionToken() == null) {
                    more = false;
                } else {
                    try {
                        list = this.server.listSets(list.getResumptionToken());
                    } catch (Exception e) {
                        LOG.error(e.getLocalizedMessage(), e);
                        this.ascstate.reportProblem(e.getMessage());
                    }
                }
            }
        }
        return false;
    }

    public String[] listMetadataFormats() {
        try {
            MetadataFormatsList metadataformatlist = this.server.listMetadataFormats();
            if (metadataformatlist == null) {
                return new String[0];
            }
            List<MetadataFormat> list = metadataformatlist.asList();
            ArrayList<String> mda = new ArrayList<String>();
            for (MetadataFormat mdf : list) {
                mda.add(mdf.getPrefix());
            }
            LOG.debug("Metadataformats are " + mda);
            return mda.toArray(new String[mda.size()]);
        } catch (OAIException e) {
            throw new DbcException(e);
        }
    }

}