Return-Path: X-Original-To: apmail-cloudstack-commits-archive@www.apache.org Delivered-To: apmail-cloudstack-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 3A81B10B14 for ; Fri, 7 Feb 2014 21:47:26 +0000 (UTC) Received: (qmail 57868 invoked by uid 500); 7 Feb 2014 21:46:27 -0000 Delivered-To: apmail-cloudstack-commits-archive@cloudstack.apache.org Received: (qmail 57157 invoked by uid 500); 7 Feb 2014 21:46:16 -0000 Mailing-List: contact commits-help@cloudstack.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@cloudstack.apache.org Delivered-To: mailing list commits@cloudstack.apache.org Received: (qmail 56929 invoked by uid 99); 7 Feb 2014 21:46:12 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 07 Feb 2014 21:46:12 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id E322D91FA69; Fri, 7 Feb 2014 21:46:11 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: mchen@apache.org To: commits@cloudstack.apache.org Date: Fri, 07 Feb 2014 21:46:22 -0000 Message-Id: In-Reply-To: References: X-Mailer: ASF-Git Admin Mailer Subject: [13/50] [abbrv] git commit: updated refs/heads/rbac to 3b58a45 stratosphere-ssp plugin: Fix HttpClient4 connection leak Replaced HttpClient#execute(HttpUriRequest) with HttpClient#execute(HttpUriRequest,ResponseHandler). 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/0d222b14 Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/0d222b14 Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/0d222b14 Branch: refs/heads/rbac Commit: 0d222b14a156f9ec60c137b83ef3cc7e78c90133 Parents: 54f32a8 Author: Hiroaki KAWAI Authored: Wed Feb 5 16:20:10 2014 +0900 Committer: Hiroaki KAWAI Committed: Wed Feb 5 16:23:32 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/0d222b14/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 4d0be2d..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(), "UTF-8"), - 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(), "UTF-8"), - 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(), "UTF-8"), - 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/0d222b14/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); }