30
30
*/
31
31
package com .notnoop .mpns ;
32
32
33
+ import com .notnoop .mpns .exceptions .InvalidSSLConfig ;
34
+ import com .notnoop .mpns .exceptions .RuntimeIOException ;
35
+ import com .notnoop .mpns .internal .AbstractMpnsService ;
36
+ import com .notnoop .mpns .internal .MpnsPooledService ;
37
+ import com .notnoop .mpns .internal .MpnsQueuedService ;
38
+ import com .notnoop .mpns .internal .MpnsServiceImpl ;
39
+ import com .notnoop .mpns .internal .Utilities ;
33
40
import java .io .FileInputStream ;
34
41
import java .io .FileNotFoundException ;
35
42
import java .io .InputStream ;
36
43
import java .util .concurrent .ExecutorService ;
37
44
import java .util .concurrent .Executors ;
38
-
39
45
import javax .net .ssl .SSLContext ;
40
-
41
46
import org .apache .http .HttpHost ;
42
47
import org .apache .http .client .HttpClient ;
48
+ import org .apache .http .client .config .RequestConfig ;
43
49
import org .apache .http .conn .params .ConnRoutePNames ;
44
- import org .apache .http .conn .scheme .Scheme ;
45
- import org .apache .http .conn .ssl .SSLSocketFactory ;
46
- import org .apache .http .impl .client .DefaultHttpClient ;
47
- import org .apache .http .params .HttpConnectionParams ;
48
- import org .apache .http .params .HttpParams ;
49
-
50
- import com .notnoop .mpns .exceptions .InvalidSSLConfig ;
51
- import com .notnoop .mpns .exceptions .RuntimeIOException ;
52
- import com .notnoop .mpns .internal .AbstractMpnsService ;
53
- import com .notnoop .mpns .internal .MpnsPooledService ;
54
- import com .notnoop .mpns .internal .MpnsQueuedService ;
55
- import com .notnoop .mpns .internal .MpnsServiceImpl ;
56
- import com .notnoop .mpns .internal .Utilities ;
50
+ import org .apache .http .impl .client .HttpClientBuilder ;
57
51
58
52
/**
59
53
* The class is used to create instances of {@link MpnsService}.
60
54
*
61
- * Note that this class is not synchronized. If multiple threads access a
62
- * {@code MpnsServiceBuilder} instance concurrently, and at least on of the
63
- * threads modifies one of the attributes structurally, it must be
64
- * synchronized externally.
55
+ * Note that this class is not synchronized. If multiple threads access a {@code MpnsServiceBuilder} instance
56
+ * concurrently, and at least on of the threads modifies one of the attributes structurally, it must be synchronized
57
+ * externally.
65
58
*
66
59
* Starting a new {@code MpnsService} is easy:
67
60
*
73
66
public class MpnsServiceBuilder {
74
67
75
68
private SSLContext sslContext ;
76
-
69
+
77
70
private int pooledMax = 1 ;
78
71
private ExecutorService executor = null ;
79
72
@@ -87,11 +80,11 @@ public class MpnsServiceBuilder {
87
80
/**
88
81
* Constructs a new instance of {@code MpnsServiceBuilder}
89
82
*/
90
- public MpnsServiceBuilder () { }
91
-
83
+ public MpnsServiceBuilder () {
84
+ }
85
+
92
86
public MpnsServiceBuilder withCert (String fileName , String password , String ksType , String kAlgo )
93
- throws RuntimeIOException , InvalidSSLConfig
94
- {
87
+ throws RuntimeIOException , InvalidSSLConfig {
95
88
FileInputStream stream = null ;
96
89
try {
97
90
stream = new FileInputStream (fileName );
@@ -102,34 +95,32 @@ public MpnsServiceBuilder withCert(String fileName, String password, String ksTy
102
95
Utilities .close (stream );
103
96
}
104
97
}
105
-
98
+
106
99
public MpnsServiceBuilder withCert (InputStream stream , String password , String ksType , String kAlgo )
107
- throws InvalidSSLConfig
108
- {
100
+ throws InvalidSSLConfig {
109
101
if (password == null || password .isEmpty ()) {
110
- throw new IllegalArgumentException ("Passwords must be specified." +
111
- "Oracle Java SDK does not support passwordless p12 certificates" );
102
+ throw new IllegalArgumentException ("Passwords must be specified."
103
+ + "Oracle Java SDK does not support passwordless p12 certificates" );
112
104
}
113
105
114
- return withSSLContext (Utilities .newSSLContext (stream , password ,ksType , kAlgo ));
106
+ return withSSLContext (Utilities .newSSLContext (stream , password , ksType , kAlgo ));
115
107
}
116
-
108
+
117
109
public MpnsServiceBuilder withSSLContext (SSLContext sslContext ) {
118
110
this .sslContext = sslContext ;
119
111
return this ;
120
112
}
121
113
122
114
/**
123
- * Specify the address of the HTTP proxy the connection should
124
- * use.
115
+ * Specify the address of the HTTP proxy the connection should use.
125
116
*
126
- * <p>Read the <a href="http://java.sun.com/javase/6/docs/technotes/guides/net/proxies.html">
127
- * Java Networking and Proxies</a> guide to understand the
128
- * proxies complexity.
117
+ * <p>
118
+ * Read the <a href="http://java.sun.com/javase/6/docs/technotes/guides/net/proxies.html">
119
+ * Java Networking and Proxies</a> guide to understand the proxies complexity.
129
120
*
130
- * @param host the hostname of the HTTP proxy
131
- * @param port the port of the HTTP proxy server
132
- * @return this
121
+ * @param host the hostname of the HTTP proxy
122
+ * @param port the port of the HTTP proxy server
123
+ * @return this
133
124
*/
134
125
public MpnsServiceBuilder withHttpProxy (String host , int port ) {
135
126
this .proxy = new HttpHost (host , port );
@@ -152,14 +143,12 @@ public MpnsServiceBuilder withHttpProxy(String host, int port) {
152
143
// this.proxy = proxy;
153
144
// return this;
154
145
// }
155
-
156
146
/**
157
147
* Sets the HttpClient instance along with any configuration
158
148
*
159
- * NOTE: This is an advanced option that should be probably be used as a
160
- * last resort.
149
+ * NOTE: This is an advanced option that should be probably be used as a last resort.
161
150
*
162
- * @param httpClient the httpClient to be used
151
+ * @param httpClient the httpClient to be used
163
152
* @return this
164
153
*/
165
154
public MpnsServiceBuilder withHttpClient (HttpClient httpClient ) {
@@ -180,8 +169,8 @@ public MpnsServiceBuilder asPool(int maxConnections) {
180
169
/**
181
170
* Constructs a pool of connections to the notification servers.
182
171
*
183
- * Note: The maxConnections here is used as a hint to how many connections
184
- * get created.
172
+ * Note: The maxConnections here is used as a hint to how many connections get created.
173
+ *
185
174
* @param executor the executor
186
175
* @param maxConnections the maximum number of connections in the pool
187
176
* @return this
@@ -193,10 +182,9 @@ public MpnsServiceBuilder asPool(ExecutorService executor, int maxConnections) {
193
182
}
194
183
195
184
/**
196
- * Constructs a new thread with a processing queue to process
197
- * notification requests.
185
+ * Constructs a new thread with a processing queue to process notification requests.
198
186
*
199
- * @return this
187
+ * @return this
200
188
*/
201
189
public MpnsServiceBuilder asQueued () {
202
190
this .isQueued = true ;
@@ -206,7 +194,7 @@ public MpnsServiceBuilder asQueued() {
206
194
/**
207
195
* Sets the timeout for the connection
208
196
*
209
- * @param timeout the time out period in millis
197
+ * @param timeout the time out period in millis
210
198
* @return this
211
199
*/
212
200
public MpnsServiceBuilder timeout (int timeout ) {
@@ -220,40 +208,50 @@ public MpnsServiceBuilder delegate(MpnsDelegate delegate) {
220
208
}
221
209
222
210
/**
223
- * Returns a fully initialized instance of {@link MpnsService},
224
- * according to the requested settings.
211
+ * Returns a fully initialized instance of {@link MpnsService}, according to the requested settings.
225
212
*
226
- * @return a new instance of MpnsService
213
+ * @return a new instance of MpnsService
227
214
*/
228
215
public MpnsService build () {
229
216
checkInitialization ();
230
217
231
- // Client Configuration
232
- HttpClient client ;
218
+ // HTTP Client Configuration
219
+ HttpClient client = null ;
233
220
if (httpClient != null ) {
221
+ // XXX Why?
234
222
client = httpClient ;
235
- } else if (pooledMax == 1 ) {
236
- client = new DefaultHttpClient ();
237
-
238
- SSLSocketFactory socketFactory = new SSLSocketFactory (sslContext );
239
- Scheme sch = new Scheme ("https" , 443 , socketFactory );
240
- client .getConnectionManager ().getSchemeRegistry ().register (sch );
241
223
} else {
242
- client = new DefaultHttpClient (Utilities .poolManager (pooledMax ));
243
-
244
- SSLSocketFactory socketFactory = new SSLSocketFactory (sslContext );
245
- Scheme sch = new Scheme ("https" , 443 , socketFactory );
246
- client .getConnectionManager ().getSchemeRegistry ().register (sch );
247
- }
224
+ // Creating a new HTTP client
225
+ HttpClientBuilder builder = HttpClientBuilder .create ();
248
226
249
- if (proxy != null ) {
250
- client .getParams ().setParameter (ConnRoutePNames .DEFAULT_PROXY , proxy );
251
- }
227
+ // Connection pool settings
228
+ if (pooledMax != 1 ) {
229
+ builder .setConnectionManager (Utilities .poolManager (pooledMax ));
230
+ builder .setMaxConnTotal (pooledMax );
231
+ }
232
+
233
+ // Connection parameters
234
+ RequestConfig .Builder requestConfigBuilder = RequestConfig .custom ();
235
+ if (timeout > 0 ) {
236
+ requestConfigBuilder
237
+ .setConnectTimeout (timeout )
238
+ .setSocketTimeout (timeout );
239
+ }
240
+
241
+ // Proxy settings
242
+ if (proxy != null ) {
243
+ // XXX Didn't find the replacement for this deprecated method
244
+ client .getParams ().setParameter (ConnRoutePNames .DEFAULT_PROXY , proxy );
245
+ }
246
+
247
+ // SSL
248
+ if (this .sslContext != null ) {
249
+ builder .setSslcontext (sslContext );
250
+ }
252
251
253
- if (timeout > 0 ) {
254
- HttpParams params = client .getParams ();
255
- HttpConnectionParams .setConnectionTimeout (params , timeout );
256
- HttpConnectionParams .setSoTimeout (params , timeout );
252
+ // Build the http client
253
+ builder .setDefaultRequestConfig (requestConfigBuilder .build ());
254
+ client = builder .build ();
257
255
}
258
256
259
257
// Configure service
0 commit comments