You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudstack.apache.org by ka...@apache.org on 2014/02/05 08:37:38 UTC
git commit: updated refs/heads/4.3-forward to 09e27fd
Updated Branches:
refs/heads/4.3-forward e7ec79b5f -> 09e27fd7b
stratosphere-ssp plugin: Fix HttpClient4 connection leak
Replaced HttpClient#execute(HttpUriRequest) with
HttpClient#execute(HttpUriRequest,ResponseHandler<T>).
The former requires extra EntityUtils#consume(HttpEntity).
Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/09e27fd7
Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/09e27fd7
Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/09e27fd7
Branch: refs/heads/4.3-forward
Commit: 09e27fd7bf92a7ff723e36c8ca485e54ee00e8c7
Parents: e7ec79b
Author: Hiroaki KAWAI <ka...@stratosphere.co.jp>
Authored: Wed Feb 5 16:35:18 2014 +0900
Committer: Hiroaki KAWAI <ka...@stratosphere.co.jp>
Committed: Wed Feb 5 16:37:10 2014 +0900
----------------------------------------------------------------------
.../cloudstack/network/element/SspClient.java | 110 ++++++-------------
.../network/element/SspClientTest.java | 20 +---
2 files changed, 36 insertions(+), 94 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/09e27fd7/plugins/network-elements/stratosphere-ssp/src/org/apache/cloudstack/network/element/SspClient.java
----------------------------------------------------------------------
diff --git a/plugins/network-elements/stratosphere-ssp/src/org/apache/cloudstack/network/element/SspClient.java b/plugins/network-elements/stratosphere-ssp/src/org/apache/cloudstack/network/element/SspClient.java
index c0db92c..30630a3 100644
--- a/plugins/network-elements/stratosphere-ssp/src/org/apache/cloudstack/network/element/SspClient.java
+++ b/plugins/network-elements/stratosphere-ssp/src/org/apache/cloudstack/network/element/SspClient.java
@@ -17,15 +17,15 @@
package org.apache.cloudstack.network.element;
import java.io.IOException;
-import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
-import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
+import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
+import org.apache.http.client.HttpResponseException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpPost;
@@ -35,6 +35,7 @@ import org.apache.http.client.params.ClientPNames;
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
@@ -42,8 +43,6 @@ import org.apache.http.params.CoreConnectionPNames;
import org.apache.log4j.Logger;
import com.google.gson.Gson;
-import com.google.gson.JsonIOException;
-import com.google.gson.JsonSyntaxException;
import com.google.gson.annotations.SerializedName;
/**
@@ -74,7 +73,7 @@ public class SspClient {
return s_client;
}
- private HttpResponse innerExecuteMethod(HttpRequestBase req, String path) {
+ private String executeMethod(HttpRequestBase req, String path) {
try {
URI base = new URI(apiUrl);
req.setURI(new URI(base.getScheme(), base.getUserInfo(), base.getHost(),
@@ -83,23 +82,26 @@ public class SspClient {
s_logger.error("invalid API URL " + apiUrl + " path " + path, e);
return null;
}
- HttpResponse res = null;
try {
- res = getHttpClient().execute(req);
- s_logger.info("ssp api call:" + req + " status=" + res.getStatusLine());
+ String content = null;
+ try {
+ content = getHttpClient().execute(req, new BasicResponseHandler());
+ s_logger.info("ssp api call: " + req);
+ } catch (HttpResponseException e) {
+ s_logger.info("ssp api call failed: " + req, e);
+ if (e.getStatusCode() == HttpStatus.SC_UNAUTHORIZED && login()) {
+ req.reset();
+ content = getHttpClient().execute(req, new BasicResponseHandler());
+ s_logger.info("ssp api retry call: " + req);
+ }
+ }
+ return content;
+ } catch (ClientProtocolException e) { // includes HttpResponseException
+ s_logger.error("ssp api call failed: " + req, e);
} catch (IOException e) {
s_logger.error("ssp api call failed: " + req, e);
}
- return res;
- }
-
- private HttpResponse executeMethod(HttpRequestBase req, String path) {
- HttpResponse res = innerExecuteMethod(req, path);
- if (res.getStatusLine().getStatusCode() == HttpStatus.SC_UNAUTHORIZED && login()) {
- req.reset();
- res = innerExecuteMethod(req, path);
- }
- return res;
+ return null;
}
public boolean login() {
@@ -112,9 +114,7 @@ public class SspClient {
s_logger.error("invalid username or password", e);
return false;
}
-
- HttpResponse res = this.innerExecuteMethod(method, "/ws.v1/login");
- if (res != null && res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
+ if (executeMethod(method, "/ws.v1/login") != null) {
return true;
}
return false;
@@ -134,29 +134,14 @@ public class SspClient {
HttpPost method = new HttpPost();
method.setEntity(new StringEntity(new Gson().toJson(req), ContentType.APPLICATION_JSON));
- HttpResponse res = executeMethod(method, "/ssp.v1/tenant-networks");
- if (res == null || res.getStatusLine().getStatusCode() != HttpStatus.SC_CREATED) {
- return null;
- }
- try {
- return new Gson().fromJson(new InputStreamReader(res.getEntity().getContent()),
- TenantNetwork.class);
- } catch (JsonSyntaxException e) {
- s_logger.error("reading response body failed", e);
- } catch (JsonIOException e) {
- s_logger.error("reading response body failed", e);
- } catch (IllegalStateException e) {
- s_logger.error("reading response body failed", e);
- } catch (IOException e) {
- s_logger.error("reading response body failed", e);
- }
- return null;
+ return new Gson().fromJson(
+ executeMethod(method, "/ssp.v1/tenant-networks"),
+ TenantNetwork.class);
}
public boolean deleteTenantNetwork(String tenantNetworkUuid) {
HttpDelete method = new HttpDelete();
- HttpResponse res = executeMethod(method, "/ssp.v1/tenant-networks/" + tenantNetworkUuid);
- if (res != null && res.getStatusLine().getStatusCode() == HttpStatus.SC_NO_CONTENT) {
+ if (executeMethod(method, "/ssp.v1/tenant-networks/" + tenantNetworkUuid) != null) {
return true;
}
return false;
@@ -182,31 +167,14 @@ public class SspClient {
HttpPost method = new HttpPost();
method.setEntity(new StringEntity(new Gson().toJson(req), ContentType.APPLICATION_JSON));
- HttpResponse res = executeMethod(method, "/ssp.v1/tenant-ports");
-
- if (res == null || res.getStatusLine().getStatusCode() != HttpStatus.SC_CREATED) {
- return null;
- }
- try {
- return new Gson().fromJson(new InputStreamReader(res.getEntity().getContent()),
- TenantPort.class);
- } catch (JsonSyntaxException e) {
- s_logger.error("reading response body failed", e);
- } catch (JsonIOException e) {
- s_logger.error("reading response body failed", e);
- } catch (IllegalStateException e) {
- s_logger.error("reading response body failed", e);
- } catch (IOException e) {
- s_logger.error("reading response body failed", e);
- }
- return null;
+ return new Gson().fromJson(
+ executeMethod(method, "/ssp.v1/tenant-ports"),
+ TenantPort.class);
}
public boolean deleteTenantPort(String tenantPortUuid) {
HttpDelete method = new HttpDelete();
- HttpResponse res = executeMethod(method, "/ssp.v1/tenant-ports/" + tenantPortUuid);
-
- if (res != null && res.getStatusLine().getStatusCode() == HttpStatus.SC_NO_CONTENT) {
+ if (executeMethod(method, "/ssp.v1/tenant-ports/" + tenantPortUuid) != null) {
return true;
}
return false;
@@ -223,22 +191,8 @@ public class SspClient {
HttpPut method = new HttpPut();
method.setEntity(new StringEntity(new Gson().toJson(req), ContentType.APPLICATION_JSON));
- HttpResponse res = executeMethod(method, "/ssp.v1/tenant-ports/" + portUuid);
- if (res == null || res.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
- return null;
- }
- try {
- return new Gson().fromJson(new InputStreamReader(res.getEntity().getContent()),
- TenantPort.class);
- } catch (JsonSyntaxException e) {
- s_logger.error("reading response body failed", e);
- } catch (JsonIOException e) {
- s_logger.error("reading response body failed", e);
- } catch (IllegalStateException e) {
- s_logger.error("reading response body failed", e);
- } catch (IOException e) {
- s_logger.error("reading response body failed", e);
- }
- return null;
+ return new Gson().fromJson(
+ executeMethod(method, "/ssp.v1/tenant-ports/" + portUuid),
+ TenantPort.class);
}
}
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/09e27fd7/plugins/network-elements/stratosphere-ssp/test/org/apache/cloudstack/network/element/SspClientTest.java
----------------------------------------------------------------------
diff --git a/plugins/network-elements/stratosphere-ssp/test/org/apache/cloudstack/network/element/SspClientTest.java b/plugins/network-elements/stratosphere-ssp/test/org/apache/cloudstack/network/element/SspClientTest.java
index 627cc87..a6b723c 100644
--- a/plugins/network-elements/stratosphere-ssp/test/org/apache/cloudstack/network/element/SspClientTest.java
+++ b/plugins/network-elements/stratosphere-ssp/test/org/apache/cloudstack/network/element/SspClientTest.java
@@ -23,15 +23,11 @@ import static org.mockito.Mockito.when;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.doReturn;
-import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
-
-import java.io.ByteArrayInputStream;
import java.util.UUID;
-import org.apache.commons.httpclient.HttpStatus;
-import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpUriRequest;
+import org.apache.http.impl.client.BasicResponseHandler;
import org.junit.Test;
public class SspClientTest {
@@ -46,10 +42,8 @@ public class SspClientTest {
SspClient sspClient = spy(new SspClient(apiUrl, username, password));
HttpClient client = mock(HttpClient.class);
- HttpResponse res = mock(HttpResponse.class, RETURNS_DEEP_STUBS);
doReturn(client).when(sspClient).getHttpClient();
- when(client.execute(any(HttpUriRequest.class))).thenReturn(res);
- when(res.getStatusLine().getStatusCode()).thenReturn(HttpStatus.SC_OK);
+ when(client.execute(any(HttpUriRequest.class), any(BasicResponseHandler.class))).thenReturn("");
assertTrue(sspClient.login());
assertTrue(sspClient.login());
@@ -63,14 +57,10 @@ public class SspClientTest {
SspClient sspClient = spy(new SspClient(apiUrl, username, password));
HttpClient client = mock(HttpClient.class);
- HttpResponse res = mock(HttpResponse.class, RETURNS_DEEP_STUBS);
doReturn(client).when(sspClient).getHttpClient();
- when(client.execute(any(HttpUriRequest.class))).thenReturn(res);
- when(res.getStatusLine().getStatusCode()).thenReturn(HttpStatus.SC_CREATED);
String body = "{\"uuid\":\"" + tenant_net_uuid + "\",\"name\":\"" + networkName
+ "\",\"tenant_uuid\":\"" + uuid + "\"}";
- when(res.getEntity().getContent()).thenReturn(
- new ByteArrayInputStream(body.getBytes("UTF-8")));
+ when(client.execute(any(HttpUriRequest.class), any(BasicResponseHandler.class))).thenReturn(body);
SspClient.TenantNetwork tnet = sspClient.createTenantNetwork(uuid, networkName);
assertEquals(tnet.name, networkName);
@@ -84,10 +74,8 @@ public class SspClientTest {
SspClient sspClient = spy(new SspClient(apiUrl, username, password));
HttpClient client = mock(HttpClient.class);
- HttpResponse res = mock(HttpResponse.class, RETURNS_DEEP_STUBS);
doReturn(client).when(sspClient).getHttpClient();
- when(client.execute(any(HttpUriRequest.class))).thenReturn(res);
- when(res.getStatusLine().getStatusCode()).thenReturn(HttpStatus.SC_NO_CONTENT);
+ when(client.execute(any(HttpUriRequest.class), any(BasicResponseHandler.class))).thenReturn("");
sspClient.deleteTenantNetwork(tenant_net_uuid);
}