Search in sources :

Example 1 with WebContext

use of org.folio.circulation.support.http.server.WebContext in project mod-circulation by folio-org.

the class FakeStorageModule method getMany.

private void getMany(RoutingContext routingContext) {
    WebContext context = new WebContext(routingContext);
    Integer limit = context.getIntegerParameter("limit", 10);
    Integer offset = context.getIntegerParameter("offset", 0);
    String query = context.getStringParameter("query", MATCH_ALL_RECORDS);
    log.debug("Handling {}", routingContext.request().uri());
    if (query != null) {
        queries.add(format("%s?%s", routingContext.request().path(), query));
    }
    Map<String, JsonObject> resourcesForTenant = getResourcesForTenant(context);
    List<JsonObject> filteredItems = new FakeCQLToJSONInterpreter().execute(resourcesForTenant.values(), query);
    List<JsonObject> pagedItems = filteredItems.stream().skip(offset).limit(limit).collect(Collectors.toList());
    JsonObject result = new JsonObject();
    result.put(collectionPropertyName, new JsonArray(pagedItems));
    result.put("totalRecords", filteredItems.size());
    log.debug("Found {} resources: {}", recordTypeName, result.encodePrettily());
    HttpServerResponse response = routingContext.response();
    String json = Json.encodePrettily(result);
    Buffer buffer = Buffer.buffer(json, "UTF-8");
    response.setStatusCode(200);
    response.putHeader("content-type", "application/json; charset=utf-8");
    response.putHeader("content-length", Integer.toString(buffer.length()));
    response.putHeader("X-Okapi-Trace", "GET mod-authtoken-1.4.1-SNAPSHOT.21");
    response.putHeader("X-Okapi-Trace", "GET mod-circulation-10.1.0-SNAPSHOT.131");
    response.write(buffer);
    response.end();
}
Also used : JsonArray(io.vertx.core.json.JsonArray) Buffer(io.vertx.core.buffer.Buffer) WebContext(org.folio.circulation.support.http.server.WebContext) HttpServerResponse(io.vertx.core.http.HttpServerResponse) JsonObject(io.vertx.core.json.JsonObject)

Example 2 with WebContext

use of org.folio.circulation.support.http.server.WebContext in project mod-circulation by folio-org.

the class FakeStorageModule method checkUniqueProperties.

private void checkUniqueProperties(RoutingContext routingContext) {
    if (uniqueProperties.isEmpty()) {
        routingContext.next();
        return;
    }
    final WebContext context = new WebContext(routingContext);
    JsonObject body = getJsonFromBody(routingContext);
    ArrayList<ValidationError> errors = new ArrayList<>();
    uniqueProperties.forEach(uniqueProperty -> {
        String proposedValue = body.getString(uniqueProperty);
        Map<String, JsonObject> records = getResourcesForTenant(new WebContext(routingContext));
        if (records.values().stream().map(record -> record.getString(uniqueProperty)).anyMatch(usedValue -> usedValue.equals(proposedValue))) {
            errors.add(new ValidationError(format("%s with this %s already exists", recordTypeName, uniqueProperty), uniqueProperty, proposedValue));
            context.write(new ValidationErrorFailure(errors));
        }
    });
    if (errors.isEmpty()) {
        routingContext.next();
    }
}
Also used : Arrays(java.util.Arrays) ZonedDateTime(java.time.ZonedDateTime) BiFunction(java.util.function.BiFunction) RequiredArgsConstructor(lombok.RequiredArgsConstructor) Router(io.vertx.ext.web.Router) DateFormatUtil.formatDateTime(org.folio.circulation.support.utils.DateFormatUtil.formatDateTime) RoutingContext(io.vertx.ext.web.RoutingContext) BodyHandler(io.vertx.ext.web.handler.BodyHandler) StringUtils(org.apache.commons.lang3.StringUtils) Map(java.util.Map) CommonFailures.failedDueToServerError(org.folio.circulation.support.results.CommonFailures.failedDueToServerError) JsonObject(io.vertx.core.json.JsonObject) MATCH_ALL_RECORDS(api.support.fakes.CqlPredicate.MATCH_ALL_RECORDS) MethodHandles(java.lang.invoke.MethodHandles) Collection(java.util.Collection) Set(java.util.Set) UUID(java.util.UUID) ValidationError(org.folio.circulation.support.http.server.ValidationError) Result(org.folio.circulation.support.results.Result) EqualsAndHashCode(lombok.EqualsAndHashCode) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) Objects(java.util.Objects) NoContentResponse.noContent(org.folio.circulation.support.http.server.NoContentResponse.noContent) List(java.util.List) Stream(java.util.stream.Stream) Logger(org.apache.logging.log4j.Logger) Buffer(io.vertx.core.buffer.Buffer) ClockUtil(org.folio.circulation.support.utils.ClockUtil) HttpServerResponse(io.vertx.core.http.HttpServerResponse) AbstractVerticle(io.vertx.core.AbstractVerticle) ClockUtil.getZonedDateTime(org.folio.circulation.support.utils.ClockUtil.getZonedDateTime) Json(io.vertx.core.json.Json) Getter(lombok.Getter) WebContext(org.folio.circulation.support.http.server.WebContext) HashMap(java.util.HashMap) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) APITestContext(api.support.APITestContext) ClientErrorResponse(org.folio.circulation.support.http.server.ClientErrorResponse) ValidationErrorFailure(org.folio.circulation.support.ValidationErrorFailure) JsonSchemaValidator(org.folio.circulation.infrastructure.serialization.JsonSchemaValidator) JsonArray(io.vertx.core.json.JsonArray) StringUtils.isBlank(org.apache.commons.lang3.StringUtils.isBlank) JsonHttpResponse.created(org.folio.circulation.support.http.server.JsonHttpResponse.created) HttpMethod(io.vertx.core.http.HttpMethod) HttpStatus(org.folio.HttpStatus) Collections(java.util.Collections) LogManager(org.apache.logging.log4j.LogManager) WebContext(org.folio.circulation.support.http.server.WebContext) ArrayList(java.util.ArrayList) JsonObject(io.vertx.core.json.JsonObject) ValidationError(org.folio.circulation.support.http.server.ValidationError) ValidationErrorFailure(org.folio.circulation.support.ValidationErrorFailure)

Example 3 with WebContext

use of org.folio.circulation.support.http.server.WebContext in project mod-circulation by folio-org.

the class FakeStorageModule method delete.

private void delete(RoutingContext routingContext) {
    WebContext context = new WebContext(routingContext);
    String id = routingContext.request().getParam("id");
    Map<String, JsonObject> resourcesForTenant = getResourcesForTenant(context);
    if (resourcesForTenant.containsKey(id)) {
        resourcesForTenant.remove(id);
        noContent().writeTo(routingContext.response());
    } else {
        ClientErrorResponse.notFound(routingContext.response());
    }
}
Also used : WebContext(org.folio.circulation.support.http.server.WebContext) JsonObject(io.vertx.core.json.JsonObject)

Example 4 with WebContext

use of org.folio.circulation.support.http.server.WebContext in project mod-circulation by folio-org.

the class FakeStorageModule method create.

private void create(RoutingContext routingContext) {
    WebContext context = new WebContext(routingContext);
    JsonObject body = preProcessBody(null, getJsonFromBody(routingContext));
    String id = body.getString("id", UUID.randomUUID().toString());
    body.put("id", id);
    final ZonedDateTime now = getZonedDateTime();
    if (includeChangeMetadata) {
        final String fakeUserId = APITestContext.getUserId();
        body.put(changeMetadataPropertyName, new JsonObject().put("createdDate", formatDateTime(now)).put("createdByUserId", fakeUserId).put("updatedDate", formatDateTime(now)).put("updatedByUserId", fakeUserId));
    }
    final Map<String, JsonObject> existingRecords = getResourcesForTenant(context);
    if (constraint == null) {
        existingRecords.put(id, body);
        log.debug("Created {} resource: {}", recordTypeName, id);
        created(body).writeTo(routingContext.response());
    } else {
        final Result<Object> checkConstraint = constraint.apply(existingRecords.values(), body);
        if (checkConstraint.succeeded()) {
            existingRecords.put(id, body);
            log.debug("Created {} resource: {}", recordTypeName, id);
            created(body).writeTo(routingContext.response());
        } else {
            checkConstraint.cause().writeTo(routingContext.response());
        }
    }
}
Also used : WebContext(org.folio.circulation.support.http.server.WebContext) ZonedDateTime(java.time.ZonedDateTime) ClockUtil.getZonedDateTime(org.folio.circulation.support.utils.ClockUtil.getZonedDateTime) JsonObject(io.vertx.core.json.JsonObject) JsonObject(io.vertx.core.json.JsonObject)

Example 5 with WebContext

use of org.folio.circulation.support.http.server.WebContext in project mod-circulation by folio-org.

the class FakeStorageModule method batchUpdate.

private void batchUpdate(RoutingContext routingContext) {
    WebContext context = new WebContext(routingContext);
    JsonObject body = routingContext.getBodyAsJson();
    if (batchUpdatePreProcessor != null) {
        body = batchUpdatePreProcessor.apply(body);
    }
    JsonArray entities = body.getJsonArray(collectionPropertyName);
    Result<Void> lastResult = Result.succeeded(null);
    for (int entityIndex = 0; entityIndex < entities.size(); entityIndex++) {
        JsonObject entity = entities.getJsonObject(entityIndex);
        String id = entity.getString("id");
        lastResult = lastResult.next(notUsed -> replaceSingleItem(context, id, entity));
    }
    if (lastResult.failed()) {
        lastResult.cause().writeTo(routingContext.response());
    } else {
        routingContext.response().setStatusCode(201).end();
    }
}
Also used : JsonArray(io.vertx.core.json.JsonArray) Arrays(java.util.Arrays) ZonedDateTime(java.time.ZonedDateTime) BiFunction(java.util.function.BiFunction) RequiredArgsConstructor(lombok.RequiredArgsConstructor) Router(io.vertx.ext.web.Router) DateFormatUtil.formatDateTime(org.folio.circulation.support.utils.DateFormatUtil.formatDateTime) RoutingContext(io.vertx.ext.web.RoutingContext) BodyHandler(io.vertx.ext.web.handler.BodyHandler) StringUtils(org.apache.commons.lang3.StringUtils) Map(java.util.Map) CommonFailures.failedDueToServerError(org.folio.circulation.support.results.CommonFailures.failedDueToServerError) JsonObject(io.vertx.core.json.JsonObject) MATCH_ALL_RECORDS(api.support.fakes.CqlPredicate.MATCH_ALL_RECORDS) MethodHandles(java.lang.invoke.MethodHandles) Collection(java.util.Collection) Set(java.util.Set) UUID(java.util.UUID) ValidationError(org.folio.circulation.support.http.server.ValidationError) Result(org.folio.circulation.support.results.Result) EqualsAndHashCode(lombok.EqualsAndHashCode) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) Objects(java.util.Objects) NoContentResponse.noContent(org.folio.circulation.support.http.server.NoContentResponse.noContent) List(java.util.List) Stream(java.util.stream.Stream) Logger(org.apache.logging.log4j.Logger) Buffer(io.vertx.core.buffer.Buffer) ClockUtil(org.folio.circulation.support.utils.ClockUtil) HttpServerResponse(io.vertx.core.http.HttpServerResponse) AbstractVerticle(io.vertx.core.AbstractVerticle) ClockUtil.getZonedDateTime(org.folio.circulation.support.utils.ClockUtil.getZonedDateTime) Json(io.vertx.core.json.Json) Getter(lombok.Getter) WebContext(org.folio.circulation.support.http.server.WebContext) HashMap(java.util.HashMap) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) APITestContext(api.support.APITestContext) ClientErrorResponse(org.folio.circulation.support.http.server.ClientErrorResponse) ValidationErrorFailure(org.folio.circulation.support.ValidationErrorFailure) JsonSchemaValidator(org.folio.circulation.infrastructure.serialization.JsonSchemaValidator) JsonArray(io.vertx.core.json.JsonArray) StringUtils.isBlank(org.apache.commons.lang3.StringUtils.isBlank) JsonHttpResponse.created(org.folio.circulation.support.http.server.JsonHttpResponse.created) HttpMethod(io.vertx.core.http.HttpMethod) HttpStatus(org.folio.HttpStatus) Collections(java.util.Collections) LogManager(org.apache.logging.log4j.LogManager) WebContext(org.folio.circulation.support.http.server.WebContext) JsonObject(io.vertx.core.json.JsonObject)

Aggregations

WebContext (org.folio.circulation.support.http.server.WebContext)52 RoutingContext (io.vertx.ext.web.RoutingContext)38 Clients (org.folio.circulation.support.Clients)37 HttpClient (io.vertx.core.http.HttpClient)36 JsonObject (io.vertx.core.json.JsonObject)36 ItemRepository (org.folio.circulation.infrastructure.storage.inventory.ItemRepository)35 UserRepository (org.folio.circulation.infrastructure.storage.users.UserRepository)34 Router (io.vertx.ext.web.Router)32 LoanRepository (org.folio.circulation.infrastructure.storage.loans.LoanRepository)32 Result (org.folio.circulation.support.results.Result)32 CompletableFuture (java.util.concurrent.CompletableFuture)28 EventPublisher (org.folio.circulation.services.EventPublisher)27 RouteRegistration (org.folio.circulation.support.RouteRegistration)23 NoContentResponse (org.folio.circulation.support.http.server.NoContentResponse)23 Result.succeeded (org.folio.circulation.support.results.Result.succeeded)23 ValidationErrorFailure.singleValidationError (org.folio.circulation.support.ValidationErrorFailure.singleValidationError)22 MappingFunctions.toFixedValue (org.folio.circulation.support.results.MappingFunctions.toFixedValue)22 Loan (org.folio.circulation.domain.Loan)19 LogManager (org.apache.logging.log4j.LogManager)18 Logger (org.apache.logging.log4j.Logger)18