1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 package org.apache.http.impl.nio.pool;
28
29 import java.io.IOException;
30
31 import javax.net.ssl.SSLContext;
32
33 import org.apache.http.HttpHost;
34 import org.apache.http.HttpResponseFactory;
35 import org.apache.http.annotation.Immutable;
36 import org.apache.http.impl.DefaultHttpResponseFactory;
37 import org.apache.http.impl.nio.DefaultNHttpClientConnectionFactory;
38 import org.apache.http.impl.nio.SSLNHttpClientConnectionFactory;
39 import org.apache.http.nio.NHttpClientConnection;
40 import org.apache.http.nio.NHttpConnectionFactory;
41 import org.apache.http.nio.pool.NIOConnFactory;
42 import org.apache.http.nio.reactor.IOEventDispatch;
43 import org.apache.http.nio.reactor.IOSession;
44 import org.apache.http.nio.reactor.ssl.SSLSetupHandler;
45 import org.apache.http.nio.util.ByteBufferAllocator;
46 import org.apache.http.nio.util.HeapByteBufferAllocator;
47 import org.apache.http.params.HttpParams;
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64 @Immutable
65 public class BasicNIOConnFactory implements NIOConnFactory<HttpHost, NHttpClientConnection> {
66
67 private final NHttpConnectionFactory<? extends NHttpClientConnection> plainFactory;
68 private final NHttpConnectionFactory<? extends NHttpClientConnection> sslFactory;
69
70 public BasicNIOConnFactory(
71 final NHttpConnectionFactory<? extends NHttpClientConnection> plainFactory,
72 final NHttpConnectionFactory<? extends NHttpClientConnection> sslFactory) {
73 super();
74 if (plainFactory == null) {
75 throw new IllegalArgumentException("Plain HTTP client connection factory may not be null");
76 }
77 this.plainFactory = plainFactory;
78 this.sslFactory = sslFactory;
79 }
80
81 public BasicNIOConnFactory(
82 final NHttpConnectionFactory<? extends NHttpClientConnection> plainFactory) {
83 this(plainFactory, null);
84 }
85
86 public BasicNIOConnFactory(
87 final SSLContext sslcontext,
88 final SSLSetupHandler sslHandler,
89 final HttpResponseFactory responseFactory,
90 final ByteBufferAllocator allocator,
91 final HttpParams params) {
92 this(new DefaultNHttpClientConnectionFactory(
93 responseFactory, allocator, params),
94 new SSLNHttpClientConnectionFactory(
95 sslcontext, sslHandler, responseFactory, allocator, params));
96 }
97
98 public BasicNIOConnFactory(
99 final SSLContext sslcontext,
100 final SSLSetupHandler sslHandler,
101 final HttpParams params) {
102 this(sslcontext, sslHandler,
103 new DefaultHttpResponseFactory(), new HeapByteBufferAllocator(), params);
104 }
105
106 public BasicNIOConnFactory(final HttpParams params) {
107 this(null, null, params);
108 }
109
110 public NHttpClientConnection create(final HttpHost route, final IOSession session) throws IOException {
111 NHttpClientConnection conn;
112 if (route.getSchemeName().equalsIgnoreCase("https")) {
113 if (this.sslFactory == null) {
114 throw new IOException("SSL not supported");
115 }
116 conn = this.sslFactory.createConnection(session);
117 } else {
118 conn = this.plainFactory.createConnection(session);
119 }
120 session.setAttribute(IOEventDispatch.CONNECTION_KEY, conn);
121 return conn;
122 }
123
124 }