Search in sources :

Example 1 with ProxyType

use of org.folio.okapi.bean.RoutingEntry.ProxyType in project okapi by folio-org.

the class ProxyService method proxyR.

private void proxyR(Iterator<ModuleInstance> it, ProxyContext pc, ReadStream<Buffer> stream, Buffer bcontent) {
    RoutingContext ctx = pc.getCtx();
    if (!it.hasNext()) {
        stream.resume();
        pc.debug("proxyR: Not found");
        // Should have been caught earlier
        pc.responseText(404, "");
    } else {
        ModuleInstance mi = it.next();
        String tenantId = ctx.request().getHeader(XOkapiHeaders.TENANT);
        if (tenantId == null || tenantId.isEmpty()) {
            // Should not happen, we have validated earlier
            tenantId = "???";
        }
        String metricKey = "proxy." + tenantId + ".module." + mi.getModuleDescriptor().getId();
        pc.startTimer(metricKey);
        // Pass the right token
        ctx.request().headers().remove(XOkapiHeaders.TOKEN);
        String token = mi.getAuthToken();
        if (token != null && !token.isEmpty()) {
            ctx.request().headers().add(XOkapiHeaders.TOKEN, token);
        }
        // Pass the X-Okapi-Filter header for filters (only)
        // And all kind of things for the auth filter
        ctx.request().headers().remove(XOkapiHeaders.FILTER);
        if (mi.getRoutingEntry().getPhase() != null) {
            String pth = mi.getRoutingEntry().getPathPattern();
            if (pth == null) {
                pth = mi.getRoutingEntry().getPath();
            }
            String filt = mi.getRoutingEntry().getPhase() + " " + pth;
            pc.debug("Adding " + XOkapiHeaders.FILTER + ": " + filt);
            ctx.request().headers().add(XOkapiHeaders.FILTER, filt);
            // The auth filter needs all kinds of special headers
            if ("auth".equals(mi.getRoutingEntry().getPhase())) {
                authHeaders(pc.getModList(), ctx.request().headers(), pc);
            }
        }
        ProxyType pType = mi.getRoutingEntry().getProxyType();
        if (pType != ProxyType.REDIRECT) {
            pc.debug("Invoking module " + mi.getModuleDescriptor().getId() + " type " + pType + " level " + mi.getRoutingEntry().getPhaseLevel() + " path " + mi.getPath() + " url " + mi.getUrl());
        }
        switch(pType) {
            case REQUEST_ONLY:
                proxyRequestOnly(it, pc, stream, bcontent, mi);
                break;
            case REQUEST_RESPONSE:
                proxyRequestResponse(it, pc, stream, bcontent, mi);
                break;
            case HEADERS:
                proxyHeaders(it, pc, stream, bcontent, mi);
                break;
            case REDIRECT:
                proxyRedirect(it, pc, stream, bcontent, mi);
                break;
            case INTERNAL:
                proxyInternal(it, pc, stream, bcontent, mi);
                break;
            case REQUEST_RESPONSE_1_0:
                proxyRequestResponse10(it, pc, stream, bcontent, mi);
                break;
            default:
                // Should not happen
                pc.responseText(500, "Bad proxy type '" + pType + "' in module " + mi.getModuleDescriptor().getId());
                break;
        }
    }
}
Also used : RoutingContext(io.vertx.ext.web.RoutingContext) ProxyType(org.folio.okapi.bean.RoutingEntry.ProxyType) ModuleInstance(org.folio.okapi.bean.ModuleInstance)

Aggregations

RoutingContext (io.vertx.ext.web.RoutingContext)1 ModuleInstance (org.folio.okapi.bean.ModuleInstance)1 ProxyType (org.folio.okapi.bean.RoutingEntry.ProxyType)1