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

org.restheart.handlers.PipelinedWrappingHandler Maven / Gradle / Ivy

There is a newer version: 8.1.7
Show newest version
/*-
 * ========================LICENSE_START=================================
 * restheart-commons
 * %%
 * Copyright (C) 2019 - 2024 SoftInstigate
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * =========================LICENSE_END==================================
 */
package org.restheart.handlers;

import org.restheart.exchange.ServiceRequest;
import org.restheart.exchange.ServiceResponse;
import org.restheart.plugins.Service;

import io.undertow.server.HttpHandler;
import io.undertow.server.HttpServerExchange;

/**
 * wraps a HttpHandler into a PipelinedHttpHandler
 *
 * @author Andrea Di Cesare {@literal }
 */
public class PipelinedWrappingHandler extends PipelinedHandler {

    private final HttpHandler wrapped;

    /**
     * Creates a new instance of PipedWrappingHandler
     *
     * @param next
     * @param handler
     */
    private PipelinedWrappingHandler(PipelinedHandler next, HttpHandler handler) {
        super(next);
        wrapped = handler;
    }

    /**
     * Creates a new instance of PipedWrappingHandler
     *
     * @param next
     * @param service
     */
    private , S extends ServiceResponse> PipelinedWrappingHandler(PipelinedHandler next, Service service) {
        super(next);
        wrapped = new ServiceWrapper<>(service);
    }

    /**
     * Creates a new instance of PipedWrappingHandler
     *
     * @param handler
     */
    private PipelinedWrappingHandler(HttpHandler handler) {
        super(null);
        wrapped = handler;
    }

    /**
     *
     * @param handler
     * @return the wrapping handler
     */
    public static PipelinedWrappingHandler wrap(HttpHandler handler) {
        return wrap(null, handler);
    }

    /**
     *
     * @param  Request
     * @param  Response
     * @param service
     * @return the wrapping handler
     */
    public static , S extends ServiceResponse> PipelinedWrappingHandler wrap(Service service) {
        return wrap(null, service);
    }

    /**
     *
     * @param next
     * @param handler
     * @return the wrapping handler
     */
    public static PipelinedWrappingHandler wrap(PipelinedHandler next, HttpHandler handler) {
        return new PipelinedWrappingHandler(next, handler);
    }

    /**
     *
     * @param  Request
     * @param  Response
     * @param next
     * @param service
     * @return the wrapping handler
     */
    public static , S extends ServiceResponse> PipelinedWrappingHandler wrap(PipelinedHandler next, Service service) {
        return new PipelinedWrappingHandler(next, service);
    }

    /**
     *
     * @param exchange
     * @throws Exception
     */
    @Override
    public void handleRequest(HttpServerExchange exchange) throws Exception {
        if (wrapped == null) {
            next(exchange);
        } else {
            wrapped.handleRequest(exchange);

            if (!exchange.isResponseComplete()) {
                next(exchange);
            }
        }
    }
}

class ServiceWrapper, S extends ServiceResponse> extends PipelinedHandler {
    final Service service;

    ServiceWrapper(Service service) {
        this.service = service;
    }

    @Override
    public void handleRequest(HttpServerExchange exchange) throws Exception {
        service.handle(service.request().apply(exchange),
                service.response().apply(exchange));
    }
}