Search in sources :

Example 1 with ResumptionTokenType

use of org.openarchives.oai._2.ResumptionTokenType in project mod-oai-pmh by folio-org.

the class AbstractGetRecordsHelper method processRecords.

protected Response processRecords(Context ctx, Request request, JsonObject instancesResponseBody) {
    JsonArray instances = storageHelper.getItems(instancesResponseBody);
    Integer totalRecords = storageHelper.getTotalRecords(instancesResponseBody);
    logger.debug("{} entries retrieved out of {}.", instances != null ? instances.size() : 0, totalRecords);
    if (request.isRestored() && !canResumeRequestSequence(request, totalRecords, instances)) {
        OAIPMH oaipmh = getResponseHelper().buildBaseOaipmhResponse(request).withErrors(new OAIPMHerrorType().withCode(BAD_RESUMPTION_TOKEN).withValue(RESUMPTION_TOKEN_FLOW_ERROR));
        return getResponseHelper().buildFailureResponse(oaipmh, request);
    }
    ResumptionTokenType resumptionToken = buildResumptionToken(request, instances, totalRecords);
    /*
     * According to OAI-PMH guidelines: it is recommended that the responseDate reflect the time of the repository's clock at the start
     * of any database query or search function necessary to answer the list request, rather than when the output is written.
     */
    final OAIPMH oaipmh = getResponseHelper().buildBaseOaipmhResponse(request);
    final Map<String, RecordType> recordsMap = buildRecords(ctx, request, instances);
    if (recordsMap.isEmpty()) {
        return buildNoRecordsFoundOaiResponse(oaipmh, request);
    } else {
        addRecordsToOaiResponse(oaipmh, recordsMap.values());
        addResumptionTokenToOaiResponse(oaipmh, resumptionToken);
        return buildResponse(oaipmh, request);
    }
}
Also used : JsonArray(io.vertx.core.json.JsonArray) OAIPMH(org.openarchives.oai._2.OAIPMH) OAIPMHerrorType(org.openarchives.oai._2.OAIPMHerrorType) ResumptionTokenType(org.openarchives.oai._2.ResumptionTokenType) RecordType(org.openarchives.oai._2.RecordType)

Example 2 with ResumptionTokenType

use of org.openarchives.oai._2.ResumptionTokenType in project mod-oai-pmh by folio-org.

the class MarcWithHoldingsRequestHelper method buildResumptionTokenFromRequest.

private ResumptionTokenType buildResumptionTokenFromRequest(Request request, String requestId, long returnedCount, String nextInstanceId) {
    long cursor = request.getOffset();
    if (nextInstanceId == null) {
        return new ResumptionTokenType().withValue("").withCursor(BigInteger.valueOf(cursor));
    }
    Map<String, String> extraParams = new HashMap<>();
    extraParams.put(OFFSET_PARAM, String.valueOf(cursor + returnedCount));
    extraParams.put(REQUEST_ID_PARAM, requestId);
    extraParams.put(NEXT_RECORD_ID_PARAM, nextInstanceId);
    extraParams.put(EXPIRATION_DATE_RESUMPTION_TOKEN_PARAM, String.valueOf(Instant.now().with(ChronoField.NANO_OF_SECOND, 0).plusSeconds(RESUMPTION_TOKEN_TIMEOUT)));
    if (request.getUntil() == null) {
        extraParams.put(UNTIL_PARAM, getUntilDate(request, request.getFrom()));
    }
    int pk = request.getNextInstancePkValue();
    if (pk > 0) {
        extraParams.put(NEXT_INSTANCE_PK_VALUE, String.valueOf(pk));
    }
    String resumptionToken = request.toResumptionToken(extraParams);
    return new ResumptionTokenType().withValue(resumptionToken).withExpirationDate(Instant.now().with(ChronoField.NANO_OF_SECOND, 0).plusSeconds(RESUMPTION_TOKEN_TIMEOUT)).withCursor(BigInteger.valueOf(cursor));
}
Also used : ResumptionTokenType(org.openarchives.oai._2.ResumptionTokenType) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 3 with ResumptionTokenType

use of org.openarchives.oai._2.ResumptionTokenType in project mod-oai-pmh by folio-org.

the class OaiPmhImplTest method makeResumptionTokenRequestsAndVerifyCount.

private ResumptionTokenType makeResumptionTokenRequestsAndVerifyCount(List<HeaderType> totalRecords, ResumptionTokenType resumptionToken, VerbType verb, int desiredCount, int expectedCursor) {
    RequestSpecification request = createBaseRequest().with().param(VERB_PARAM, verb.value()).param(RESUMPTION_TOKEN_PARAM, resumptionToken.getValue());
    OAIPMH oaipmh;
    List<HeaderType> records;
    oaipmh = verify200WithXml(request, verb);
    verifyListResponse(oaipmh, verb, desiredCount);
    resumptionToken = getResumptionToken(oaipmh, verb);
    assertThat(resumptionToken, is(notNullValue()));
    assertEquals(expectedCursor, resumptionToken.getCursor().intValue());
    records = getHeadersListDependOnVerbType(verb, oaipmh);
    totalRecords.addAll(records);
    return resumptionToken;
}
Also used : OAIPMH(org.openarchives.oai._2.OAIPMH) HeaderType(org.openarchives.oai._2.HeaderType) RequestSpecification(io.restassured.specification.RequestSpecification)

Example 4 with ResumptionTokenType

use of org.openarchives.oai._2.ResumptionTokenType in project mod-oai-pmh by folio-org.

the class OaiPmhImplTest method getOaiListVerbResumptionFlowStarted.

@ParameterizedTest
@MethodSource("metadataPrefixAndVerbProviderExceptMarc21withHoldings")
void getOaiListVerbResumptionFlowStarted(MetadataPrefix metadataPrefix, VerbType verb) {
    RequestSpecification request = createBaseRequest().with().param(VERB_PARAM, verb.value()).param(FROM_PARAM, PARTITIONABLE_RECORDS_DATE_TIME).param(METADATA_PREFIX_PARAM, metadataPrefix.getName()).param(SET_PARAM, "all");
    OAIPMH oaipmh = verify200WithXml(request, verb);
    verifyListResponse(oaipmh, verb, 10);
    ResumptionTokenType resumptionToken = getResumptionToken(oaipmh, verb);
    assertThat(resumptionToken, is(notNullValue()));
    assertThat(resumptionToken.getCompleteListSize(), is(equalTo(BigInteger.valueOf(100))));
    assertThat(resumptionToken.getCursor(), is(equalTo(BigInteger.ZERO)));
    assertThat(resumptionToken.getExpirationDate(), is(notNullValue()));
    String resumptionTokenValue = new String(Base64.getUrlDecoder().decode(resumptionToken.getValue()), StandardCharsets.UTF_8);
    List<NameValuePair> params = URLEncodedUtils.parse(resumptionTokenValue, StandardCharsets.UTF_8);
    assertThat(params, is(hasSize(8)));
    assertThat(getParamValue(params, METADATA_PREFIX_PARAM), is(equalTo(metadataPrefix.getName())));
    assertThat(getParamValue(params, FROM_PARAM), is(equalTo(PARTITIONABLE_RECORDS_DATE_TIME)));
    assertThat(getParamValue(params, UNTIL_PARAM), is((notNullValue())));
    assertThat(getParamValue(params, SET_PARAM), is(equalTo("all")));
    assertThat(getParamValue(params, OFFSET_PARAM), is(equalTo("10")));
    assertThat(getParamValue(params, TOTAL_RECORDS_PARAM), is(equalTo("100")));
    assertThat(getParamValue(params, NEXT_RECORD_ID_PARAM), is(equalTo("6506b79b-7702-48b2-9774-a1c538fdd34e")));
}
Also used : OAIPMH(org.openarchives.oai._2.OAIPMH) NameValuePair(org.apache.http.NameValuePair) ResumptionTokenType(org.openarchives.oai._2.ResumptionTokenType) RequestSpecification(io.restassured.specification.RequestSpecification) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Matchers.isEmptyString(org.hamcrest.Matchers.isEmptyString) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 5 with ResumptionTokenType

use of org.openarchives.oai._2.ResumptionTokenType in project mod-oai-pmh by folio-org.

the class OaiPmhImplTest method getOaiRecordsWithUntilAndMetadataPrefixMarc21AndResumptionToken.

@ParameterizedTest
@MethodSource("metadataPrefixAndVerbProviderExceptMarc21withHoldings")
void getOaiRecordsWithUntilAndMetadataPrefixMarc21AndResumptionToken(MetadataPrefix prefix, VerbType verb) {
    RequestSpecification request = createBaseRequest().with().param(VERB_PARAM, verb.value()).param(METADATA_PREFIX_PARAM, prefix.getName()).param(UNTIL_PARAM, LocalDateTime.now(ZoneOffset.UTC).format(ISO_UTC_DATE_TIME));
    OAIPMH oaipmh = verify200WithXml(request, verb);
    verifyListResponse(oaipmh, verb, 9);
    ResumptionTokenType actualResumptionToken = getResumptionToken(oaipmh, verb);
    assertThat(actualResumptionToken, is(notNullValue()));
    assertThat(actualResumptionToken.getValue(), is(notNullValue()));
    RequestSpecification requestWithResumptionToken = createBaseRequest().with().param(VERB_PARAM, verb.value()).param(RESUMPTION_TOKEN_PARAM, actualResumptionToken.getValue());
    OAIPMH oai = verify200WithXml(requestWithResumptionToken, verb);
    ResumptionTokenType nextResumptionToken = getResumptionToken(oai, verb);
    assertThat(nextResumptionToken, is(notNullValue()));
    assertThat(nextResumptionToken.getValue(), is(notNullValue()));
    assertThat(nextResumptionToken.getCompleteListSize(), is(equalTo(BigInteger.valueOf(11))));
}
Also used : OAIPMH(org.openarchives.oai._2.OAIPMH) ResumptionTokenType(org.openarchives.oai._2.ResumptionTokenType) RequestSpecification(io.restassured.specification.RequestSpecification) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

OAIPMH (org.openarchives.oai._2.OAIPMH)18 ResumptionTokenType (org.openarchives.oai._2.ResumptionTokenType)18 RequestSpecification (io.restassured.specification.RequestSpecification)15 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)14 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)11 Matchers.isEmptyString (org.hamcrest.Matchers.isEmptyString)11 MethodSource (org.junit.jupiter.params.provider.MethodSource)10 NameValuePair (org.apache.http.NameValuePair)5 HeaderType (org.openarchives.oai._2.HeaderType)3 JsonArray (io.vertx.core.json.JsonArray)2 Response (javax.ws.rs.core.Response)2 ResponseHelper (org.folio.oaipmh.helpers.response.ResponseHelper)2 Test (org.junit.jupiter.api.Test)2 EnumSource (org.junit.jupiter.params.provider.EnumSource)2 OAIPMHerrorType (org.openarchives.oai._2.OAIPMHerrorType)2 RecordType (org.openarchives.oai._2.RecordType)2 Context (io.vertx.core.Context)1 Future (io.vertx.core.Future)1 Promise (io.vertx.core.Promise)1 DecodeException (io.vertx.core.json.DecodeException)1