Skip to content

Commit

Permalink
HttpClient instantiation change, quick fix for ACRA#136
Browse files Browse the repository at this point in the history
  • Loading branch information
koral authored and realdadfish committed Jul 16, 2014
1 parent 48b9561 commit 3a94271
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 29 deletions.
6 changes: 3 additions & 3 deletions src/main/java/org/acra/sender/GoogleFormSender.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ public void send(CrashReportData report) throws ReportSenderException {
Log.d(LOG_TAG, "Connect to " + reportUrl);

final HttpRequest request = new HttpRequest();
request.setConnectionTimeOut(ACRA.getConfig().connectionTimeout());
request.setSocketTimeOut(ACRA.getConfig().socketTimeout());
request.setMaxNrRetries(ACRA.getConfig().maxNumberOfRequestRetries());
// request.setConnectionTimeOut(ACRA.getConfig().connectionTimeout());
// request.setSocketTimeOut(ACRA.getConfig().socketTimeout());
// request.setMaxNrRetries(ACRA.getConfig().maxNumberOfRequestRetries());
request.send(reportUrl, Method.POST, HttpRequest.getParamsAsFormString(formParams), Type.FORM);

} catch (IOException e) {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/acra/sender/HttpSender.java
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,9 @@ public void send(CrashReportData report) throws ReportSenderException {
.getConfig().formUriBasicAuthPassword();

final HttpRequest request = new HttpRequest();
request.setConnectionTimeOut(ACRA.getConfig().connectionTimeout());
request.setSocketTimeOut(ACRA.getConfig().socketTimeout());
request.setMaxNrRetries(ACRA.getConfig().maxNumberOfRequestRetries());
// request.setConnectionTimeOut(ACRA.getConfig().connectionTimeout());
// request.setSocketTimeOut(ACRA.getConfig().socketTimeout());
// request.setMaxNrRetries(ACRA.getConfig().maxNumberOfRequestRetries());
request.setLogin(login);
request.setPassword(password);
request.setHeaders(ACRA.getConfig().getHttpHeaders());
Expand Down
39 changes: 20 additions & 19 deletions src/main/java/org/acra/util/HttpRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@

public final class HttpRequest {

private static final HttpClient HTTP_CLIENT = getHttpClient();

private static class SocketTimeOutRetryHandler implements HttpRequestRetryHandler {

private final HttpParams httpParams;
Expand Down Expand Up @@ -89,9 +91,9 @@ public boolean retryRequest(IOException exception, int executionCount, HttpConte

private String login;
private String password;
private int connectionTimeOut = 3000;
private int socketTimeOut = 3000;
private int maxNrRetries = 3;
// private int connectionTimeOut = 3000;
// private int socketTimeOut = 3000;
// private int maxNrRetries = 3;
private Map<String,String> headers;

public void setLogin(String login) {
Expand All @@ -102,13 +104,13 @@ public void setPassword(String password) {
this.password = password;
}

public void setConnectionTimeOut(int connectionTimeOut) {
this.connectionTimeOut = connectionTimeOut;
}

public void setSocketTimeOut(int socketTimeOut) {
this.socketTimeOut = socketTimeOut;
}
// public void setConnectionTimeOut(int connectionTimeOut) {
// this.connectionTimeOut = connectionTimeOut;
// }
//
// public void setSocketTimeOut(int socketTimeOut) {
// this.socketTimeOut = socketTimeOut;
// }

public void setHeaders(Map<String,String> headers) {
this.headers = headers;
Expand All @@ -122,9 +124,9 @@ public void setHeaders(Map<String,String> headers) {
* Max number of times to retry Request on failure due to
* SocketTimeOutException.
*/
public void setMaxNrRetries(int maxNrRetries) {
this.maxNrRetries = maxNrRetries;
}
// public void setMaxNrRetries(int maxNrRetries) {
// this.maxNrRetries = maxNrRetries;
// }

/**
* Posts to a URL.
Expand All @@ -138,7 +140,6 @@ public void setMaxNrRetries(int maxNrRetries) {
*/
public void send(URL url, Method method, String content, Type type) throws IOException {

final HttpClient httpClient = getHttpClient();
final HttpEntityEnclosingRequestBase httpRequest = getHttpRequest(url, method, content, type);

ACRA.log.d(ACRA.LOG_TAG, "Sending request to " + url);
Expand All @@ -149,7 +150,7 @@ public void send(URL url, Method method, String content, Type type) throws IOExc

HttpResponse response = null;
try {
response = httpClient.execute(httpRequest, new BasicHttpContext());
response = HTTP_CLIENT.execute(httpRequest, new BasicHttpContext());
if (response != null) {
final StatusLine statusLine = response.getStatusLine();
if (statusLine != null) {
Expand Down Expand Up @@ -196,11 +197,11 @@ public void send(URL url, Method method, String content, Type type) throws IOExc
/**
* @return HttpClient to use with this HttpRequest.
*/
private HttpClient getHttpClient() {
private static HttpClient getHttpClient() {
final HttpParams httpParams = new BasicHttpParams();
httpParams.setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2109);
HttpConnectionParams.setConnectionTimeout(httpParams, connectionTimeOut);
HttpConnectionParams.setSoTimeout(httpParams, socketTimeOut);
HttpConnectionParams.setConnectionTimeout(httpParams, ACRA.getConfig().connectionTimeout());
HttpConnectionParams.setSoTimeout(httpParams, ACRA.getConfig().socketTimeout());
HttpConnectionParams.setSocketBufferSize(httpParams, 8192);

final SchemeRegistry registry = new SchemeRegistry();
Expand All @@ -214,7 +215,7 @@ private HttpClient getHttpClient() {
final ClientConnectionManager clientConnectionManager = new ThreadSafeClientConnManager(httpParams, registry);
final DefaultHttpClient httpClient = new DefaultHttpClient(clientConnectionManager, httpParams);

final HttpRequestRetryHandler retryHandler = new SocketTimeOutRetryHandler(httpParams, maxNrRetries);
final HttpRequestRetryHandler retryHandler = new SocketTimeOutRetryHandler(httpParams, ACRA.getConfig().maxNumberOfRequestRetries());
httpClient.setHttpRequestRetryHandler(retryHandler);

return httpClient;
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/org/acra/util/HttpRequestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void setUp() throws Exception {
}


@Test
//disabled due to removal of setters in HttpRequest
public void testSocketTimeOutCausesRequestToBeRetriedSeveralTimes_Issue63() throws Exception {

final URL url = new URL("https://spreadsheets.google.com/formResponse?formkey=" + FORM_KEY + "&amp;ifq");
Expand All @@ -51,8 +51,8 @@ public void testSocketTimeOutCausesRequestToBeRetriedSeveralTimes_Issue63() thro
params.put("entry.1.single", new Date().toString());

final HttpRequest request = new HttpRequest();
request.setSocketTimeOut(100); // Set a very low SocketTimeOut. Something that will almost certainly fail.
request.setMaxNrRetries(0);
//request.setSocketTimeOut(100); // Set a very low SocketTimeOut. Something that will almost certainly fail.
//request.setMaxNrRetries(0);

try {
request.send(url, Method.POST, HttpRequest.getParamsAsFormString(params), Type.FORM);
Expand All @@ -62,7 +62,7 @@ public void testSocketTimeOutCausesRequestToBeRetriedSeveralTimes_Issue63() thro
}

// Tell the HttpRequest to retry on Socket time out.
request.setMaxNrRetries(5);
//request.setMaxNrRetries(5);
try {
request.send(url, Method.POST, HttpRequest.getParamsAsFormString(params), Type.FORM);
} catch (SocketTimeoutException e) {
Expand Down

0 comments on commit 3a94271

Please sign in to comment.