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

java-helidon.server.libraries.se.handlerUtils.mustache.save Maven / Gradle / Ivy

There is a newer version: 7.9.0
Show newest version
package {{apiPackage}};

import java.util.logging.Logger;

import io.helidon.webserver.http.ServerRequest;
import io.helidon.webserver.http.ServerResponse;

/**
 * Utility methods related to handlers for Helidon 4+ support.
 */
class HandlerUtils {

    private static final Logger LOGGER = Logger.getLogger(HandlerUtils.class.getName());

    private HandlerUtils() {
    }

    /**
     * Handler which accepts a request, response, and a body parameter type and, if the body part is declared as required,
     * enforces that.
     *
     * @param  type of the body parameter
     */
    static class Handler implements io.helidon.webserver.http.Handler {

        private final Class type;
        private final boolean isRequired;
        private final Op op;

        Handler(Class type, boolean isRequired, Op op) {
            this.type = type;
            this.isRequired = isRequired;
            this.op = op;
        }

        /**
         * Returns a handler which extracts a body parameter from the content and, if the parameter is required, enforces that.
         *
         * @param type       type of the body parameter
         * @param isRequired whether the body parameter is declared as required
         * @param op         operation the handler is to invoke with the request, response, and body parameter
         * @param         type of the body parameter
         * @return the new handler
         */
        static  Handler create(Class type, boolean isRequired, Handler.Op op) {
            return new Handler<>(type, isRequired, op);
        }

        @Override
        public void handle(ServerRequest serverRequest, ServerResponse serverResponse)
                throws Exception {
            ValidatorUtils.Validator validator = isRequired ? ValidatorUtils.validator() : null;
            T body = serverRequest.content().as(type);
            if (isRequired) {
                validator.require("body", body);
                validator.execute();
            }
            op.apply(serverRequest, serverResponse, body);
        }

        /**
         * An operation to which the handler delegates upon receipt of a request and extraction of the body parameter.
         *
         * @param  type of the body parameter
         */
        interface Op {
            void apply(ServerRequest request, ServerResponse response, T body);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy