tsegismont / http-proxy-playground

Apache License 2.0
4 stars 2 forks source link

Interceptor shortcuts can not be used for if statement #3

Open wzy1935 opened 1 month ago

wzy1935 commented 1 month ago

Some interceptors can not be represented with shortcuts if they contain "if" statements, e.g. ProductImageFieldInterceptor:


  @Override
  public Future<Void> handleProxyResponse(ProxyContext context) {
    String uri = context.request().getURI();
    if (!uri.endsWith("/products")) {  //  <------------------- HERE
      return context.sendResponse();
    }
    ProxyResponse proxyResponse = context.response();
    Body body = proxyResponse.getBody();

    return vertx.fileSystem().createTempFile("foo", "bar")
      .compose(tempFileName -> {
        return vertx.fileSystem().open(tempFileName, new OpenOptions())
          .compose(asyncFile -> {
            return body.stream().pipeTo(asyncFile).compose(v -> vertx.fileSystem().readFile(tempFileName).map(Buffer::toJsonArray));
          }).onComplete(v -> vertx.fileSystem().delete(tempFileName));
      })
      .map(jsonArray -> {
        for (int i = 0; i < jsonArray.size(); i++) {
          JsonObject jsonObject = jsonArray.getJsonObject(i);
          jsonObject.remove("image");
        }
        return jsonArray;
      })
      .compose(jsonArray -> {
        proxyResponse.setBody(Body.body(jsonArray.toBuffer()));
        return context.sendResponse();
      });
  }
tsegismont commented 1 month ago

Perhaps could we wrap an interceptor and evaluate a condition on ProxyRequest. Something like:

ConditionalTransformer.applyIf(predicate,BodyInterceptor.modifyRequestBody(myTransformer));

With predicate evaluating the ProxyContext and returning a Boolean