Skip to content

Commit 6faa53d

Browse files
AvromAvrom
authored andcommitted
Fix for Issue OfficeDev#276
1 parent c88387e commit 6faa53d

6 files changed

Lines changed: 98 additions & 9 deletions

File tree

src/main/java/microsoft/exchange/webservices/data/core/ExchangeService.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3744,6 +3744,17 @@ public HttpWebRequest prepareHttpWebRequest()
37443744
.getAcceptGzipEncoding(), true);
37453745
}
37463746

3747+
public HttpWebRequest prepareHttpPoolingWebRequest()
3748+
throws ServiceLocalException, URISyntaxException {
3749+
try {
3750+
this.url = this.adjustServiceUriFromCredentials(this.getUrl());
3751+
} catch (Exception e) {
3752+
LOG.error(e);
3753+
}
3754+
return this.prepareHttpPoolingWebRequestForUrl(url, this
3755+
.getAcceptGzipEncoding(), true);
3756+
}
3757+
37473758
/**
37483759
* Processes an HTTP error response.
37493760
*

src/main/java/microsoft/exchange/webservices/data/core/ExchangeServiceBase.java

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import microsoft.exchange.webservices.data.core.exception.service.local.ServiceLocalException;
3535
import microsoft.exchange.webservices.data.misc.EwsTraceListener;
3636
import microsoft.exchange.webservices.data.misc.ITraceListener;
37+
3738
import org.apache.http.client.AuthenticationStrategy;
3839
import org.apache.http.client.CookieStore;
3940
import org.apache.http.client.protocol.HttpClientContext;
@@ -46,6 +47,7 @@
4647
import org.apache.http.impl.client.CloseableHttpClient;
4748
import org.apache.http.impl.client.HttpClients;
4849
import org.apache.http.impl.conn.BasicHttpClientConnectionManager;
50+
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
4951

5052
import javax.xml.stream.XMLStreamException;
5153
import javax.xml.stream.XMLStreamWriter;
@@ -142,7 +144,12 @@ public abstract class ExchangeServiceBase implements Closeable {
142144

143145
protected HttpClientContext httpContext;
144146

145-
protected HttpClientWebRequest request = null;
147+
protected CloseableHttpClient httpPoolingClient;
148+
149+
private int maximumPoolingConnections = 10;
150+
151+
152+
// protected HttpClientWebRequest request = null;
146153

147154
// protected static HttpStatusCode AccountIsLocked = (HttpStatusCode)456;
148155

@@ -193,8 +200,32 @@ private void initializeHttpClient() {
193200
.build();
194201
}
195202

203+
private void initializeHttpPoolingClient() {
204+
Registry<ConnectionSocketFactory> registry = createConnectionSocketFactoryRegistry();
205+
PoolingHttpClientConnectionManager httpConnectionManager = new PoolingHttpClientConnectionManager(registry);
206+
httpConnectionManager.setMaxTotal(maximumPoolingConnections);
207+
httpConnectionManager.setDefaultMaxPerRoute(maximumPoolingConnections);
208+
AuthenticationStrategy authStrategy = new CookieProcessingTargetAuthenticationStrategy();
209+
210+
httpPoolingClient = HttpClients.custom()
211+
.setConnectionManager(httpConnectionManager)
212+
.setTargetAuthenticationStrategy(authStrategy)
213+
.build();
214+
}
215+
216+
196217
/**
197-
* Create registry with configured {@link ConnectionSocketFactory} instances.
218+
* Sets the maximum number of connections for the pooling connection manager which is used for subscriptions.
219+
* <p>
220+
* Default is 10.
221+
* @param maximumPoolConnections
222+
*/
223+
public void setMaximumPoolingConnections(int maximumPoolingConnections) {
224+
this.maximumPoolingConnections = maximumPoolingConnections;
225+
}
226+
227+
/**
228+
* Create registry with configured {@see ConnectionSocketFactory} instances.
198229
* Override this method to change how to work with different schemas.
199230
*
200231
* @return registry object
@@ -226,6 +257,8 @@ private void initializeHttpContext() {
226257
public void close() {
227258
try {
228259
httpClient.close();
260+
if (httpPoolingClient != null)
261+
httpPoolingClient.close();
229262
} catch (IOException e) {
230263
// Ignore exception while closing the HttpClient.
231264
}
@@ -274,9 +307,33 @@ protected HttpWebRequest prepareHttpWebRequestForUrl(URI url, boolean acceptGzip
274307
throw new ServiceLocalException(strErr);
275308
}
276309

277-
request = new HttpClientWebRequest(httpClient, httpContext);
278-
request.setProxy(getWebProxy());
310+
HttpClientWebRequest request = new HttpClientWebRequest(httpClient, httpContext);
311+
prepareHttpWebRequestForUrl(url, acceptGzipEncoding, allowAutoRedirect, request);
312+
313+
return request;
314+
}
315+
316+
protected HttpWebRequest prepareHttpPoolingWebRequestForUrl(URI url, boolean acceptGzipEncoding,
317+
boolean allowAutoRedirect) throws ServiceLocalException, URISyntaxException {
318+
// Verify that the protocol is something that we can handle
319+
String scheme = url.getScheme();
320+
if (!scheme.equalsIgnoreCase(EWSConstants.HTTP_SCHEME)
321+
&& !scheme.equalsIgnoreCase(EWSConstants.HTTPS_SCHEME)) {
322+
String strErr = String.format("Protocol %s isn't supported for service request.", scheme);
323+
throw new ServiceLocalException(strErr);
324+
}
279325

326+
if (httpPoolingClient == null)
327+
initializeHttpPoolingClient();
328+
HttpClientWebRequest request = new HttpClientWebRequest(httpPoolingClient, httpContext);
329+
prepareHttpWebRequestForUrl(url, acceptGzipEncoding, allowAutoRedirect, request);
330+
331+
return request;
332+
}
333+
334+
private void prepareHttpWebRequestForUrl(URI url, boolean acceptGzipEncoding, boolean allowAutoRedirect, HttpClientWebRequest request)
335+
throws ServiceLocalException, URISyntaxException
336+
{
280337
try {
281338
request.setUrl(url.toURL());
282339
} catch (MalformedURLException e) {
@@ -298,8 +355,6 @@ protected HttpWebRequest prepareHttpWebRequestForUrl(URI url, boolean acceptGzip
298355
request.prepareConnection();
299356

300357
httpResponseHeaders.clear();
301-
302-
return request;
303358
}
304359

305360
protected void prepareCredentials(HttpWebRequest request) throws ServiceLocalException, URISyntaxException {

src/main/java/microsoft/exchange/webservices/data/core/request/GetStreamingEventsRequest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,5 +149,9 @@ protected static void setHeartbeatFrequency(int heartbeatFrequency) {
149149
GetStreamingEventsRequest.heartbeatFrequency = heartbeatFrequency;
150150
}
151151

152-
152+
@Override
153+
protected HttpWebRequest buildEwsHttpWebRequest() throws Exception
154+
{
155+
return super.buildEwsHttpPoolingWebRequest();
156+
}
153157
}

src/main/java/microsoft/exchange/webservices/data/core/request/ServiceRequestBase.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -661,8 +661,18 @@ protected HttpWebRequest validateAndEmitRequest() throws Exception {
661661
* @throws Exception on error
662662
*/
663663
protected HttpWebRequest buildEwsHttpWebRequest() throws Exception {
664-
try {
665664
HttpWebRequest request = service.prepareHttpWebRequest();
665+
return buildEwsHttpWebRequest(request);
666+
}
667+
668+
protected HttpWebRequest buildEwsHttpPoolingWebRequest() throws Exception {
669+
HttpWebRequest request = service.prepareHttpPoolingWebRequest();
670+
return buildEwsHttpWebRequest(request);
671+
}
672+
673+
private HttpWebRequest buildEwsHttpWebRequest(HttpWebRequest request) throws Exception
674+
{
675+
try {
666676

667677
service.traceHttpRequestHeaders(TraceFlags.EwsRequestHttpHeaders, request);
668678

src/main/java/microsoft/exchange/webservices/data/core/request/SubscribeRequest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,4 +256,9 @@ public void setWatermark(String watermark) {
256256
this.watermark = watermark;
257257
}
258258

259+
@Override
260+
protected HttpWebRequest buildEwsHttpWebRequest() throws Exception
261+
{
262+
return super.buildEwsHttpPoolingWebRequest();
263+
}
259264
}

src/main/java/microsoft/exchange/webservices/data/core/request/UnsubscribeRequest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,5 +164,9 @@ public String getSubscriptionId() {
164164
public void setSubscriptionId(String subscriptionId) {
165165
this.subscriptionId = subscriptionId;
166166
}
167-
167+
@Override
168+
protected HttpWebRequest buildEwsHttpWebRequest() throws Exception
169+
{
170+
return super.buildEwsHttpPoolingWebRequest();
171+
}
168172
}

0 commit comments

Comments
 (0)