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

co.easimart.vertx.http.JsonBodyHandler Maven / Gradle / Ivy

The newest version!
package co.easimart.vertx.http;

import java.time.Instant;
import java.util.Map;
import java.util.function.Supplier;

import co.easimart.vertx.util.ErrorCodeException;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.vertx.core.Handler;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.HttpServerRequest;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.RoutingContext;

/**
 * Routing context handler to handle http request with body in json format
 */
public class JsonBodyHandler implements Handler {

    @FunctionalInterface
    public interface Handler {
        void handle(RoutingContext context, HttpServerRequest req, JsonObject jsonBody);
    }

    private final Handler handler;
    private final long bodySizeLimit;

    public JsonBodyHandler(Handler handler) {
        this(handler, 2 * 1024 * 1024L /* 2MB */);
    }

    public JsonBodyHandler(Handler handler, long bodySizeLimit) {
        this.handler = handler;
        this.bodySizeLimit = bodySizeLimit;
    }

    public static JsonBodyHandler wrap(Handler handler) {
        return new JsonBodyHandler(handler);
    }

    public static JsonBodyHandler wrap(Handler handler, long bodySizeLimit) {
        return new JsonBodyHandler(handler, bodySizeLimit);
    }

    @Override
    public void handle(RoutingContext routingContext) {
        HttpServerRequest req = routingContext.request();

        Buffer body = Buffer.buffer();
        req.handler(buffer -> {
            if (body.length() + buffer.length() > this.bodySizeLimit) {
                routingContext.fail(
                        new ErrorCodeException(
                                ErrorCodeException.CommonCode.TOO_LARGE.code(),
                                "Request body is too large."
                        )
                );
                return;
            }
            body.appendBuffer(buffer);
        });
        req.endHandler(r -> {
            try {
                JsonObject jsonBody = new JsonObjectWithTypeHandling(body.toString());
                if (handler != null) {
                    handler.handle(routingContext, req, jsonBody);
                }
            } catch (Throwable t) {
                routingContext.fail(t);
            }
        });
    }


    public static class JsonObjectWithTypeHandling extends JsonObject {
        public JsonObjectWithTypeHandling(String json) {
            super(json);
        }

        public JsonObjectWithTypeHandling() {
            super();
        }

        public JsonObjectWithTypeHandling(Map map) {
            super(map);
        }

        private  R handleClassCast(Supplier func, String key, String type) {
            try {
                return func.get();
            } catch (ClassCastException ex) {
                throw new HttpErrorCodeException(HttpResponseStatus.BAD_REQUEST, -1, key + " in json is not " + type);
            }
        }

        @Override
        public String getString(String key) {
            return handleClassCast(() -> super.getString(key), key, "string");

        }

        @Override
        public Integer getInteger(String key) {
            return handleClassCast(() -> super.getInteger(key), key, "integer");
        }

        @Override
        public Long getLong(String key) {
            return handleClassCast(() -> super.getLong(key), key, "long");
        }

        @Override
        public Double getDouble(String key) {
            return handleClassCast(() -> super.getDouble(key), key, "double");
        }

        @Override
        public Float getFloat(String key) {
            return handleClassCast(() -> super.getFloat(key), key, "float");
        }

        @Override
        public Boolean getBoolean(String key) {
            return handleClassCast(() -> super.getBoolean(key), key, "boolean");
        }

        @Override
        public JsonObject getJsonObject(String key) {
            return handleClassCast(() -> super.getJsonObject(key), key, "json object");
        }

        @Override
        public JsonArray getJsonArray(String key) {
            return handleClassCast(() -> super.getJsonArray(key), key, "json array");
        }

        @Override
        public byte[] getBinary(String key) {
            return handleClassCast(() -> super.getBinary(key), key, "base64 string");
        }

        @Override
        public Instant getInstant(String key) {
            return handleClassCast(() -> super.getInstant(key), key, "datetime");
        }

        @Override
        public String getString(String key, String def) {
            return handleClassCast(() -> super.getString(key, def), key, "string");
        }

        @Override
        public Integer getInteger(String key, Integer def) {
            return handleClassCast(() -> super.getInteger(key, def), key, "integer");
        }

        @Override
        public Long getLong(String key, Long def) {
            return handleClassCast(() -> super.getLong(key, def), key, "long");
        }

        @Override
        public Double getDouble(String key, Double def) {
            return handleClassCast(() -> super.getDouble(key, def), key, "double");
        }

        @Override
        public Float getFloat(String key, Float def) {
            return handleClassCast(() -> super.getFloat(key, def), key, "float");
        }

        @Override
        public Boolean getBoolean(String key, Boolean def) {
            return handleClassCast(() -> super.getBoolean(key, def), key, "boolean");
        }

        @Override
        public JsonObject getJsonObject(String key, JsonObject def) {
            return handleClassCast(() -> super.getJsonObject(key, def), key, "json object");
        }

        @Override
        public JsonArray getJsonArray(String key, JsonArray def) {
            return handleClassCast(() -> super.getJsonArray(key, def), key, "json array");
        }

        @Override
        public byte[] getBinary(String key, byte[] def) {
            return handleClassCast(() -> super.getBinary(key, def), key, "base64 string");
        }

        @Override
        public Instant getInstant(String key, Instant def) {
            return handleClassCast(() -> super.getInstant(key, def), key, "datetime");
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy