Skip to content
This repository was archived by the owner on Oct 26, 2024. It is now read-only.

Commit 47f8647

Browse files
committed
Upgraded HttpClient.
Fixed some bugs.
1 parent 2ecde23 commit 47f8647

File tree

5 files changed

+99
-97
lines changed

5 files changed

+99
-97
lines changed

README.markdown

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ To send a notification, you can do it in two steps:
3333

3434
2. Create and send the message
3535

36-
MpnsMessage notification = MPNS.newMessage()
36+
MpnsNotification notification = MPNS.newNotification()
3737
.tile().count(2).title("Tile message")
3838
.build();
3939
String subscriptionUri = "https://..../"

pom.xml

+3-4
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@
3232
<plugin>
3333
<groupId>org.apache.maven.plugins</groupId>
3434
<artifactId>maven-compiler-plugin</artifactId>
35-
<version>2.3.2</version>
35+
<version>3.1</version>
3636
<configuration>
37-
<source>1.6</source>
38-
<target>1.6</target>
37+
<source>1.7</source>
38+
<target>1.7</target>
3939
</configuration>
4040
</plugin>
4141
<plugin>
@@ -162,7 +162,6 @@
162162
<artifactId>jcl-over-slf4j</artifactId>
163163
<version>1.6.1</version>
164164
</dependency>
165-
166165
</dependencies>
167166

168167
<!-- Site Generation -->

src/main/java/com/notnoop/mpns/MpnsResponse.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ public enum MpnsResponse {
110110
* use an exponential backoff algorithm in minute increments.
111111
*/
112112
SERVICE_UNAVAILABLE(503, null, null, null, false, true);
113+
113114

114115
//// Response Code,NotificationStatus,DeviceConnectionStatus,SubscriptionStatus,Comments
115116
private final int responseCode;
@@ -120,7 +121,9 @@ public enum MpnsResponse {
120121
private final boolean success;
121122
private final boolean shouldRetry;
122123

123-
MpnsResponse(int responseCode, String notificationStatus,
124+
MpnsResponse(
125+
int responseCode,
126+
String notificationStatus,
124127
String deviceConnectionStatus,
125128
String subscriptionStatus,
126129
boolean success,

src/main/java/com/notnoop/mpns/MpnsServiceBuilder.java

+71-73
Original file line numberDiff line numberDiff line change
@@ -30,38 +30,31 @@
3030
*/
3131
package com.notnoop.mpns;
3232

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;
3340
import java.io.FileInputStream;
3441
import java.io.FileNotFoundException;
3542
import java.io.InputStream;
3643
import java.util.concurrent.ExecutorService;
3744
import java.util.concurrent.Executors;
38-
3945
import javax.net.ssl.SSLContext;
40-
4146
import org.apache.http.HttpHost;
4247
import org.apache.http.client.HttpClient;
48+
import org.apache.http.client.config.RequestConfig;
4349
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;
5751

5852
/**
5953
* The class is used to create instances of {@link MpnsService}.
6054
*
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.
6558
*
6659
* Starting a new {@code MpnsService} is easy:
6760
*
@@ -73,7 +66,7 @@
7366
public class MpnsServiceBuilder {
7467

7568
private SSLContext sslContext;
76-
69+
7770
private int pooledMax = 1;
7871
private ExecutorService executor = null;
7972

@@ -87,11 +80,11 @@ public class MpnsServiceBuilder {
8780
/**
8881
* Constructs a new instance of {@code MpnsServiceBuilder}
8982
*/
90-
public MpnsServiceBuilder() { }
91-
83+
public MpnsServiceBuilder() {
84+
}
85+
9286
public MpnsServiceBuilder withCert(String fileName, String password, String ksType, String kAlgo)
93-
throws RuntimeIOException, InvalidSSLConfig
94-
{
87+
throws RuntimeIOException, InvalidSSLConfig {
9588
FileInputStream stream = null;
9689
try {
9790
stream = new FileInputStream(fileName);
@@ -102,34 +95,32 @@ public MpnsServiceBuilder withCert(String fileName, String password, String ksTy
10295
Utilities.close(stream);
10396
}
10497
}
105-
98+
10699
public MpnsServiceBuilder withCert(InputStream stream, String password, String ksType, String kAlgo)
107-
throws InvalidSSLConfig
108-
{
100+
throws InvalidSSLConfig {
109101
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");
112104
}
113105

114-
return withSSLContext(Utilities.newSSLContext(stream, password,ksType, kAlgo));
106+
return withSSLContext(Utilities.newSSLContext(stream, password, ksType, kAlgo));
115107
}
116-
108+
117109
public MpnsServiceBuilder withSSLContext(SSLContext sslContext) {
118110
this.sslContext = sslContext;
119111
return this;
120112
}
121113

122114
/**
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.
125116
*
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.
129120
*
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
133124
*/
134125
public MpnsServiceBuilder withHttpProxy(String host, int port) {
135126
this.proxy = new HttpHost(host, port);
@@ -152,14 +143,12 @@ public MpnsServiceBuilder withHttpProxy(String host, int port) {
152143
// this.proxy = proxy;
153144
// return this;
154145
// }
155-
156146
/**
157147
* Sets the HttpClient instance along with any configuration
158148
*
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.
161150
*
162-
* @param httpClient the httpClient to be used
151+
* @param httpClient the httpClient to be used
163152
* @return this
164153
*/
165154
public MpnsServiceBuilder withHttpClient(HttpClient httpClient) {
@@ -180,8 +169,8 @@ public MpnsServiceBuilder asPool(int maxConnections) {
180169
/**
181170
* Constructs a pool of connections to the notification servers.
182171
*
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+
*
185174
* @param executor the executor
186175
* @param maxConnections the maximum number of connections in the pool
187176
* @return this
@@ -193,10 +182,9 @@ public MpnsServiceBuilder asPool(ExecutorService executor, int maxConnections) {
193182
}
194183

195184
/**
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.
198186
*
199-
* @return this
187+
* @return this
200188
*/
201189
public MpnsServiceBuilder asQueued() {
202190
this.isQueued = true;
@@ -206,7 +194,7 @@ public MpnsServiceBuilder asQueued() {
206194
/**
207195
* Sets the timeout for the connection
208196
*
209-
* @param timeout the time out period in millis
197+
* @param timeout the time out period in millis
210198
* @return this
211199
*/
212200
public MpnsServiceBuilder timeout(int timeout) {
@@ -220,40 +208,50 @@ public MpnsServiceBuilder delegate(MpnsDelegate delegate) {
220208
}
221209

222210
/**
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.
225212
*
226-
* @return a new instance of MpnsService
213+
* @return a new instance of MpnsService
227214
*/
228215
public MpnsService build() {
229216
checkInitialization();
230217

231-
// Client Configuration
232-
HttpClient client;
218+
// HTTP Client Configuration
219+
HttpClient client = null;
233220
if (httpClient != null) {
221+
// XXX Why?
234222
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);
241223
} 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();
248226

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+
}
252251

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();
257255
}
258256

259257
// Configure service

0 commit comments

Comments
 (0)