Skip to content

Commit d979d7d

Browse files
author
Mike Noordermeer
committed
Fix keep-alive handling.
We now try to keep the connection alive by simply consuming the full response. If that does not work, we revert to the old behaviour (to make sure we always close the underlying resources).
1 parent 03db6b8 commit d979d7d

3 files changed

Lines changed: 27 additions & 9 deletions

File tree

src/main/java/microsoft/exchange/webservices/data/HangingServiceRequestBase.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ private void parseResponses(Object state) {
260260
EwsServiceMultiResponseXmlReader.create(tracingStream, getService());
261261
responseObject = this.readResponse(ewsXmlReader);
262262
this.responseHandler.handleResponseObject(responseObject);
263-
/* }catch(Exception ex){
263+
/* }catch(Exception ex){
264264
this.disconnect(HangingRequestDisconnectReason.Exception, ex);
265265
return;
266266
@@ -343,8 +343,11 @@ private void setIsConnected(boolean value) {
343343
*/
344344
protected void disconnect() {
345345
synchronized (this) {
346-
//this.request.close();
347-
this.response.close();
346+
try {
347+
this.response.close();
348+
} catch (IOException e) {
349+
// Ignore exception on disconnection
350+
}
348351
this.disconnect(HangingRequestDisconnectReason.UserInitiated, null);
349352
}
350353
}
@@ -358,7 +361,11 @@ protected void disconnect() {
358361
protected void disconnect(HangingRequestDisconnectReason reason,
359362
Exception exception) {
360363
if (this.isConnected()) {
361-
this.response.close();
364+
try {
365+
this.response.close();
366+
} catch (IOException e) {
367+
// Ignore exception on disconnection
368+
}
362369
this.internalOnDisconnect(reason, exception);
363370
}
364371
}

src/main/java/microsoft/exchange/webservices/data/HttpClientWebRequest.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of
1818
import org.apache.http.client.CredentialsProvider;
1919
import org.apache.http.client.config.AuthSchemes;
2020
import org.apache.http.client.config.RequestConfig;
21+
import org.apache.http.client.methods.CloseableHttpResponse;
2122
import org.apache.http.client.methods.HttpPost;
2223
import org.apache.http.client.protocol.HttpClientContext;
2324
import org.apache.http.config.SocketConfig;
2425
import org.apache.http.impl.client.*;
26+
import org.apache.http.util.EntityUtils;
2527

2628
import java.io.*;
2729
import java.util.Arrays;
@@ -39,7 +41,7 @@ class HttpClientWebRequest extends HttpWebRequest {
3941
* The Http Method.
4042
*/
4143
private HttpPost httpPost = null;
42-
private HttpResponse response = null;
44+
private CloseableHttpResponse response = null;
4345

4446
private final CloseableHttpClient httpClient;
4547
private final HttpClientContext httpContext;
@@ -57,11 +59,20 @@ public HttpClientWebRequest(CloseableHttpClient httpClient, HttpClientContext ht
5759
* Releases the connection by Closing.
5860
*/
5961
@Override
60-
public void close() {
61-
if (null != httpPost) {
62+
public void close() throws IOException {
63+
// First check if we can close the response, by consuming the complete response
64+
// This releases the connection but keeps it alive for future requests
65+
// If that is not possible, we simply cleanup the whole connection
66+
if (response != null && response.getEntity() != null) {
67+
EntityUtils.consume(response.getEntity());
68+
} else if (httpPost != null) {
6269
httpPost.releaseConnection();
63-
//postMethod.abort();
6470
}
71+
72+
// We set httpPost to null to prevent the connection from being closed again by an accidental
73+
// second call to close()
74+
// The response is kept, in case something in the library still wants to read something from it,
75+
// like response code or headers
6576
httpPost = null;
6677
}
6778

src/main/java/microsoft/exchange/webservices/data/HttpWebRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ public void setCredentials(String domain, String user, String pwd) {
475475
/**
476476
* Close.
477477
*/
478-
public abstract void close();
478+
public abstract void close() throws IOException;
479479

480480
/**
481481
* Prepare connection.

0 commit comments

Comments
 (0)