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;
28
29 import java.nio.charset.Charset;
30 import java.nio.charset.CharsetDecoder;
31 import java.nio.charset.CharsetEncoder;
32 import java.nio.charset.CodingErrorAction;
33
34 import org.apache.http.HttpResponse;
35 import org.apache.http.HttpResponseFactory;
36 import org.apache.http.annotation.Immutable;
37 import org.apache.http.config.ConnectionConfig;
38 import org.apache.http.impl.DefaultHttpResponseFactory;
39 import org.apache.http.impl.nio.codecs.DefaultHttpResponseParserFactory;
40 import org.apache.http.nio.NHttpClientConnection;
41 import org.apache.http.nio.NHttpConnectionFactory;
42 import org.apache.http.nio.NHttpMessageParserFactory;
43 import org.apache.http.nio.reactor.IOSession;
44 import org.apache.http.nio.util.ByteBufferAllocator;
45 import org.apache.http.nio.util.HeapByteBufferAllocator;
46 import org.apache.http.params.HttpParamConfig;
47 import org.apache.http.params.HttpParams;
48 import org.apache.http.util.Args;
49
50
51
52
53
54
55 @SuppressWarnings("deprecation")
56 @Immutable
57 public class DefaultNHttpClientConnectionFactory
58 implements NHttpConnectionFactory<DefaultNHttpClientConnection> {
59
60 private final NHttpMessageParserFactory<HttpResponse> responseParserFactory;
61 private final ByteBufferAllocator allocator;
62 private final ConnectionConfig config;
63
64
65
66
67
68
69 @Deprecated
70 public DefaultNHttpClientConnectionFactory(
71 final HttpResponseFactory responseFactory,
72 final ByteBufferAllocator allocator,
73 final HttpParams params) {
74 super();
75 Args.notNull(responseFactory, "HTTP response factory");
76 Args.notNull(allocator, "Byte buffer allocator");
77 Args.notNull(params, "HTTP parameters");
78 this.allocator = allocator;
79 this.responseParserFactory = new DefaultHttpResponseParserFactory(null, responseFactory);
80 this.config = HttpParamConfig.getConnectionConfig(params);
81 }
82
83
84
85
86
87 @Deprecated
88 public DefaultNHttpClientConnectionFactory(final HttpParams params) {
89 this(DefaultHttpResponseFactory.INSTANCE, HeapByteBufferAllocator.INSTANCE, params);
90 }
91
92
93
94
95 public DefaultNHttpClientConnectionFactory(
96 final NHttpMessageParserFactory<HttpResponse> responseParserFactory,
97 final ByteBufferAllocator allocator,
98 final ConnectionConfig config) {
99 super();
100 this.allocator = allocator != null ? allocator : HeapByteBufferAllocator.INSTANCE;
101 this.responseParserFactory = responseParserFactory != null ? responseParserFactory :
102 DefaultHttpResponseParserFactory.INSTANCE;
103 this.config = config != null ? config : ConnectionConfig.DEFAULT;
104 }
105
106
107
108
109 public DefaultNHttpClientConnectionFactory(final ConnectionConfig config) {
110 this(null, null, config);
111 }
112
113
114
115
116 @Deprecated
117 protected DefaultNHttpClientConnection createConnection(
118 final IOSession session,
119 final HttpResponseFactory responseFactory,
120 final ByteBufferAllocator allocator,
121 final HttpParams params) {
122 return new DefaultNHttpClientConnection(session, responseFactory, allocator, params);
123 }
124
125 public DefaultNHttpClientConnection createConnection(final IOSession session) {
126 CharsetDecoder chardecoder = null;
127 CharsetEncoder charencoder = null;
128 final Charset charset = this.config.getCharset();
129 final CodingErrorAction malformedInputAction = this.config.getMalformedInputAction() != null ?
130 this.config.getMalformedInputAction() : CodingErrorAction.REPORT;
131 final CodingErrorAction unmappableInputAction = this.config.getUnmappableInputAction() != null ?
132 this.config.getUnmappableInputAction() : CodingErrorAction.REPORT;
133 if (charset != null) {
134 chardecoder = charset.newDecoder();
135 chardecoder.onMalformedInput(malformedInputAction);
136 chardecoder.onUnmappableCharacter(unmappableInputAction);
137 charencoder = charset.newEncoder();
138 charencoder.onMalformedInput(malformedInputAction);
139 charencoder.onUnmappableCharacter(unmappableInputAction);
140 }
141 return new DefaultNHttpClientConnection(
142 session,
143 this.config.getBufferSize(),
144 this.config.getFragmentSizeHint(),
145 this.allocator,
146 chardecoder, charencoder, this.config.getMessageConstraints(),
147 null, null, null,
148 this.responseParserFactory);
149 }
150
151 }