Search in sources :

Example 1 with Failure

use of org.folio.okapi.common.Failure in project okapi by folio-org.

the class ProxyService method proxyHeaders.

private void proxyHeaders(Iterator<ModuleInstance> it, ProxyContext pc, ReadStream<Buffer> stream, Buffer bcontent, ModuleInstance mi) {
    RoutingContext ctx = pc.getCtx();
    HttpClientRequest cReq = httpClient.requestAbs(ctx.request().method(), makeUrl(mi, ctx), res -> {
        if (res.statusCode() < 200 || res.statusCode() >= 300) {
            proxyResponseImmediate(pc, res, mi);
            if (bcontent == null) {
                stream.resume();
            }
        } else if (it.hasNext()) {
            relayToRequest(res, pc, mi);
            makeTraceHeader(mi, res.statusCode(), pc);
            res.endHandler(x -> proxyR(it, pc, stream, bcontent));
        } else {
            relayToResponse(ctx.response(), res);
            makeTraceHeader(mi, res.statusCode(), pc);
            if (bcontent == null) {
                stream.handler(data -> {
                    ctx.response().write(data);
                    pc.trace("ProxyHeaders request chunk '" + data.toString() + "'");
                });
                stream.endHandler(v -> {
                    ctx.response().end();
                    pc.trace("ProxyHeaders request end");
                });
                stream.exceptionHandler(e -> pc.warn("proxyHeaders: content exception ", e));
                stream.resume();
            } else {
                pc.trace("ProxyHeaders request buf '" + bcontent + "'");
                ctx.response().end(bcontent);
            }
        }
    });
    cReq.exceptionHandler(e -> {
        pc.warn("proxyHeaders failure: " + mi.getUrl() + ": ", e);
        pc.responseText(500, "proxyHeaders failure: " + mi.getModuleDescriptor().getId() + " " + mi.getUrl() + ": " + e + " " + e.getMessage());
    });
    cReq.headers().setAll(ctx.request().headers());
    cReq.headers().remove("Content-Length");
    cReq.end();
    log(pc, cReq);
}
Also used : HttpServerRequest(io.vertx.core.http.HttpServerRequest) Json(io.vertx.core.json.Json) Arrays(java.util.Arrays) ModuleDescriptor(org.folio.okapi.bean.ModuleDescriptor) XOkapiHeaders(org.folio.okapi.common.XOkapiHeaders) NOT_FOUND(org.folio.okapi.common.ErrorType.NOT_FOUND) MultiMap(io.vertx.core.MultiMap) OkapiToken(org.folio.okapi.common.OkapiToken) HashMap(java.util.HashMap) Random(java.util.Random) RoutingContext(io.vertx.ext.web.RoutingContext) Tenant(org.folio.okapi.bean.Tenant) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) OkapiClient(org.folio.okapi.common.OkapiClient) OkapiLogger(org.folio.okapi.common.OkapiLogger) HttpClientRequest(io.vertx.core.http.HttpClientRequest) HttpClientResponse(io.vertx.core.http.HttpClientResponse) Matcher(java.util.regex.Matcher) DeploymentDescriptor(org.folio.okapi.bean.DeploymentDescriptor) ProxyType(org.folio.okapi.bean.RoutingEntry.ProxyType) Map(java.util.Map) RoutingEntry(org.folio.okapi.bean.RoutingEntry) ProxyContext(org.folio.okapi.util.ProxyContext) ReadStream(io.vertx.core.streams.ReadStream) DiscoveryManager(org.folio.okapi.discovery.DiscoveryManager) ExtendedAsyncResult(org.folio.okapi.common.ExtendedAsyncResult) JsonObject(io.vertx.core.json.JsonObject) Failure(org.folio.okapi.common.Failure) InternalModule(org.folio.okapi.web.InternalModule) HttpClientOptions(io.vertx.core.http.HttpClientOptions) Logger(io.vertx.core.logging.Logger) USER(org.folio.okapi.common.ErrorType.USER) Iterator(java.util.Iterator) Vertx(io.vertx.core.Vertx) Set(java.util.Set) Success(org.folio.okapi.common.Success) ModuleInstance(org.folio.okapi.bean.ModuleInstance) List(java.util.List) INTERNAL(org.folio.okapi.common.ErrorType.INTERNAL) Buffer(io.vertx.core.buffer.Buffer) HttpMethod(io.vertx.core.http.HttpMethod) HttpServerResponse(io.vertx.core.http.HttpServerResponse) DropwizardHelper(org.folio.okapi.util.DropwizardHelper) Pattern(java.util.regex.Pattern) Handler(io.vertx.core.Handler) Comparator(java.util.Comparator) HttpClient(io.vertx.core.http.HttpClient) RoutingContext(io.vertx.ext.web.RoutingContext) HttpClientRequest(io.vertx.core.http.HttpClientRequest)

Example 2 with Failure

use of org.folio.okapi.common.Failure in project okapi by folio-org.

the class PullManager method getList.

private void getList(String urlBase, Handler<ExtendedAsyncResult<ModuleDescriptor[]>> fut) {
    String url = urlBase;
    if (!url.endsWith("/")) {
        url += "/";
    }
    url += "_/proxy/modules";
    final Buffer body = Buffer.buffer();
    HttpClientRequest req = httpClient.getAbs(url, res -> {
        res.handler(body::appendBuffer);
        res.endHandler(x -> {
            if (res.statusCode() != 200) {
                fut.handle(new Failure<>(ErrorType.USER, body.toString()));
            } else {
                ModuleDescriptor[] ml = Json.decodeValue(body.toString(), ModuleDescriptor[].class);
                fut.handle(new Success<>(ml));
            }
        });
        res.exceptionHandler(x -> fut.handle(new Failure<>(ErrorType.INTERNAL, x.getMessage())));
    });
    req.exceptionHandler(x -> fut.handle(new Failure<>(ErrorType.INTERNAL, x.getMessage())));
    req.end();
}
Also used : Buffer(io.vertx.core.buffer.Buffer) ModuleDescriptor(org.folio.okapi.bean.ModuleDescriptor) HttpClientRequest(io.vertx.core.http.HttpClientRequest) Failure(org.folio.okapi.common.Failure)

Example 3 with Failure

use of org.folio.okapi.common.Failure in project okapi by folio-org.

the class MongoUtil method delete.

public void delete(String id, Handler<ExtendedAsyncResult<Void>> fut) {
    JsonObject jq = new JsonObject().put("_id", id);
    cli.removeDocument(collection, jq, rres -> {
        if (rres.failed()) {
            fut.handle(new Failure<>(INTERNAL, rres.cause()));
        } else if (rres.result().getRemovedCount() == 0) {
            fut.handle(new Failure<>(NOT_FOUND, id));
        } else {
            fut.handle(new Success<>());
        }
    });
}
Also used : JsonObject(io.vertx.core.json.JsonObject) Failure(org.folio.okapi.common.Failure) Success(org.folio.okapi.common.Success)

Example 4 with Failure

use of org.folio.okapi.common.Failure in project okapi by folio-org.

the class InternalModule method pullModules.

private void pullModules(String body, Handler<ExtendedAsyncResult<String>> fut) {
    try {
        final PullDescriptor pmd = Json.decodeValue(body, PullDescriptor.class);
        pullManager.pull(pmd, res -> {
            if (res.failed()) {
                fut.handle(new Failure<>(res.getType(), res.cause()));
                return;
            }
            fut.handle(new Success<>(Json.encodePrettily(res.result())));
        });
    } catch (DecodeException ex) {
        fut.handle(new Failure<>(USER, ex));
    }
}
Also used : PullDescriptor(org.folio.okapi.bean.PullDescriptor) DecodeException(io.vertx.core.json.DecodeException) Failure(org.folio.okapi.common.Failure)

Example 5 with Failure

use of org.folio.okapi.common.Failure in project okapi by folio-org.

the class InternalModule method updateTenant.

private void updateTenant(String id, String body, Handler<ExtendedAsyncResult<String>> fut) {
    try {
        final TenantDescriptor td = Json.decodeValue(body, TenantDescriptor.class);
        if (!id.equals(td.getId())) {
            fut.handle(new Failure<>(USER, "Tenant.id=" + td.getId() + " id=" + id));
            return;
        }
        Tenant t = new Tenant(td);
        tenantManager.updateDescriptor(td, res -> {
            if (res.failed()) {
                fut.handle(new Failure<>(res.getType(), res.cause()));
                return;
            }
            final String s = Json.encodePrettily(t.getDescriptor());
            fut.handle(new Success<>(s));
        });
    } catch (DecodeException ex) {
        fut.handle(new Failure<>(USER, ex));
    }
}
Also used : Tenant(org.folio.okapi.bean.Tenant) TenantDescriptor(org.folio.okapi.bean.TenantDescriptor) DecodeException(io.vertx.core.json.DecodeException) Failure(org.folio.okapi.common.Failure)

Aggregations

Failure (org.folio.okapi.common.Failure)18 DecodeException (io.vertx.core.json.DecodeException)13 ModuleDescriptor (org.folio.okapi.bean.ModuleDescriptor)6 TenantModuleDescriptor (org.folio.okapi.bean.TenantModuleDescriptor)6 Buffer (io.vertx.core.buffer.Buffer)4 HttpClientRequest (io.vertx.core.http.HttpClientRequest)4 DeploymentDescriptor (org.folio.okapi.bean.DeploymentDescriptor)4 Tenant (org.folio.okapi.bean.Tenant)4 JsonObject (io.vertx.core.json.JsonObject)3 ArrayList (java.util.ArrayList)3 Handler (io.vertx.core.Handler)2 MultiMap (io.vertx.core.MultiMap)2 Vertx (io.vertx.core.Vertx)2 HttpClient (io.vertx.core.http.HttpClient)2 HttpClientOptions (io.vertx.core.http.HttpClientOptions)2 HttpClientResponse (io.vertx.core.http.HttpClientResponse)2 HttpMethod (io.vertx.core.http.HttpMethod)2 HttpServerRequest (io.vertx.core.http.HttpServerRequest)2 HttpServerResponse (io.vertx.core.http.HttpServerResponse)2 Json (io.vertx.core.json.Json)2