All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.vertx.httpproxy.interceptors.impl.BodyTransformerImpl Maven / Gradle / Ivy

There is a newer version: 5.0.0.CR3
Show newest version
package io.vertx.httpproxy.interceptors.impl;

import io.vertx.core.buffer.Buffer;
import io.vertx.core.json.Json;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.vertx.httpproxy.interceptors.BodyTransformer;

import java.util.Objects;
import java.util.function.Function;

public class BodyTransformerImpl implements BodyTransformer {
  private final Function transformer;

  @Override
  public Buffer apply(Buffer buffer) {
    return transformer.apply(buffer);
  }

  private BodyTransformerImpl(Function transformer) {
    this.transformer = Objects.requireNonNull(transformer);
  }

  public static BodyTransformerImpl transformJsonObject(Function transformer) {
    return new BodyTransformerImpl(buffer -> transformer.apply(buffer.toJsonObject()).toBuffer());
  }

  public static BodyTransformerImpl transformJsonArray(Function transformer) {
    return new BodyTransformerImpl(buffer -> transformer.apply(buffer.toJsonArray()).toBuffer());
  }

  public static BodyTransformerImpl transformJson(Function transformer) {
    return new BodyTransformerImpl(buffer -> Json.encodeToBuffer(transformer.apply(Json.decodeValue(buffer))));
  }

  public static BodyTransformerImpl transformText(Function transformer, String encoding) {
    return new BodyTransformerImpl(buffer -> Buffer.buffer(transformer.apply(buffer.toString(encoding))));
  }

  public static BodyTransformerImpl discard() {
    return new BodyTransformerImpl(buffer -> Buffer.buffer());
  }


}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy