Search in sources :

Example 1 with ResponseHelper

use of org.folio.oaipmh.helpers.response.ResponseHelper in project mod-oai-pmh by folio-org.

the class GetOaiIdentifiersHelper method handle.

@Override
public Future<javax.ws.rs.core.Response> handle(Request request, Context ctx) {
    Promise<javax.ws.rs.core.Response> promise = Promise.promise();
    try {
        ResponseHelper responseHelper = getResponseHelper();
        List<OAIPMHerrorType> errors = validateRequest(request);
        if (!errors.isEmpty()) {
            OAIPMH oai;
            if (request.isRestored()) {
                oai = responseHelper.buildOaipmhResponseWithErrors(request, BAD_RESUMPTION_TOKEN, RESUMPTION_TOKEN_FORMAT_ERROR);
            } else {
                oai = responseHelper.buildOaipmhResponseWithErrors(request, errors);
            }
            promise.complete(getResponseHelper().buildFailureResponse(oai, request));
            return promise.future();
        }
        requestAndProcessSrsRecords(request, ctx, promise);
    } catch (Exception e) {
        handleException(promise, e);
    }
    return promise.future();
}
Also used : Response(javax.ws.rs.core.Response) OAIPMH(org.openarchives.oai._2.OAIPMH) OAIPMHerrorType(org.openarchives.oai._2.OAIPMHerrorType) ResponseHelper(org.folio.oaipmh.helpers.response.ResponseHelper)

Example 2 with ResponseHelper

use of org.folio.oaipmh.helpers.response.ResponseHelper in project mod-oai-pmh by folio-org.

the class GetOaiIdentifiersHelper method buildListIdentifiers.

/**
 * Builds {@link ListIdentifiersType} with headers if there is any item or {@code null}
 *
 * @param request           request
 * @param srsRecords the response from the storage which contains items
 * @return {@link ListIdentifiersType} with headers if there is any or {@code null}
 */
private OAIPMH buildListIdentifiers(Request request, JsonObject srsRecords) {
    ResponseHelper responseHelper = getResponseHelper();
    JsonArray instances = storageHelper.getItems(srsRecords);
    Integer totalRecords = storageHelper.getTotalRecords(srsRecords);
    if (request.isRestored() && !canResumeRequestSequence(request, totalRecords, instances)) {
        return responseHelper.buildOaipmhResponseWithErrors(request, BAD_RESUMPTION_TOKEN, RESUMPTION_TOKEN_FLOW_ERROR);
    }
    if (instances != null && !instances.isEmpty()) {
        logger.debug("{} entries retrieved out of {}.", instances.size(), totalRecords);
        ListIdentifiersType identifiers = new ListIdentifiersType().withResumptionToken(buildResumptionToken(request, instances, totalRecords));
        String identifierPrefix = request.getIdentifierPrefix();
        instances.stream().map(object -> (JsonObject) object).filter(instance -> StringUtils.isNotEmpty(storageHelper.getIdentifierId(instance))).filter(instance -> filterInstance(request, instance)).map(instance -> addHeader(identifierPrefix, request, instance)).forEach(identifiers::withHeaders);
        if (identifiers.getHeaders().isEmpty()) {
            OAIPMH oaipmh = responseHelper.buildBaseOaipmhResponse(request);
            return oaipmh.withErrors(createNoRecordsFoundError());
        }
        ResumptionTokenType resumptionToken = buildResumptionToken(request, instances, totalRecords);
        OAIPMH oaipmh = responseHelper.buildBaseOaipmhResponse(request).withListIdentifiers(identifiers);
        addResumptionTokenToOaiResponse(oaipmh, resumptionToken);
        return oaipmh;
    }
    return responseHelper.buildOaipmhResponseWithErrors(request, createNoRecordsFoundError());
}
Also used : JsonArray(io.vertx.core.json.JsonArray) RESUMPTION_TOKEN_FLOW_ERROR(org.folio.oaipmh.Constants.RESUMPTION_TOKEN_FLOW_ERROR) Promise(io.vertx.core.Promise) ListIdentifiersType(org.openarchives.oai._2.ListIdentifiersType) Context(io.vertx.core.Context) Future(io.vertx.core.Future) StringUtils(org.apache.commons.lang3.StringUtils) JsonArray(io.vertx.core.json.JsonArray) ResponseHelper(org.folio.oaipmh.helpers.response.ResponseHelper) OAIPMHerrorType(org.openarchives.oai._2.OAIPMHerrorType) ResumptionTokenType(org.openarchives.oai._2.ResumptionTokenType) List(java.util.List) Logger(org.apache.logging.log4j.Logger) Request(org.folio.oaipmh.Request) Response(javax.ws.rs.core.Response) OAIPMH(org.openarchives.oai._2.OAIPMH) BAD_RESUMPTION_TOKEN(org.openarchives.oai._2.OAIPMHerrorcodeType.BAD_RESUMPTION_TOKEN) RESUMPTION_TOKEN_FORMAT_ERROR(org.folio.oaipmh.Constants.RESUMPTION_TOKEN_FORMAT_ERROR) JsonObject(io.vertx.core.json.JsonObject) LogManager(org.apache.logging.log4j.LogManager) ListIdentifiersType(org.openarchives.oai._2.ListIdentifiersType) OAIPMH(org.openarchives.oai._2.OAIPMH) ResumptionTokenType(org.openarchives.oai._2.ResumptionTokenType) ResponseHelper(org.folio.oaipmh.helpers.response.ResponseHelper)

Example 3 with ResponseHelper

use of org.folio.oaipmh.helpers.response.ResponseHelper in project mod-oai-pmh by folio-org.

the class MarcWithHoldingsRequestHelper method buildRecordsResponse.

private Future<Response> buildRecordsResponse(Request request, String requestId, List<JsonObject> batch, Map<String, JsonObject> srsResponse, boolean firstBatch, String nextInstanceId, boolean deletedRecordSupport) {
    Promise<Response> promise = Promise.promise();
    try {
        List<RecordType> records = buildRecordsList(request, batch, srsResponse, deletedRecordSupport);
        logger.debug("Build records response, instances = {}, instances with srs records = {}.", batch.size(), records.size());
        ResponseHelper responseHelper = getResponseHelper();
        OAIPMH oaipmh = responseHelper.buildBaseOaipmhResponse(request);
        if (records.isEmpty() && nextInstanceId == null && firstBatch) {
            oaipmh.withErrors(createNoRecordsFoundError());
        } else {
            oaipmh.withListRecords(new ListRecordsType().withRecords(records));
        }
        Response response;
        if (oaipmh.getErrors().isEmpty()) {
            if (!firstBatch || nextInstanceId != null) {
                ResumptionTokenType resumptionToken = buildResumptionTokenFromRequest(request, requestId, records.size(), nextInstanceId);
                oaipmh.getListRecords().withResumptionToken(resumptionToken);
            }
            response = responseHelper.buildSuccessResponse(oaipmh);
        } else {
            response = responseHelper.buildFailureResponse(oaipmh, request);
        }
        promise.complete(response);
    } catch (Exception e) {
        handleException(promise, e);
    }
    return promise.future();
}
Also used : Response(javax.ws.rs.core.Response) HttpResponse(io.vertx.ext.web.client.HttpResponse) OAIPMH(org.openarchives.oai._2.OAIPMH) ResumptionTokenType(org.openarchives.oai._2.ResumptionTokenType) RecordType(org.openarchives.oai._2.RecordType) ResponseHelper(org.folio.oaipmh.helpers.response.ResponseHelper) DecodeException(io.vertx.core.json.DecodeException) ListRecordsType(org.openarchives.oai._2.ListRecordsType)

Example 4 with ResponseHelper

use of org.folio.oaipmh.helpers.response.ResponseHelper in project mod-oai-pmh by folio-org.

the class OaiPmhImpl method getFutureWithErrorResponse.

private Future<Response> getFutureWithErrorResponse(Throwable t, Request request) {
    final Response errorResponse;
    if (t instanceof IllegalArgumentException) {
        final ResponseHelper rh = ResponseHelper.getInstance();
        OAIPMH oaipmh = rh.buildOaipmhResponseWithErrors(request, BAD_RESUMPTION_TOKEN, RESUMPTION_TOKEN_FORMAT_ERROR);
        errorResponse = rh.buildFailureResponse(oaipmh, request);
    } else {
        errorResponse = GetOaiRecordsResponse.respond500WithTextPlain(t.getMessage());
    }
    return succeededFuture(errorResponse);
}
Also used : Response(javax.ws.rs.core.Response) OAIPMH(org.openarchives.oai._2.OAIPMH) ResponseHelper(org.folio.oaipmh.helpers.response.ResponseHelper)

Example 5 with ResponseHelper

use of org.folio.oaipmh.helpers.response.ResponseHelper in project mod-oai-pmh by folio-org.

the class GetOaiSetsHelper method handle.

@Override
public Future<Response> handle(Request request, Context ctx) {
    Promise<Response> promise = Promise.promise();
    ResponseHelper responseHelper = getResponseHelper();
    try {
        OAIPMH oai;
        if (request.getResumptionToken() != null) {
            oai = responseHelper.buildOaipmhResponseWithErrors(request, BAD_RESUMPTION_TOKEN, RESUMPTION_TOKEN_FORMAT_ERROR);
            promise.complete(responseHelper.buildFailureResponse(oai, request));
            return promise.future();
        }
        oai = responseHelper.buildBaseOaipmhResponse(request).withListSets(new ListSetsType().withSets(getSupportedSetTypes()));
        promise.complete(responseHelper.buildSuccessResponse(oai));
    } catch (Exception e) {
        logger.error("Error happened while processing ListSets verb request.", e);
        promise.fail(e);
    }
    return promise.future();
}
Also used : Response(javax.ws.rs.core.Response) OAIPMH(org.openarchives.oai._2.OAIPMH) ListSetsType(org.openarchives.oai._2.ListSetsType) ResponseHelper(org.folio.oaipmh.helpers.response.ResponseHelper)

Aggregations

Response (javax.ws.rs.core.Response)5 ResponseHelper (org.folio.oaipmh.helpers.response.ResponseHelper)5 OAIPMH (org.openarchives.oai._2.OAIPMH)5 OAIPMHerrorType (org.openarchives.oai._2.OAIPMHerrorType)2 ResumptionTokenType (org.openarchives.oai._2.ResumptionTokenType)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 JsonArray (io.vertx.core.json.JsonArray)1 JsonObject (io.vertx.core.json.JsonObject)1 HttpResponse (io.vertx.ext.web.client.HttpResponse)1 List (java.util.List)1 StringUtils (org.apache.commons.lang3.StringUtils)1 LogManager (org.apache.logging.log4j.LogManager)1 Logger (org.apache.logging.log4j.Logger)1 RESUMPTION_TOKEN_FLOW_ERROR (org.folio.oaipmh.Constants.RESUMPTION_TOKEN_FLOW_ERROR)1 RESUMPTION_TOKEN_FORMAT_ERROR (org.folio.oaipmh.Constants.RESUMPTION_TOKEN_FORMAT_ERROR)1 Request (org.folio.oaipmh.Request)1 ListIdentifiersType (org.openarchives.oai._2.ListIdentifiersType)1