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

com.lyncode.xoai.serviceprovider.handler.ListIdentifierHandler Maven / Gradle / Ivy

The newest version!
/**
 * Copyright 2012 Lyncode
 *
 * 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
 *
 *     client://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.
 */

package com.lyncode.xoai.serviceprovider.handler;

import static com.lyncode.xml.matchers.QNameMatchers.localPart;
import static com.lyncode.xml.matchers.XmlEventMatchers.aStartElement;
import static com.lyncode.xml.matchers.XmlEventMatchers.anEndElement;
import static com.lyncode.xml.matchers.XmlEventMatchers.elementName;
import static com.lyncode.xml.matchers.XmlEventMatchers.text;
import static com.lyncode.xoai.model.oaipmh.Verb.Type.ListIdentifiers;
import static com.lyncode.xoai.serviceprovider.parameters.Parameters.parameters;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.core.AllOf.allOf;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.stream.events.XMLEvent;

import org.hamcrest.Matcher;

import com.lyncode.xml.XmlReader;
import com.lyncode.xml.exceptions.XmlReaderException;
import com.lyncode.xoai.model.oaipmh.Header;
import com.lyncode.xoai.serviceprovider.client.OAIClient;
import com.lyncode.xoai.serviceprovider.exceptions.InvalidOAIResponse;
import com.lyncode.xoai.serviceprovider.exceptions.OAIRequestException;
import com.lyncode.xoai.serviceprovider.lazy.Source;
import com.lyncode.xoai.serviceprovider.model.Context;
import com.lyncode.xoai.serviceprovider.parameters.ListIdentifiersParameters;
import com.lyncode.xoai.serviceprovider.parsers.ListIdentifiersParser;

public class ListIdentifierHandler implements Source
{ private Context context; private ListIdentifiersParameters parameters; private OAIClient client; private String resumptionToken; private boolean ended = false; public ListIdentifierHandler(Context context, ListIdentifiersParameters parameters) { this.context = context; this.parameters = parameters; this.client = context.getClient(); } @Override public List
nextIteration() { List
headers = new ArrayList
(); try { InputStream stream = null; if (resumptionToken == null) { // First call stream = client.execute(parameters() .withVerb(ListIdentifiers) .include(parameters)); } else { // Resumption calls stream = client.execute(parameters() .withVerb(ListIdentifiers) .include(parameters) .withResumptionToken(resumptionToken)); } XmlReader reader = new XmlReader(stream); ListIdentifiersParser parser = new ListIdentifiersParser(reader); while (parser.hasNext()) headers.add(parser.next()); if (reader.current(resumptionToken())) { if (reader.next(text(), anEndElement()).current(text())) { String text = reader.getText(); if (text == null || "".equals(text.trim())) ended = true; else resumptionToken = text; } else ended = true; } else ended = true; stream.close(); return headers; } catch (XmlReaderException e) { throw new InvalidOAIResponse(e); } catch (OAIRequestException e) { throw new InvalidOAIResponse(e); } catch (IOException e) { throw new InvalidOAIResponse(e); } } private Matcher resumptionToken() { return allOf(aStartElement(), elementName(localPart(equalTo("resumptionToken")))); } @Override public boolean endReached() { return ended; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy