Search in sources :

Example 16 with WebContext

use of org.folio.inventory.common.WebContext in project mod-inventory by folio-org.

the class FakeStorageModule method get.

private void get(RoutingContext routingContext) {
    WebContext context = new WebContext(routingContext);
    String id = routingContext.request().getParam("id");
    Map<String, JsonObject> resourcesForTenant = getResourcesForTenant(context);
    if (resourcesForTenant.containsKey(id)) {
        final JsonObject resourceRepresentation = resourcesForTenant.get(id);
        System.out.printf("Found %s resource: %s%n", recordTypeName, resourceRepresentation.encodePrettily());
        JsonResponse.success(routingContext.response(), resourceRepresentation);
    } else {
        System.out.printf("Failed to find %s resource: %s%n", recordTypeName, id);
        ClientErrorResponse.notFound(routingContext.response());
    }
}
Also used : WebContext(org.folio.inventory.common.WebContext) JsonObject(io.vertx.core.json.JsonObject)

Example 17 with WebContext

use of org.folio.inventory.common.WebContext in project mod-inventory by folio-org.

the class FakeStorageModule method create.

private void create(RoutingContext routingContext) {
    WebContext context = new WebContext(routingContext);
    JsonObject body = getJsonFromBody(routingContext);
    setDefaultProperties(body);
    String id = body.getString("id");
    createElement(context, body).thenAccept(notUsed -> {
        System.out.printf("Created %s resource: %s%n", recordTypeName, id);
        JsonResponse.created(routingContext.response(), body);
    }).exceptionally(error -> {
        EndpointFailureHandler.handleFailure(EndpointFailureHandler.getKnownException(error), routingContext);
        return null;
    });
}
Also used : WebContext(org.folio.inventory.common.WebContext) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) ClientErrorResponse(org.folio.inventory.support.http.server.ClientErrorResponse) Router(io.vertx.ext.web.Router) RecordPreProcessor(support.fakes.processors.RecordPreProcessor) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) RoutingContext(io.vertx.ext.web.RoutingContext) BodyHandler(io.vertx.ext.web.handler.BodyHandler) StringUtils(org.apache.commons.lang3.StringUtils) Supplier(java.util.function.Supplier) ID_FOR_FAILURE(api.ApiTestSuite.ID_FOR_FAILURE) ArrayList(java.util.ArrayList) ServerErrorResponse(org.folio.inventory.support.http.server.ServerErrorResponse) JsonResponse(org.folio.inventory.support.http.server.JsonResponse) ID_FOR_OPTIMISTIC_LOCKING_FAILURE(api.ApiTestSuite.ID_FOR_OPTIMISTIC_LOCKING_FAILURE) Map(java.util.Map) JsonObject(io.vertx.core.json.JsonObject) EndpointFailureHandler(org.folio.inventory.support.EndpointFailureHandler) Collection(java.util.Collection) DateTime(org.joda.time.DateTime) HttpHeaders(io.vertx.core.http.HttpHeaders) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) JsonArray(io.vertx.core.json.JsonArray) List(java.util.List) AbstractVerticle(io.vertx.core.AbstractVerticle) ValidationError(org.folio.inventory.support.http.server.ValidationError) SuccessResponse(org.folio.inventory.support.http.server.SuccessResponse) WebContext(org.folio.inventory.common.WebContext) JsonObject(io.vertx.core.json.JsonObject)

Example 18 with WebContext

use of org.folio.inventory.common.WebContext in project mod-inventory by folio-org.

the class FakeStorageModule method replace.

private void replace(RoutingContext routingContext) {
    WebContext context = new WebContext(routingContext);
    String id = routingContext.request().getParam("id");
    JsonObject rawBody = getJsonFromBody(routingContext);
    Map<String, JsonObject> resourcesForTenant = getResourcesForTenant(context);
    preProcessRecords(resourcesForTenant.get(id), rawBody).thenAccept(body -> {
        setDefaultProperties(body);
        if (ID_FOR_FAILURE.toString().equals(id)) {
            ServerErrorResponse.internalError(routingContext.response(), "Test Internal Server Error");
        } else if (ID_FOR_OPTIMISTIC_LOCKING_FAILURE.toString().equals(id)) {
            ClientErrorResponse.optimisticLocking(routingContext.response(), "Optimistic Locking");
        } else if (resourcesForTenant.containsKey(id)) {
            System.out.printf("Replaced %s resource: %s%n", recordTypeName, id);
            resourcesForTenant.replace(id, body);
            SuccessResponse.noContent(routingContext.response());
        } else {
            System.out.printf("Created %s resource: %s%n", recordTypeName, id);
            resourcesForTenant.put(id, body);
            SuccessResponse.noContent(routingContext.response());
        }
    });
}
Also used : WebContext(org.folio.inventory.common.WebContext) JsonObject(io.vertx.core.json.JsonObject)

Example 19 with WebContext

use of org.folio.inventory.common.WebContext in project mod-inventory by folio-org.

the class FakeStorageModule method empty.

private void empty(RoutingContext routingContext) {
    WebContext context = new WebContext(routingContext);
    if (!hasCollectionDelete) {
        ClientErrorResponse.notFound(routingContext.response());
        return;
    }
    Map<String, JsonObject> resourcesForTenant = getResourcesForTenant(context);
    resourcesForTenant.clear();
    SuccessResponse.noContent(routingContext.response());
}
Also used : WebContext(org.folio.inventory.common.WebContext) JsonObject(io.vertx.core.json.JsonObject)

Example 20 with WebContext

use of org.folio.inventory.common.WebContext in project mod-inventory by folio-org.

the class Items method getById.

private void getById(RoutingContext routingContext) {
    WebContext context = new WebContext(routingContext);
    storage.getItemCollection(context).findById(routingContext.request().getParam("id"), (Success<Item> itemResponse) -> {
        Item item = itemResponse.getResult();
        if (item != null) {
            respondWithItemRepresentation(item, STATUS_SUCCESS, routingContext, context);
        } else {
            ClientErrorResponse.notFound(routingContext.response());
        }
    }, FailureResponseConsumer.serverError(routingContext.response()));
}
Also used : Item(org.folio.inventory.domain.items.Item) WebContext(org.folio.inventory.common.WebContext) Success(org.folio.inventory.common.domain.Success)

Aggregations

WebContext (org.folio.inventory.common.WebContext)36 JsonObject (io.vertx.core.json.JsonObject)27 RoutingContext (io.vertx.ext.web.RoutingContext)20 Router (io.vertx.ext.web.Router)19 Collectors (java.util.stream.Collectors)17 JsonArray (io.vertx.core.json.JsonArray)16 ServerErrorResponse (org.folio.inventory.support.http.server.ServerErrorResponse)16 BodyHandler (io.vertx.ext.web.handler.BodyHandler)15 MalformedURLException (java.net.MalformedURLException)15 URL (java.net.URL)15 ArrayList (java.util.ArrayList)15 List (java.util.List)15 CollectionResourceClient (org.folio.inventory.storage.external.CollectionResourceClient)15 HttpClient (io.vertx.core.http.HttpClient)14 CompletableFuture (java.util.concurrent.CompletableFuture)14 Storage (org.folio.inventory.storage.Storage)14 JsonResponse (org.folio.inventory.support.http.server.JsonResponse)14 UnsupportedEncodingException (java.io.UnsupportedEncodingException)12 ClientErrorResponse (org.folio.inventory.support.http.server.ClientErrorResponse)12 PagingParameters (org.folio.inventory.common.api.request.PagingParameters)11