siva
siva

Reputation: 1

Capturing http response as plain text while using vert.x reverse proxy

I am trying to implement a HTTP proxy using vert.x reverse proxy to capture the incoming request and outgoing response. But for the response I am not able to capture it as a plain text whereas for the request I use interceptor that intercepts the request and logs the request as plain text. How can I capture the response as a plain text.

This is my code public Future start(Vertx vertx) { HttpClient proxyClient = vertx.createHttpClient();

HttpProxy proxy = HttpProxy.reverseProxy(proxyClient);
proxy.origin(targetPort, targetHost);
proxy.addInterceptor(new ProxyInterceptor() {
  @Override
  public Future<ProxyResponse> handleProxyRequest(ProxyContext context) {
    ProxyRequest proxyRequest = context.request();

    HttpServerRequest originalRequest = proxyRequest.proxiedRequest();
    originalRequest.bodyHandler(requestBody -> {
      LOG.info("Request Body: {}", requestBody.toString("UTF-8"));
    });
    // Continue the interception chain
    return context.sendRequest();
  }
});

proxy.addInterceptor(new ProxyInterceptor() {
  @Override
  public Future<Void> handleProxyResponse(ProxyContext context) {
    ProxyResponse proxyResponse = context.response();
  // there is no proxyResponse.proxiedResponse() method and no handlers for ProxyResponse either

    return context.response().send();
  }
});

HttpServer proxyServer = vertx.createHttpServer();

proxyServer.requestHandler(proxy).listen(listeningPort);

}

Thanks

Tried to use the same logic in request receptor for response but turns out there is no proxyResponse.proxiedResponse() method and no handlers for ProxyResponse either

Upvotes: 0

Views: 213

Answers (1)

afei
afei

Reputation: 86

i should you can do it use context.response.getBody() and setBody(),getBody().stream() is a ReadStream,you can handler like readstream,then setBody to the response


i use another way do this:

 @Override
public Future<ProxyResponse> handleProxyRequest(ProxyContext context) {
    log.info("rename config {}", changeConfig);
    ProxyRequest request = context.request();
    Filter filter = new Filter(changeConfig);
    Body body = request.getBody();
    request.proxiedRequest().headers().add("transfer-encoding", "chunked");
    request.setBody(Body.body(filter.init(body.stream())));
    return context.sendRequest();
}

the filter class like this:

static class Filter implements ReadStream<Buffer> {

    public Filter(JsonArray changeConfig) {
        this.changeConfig = changeConfig;
    }

    private final AtomicBoolean paused = new AtomicBoolean();
    private ReadStream<Buffer> stream;
    private Handler<Buffer> dataHandler;
    private Handler<Throwable> exceptionHandler;
    private Handler<Void> endHandler;
    private JsonArray changeConfig;

    ReadStream<Buffer> init(ReadStream<Buffer> s) {
        stream = s;
        stream.handler(buff -> {
            JsonObject jsonObject = buff.toJsonObject();
            log.info("handler start {}", buff.toJsonObject());
            if (dataHandler != null) {
                for (Object c : changeConfig.getList()) {
                    Map<String, String> config = (Map<String, String>) c;
                    RequestChangeType type = RequestChangeType.valueOf(config.get(CHANGE_TYPE));
                    switch (type) {
                        case BodyNameChange:
                            String originName = config.get(ORIGIN_NAME);
                            String targetName = config.get(TARGET_NAME);
                            if (jsonObject.containsKey(originName)) {
                                jsonObject.put(targetName, jsonObject.getValue(originName));
                                jsonObject.remove(originName);
                            }
                            break;
                        case BodyValueFix:
                            String paramName = config.get(PARAM_NAME);
                            String fixValue = config.get(FIX_VALUE);
                            jsonObject.put(paramName, fixValue);
                    }
                }
                dataHandler.handle(jsonObject.toBuffer());
            }
        });
        stream.exceptionHandler(err -> {
            if (exceptionHandler != null) {
                exceptionHandler.handle(err);
            }
        });
        stream.endHandler(v -> {
            log.info("handler end");
            if (endHandler != null) {
                endHandler.handle(v);
            }
        });
        return this;
    }

    @Override
    public ReadStream<Buffer> pause() {
        paused.set(true);
        stream.pause();
        return this;
    }

    @Override
    public ReadStream<Buffer> resume() {
        stream.resume();
        return this;
    }

    @Override
    public ReadStream<Buffer> fetch(long amount) {
        stream.fetch(amount);
        return this;
    }

    @Override
    public ReadStream<Buffer> exceptionHandler(Handler<Throwable> handler) {
        exceptionHandler = handler;
        return this;
    }

    @Override
    public ReadStream<Buffer> handler(Handler<Buffer> handler) {
        dataHandler = handler;
        return this;
    }

    @Override
    public ReadStream<Buffer> endHandler(Handler<Void> handler) {
        endHandler = handler;
        return this;
    }
}

Upvotes: 0

Related Questions