Search in sources :

Example 1 with OKAPI_TENANT

use of org.folio.oaipmh.Constants.OKAPI_TENANT in project mod-oai-pmh by folio-org.

the class MarcWithHoldingsRequestHelper method enrichInstances.

private Future<List<JsonObject>> enrichInstances(List<JsonObject> result, Request request) {
    Map<String, JsonObject> instances = result.stream().collect(LinkedHashMap::new, (map, instance) -> map.put(instance.getString(INSTANCE_ID_FIELD_NAME), instance), Map::putAll);
    Promise<List<JsonObject>> promise = Promise.promise();
    var webClient = WebClientProvider.getWebClient();
    var httpRequest = webClient.postAbs(request.getOkapiUrl() + INVENTORY_ITEMS_AND_HOLDINGS_ENDPOINT);
    if (request.getOkapiUrl().contains("https:")) {
        httpRequest.ssl(true);
    }
    httpRequest.putHeader(OKAPI_TOKEN, request.getOkapiToken());
    httpRequest.putHeader(OKAPI_TENANT, TenantTool.tenantId(request.getOkapiHeaders()));
    httpRequest.putHeader(ACCEPT, APPLICATION_JSON);
    httpRequest.putHeader(CONTENT_TYPE, APPLICATION_JSON);
    JsonObject entries = new JsonObject();
    entries.put(INSTANCE_IDS_ENRICH_PARAM_NAME, new JsonArray(new ArrayList<>(instances.keySet())));
    entries.put(SKIP_SUPPRESSED_FROM_DISCOVERY_RECORDS, isSkipSuppressed(request));
    Promise<Boolean> responseChecked = Promise.promise();
    var jsonParser = new OaiPmhJsonParser().objectValueMode();
    jsonParser.handler(event -> {
        JsonObject itemsAndHoldingsFields = event.objectValue();
        String instanceId = itemsAndHoldingsFields.getString(INSTANCE_ID_FIELD_NAME);
        JsonObject instance = instances.get(instanceId);
        if (instance != null) {
            enrichDiscoverySuppressed(itemsAndHoldingsFields, instance);
            instance.put(RecordMetadataManager.ITEMS_AND_HOLDINGS_FIELDS, itemsAndHoldingsFields);
            // case when no items
            if (itemsAndHoldingsFields.getJsonArray(ITEMS).isEmpty()) {
                enrichOnlyEffectiveLocationEffectiveCallNumberFromHoldings(instance);
            } else {
                adjustItems(instance);
            }
        } else {
            logger.info("Instance with instanceId {} wasn't in the request.", instanceId);
        }
    });
    jsonParser.exceptionHandler(throwable -> responseChecked.future().onSuccess(invalidResponseReceivedAndProcessed -> {
        if (invalidResponseReceivedAndProcessed) {
            return;
        }
        logger.error("Error has been occurred at JsonParser while reading data from items-and-holdings response. Message:{}", throwable.getMessage(), throwable);
        promise.fail(throwable);
    }));
    httpRequest.as(BodyCodec.jsonStream(jsonParser)).sendBuffer(entries.toBuffer()).onSuccess(response -> {
        switch(response.statusCode()) {
            case 200:
                responseChecked.complete(false);
                break;
            case 403:
                {
                    String errorMsg = getErrorFromStorageMessage(INVENTORY_STORAGE, request.getOkapiUrl() + INVENTORY_ITEMS_AND_HOLDINGS_ENDPOINT, ENRICH_INSTANCES_MISSED_PERMISSION);
                    logger.error(errorMsg);
                    promise.fail(new IllegalStateException(errorMsg));
                    responseChecked.complete(true);
                    break;
                }
            default:
                {
                    String errorFromStorageMessage = getErrorFromStorageMessage(INVENTORY_STORAGE, request.getOkapiUrl() + INVENTORY_ITEMS_AND_HOLDINGS_ENDPOINT, response.statusMessage());
                    String errorMessage = errorFromStorageMessage + response.statusCode();
                    logger.error(errorMessage);
                    promise.fail(new IllegalStateException(errorFromStorageMessage));
                    responseChecked.complete(true);
                }
        }
        promise.complete(new ArrayList<>(instances.values()));
    }).onFailure(e -> {
        logger.error(e.getMessage());
        promise.fail(e);
    });
    return promise.future();
}
Also used : 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) ArrayList(java.util.ArrayList) JsonObject(io.vertx.core.json.JsonObject) LinkedHashMap(java.util.LinkedHashMap) JsonArray(io.vertx.core.json.JsonArray) List(java.util.List) ArrayList(java.util.ArrayList) Collectors.toList(java.util.stream.Collectors.toList) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with OKAPI_TENANT

use of org.folio.oaipmh.Constants.OKAPI_TENANT in project mod-oai-pmh by folio-org.

the class MarcWithHoldingsRequestHelper method buildInventoryQuery.

private HttpRequest<Buffer> buildInventoryQuery(Request request) {
    Map<String, String> paramMap = new HashMap<>();
    Date date = convertStringToDate(request.getFrom(), false, false);
    if (date != null) {
        paramMap.put(START_DATE_PARAM_NAME, dateFormat.format(date));
    }
    date = convertStringToDate(request.getUntil(), true, false);
    if (date != null) {
        paramMap.put(END_DATE_PARAM_NAME, dateFormat.format(date));
    }
    paramMap.put(DELETED_RECORD_SUPPORT_PARAM_NAME, String.valueOf(RepositoryConfigurationUtil.isDeletedRecordsEnabled(request.getRequestId())));
    paramMap.put(SKIP_SUPPRESSED_FROM_DISCOVERY_RECORDS, String.valueOf(isSkipSuppressed(request)));
    final String params = paramMap.entrySet().stream().map(e -> e.getKey() + "=" + e.getValue()).collect(Collectors.joining("&"));
    String inventoryQuery = format("%s%s?%s", request.getOkapiUrl(), INVENTORY_UPDATED_INSTANCES_ENDPOINT, params);
    logger.debug("Sending request to {}", inventoryQuery);
    final HttpRequest<Buffer> httpRequest = WebClientProvider.getWebClientToDownloadInstances().getAbs(inventoryQuery);
    httpRequest.putHeader(OKAPI_TOKEN, request.getOkapiToken());
    httpRequest.putHeader(OKAPI_TENANT, TenantTool.tenantId(request.getOkapiHeaders()));
    httpRequest.putHeader(ACCEPT, APPLICATION_JSON);
    if (request.getOkapiUrl().contains("https")) {
        httpRequest.ssl(true);
    }
    return httpRequest;
}
Also used : 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) Buffer(io.vertx.core.buffer.Buffer) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Date(java.util.Date)

Aggregations

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