/*
* Copyright (C) 2014 Stefano Fornari.
* All Rights Reserved. No use, copying or distribution of this
* work may be made except in accordance with a valid license
* agreement from Stefano Fornari. This notice must be
* included on all copies, modifications and derivatives of this
* work.
*
* STEFANO FORNARI MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY
* OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
* THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
* PURPOSE, OR NON-INFRINGEMENT. STEFANO FORNARI SHALL NOT BE LIABLE FOR ANY
* DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
* THIS SOFTWARE OR ITS DERIVATIVES.
*/
package ste.web.http;
import java.io.IOException;
import java.net.Socket;
import org.apache.http.HttpConnectionFactory;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.annotation.Immutable;
import org.apache.http.config.ConnectionConfig;
import org.apache.http.entity.ContentLengthStrategy;
import org.apache.http.impl.ConnSupport;
import org.apache.http.io.HttpMessageParserFactory;
import org.apache.http.io.HttpMessageWriterFactory;
/**
*
* @author ste
*/
@Immutable
public class BasicHttpConnectionFactory
implements HttpConnectionFactory<BasicHttpConnection> {
public static final BasicHttpConnectionFactory INSTANCE = new BasicHttpConnectionFactory();
private final ConnectionConfig config;
private final ContentLengthStrategy incomingContentStrategy;
private final ContentLengthStrategy outgoingContentStrategy;
private final HttpMessageParserFactory<HttpRequest> requestParserFactory;
private final HttpMessageWriterFactory<HttpResponse> responseWriterFactory;
public BasicHttpConnectionFactory(
final ConnectionConfig cconfig,
final ContentLengthStrategy incomingContentStrategy,
final ContentLengthStrategy outgoingContentStrategy,
final HttpMessageParserFactory<HttpRequest> requestParserFactory,
final HttpMessageWriterFactory<HttpResponse> responseWriterFactory) {
super();
this.config = cconfig != null ? cconfig : ConnectionConfig.DEFAULT;
this.incomingContentStrategy = incomingContentStrategy;
this.outgoingContentStrategy = outgoingContentStrategy;
this.requestParserFactory = requestParserFactory;
this.responseWriterFactory = responseWriterFactory;
}
public BasicHttpConnectionFactory(
final ConnectionConfig cconfig,
final HttpMessageParserFactory<HttpRequest> requestParserFactory,
final HttpMessageWriterFactory<HttpResponse> responseWriterFactory) {
this(cconfig, null, null, requestParserFactory, responseWriterFactory);
}
public BasicHttpConnectionFactory(final ConnectionConfig cconfig) {
this(cconfig, null, null, null, null);
}
public BasicHttpConnectionFactory() {
this(null, null, null, null, null);
}
public BasicHttpConnection createConnection(final Socket socket) throws IOException {
final BasicHttpConnection conn = new BasicHttpConnection(
this.config.getBufferSize(),
this.config.getFragmentSizeHint(),
ConnSupport.createDecoder(this.config),
ConnSupport.createEncoder(this.config),
this.config.getMessageConstraints(),
this.incomingContentStrategy,
this.outgoingContentStrategy,
this.requestParserFactory,
this.responseWriterFactory);
conn.bind(socket);
return conn;
}
}