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

io.reactivex.netty.protocol.http.server.HttpServer Maven / Gradle / Ivy

There is a newer version: 0.3.18
Show newest version
/*
 * Copyright 2014 Netflix, Inc.
 *
 * 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.
 */

package io.reactivex.netty.protocol.http.server;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.util.concurrent.EventExecutorGroup;
import io.reactivex.netty.pipeline.PipelineConfigurator;
import io.reactivex.netty.pipeline.PipelineConfiguratorComposite;
import io.reactivex.netty.server.ErrorHandler;
import io.reactivex.netty.server.RxServer;

import java.util.List;

import static io.reactivex.netty.protocol.http.server.HttpServerRequest.DEFAULT_CONTENT_SUBSCRIPTION_TIMEOUT_MS;

/**
 * @author Nitesh Kant
 */
public class HttpServer extends RxServer, HttpServerResponse> {

    private final HttpConnectionHandler connectionHandler;

    public HttpServer(ServerBootstrap bootstrap, int port,
                      PipelineConfigurator, HttpServerResponse> pipelineConfigurator,
                      RequestHandler requestHandler) {
        this(bootstrap, port, pipelineConfigurator, requestHandler, null);
    }

    public HttpServer(ServerBootstrap bootstrap, int port,
                      PipelineConfigurator, HttpServerResponse> pipelineConfigurator,
                      RequestHandler requestHandler, EventExecutorGroup requestProcessingExecutor) {
        this(bootstrap, port, pipelineConfigurator, new HttpConnectionHandler(requestHandler),
             requestProcessingExecutor, DEFAULT_CONTENT_SUBSCRIPTION_TIMEOUT_MS);
    }

    protected HttpServer(ServerBootstrap bootstrap, int port,
               PipelineConfigurator, HttpServerResponse> pipelineConfigurator,
               HttpConnectionHandler connectionHandler) {
        this(bootstrap, port, pipelineConfigurator, connectionHandler, null, DEFAULT_CONTENT_SUBSCRIPTION_TIMEOUT_MS);
    }

    protected HttpServer(ServerBootstrap bootstrap, int port,
               PipelineConfigurator, HttpServerResponse> pipelineConfigurator,
               HttpConnectionHandler connectionHandler, EventExecutorGroup requestProcessingExecutor,
               long requestContentSubscriptionTimeoutMs) {
        super(bootstrap, port, addRequiredConfigurator(pipelineConfigurator, requestProcessingExecutor,
                                                       requestContentSubscriptionTimeoutMs),
              connectionHandler, requestProcessingExecutor);
        @SuppressWarnings({"unchecked", "rawtypes"})
        List constituentConfigurators =
                ((PipelineConfiguratorComposite) this.pipelineConfigurator).getConstituentConfigurators();
        boolean updatedSubject = false;
        for (@SuppressWarnings("rawtypes") PipelineConfigurator configurator : constituentConfigurators) {
            if (configurator instanceof ServerRequiredConfigurator) {
                updatedSubject = true;
                @SuppressWarnings("unchecked")
                ServerRequiredConfigurator requiredConfigurator = (ServerRequiredConfigurator) configurator;
                requiredConfigurator.useMetricEventsSubject(eventsSubject);
            }
        }
        if (!updatedSubject) {
            throw new IllegalStateException("No server required configurator added.");
        }
        connectionHandler.useMetricEventsSubject(eventsSubject);
        this.connectionHandler = connectionHandler;
    }

    public HttpServer withErrorResponseGenerator(ErrorResponseGenerator responseGenerator) {
        if (null == responseGenerator) {
            throw new IllegalArgumentException("Response generator can not be null.");
        }
        connectionHandler.setResponseGenerator(responseGenerator);
        return this;
    }

    @Override
    public HttpServer start() {
        super.start();
        return this;
    }

    @Override
    public HttpServer withErrorHandler(ErrorHandler errorHandler) {
        super.withErrorHandler(errorHandler);
        return this;
    }

    private static  PipelineConfigurator, HttpServerResponse> addRequiredConfigurator(
            PipelineConfigurator, HttpServerResponse> pipelineConfigurator,
            EventExecutorGroup requestProcessingExecutor, long requestContentSubscriptionTimeoutMs) {
        return new PipelineConfiguratorComposite, HttpServerResponse>(pipelineConfigurator,
                                                                                  new ServerRequiredConfigurator(requestProcessingExecutor,
                                                                                                                       requestContentSubscriptionTimeoutMs));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy