001/* 002 * ==================================================================== 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, 014 * software distributed under the License is distributed on an 015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 016 * KIND, either express or implied. See the License for the 017 * specific language governing permissions and limitations 018 * under the License. 019 * ==================================================================== 020 * 021 * This software consists of voluntary contributions made by many 022 * individuals on behalf of the Apache Software Foundation. For more 023 * information on the Apache Software Foundation, please see 024 * <http://www.apache.org/>. 025 * 026 */ 027package org.apache.http.impl.nio.pool; 028 029import java.io.IOException; 030 031import javax.net.ssl.SSLContext; 032 033import org.apache.http.HttpHost; 034import org.apache.http.HttpRequest; 035import org.apache.http.HttpResponse; 036import org.apache.http.HttpResponseFactory; 037import org.apache.http.annotation.ThreadingBehavior; 038import org.apache.http.annotation.Contract; 039import org.apache.http.config.ConnectionConfig; 040import org.apache.http.impl.DefaultHttpResponseFactory; 041import org.apache.http.impl.nio.DefaultNHttpClientConnectionFactory; 042import org.apache.http.impl.nio.SSLNHttpClientConnectionFactory; 043import org.apache.http.nio.NHttpClientConnection; 044import org.apache.http.nio.NHttpConnectionFactory; 045import org.apache.http.nio.NHttpMessageParserFactory; 046import org.apache.http.nio.NHttpMessageWriterFactory; 047import org.apache.http.nio.pool.NIOConnFactory; 048import org.apache.http.nio.reactor.IOEventDispatch; 049import org.apache.http.nio.reactor.IOSession; 050import org.apache.http.nio.reactor.ssl.SSLSetupHandler; 051import org.apache.http.nio.util.ByteBufferAllocator; 052import org.apache.http.nio.util.HeapByteBufferAllocator; 053import org.apache.http.params.HttpParams; 054import org.apache.http.util.Args; 055 056/** 057 * A basic {@link NIOConnFactory} implementation that creates 058 * {@link NHttpClientConnection} instances given a {@link HttpHost} instance. 059 * 060 * @since 4.2 061 */ 062@SuppressWarnings("deprecation") 063@Contract(threading = ThreadingBehavior.IMMUTABLE_CONDITIONAL) 064public class BasicNIOConnFactory implements NIOConnFactory<HttpHost, NHttpClientConnection> { 065 066 private final NHttpConnectionFactory<? extends NHttpClientConnection> plainFactory; 067 private final NHttpConnectionFactory<? extends NHttpClientConnection> sslFactory; 068 069 public BasicNIOConnFactory( 070 final NHttpConnectionFactory<? extends NHttpClientConnection> plainFactory, 071 final NHttpConnectionFactory<? extends NHttpClientConnection> sslFactory) { 072 super(); 073 Args.notNull(plainFactory, "Plain HTTP client connection factory"); 074 this.plainFactory = plainFactory; 075 this.sslFactory = sslFactory; 076 } 077 078 public BasicNIOConnFactory( 079 final NHttpConnectionFactory<? extends NHttpClientConnection> plainFactory) { 080 this(plainFactory, null); 081 } 082 083 /** 084 * @deprecated (4.3) use {@link BasicNIOConnFactory#BasicNIOConnFactory(SSLContext, 085 * SSLSetupHandler, NHttpMessageParserFactory, NHttpMessageWriterFactory, 086 * ByteBufferAllocator, ConnectionConfig)} 087 */ 088 @Deprecated 089 public BasicNIOConnFactory( 090 final SSLContext sslContext, 091 final SSLSetupHandler sslHandler, 092 final HttpResponseFactory responseFactory, 093 final ByteBufferAllocator allocator, 094 final HttpParams params) { 095 this(new DefaultNHttpClientConnectionFactory( 096 responseFactory, allocator, params), 097 new SSLNHttpClientConnectionFactory( 098 sslContext, sslHandler, responseFactory, allocator, params)); 099 } 100 101 /** 102 * @deprecated (4.3) use {@link BasicNIOConnFactory#BasicNIOConnFactory(SSLContext, 103 * SSLSetupHandler, ConnectionConfig)} 104 */ 105 @Deprecated 106 public BasicNIOConnFactory( 107 final SSLContext sslContext, 108 final SSLSetupHandler sslHandler, 109 final HttpParams params) { 110 this(sslContext, sslHandler, 111 DefaultHttpResponseFactory.INSTANCE, HeapByteBufferAllocator.INSTANCE, params); 112 } 113 114 /** 115 * @deprecated (4.3) use {@link BasicNIOConnFactory#BasicNIOConnFactory(ConnectionConfig)} 116 */ 117 @Deprecated 118 public BasicNIOConnFactory(final HttpParams params) { 119 this(null, null, params); 120 } 121 122 /** 123 * @since 4.3 124 */ 125 public BasicNIOConnFactory( 126 final SSLContext sslContext, 127 final SSLSetupHandler sslHandler, 128 final NHttpMessageParserFactory<HttpResponse> responseParserFactory, 129 final NHttpMessageWriterFactory<HttpRequest> requestWriterFactory, 130 final ByteBufferAllocator allocator, 131 final ConnectionConfig config) { 132 this(new DefaultNHttpClientConnectionFactory( 133 responseParserFactory, requestWriterFactory, allocator, config), 134 new SSLNHttpClientConnectionFactory( 135 sslContext, sslHandler, responseParserFactory, requestWriterFactory, 136 allocator, config)); 137 } 138 139 /** 140 * @since 4.3 141 */ 142 public BasicNIOConnFactory( 143 final SSLContext sslContext, 144 final SSLSetupHandler sslHandler, 145 final ConnectionConfig config) { 146 this(sslContext, sslHandler, null, null, null, config); 147 } 148 149 /** 150 * @since 4.3 151 */ 152 public BasicNIOConnFactory(final ConnectionConfig config) { 153 this(new DefaultNHttpClientConnectionFactory(config), null); 154 } 155 156 @Override 157 public NHttpClientConnection create(final HttpHost route, final IOSession session) throws IOException { 158 final NHttpClientConnection conn; 159 if (route.getSchemeName().equalsIgnoreCase("https")) { 160 if (this.sslFactory == null) { 161 throw new IOException("SSL not supported"); 162 } 163 conn = this.sslFactory.createConnection(session); 164 } else { 165 conn = this.plainFactory.createConnection(session); 166 } 167 session.setAttribute(IOEventDispatch.CONNECTION_KEY, conn); 168 return conn; 169 } 170 171}