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

cz.msebera.android.httpclient.impl.bootstrap.ServerBootstrap Maven / Gradle / Ivy

There is a newer version: 4.5.8
Show newest version
/*
 * ====================================================================
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * .
 *
 */
package cz.msebera.android.httpclient.impl.bootstrap;

import java.net.InetAddress;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

import javax.net.ServerSocketFactory;
import javax.net.ssl.SSLContext;

import cz.msebera.android.httpclient.ConnectionReuseStrategy;
import cz.msebera.android.httpclient.ExceptionLogger;
import cz.msebera.android.httpclient.HttpConnectionFactory;
import cz.msebera.android.httpclient.HttpRequestInterceptor;
import cz.msebera.android.httpclient.HttpResponseFactory;
import cz.msebera.android.httpclient.HttpResponseInterceptor;
import cz.msebera.android.httpclient.config.ConnectionConfig;
import cz.msebera.android.httpclient.config.SocketConfig;
import cz.msebera.android.httpclient.impl.DefaultBHttpServerConnection;
import cz.msebera.android.httpclient.impl.DefaultBHttpServerConnectionFactory;
import cz.msebera.android.httpclient.impl.DefaultConnectionReuseStrategy;
import cz.msebera.android.httpclient.impl.DefaultHttpResponseFactory;
import cz.msebera.android.httpclient.protocol.HttpExpectationVerifier;
import cz.msebera.android.httpclient.protocol.HttpProcessor;
import cz.msebera.android.httpclient.protocol.HttpProcessorBuilder;
import cz.msebera.android.httpclient.protocol.HttpRequestHandler;
import cz.msebera.android.httpclient.protocol.HttpRequestHandlerMapper;
import cz.msebera.android.httpclient.protocol.HttpService;
import cz.msebera.android.httpclient.protocol.ResponseConnControl;
import cz.msebera.android.httpclient.protocol.ResponseContent;
import cz.msebera.android.httpclient.protocol.ResponseDate;
import cz.msebera.android.httpclient.protocol.ResponseServer;
import cz.msebera.android.httpclient.protocol.UriHttpRequestHandlerMapper;

/**
 * @since 4.4
 */
public class ServerBootstrap {

    private int listenerPort;
    private InetAddress localAddress;
    private SocketConfig socketConfig;
    private ConnectionConfig connectionConfig;
    private LinkedList requestFirst;
    private LinkedList requestLast;
    private LinkedList responseFirst;
    private LinkedList responseLast;
    private String serverInfo;
    private HttpProcessor httpProcessor;
    private ConnectionReuseStrategy connStrategy;
    private HttpResponseFactory responseFactory;
    private HttpRequestHandlerMapper handlerMapper;
    private Map handlerMap;
    private HttpExpectationVerifier expectationVerifier;
    private ServerSocketFactory serverSocketFactory;
    private SSLContext sslContext;
    private SSLServerSetupHandler sslSetupHandler;
    private HttpConnectionFactory connectionFactory;
    private ExceptionLogger exceptionLogger;

    private ServerBootstrap() {
    }

    public static ServerBootstrap bootstrap() {
        return new ServerBootstrap();
    }

    /**
     * Sets listener port number.
     */
    public final ServerBootstrap setListenerPort(final int listenerPort) {
        this.listenerPort = listenerPort;
        return this;
    }

    /**
     * Assigns local interface for the listener.
     */
    public final ServerBootstrap setLocalAddress(final InetAddress localAddress) {
        this.localAddress = localAddress;
        return this;
    }

    /**
     * Sets socket configuration.
     */
    public final ServerBootstrap setSocketConfig(final SocketConfig socketConfig) {
        this.socketConfig = socketConfig;
        return this;
    }

    /**
     * Sets connection configuration.
     * 

* Please note this value can be overridden by the {@link #setConnectionFactory( * cz.msebera.android.httpclient.HttpConnectionFactory)} method. */ public final ServerBootstrap setConnectionConfig(final ConnectionConfig connectionConfig) { this.connectionConfig = connectionConfig; return this; } /** * Assigns {@link HttpProcessor} instance. */ public final ServerBootstrap setHttpProcessor(final HttpProcessor httpProcessor) { this.httpProcessor = httpProcessor; return this; } /** * Adds this protocol interceptor to the head of the protocol processing list. *

* Please note this value can be overridden by the {@link #setHttpProcessor( * cz.msebera.android.httpclient.protocol.HttpProcessor)} method. */ public final ServerBootstrap addInterceptorFirst(final HttpResponseInterceptor itcp) { if (itcp == null) { return this; } if (responseFirst == null) { responseFirst = new LinkedList(); } responseFirst.addFirst(itcp); return this; } /** * Adds this protocol interceptor to the tail of the protocol processing list. *

* Please note this value can be overridden by the {@link #setHttpProcessor( * cz.msebera.android.httpclient.protocol.HttpProcessor)} method. */ public final ServerBootstrap addInterceptorLast(final HttpResponseInterceptor itcp) { if (itcp == null) { return this; } if (responseLast == null) { responseLast = new LinkedList(); } responseLast.addLast(itcp); return this; } /** * Adds this protocol interceptor to the head of the protocol processing list. *

* Please note this value can be overridden by the {@link #setHttpProcessor( * cz.msebera.android.httpclient.protocol.HttpProcessor)} method. */ public final ServerBootstrap addInterceptorFirst(final HttpRequestInterceptor itcp) { if (itcp == null) { return this; } if (requestFirst == null) { requestFirst = new LinkedList(); } requestFirst.addFirst(itcp); return this; } /** * Adds this protocol interceptor to the tail of the protocol processing list. *

* Please note this value can be overridden by the {@link #setHttpProcessor( * cz.msebera.android.httpclient.protocol.HttpProcessor)} method. */ public final ServerBootstrap addInterceptorLast(final HttpRequestInterceptor itcp) { if (itcp == null) { return this; } if (requestLast == null) { requestLast = new LinkedList(); } requestLast.addLast(itcp); return this; } /** * Assigns {@code Server} response header value. *

* Please note this value can be overridden by the {@link #setHttpProcessor( * cz.msebera.android.httpclient.protocol.HttpProcessor)} method. */ public final ServerBootstrap setServerInfo(final String serverInfo) { this.serverInfo = serverInfo; return this; } /** * Assigns {@link ConnectionReuseStrategy} instance. */ public final ServerBootstrap setConnectionReuseStrategy(final ConnectionReuseStrategy connStrategy) { this.connStrategy = connStrategy; return this; } /** * Assigns {@link HttpResponseFactory} instance. */ public final ServerBootstrap setResponseFactory(final HttpResponseFactory responseFactory) { this.responseFactory = responseFactory; return this; } /** * Assigns {@link HttpRequestHandlerMapper} instance. */ public final ServerBootstrap setHandlerMapper(final HttpRequestHandlerMapper handlerMapper) { this.handlerMapper = handlerMapper; return this; } /** * Registers the given {@link HttpRequestHandler} as a handler for URIs * matching the given pattern. *

* Please note this value can be overridden by the {@link #setHandlerMapper( * cz.msebera.android.httpclient.protocol.HttpRequestHandlerMapper)} method. * * @param pattern the pattern to register the handler for. * @param handler the handler. */ public final ServerBootstrap registerHandler(final String pattern, final HttpRequestHandler handler) { if (pattern == null || handler == null) { return this; } if (handlerMap == null) { handlerMap = new HashMap(); } handlerMap.put(pattern, handler); return this; } /** * Assigns {@link HttpExpectationVerifier} instance. */ public final ServerBootstrap setExpectationVerifier(final HttpExpectationVerifier expectationVerifier) { this.expectationVerifier = expectationVerifier; return this; } /** * Assigns {@link HttpConnectionFactory} instance. */ public final ServerBootstrap setConnectionFactory( final HttpConnectionFactory connectionFactory) { this.connectionFactory = connectionFactory; return this; } /** * Assigns {@link cz.msebera.android.httpclient.impl.bootstrap.SSLServerSetupHandler} instance. */ public final ServerBootstrap setSslSetupHandler(final SSLServerSetupHandler sslSetupHandler) { this.sslSetupHandler = sslSetupHandler; return this; } /** * Assigns {@link javax.net.ServerSocketFactory} instance. */ public final ServerBootstrap setServerSocketFactory(final ServerSocketFactory serverSocketFactory) { this.serverSocketFactory = serverSocketFactory; return this; } /** * Assigns {@link javax.net.ssl.SSLContext} instance. *

* Please note this value can be overridden by the {@link #setServerSocketFactory( * javax.net.ServerSocketFactory)} method. */ public final ServerBootstrap setSslContext(final SSLContext sslContext) { this.sslContext = sslContext; return this; } /** * Assigns {@link cz.msebera.android.httpclient.ExceptionLogger} instance. */ public final ServerBootstrap setExceptionLogger(final ExceptionLogger exceptionLogger) { this.exceptionLogger = exceptionLogger; return this; } public HttpServer create() { HttpProcessor httpProcessorCopy = this.httpProcessor; if (httpProcessorCopy == null) { final HttpProcessorBuilder b = HttpProcessorBuilder.create(); if (requestFirst != null) { for (final HttpRequestInterceptor i: requestFirst) { b.addFirst(i); } } if (responseFirst != null) { for (final HttpResponseInterceptor i: responseFirst) { b.addFirst(i); } } String serverInfoCopy = this.serverInfo; if (serverInfoCopy == null) { serverInfoCopy = "Apache-HttpCore/1.1"; } b.addAll( new ResponseDate(), new ResponseServer(serverInfoCopy), new ResponseContent(), new ResponseConnControl()); if (requestLast != null) { for (final HttpRequestInterceptor i: requestLast) { b.addLast(i); } } if (responseLast != null) { for (final HttpResponseInterceptor i: responseLast) { b.addLast(i); } } httpProcessorCopy = b.build(); } HttpRequestHandlerMapper handlerMapperCopy = this.handlerMapper; if (handlerMapperCopy == null) { final UriHttpRequestHandlerMapper reqistry = new UriHttpRequestHandlerMapper(); if (handlerMap != null) { for (Map.Entry entry: handlerMap.entrySet()) { reqistry.register(entry.getKey(), entry.getValue()); } } handlerMapperCopy = reqistry; } ConnectionReuseStrategy connStrategyCopy = this.connStrategy; if (connStrategyCopy == null) { connStrategyCopy = DefaultConnectionReuseStrategy.INSTANCE; } HttpResponseFactory responseFactoryCopy = this.responseFactory; if (responseFactoryCopy == null) { responseFactoryCopy = DefaultHttpResponseFactory.INSTANCE; } final HttpService httpService = new HttpService( httpProcessorCopy, connStrategyCopy, responseFactoryCopy, handlerMapperCopy, this.expectationVerifier); ServerSocketFactory serverSocketFactoryCopy = this.serverSocketFactory; if (serverSocketFactoryCopy == null) { if (this.sslContext != null) { serverSocketFactoryCopy = this.sslContext.getServerSocketFactory(); } else { serverSocketFactoryCopy = ServerSocketFactory.getDefault(); } } HttpConnectionFactory connectionFactoryCopy = this.connectionFactory; if (connectionFactoryCopy == null) { if (this.connectionConfig != null) { connectionFactoryCopy = new DefaultBHttpServerConnectionFactory(this.connectionConfig); } else { connectionFactoryCopy = DefaultBHttpServerConnectionFactory.INSTANCE; } } ExceptionLogger exceptionLoggerCopy = this.exceptionLogger; if (exceptionLoggerCopy == null) { exceptionLoggerCopy = ExceptionLogger.NO_OP; } return new HttpServer( this.listenerPort > 0 ? this.listenerPort : 0, this.localAddress, this.socketConfig != null ? this.socketConfig : SocketConfig.DEFAULT, serverSocketFactoryCopy, httpService, connectionFactoryCopy, this.sslSetupHandler, exceptionLoggerCopy); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy