Search in sources :

Example 16 with Failure

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

the class InternalModule method updateModule.

private void updateModule(ProxyContext pc, String id, String body, Handler<ExtendedAsyncResult<String>> fut) {
    try {
        final ModuleDescriptor md = Json.decodeValue(body, ModuleDescriptor.class);
        if (!id.equals(md.getId())) {
            fut.handle(new Failure<>(USER, "Module.id=" + md.getId() + " id=" + id));
            return;
        }
        String validerr = md.validate(pc);
        if (!validerr.isEmpty()) {
            fut.handle(new Failure<>(USER, validerr));
            return;
        }
        moduleManager.update(md, res -> {
            if (res.failed()) {
                fut.handle(new Failure<>(res.getType(), res.cause()));
                return;
            }
            final String s = Json.encodePrettily(md);
            fut.handle(new Success<>(s));
        });
    } catch (DecodeException ex) {
        fut.handle(new Failure<>(USER, ex));
    }
}
Also used : ModuleDescriptor(org.folio.okapi.bean.ModuleDescriptor) TenantModuleDescriptor(org.folio.okapi.bean.TenantModuleDescriptor) DecodeException(io.vertx.core.json.DecodeException) Failure(org.folio.okapi.common.Failure)

Example 17 with Failure

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

the class PullManager method getRemoteUrl.

private void getRemoteUrl(Iterator<String> it, Handler<ExtendedAsyncResult<String>> fut) {
    if (!it.hasNext()) {
        fut.handle(new Failure<>(ErrorType.NOT_FOUND, "pull: none of remote URLs work"));
    } else {
        final String baseUrl = it.next();
        String url = baseUrl;
        if (!url.endsWith("/")) {
            url += "/";
        }
        url += "_/version";
        final Buffer body = Buffer.buffer();
        HttpClientRequest req = httpClient.getAbs(url, res -> {
            res.handler(body::appendBuffer);
            res.endHandler(x -> {
                if (res.statusCode() != 200) {
                    logger.info("pull for " + baseUrl + " failed with status " + res.statusCode());
                    fut.handle(new Failure<>(ErrorType.USER, "pull for " + baseUrl + " returned status " + res.statusCode() + "\n" + body.toString()));
                } else {
                    fut.handle(new Success<>(baseUrl));
                }
            });
            res.exceptionHandler(x -> fut.handle(new Failure<>(ErrorType.INTERNAL, x.getMessage())));
        });
        req.exceptionHandler(res -> {
            logger.info("pull for " + baseUrl + " failed with status " + res.getMessage());
            getRemoteUrl(it, fut);
        });
        req.end();
    }
}
Also used : Buffer(io.vertx.core.buffer.Buffer) HttpClientRequest(io.vertx.core.http.HttpClientRequest) Failure(org.folio.okapi.common.Failure)

Example 18 with Failure

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

the class ProxyService method proxyRequestHttpClient.

private void proxyRequestHttpClient(Iterator<ModuleInstance> it, ProxyContext pc, Buffer bcontent, ModuleInstance mi) {
    RoutingContext ctx = pc.getCtx();
    String url = makeUrl(mi, ctx);
    HttpMethod meth = ctx.request().method();
    HttpClientRequest cReq = httpClient.requestAbs(meth, url, res -> {
        if (res.statusCode() < 200 || res.statusCode() >= 300) {
            proxyResponseImmediate(pc, res, mi);
        } else if (it.hasNext()) {
            makeTraceHeader(mi, res.statusCode(), pc);
            pc.closeTimer();
            relayToRequest(res, pc, mi);
            proxyR(it, pc, null, bcontent);
        } else {
            relayToResponse(ctx.response(), res);
            makeTraceHeader(mi, res.statusCode(), pc);
            res.endHandler(x -> {
                pc.closeTimer();
                pc.trace("ProxyRequestHttpClient final response buf '" + bcontent + "'");
                ctx.response().end(bcontent);
            });
            res.exceptionHandler(e -> pc.warn("proxyRequestHttpClient: res exception (b)", e));
        }
    });
    cReq.exceptionHandler(e -> {
        pc.warn("proxyRequestHttpClient failure: " + url, e);
        pc.responseText(500, "proxyRequestHttpClient failure: " + mi.getModuleDescriptor().getId() + " " + mi.getUrl() + ": " + e + " " + e.getMessage());
    });
    cReq.headers().setAll(ctx.request().headers());
    cReq.headers().remove("Content-Length");
    pc.trace("ProxyRequestHttpClient request buf '" + bcontent + "'");
    cReq.end(bcontent);
    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) HttpMethod(io.vertx.core.http.HttpMethod)

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