Search in sources :

Example 1 with SEND_REQUEST

use of org.folio.oaipmh.service.MetricsCollectingService.MetricOperation.SEND_REQUEST in project mod-oai-pmh by folio-org.

the class MarcWithHoldingsRequestHelper method handle.

/**
 * Handle MarcWithHoldings request
 */
@Override
public Future<Response> handle(Request request, Context vertxContext) {
    Promise<Response> oaipmhResponsePromise = Promise.promise();
    metricsCollectingService.startMetric(request.getRequestId(), SEND_REQUEST);
    try {
        String resumptionToken = request.getResumptionToken();
        List<OAIPMHerrorType> errors = validateListRequest(request);
        if (!errors.isEmpty()) {
            return buildResponseWithErrors(request, oaipmhResponsePromise, errors);
        }
        String requestId;
        OffsetDateTime lastUpdateDate = OffsetDateTime.now(ZoneId.systemDefault());
        RequestMetadataLb requestMetadata = new RequestMetadataLb().setLastUpdatedDate(lastUpdateDate);
        Future<RequestMetadataLb> updateRequestMetadataFuture;
        if (resumptionToken == null) {
            requestId = request.getRequestId();
            requestMetadata.setRequestId(UUID.fromString(requestId));
            updateRequestMetadataFuture = instancesService.saveRequestMetadata(requestMetadata, request.getTenant());
        } else {
            requestId = request.getRequestId();
            updateRequestMetadataFuture = instancesService.updateRequestUpdatedDate(requestId, lastUpdateDate, request.getTenant());
        }
        updateRequestMetadataFuture.onSuccess(res -> {
            boolean isFirstBatch = resumptionToken == null;
            processBatch(request, vertxContext, oaipmhResponsePromise, requestId, isFirstBatch);
            if (isFirstBatch) {
                saveInstancesExecutor.executeBlocking(downloadInstancesPromise -> downloadInstances(request, oaipmhResponsePromise, downloadInstancesPromise, downloadContext), downloadInstancesResult -> {
                    instancesService.updateRequestStreamEnded(requestId, true, request.getTenant());
                    if (downloadInstancesResult.succeeded()) {
                        logger.info("Downloading instances complete.");
                    } else {
                        logger.error("Downloading instances was canceled due to the error. ", downloadInstancesResult.cause());
                        if (!oaipmhResponsePromise.future().isComplete()) {
                            oaipmhResponsePromise.fail(new IllegalStateException(downloadInstancesResult.cause()));
                        }
                    }
                });
            }
        }).onFailure(th -> handleException(oaipmhResponsePromise, th));
    } catch (Exception e) {
        handleException(oaipmhResponsePromise, e);
    }
    return oaipmhResponsePromise.future().onComplete(responseAsyncResult -> metricsCollectingService.endMetric(request.getRequestId(), SEND_REQUEST));
}
Also used : Response(javax.ws.rs.core.Response) HttpResponse(io.vertx.ext.web.client.HttpResponse) CONTENT_TYPE(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE) OFFSET_PARAM(org.folio.oaipmh.Constants.OFFSET_PARAM) DecodeException(io.vertx.core.json.DecodeException) Date(java.util.Date) NEXT_INSTANCE_PK_VALUE(org.folio.oaipmh.Constants.NEXT_INSTANCE_PK_VALUE) StatusType(org.openarchives.oai._2.StatusType) Autowired(org.springframework.beans.factory.annotation.Autowired) NEXT_RECORD_ID_PARAM(org.folio.oaipmh.Constants.NEXT_RECORD_ID_PARAM) SEND_REQUEST(org.folio.oaipmh.service.MetricsCollectingService.MetricOperation.SEND_REQUEST) RESUMPTION_TOKEN_TIMEOUT(org.folio.oaipmh.Constants.RESUMPTION_TOKEN_TIMEOUT) Context(io.vertx.core.Context) WorkerExecutor(io.vertx.core.WorkerExecutor) Tuple(io.vertx.sqlclient.Tuple) OAIPMHerrorType(org.openarchives.oai._2.OAIPMHerrorType) ResumptionTokenType(org.openarchives.oai._2.ResumptionTokenType) Request(org.folio.oaipmh.Request) NAME(org.folio.oaipmh.helpers.records.RecordMetadataManager.NAME) OAIPMH(org.openarchives.oai._2.OAIPMH) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) JsonObject(io.vertx.core.json.JsonObject) BigInteger(java.math.BigInteger) APPLICATION_JSON(javax.ws.rs.core.MediaType.APPLICATION_JSON) AbstractHelper(org.folio.oaipmh.helpers.AbstractHelper) UNTIL_PARAM(org.folio.oaipmh.Constants.UNTIL_PARAM) EXPIRATION_DATE_RESUMPTION_TOKEN_PARAM(org.folio.oaipmh.Constants.EXPIRATION_DATE_RESUMPTION_TOKEN_PARAM) OKAPI_TENANT(org.folio.oaipmh.Constants.OKAPI_TENANT) InstancesService(org.folio.oaipmh.service.InstancesService) ITEMS(org.folio.oaipmh.helpers.records.RecordMetadataManager.ITEMS) MetricsCollectingService(org.folio.oaipmh.service.MetricsCollectingService) UUID(java.util.UUID) ITEMS_AND_HOLDINGS_FIELDS(org.folio.oaipmh.helpers.records.RecordMetadataManager.ITEMS_AND_HOLDINGS_FIELDS) Instant(java.time.Instant) Future(io.vertx.core.Future) TenantTool(org.folio.rest.tools.utils.TenantTool) Collectors(java.util.stream.Collectors) PostgresClient(org.folio.rest.persist.PostgresClient) ZoneId(java.time.ZoneId) String.format(java.lang.String.format) RepositoryConfigurationUtil(org.folio.oaipmh.helpers.RepositoryConfigurationUtil) REQUEST_ID_PARAM(org.folio.oaipmh.Constants.REQUEST_ID_PARAM) CALL_NUMBER(org.folio.oaipmh.helpers.records.RecordMetadataManager.CALL_NUMBER) Objects(java.util.Objects) List(java.util.List) Logger(org.apache.logging.log4j.Logger) OffsetDateTime(java.time.OffsetDateTime) Buffer(io.vertx.core.buffer.Buffer) Response(javax.ws.rs.core.Response) WebClientProvider(org.folio.oaipmh.WebClientProvider) RETRY_ATTEMPTS(org.folio.oaipmh.Constants.RETRY_ATTEMPTS) STATUS_MESSAGE(org.folio.oaipmh.Constants.STATUS_MESSAGE) ChronoField(java.time.temporal.ChronoField) HttpResponse(io.vertx.ext.web.client.HttpResponse) SourceStorageSourceRecordsClientWrapper(org.folio.oaipmh.service.SourceStorageSourceRecordsClientWrapper) RequestMetadataLb(org.folio.rest.jooq.tables.pojos.RequestMetadataLb) BodyCodec(io.vertx.ext.web.codec.BodyCodec) INSTANCES_PROCESSING(org.folio.oaipmh.service.MetricsCollectingService.MetricOperation.INSTANCES_PROCESSING) HashMap(java.util.HashMap) OKAPI_TOKEN(org.folio.oaipmh.Constants.OKAPI_TOKEN) REPOSITORY_MAX_RECORDS_PER_RESPONSE(org.folio.oaipmh.Constants.REPOSITORY_MAX_RECORDS_PER_RESPONSE) JsonEvent(io.vertx.core.parsetools.JsonEvent) CollectionUtils(org.apache.commons.collections4.CollectionUtils) SpringContextUtil(org.folio.spring.SpringContextUtil) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) RepositoryConfigurationUtil.getBooleanProperty(org.folio.oaipmh.helpers.RepositoryConfigurationUtil.getBooleanProperty) HttpRequestImpl(io.vertx.ext.web.client.impl.HttpRequestImpl) AsyncResult(io.vertx.core.AsyncResult) STATUS_CODE(org.folio.oaipmh.Constants.STATUS_CODE) Promise(io.vertx.core.Promise) Vertx(io.vertx.core.Vertx) Instances(org.folio.rest.jooq.tables.pojos.Instances) RecordMetadataManager(org.folio.oaipmh.helpers.records.RecordMetadataManager) Maps(com.google.common.collect.Maps) HttpRequest(io.vertx.ext.web.client.HttpRequest) JsonArray(io.vertx.core.json.JsonArray) ResponseHelper(org.folio.oaipmh.helpers.response.ResponseHelper) PgConnection(io.vertx.pgclient.PgConnection) Collectors.toList(java.util.stream.Collectors.toList) RecordType(org.openarchives.oai._2.RecordType) REPOSITORY_SRS_HTTP_REQUEST_RETRY_ATTEMPTS(org.folio.oaipmh.Constants.REPOSITORY_SRS_HTTP_REQUEST_RETRY_ATTEMPTS) ACCEPT(javax.ws.rs.core.HttpHeaders.ACCEPT) LOCATION(org.folio.oaipmh.Constants.LOCATION) REPOSITORY_SUPPRESSED_RECORDS_PROCESSING(org.folio.oaipmh.Constants.REPOSITORY_SUPPRESSED_RECORDS_PROCESSING) ListRecordsType(org.openarchives.oai._2.ListRecordsType) Handler(io.vertx.core.Handler) LogManager(org.apache.logging.log4j.LogManager) Collections(java.util.Collections) OAIPMHerrorType(org.openarchives.oai._2.OAIPMHerrorType) OffsetDateTime(java.time.OffsetDateTime) RequestMetadataLb(org.folio.rest.jooq.tables.pojos.RequestMetadataLb) DecodeException(io.vertx.core.json.DecodeException)

Aggregations

Maps (com.google.common.collect.Maps)1 AsyncResult (io.vertx.core.AsyncResult)1 Context (io.vertx.core.Context)1 Future (io.vertx.core.Future)1 Handler (io.vertx.core.Handler)1 Promise (io.vertx.core.Promise)1 Vertx (io.vertx.core.Vertx)1 WorkerExecutor (io.vertx.core.WorkerExecutor)1 Buffer (io.vertx.core.buffer.Buffer)1 DecodeException (io.vertx.core.json.DecodeException)1 JsonArray (io.vertx.core.json.JsonArray)1 JsonObject (io.vertx.core.json.JsonObject)1 JsonEvent (io.vertx.core.parsetools.JsonEvent)1 HttpRequest (io.vertx.ext.web.client.HttpRequest)1 HttpResponse (io.vertx.ext.web.client.HttpResponse)1 HttpRequestImpl (io.vertx.ext.web.client.impl.HttpRequestImpl)1 BodyCodec (io.vertx.ext.web.codec.BodyCodec)1 PgConnection (io.vertx.pgclient.PgConnection)1 Tuple (io.vertx.sqlclient.Tuple)1 String.format (java.lang.String.format)1