Search in sources :

Example 1 with OAIPMHerrorType

use of org.openarchives.oai._2.OAIPMHerrorType 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 OAIPMHerrorType

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

the class AbstractHelper method validateDateRange.

private void validateDateRange(Request request, List<OAIPMHerrorType> errors) {
    Pair<GranularityType, LocalDateTime> from = null;
    Pair<GranularityType, LocalDateTime> until = null;
    // Get repository supported granularity
    boolean isDateOnly = isDateOnlyGranularity(request);
    if (request.getFrom() != null) {
        ImmutablePair<String, String> date = new ImmutablePair<>(FROM_PARAM, request.getFrom());
        from = isDateOnly ? parseDate(date, errors) : parseDateTime(date, errors);
        if (from == null) {
            // In case the 'from' date is invalid, it cannot be sent in OAI-PMH/request@from because it contradicts schema definition
            request.getOaiRequest().setFrom(null);
        }
    }
    if (request.getUntil() != null) {
        ImmutablePair<String, String> date = new ImmutablePair<>(UNTIL_PARAM, request.getUntil());
        until = isDateOnly ? parseDate(date, errors) : parseDateTime(date, errors);
        if (until == null) {
            // In case the 'until' date is invalid, it cannot be sent in OAI-PMH/request@until because it contradicts schema definition
            request.getOaiRequest().setUntil(null);
        }
    }
    if (from != null && until != null) {
        // Both arguments must have the same granularity.
        if (from.getLeft() != until.getLeft()) {
            errors.add(new OAIPMHerrorType().withCode(BAD_ARGUMENT).withValue("Invalid date range: 'from' must have the same granularity as 'until'."));
        } else if (from.getRight().isAfter(until.getRight())) {
            // The from argument must be less than or equal to the until argument.
            errors.add(new OAIPMHerrorType().withCode(BAD_ARGUMENT).withValue("Invalid date range: 'from' must be less than or equal to 'until'."));
        }
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) OAIPMHerrorType(org.openarchives.oai._2.OAIPMHerrorType) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) GranularityType(org.openarchives.oai._2.GranularityType)

Example 3 with OAIPMHerrorType

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

the class VerbValidator method validateExclusiveParam.

/**
 * In case of resumption token param presence verifies if there any other parameters were specified too. If they were then error
 * will be added to error list.
 *
 * @param verb          - verb
 * @param requestParams - request parameters
 * @param request       - oai-pmh request
 * @param errors        - list of errors
 */
private void validateExclusiveParam(Verb verb, Map<String, String> requestParams, Request request, List<OAIPMHerrorType> errors) {
    String resumptionToken = requestParams.get(verb.getExclusiveParam());
    if (verb.getExclusiveParam() != null && resumptionToken != null) {
        requestParams.keySet().stream().filter(p -> !verb.getExcludedParams().contains(p)).filter(p -> !verb.getExclusiveParam().equals(p)).findAny().ifPresent(param -> {
            if (!param.equals(VERB_PARAM)) {
                errors.add(new OAIPMHerrorType().withCode(BAD_ARGUMENT).withValue(format(EXCLUSIVE_PARAM_ERROR_MESSAGE, verb.name(), verb.getExclusiveParam())));
            }
        });
        if (!request.isResumptionTokenParsableAndValid()) {
            OAIPMHerrorType error = new OAIPMHerrorType().withCode(BAD_RESUMPTION_TOKEN).withValue(format(INVALID_RESUMPTION_TOKEN, verb.name()));
            errors.add(error);
            return;
        }
        if (request.isResumptionTokenTimeExpired()) {
            OAIPMHerrorType errorByExpiredTime = new OAIPMHerrorType().withCode(BAD_RESUMPTION_TOKEN).withValue(EXPIRED_RESUMPTION_TOKEN);
            errors.add(errorByExpiredTime);
        }
    }
}
Also used : INVALID_RESUMPTION_TOKEN(org.folio.oaipmh.Constants.INVALID_RESUMPTION_TOKEN) EXPIRED_RESUMPTION_TOKEN(org.folio.oaipmh.Constants.EXPIRED_RESUMPTION_TOKEN) Set(java.util.Set) VERB_PARAM(org.folio.oaipmh.Constants.VERB_PARAM) StringUtils(org.apache.commons.lang3.StringUtils) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) BAD_ARGUMENT(org.openarchives.oai._2.OAIPMHerrorcodeType.BAD_ARGUMENT) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Objects(java.util.Objects) OAIPMHerrorType(org.openarchives.oai._2.OAIPMHerrorType) Component(org.springframework.stereotype.Component) List(java.util.List) Request(org.folio.oaipmh.Request) Verb(org.folio.oaipmh.domain.Verb) Map(java.util.Map) BAD_RESUMPTION_TOKEN(org.openarchives.oai._2.OAIPMHerrorcodeType.BAD_RESUMPTION_TOKEN) BAD_VERB(org.openarchives.oai._2.OAIPMHerrorcodeType.BAD_VERB) OAIPMHerrorType(org.openarchives.oai._2.OAIPMHerrorType)

Example 4 with OAIPMHerrorType

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

the class VerbValidator method validate.

/**
 * Validates request parameters except 'from' and 'until' against particular verb.
 *
 * @param object        - name of verb against witch parameters are validated
 * @param requestParams - map with request parameters
 * @return list of errors.
 */
public List<OAIPMHerrorType> validate(Object object, Map<String, String> requestParams, Request request) {
    List<OAIPMHerrorType> errors = new ArrayList<>();
    String verbName = Objects.nonNull(object) ? object.toString() : "empty";
    Verb verb = Verb.fromName(verbName);
    if (Objects.nonNull(verb)) {
        validateRequiredParams(requestParams, verb, errors);
        validateExclusiveParam(verb, requestParams, request, errors);
        validateIllegalParams(verb, requestParams, errors);
    } else {
        errors.add(new OAIPMHerrorType().withCode(BAD_VERB).withValue(format(VERB_NOT_IMPLEMENTED_ERROR_MESSAGE, verbName)));
    }
    return errors;
}
Also used : OAIPMHerrorType(org.openarchives.oai._2.OAIPMHerrorType) Verb(org.folio.oaipmh.domain.Verb) ArrayList(java.util.ArrayList)

Example 5 with OAIPMHerrorType

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

the class ResponseHelperTest method shouldBuildResponseWithNotFoundStatusCodeAsDefault.

@Test
void shouldBuildResponseWithNotFoundStatusCodeAsDefault(Vertx vertx, VertxTestContext testContext) {
    setupErrorsProcessingSettingWithValue(RESPOND_WITH_ERROR);
    vertx.runOnContext(event -> testContext.verify(() -> {
        OAIPMHerrorType errorTypeForDefaultStatusCode = new OAIPMHerrorType().withCode(NO_SET_HIERARCHY).withValue(TEST_ERROR_MESSAGE);
        OAIPMH oaipmh = responseHelper.buildOaipmhResponseWithErrors(request, errorTypeForDefaultStatusCode);
        Response response = responseHelper.buildFailureResponse(oaipmh, request);
        verifyResponse(response, HttpStatus.SC_NOT_FOUND);
        testContext.completeNow();
    }));
}
Also used : Response(javax.ws.rs.core.Response) OAIPMH(org.openarchives.oai._2.OAIPMH) OAIPMHerrorType(org.openarchives.oai._2.OAIPMHerrorType) Test(org.junit.jupiter.api.Test)

Aggregations

OAIPMHerrorType (org.openarchives.oai._2.OAIPMHerrorType)22 OAIPMH (org.openarchives.oai._2.OAIPMH)16 RequestSpecification (io.restassured.specification.RequestSpecification)9 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)9 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)7 Matchers.isEmptyString (org.hamcrest.Matchers.isEmptyString)7 EnumSource (org.junit.jupiter.params.provider.EnumSource)7 ArrayList (java.util.ArrayList)4 Response (javax.ws.rs.core.Response)4 Test (org.junit.jupiter.api.Test)4 String.format (java.lang.String.format)3 List (java.util.List)3 Map (java.util.Map)3 Objects (java.util.Objects)3 Collectors (java.util.stream.Collectors)3 Request (org.folio.oaipmh.Request)3 JsonArray (io.vertx.core.json.JsonArray)2 HashMap (java.util.HashMap)2 Verb (org.folio.oaipmh.domain.Verb)2 ResponseHelper (org.folio.oaipmh.helpers.response.ResponseHelper)2