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);
}
}
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'."));
}
}
}
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);
}
}
}
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;
}
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();
}));
}
Aggregations