Updated Branches: refs/heads/master 752676a09 -> 44a220a61
Send DefaultHttpClient externally Project: http://git-wip-us.apache.org/repos/asf/incubator-stratos/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-stratos/commit/44a220a6 Tree: http://git-wip-us.apache.org/repos/asf/incubator-stratos/tree/44a220a6 Diff: http://git-wip-us.apache.org/repos/asf/incubator-stratos/diff/44a220a6 Branch: refs/heads/master Commit: 44a220a61752fa0834796f255d127b9815d0d781 Parents: 752676a Author: Manula Thantriwatte <[email protected]> Authored: Fri Dec 20 13:12:41 2013 +0530 Committer: Manula Thantriwatte <[email protected]> Committed: Fri Dec 20 13:12:41 2013 +0530 ---------------------------------------------------------------------- .../apache/stratos/cli/GenericRestClient.java | 6 +- .../java/org/apache/stratos/cli/RestClient.java | 8 +- .../stratos/cli/RestCommandLineService.java | 78 ++++++++++++++++---- 3 files changed, 70 insertions(+), 22 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/44a220a6/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/GenericRestClient.java ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/GenericRestClient.java b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/GenericRestClient.java index 51a05d9..54e7442 100644 --- a/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/GenericRestClient.java +++ b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/GenericRestClient.java @@ -19,6 +19,7 @@ package org.apache.stratos.cli; import org.apache.http.HttpResponse; +import org.apache.http.impl.client.DefaultHttpClient; public interface GenericRestClient { @@ -37,7 +38,8 @@ public interface GenericRestClient { * @throws org.apache.http.client.ClientProtocolException and IOException * if any errors occur when executing the request */ - public HttpResponse doPost(String resourcePath, String jsonParamString, String userName, String passWord) throws Exception; + public HttpResponse doPost(DefaultHttpClient httpClient, String resourcePath, String jsonParamString, + String userName, String passWord) throws Exception; /** * Handle http get request. Return String @@ -52,7 +54,7 @@ public interface GenericRestClient { * @throws org.apache.http.client.ClientProtocolException and IOException * if any errors occur when executing the request */ - public HttpResponse doGet(String resourcePath, String userName, String passWord); + public HttpResponse doGet(DefaultHttpClient httpClient, String resourcePath, String userName, String passWord); public void doDelete(); http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/44a220a6/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/RestClient.java ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/RestClient.java b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/RestClient.java index 17d98a4..f5f855b 100644 --- a/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/RestClient.java +++ b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/RestClient.java @@ -60,10 +60,10 @@ public class RestClient implements GenericRestClient{ * @throws org.apache.http.client.ClientProtocolException and IOException * if any errors occur when executing the request */ - public HttpResponse doPost(String resourcePath, String jsonParamString, String userName, String passWord) throws Exception{ + public HttpResponse doPost(DefaultHttpClient httpClient, String resourcePath, String jsonParamString, String userName, String passWord) throws Exception{ try { - DefaultHttpClient httpClient = new DefaultHttpClient(); + //DefaultHttpClient httpClient = new DefaultHttpClient(); HttpPost postRequest = new HttpPost(resourcePath); @@ -133,9 +133,9 @@ public class RestClient implements GenericRestClient{ * @throws org.apache.http.client.ClientProtocolException and IOException * if any errors occur when executing the request */ - public HttpResponse doGet(String resourcePath, String userName, String passWord) { + public HttpResponse doGet(DefaultHttpClient httpClient, String resourcePath, String userName, String passWord) { try { - DefaultHttpClient httpClient = new DefaultHttpClient(); + //DefaultHttpClient httpClient = new DefaultHttpClient(); HttpGet getRequest = new HttpGet(resourcePath); getRequest.addHeader("Content-Type", "application/json"); http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/44a220a6/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/RestCommandLineService.java ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/RestCommandLineService.java b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/RestCommandLineService.java index e982a48..a35cb8e 100644 --- a/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/RestCommandLineService.java +++ b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/RestCommandLineService.java @@ -28,6 +28,7 @@ import org.apache.axis2.transport.http.HttpTransportProperties; import org.apache.commons.lang3.StringUtils; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; +import org.apache.http.impl.client.DefaultHttpClient; import org.apache.stratos.cli.beans.*; import org.apache.stratos.cli.exception.CommandException; import org.apache.stratos.cli.utils.CliConstants; @@ -116,9 +117,11 @@ public class RestCommandLineService { throw new CommandException(e); } + DefaultHttpClient httpClient = new DefaultHttpClient(); try { + if (validateLogin) { - restClientService.doPost(restClientService.getUrl() + initializeEndpoint, "", + restClientService.doPost(httpClient, restClientService.getUrl() + initializeEndpoint, "", restClientService.getUsername(), restClientService.getPassword()); if (logger.isDebugEnabled()) { logger.debug("Tenant Domain {}", restClientService.getUsername()); @@ -134,6 +137,8 @@ public class RestCommandLineService { } catch (ConnectException e) { System.out.println("Authentication failed. Please set the STRTOS_URL"); return false; + } finally { + httpClient.getConnectionManager().shutdown(); } } @@ -170,8 +175,9 @@ public class RestCommandLineService { // List currently available multi tenant and single tenant cartridges public void listAvailableCartridges() throws CommandException { + DefaultHttpClient httpClient = new DefaultHttpClient(); try { - HttpResponse response = restClientService.doGet(restClientService.getUrl() + listAvailableCartridgesRestEndpoint, + HttpResponse response = restClientService.doGet(httpClient, restClientService.getUrl() + listAvailableCartridgesRestEndpoint, restClientService.getUsername(), restClientService.getPassword()); String responseCode = "" + response.getStatusLine().getStatusCode(); @@ -254,14 +260,16 @@ public class RestCommandLineService { } } catch (Exception e) { e.printStackTrace(); + } finally { + httpClient.getConnectionManager().shutdown(); } } // List subscribe cartridges public void listSubscribedCartridges(final boolean full) throws CommandException { + DefaultHttpClient httpClient = new DefaultHttpClient(); try { - - HttpResponse response = restClientService.doGet(restClientService.getUrl() + listSubscribedCartridgesRestEndpoint, + HttpResponse response = restClientService.doGet(httpClient, restClientService.getUrl() + listSubscribedCartridgesRestEndpoint, restClientService.getUsername(), restClientService.getPassword()); String responseCode = "" + response.getStatusLine().getStatusCode(); @@ -329,6 +337,8 @@ public class RestCommandLineService { System.out.println(); } catch (Exception e) { e.printStackTrace(); + } finally { + httpClient.getConnectionManager().shutdown(); } } @@ -336,6 +346,7 @@ public class RestCommandLineService { public void subscribe(String cartridgeType, String alias, String externalRepoURL, boolean privateRepo, String username, String password, String dataCartridgeType, String dataCartridgeAlias, String asPolicy, String depPolicy) throws CommandException { + DefaultHttpClient httpClient = new DefaultHttpClient(); CartridgeInfoBean cartridgeInfoBean = new CartridgeInfoBean(); cartridgeInfoBean.setCartridgeType(null); @@ -361,7 +372,7 @@ public class RestCommandLineService { System.out.format("Subscribing to data cartridge %s with alias %s.%n", dataCartridgeType, dataCartridgeAlias); try { - HttpResponse response = restClientService.doPost(restClientService.getUrl() + subscribCartridgeRestEndpoint, + HttpResponse response = restClientService.doPost(httpClient, restClientService.getUrl() + subscribCartridgeRestEndpoint, completeJsonSubscribeString, restClientService.getUsername(), restClientService.getPassword()); String responseCode = "" + response.getStatusLine().getStatusCode(); @@ -393,6 +404,13 @@ public class RestCommandLineService { } catch (Exception e) { e.printStackTrace(); } + finally { + httpClient.getConnectionManager().shutdown(); + } + } + + if (httpClient == null) { + httpClient = new DefaultHttpClient(); } try { @@ -411,7 +429,7 @@ public class RestCommandLineService { jsonSubscribeString = gson.toJson(cartridgeInfoBean, CartridgeInfoBean.class); completeJsonSubscribeString = "{\"cartridgeInfoBean\":" + jsonSubscribeString + "}"; - HttpResponse response = restClientService.doPost(restClientService.getUrl() + subscribCartridgeRestEndpoint, + HttpResponse response = restClientService.doPost(httpClient, restClientService.getUrl() + subscribCartridgeRestEndpoint, completeJsonSubscribeString, restClientService.getUsername(), restClientService.getPassword()); String responseCode = "" + response.getStatusLine().getStatusCode(); @@ -468,11 +486,14 @@ public class RestCommandLineService { System.out.format("Please map the %s \"%s\" to LB IP%n", hostnamesLabel, hostnames); } catch (Exception e) { e.printStackTrace(); + } finally { + httpClient.getConnectionManager().shutdown(); } } // This method helps to create the new tenant public void addTenant(String admin, String firstName, String lastaName, String password, String domain, String email){ + DefaultHttpClient httpClient = new DefaultHttpClient(); try { TenantInfoBean tenantInfo = new TenantInfoBean(); tenantInfo.setAdmin(admin); @@ -489,7 +510,7 @@ public class RestCommandLineService { String jsonString = gson.toJson(tenantInfo, TenantInfoBean.class); String completeJsonString = "{\"tenantInfoBean\":" + jsonString + "}"; - HttpResponse response = restClientService.doPost(restClientService.getUrl() + addTenantEndPoint, + HttpResponse response = restClientService.doPost(httpClient, restClientService.getUrl() + addTenantEndPoint, completeJsonString, restClientService.getUsername(), restClientService.getPassword()); String responseCode = "" + response.getStatusLine().getStatusCode(); @@ -509,24 +530,30 @@ public class RestCommandLineService { } catch (Exception e) { e.printStackTrace(); + } finally { + httpClient.getConnectionManager().shutdown(); } } // This method helps to unsubscribe cartridges public void unsubscribe(String alias) throws CommandException { + DefaultHttpClient httpClient = new DefaultHttpClient(); try { - restClientService.doPost(restClientService.getUrl() + unsubscribeTenantEndPoint, alias, + restClientService.doPost(httpClient, restClientService.getUrl() + unsubscribeTenantEndPoint, alias, restClientService.getUsername(), restClientService.getPassword()); System.out.println("You have successfully unsubscribed " + alias); } catch ( Exception e) { e.printStackTrace(); + } finally { + httpClient.getConnectionManager().shutdown(); } } // This method helps to deploy cartridge definitions public void deployCartridgeDefinition (String cartridgeDefinition) { + DefaultHttpClient httpClient = new DefaultHttpClient(); try { - HttpResponse response = restClientService.doPost(restClientService.getUrl() + cartridgeDeploymentEndPoint, + HttpResponse response = restClientService.doPost(httpClient, restClientService.getUrl() + cartridgeDeploymentEndPoint, cartridgeDefinition, restClientService.getUsername(), restClientService.getPassword()); String responseCode = "" + response.getStatusLine().getStatusCode(); @@ -543,13 +570,16 @@ public class RestCommandLineService { } } catch (Exception e) { e.printStackTrace(); + } finally { + httpClient.getConnectionManager().shutdown(); } } // This method helps to deploy partitions public void deployPartition (String partitionDefinition) { + DefaultHttpClient httpClient = new DefaultHttpClient(); try { - HttpResponse response = restClientService.doPost(restClientService.getUrl() + partitionDeploymentEndPoint, + HttpResponse response = restClientService.doPost(httpClient, restClientService.getUrl() + partitionDeploymentEndPoint, partitionDefinition, restClientService.getUsername(), restClientService.getPassword()); String responseCode = "" + response.getStatusLine().getStatusCode(); @@ -571,13 +601,16 @@ public class RestCommandLineService { } catch (Exception e) { e.printStackTrace(); + } finally { + httpClient.getConnectionManager().shutdown(); } } // This method helps to deploy autoscalling polices public void deployAutoscalingPolicy (String autoScalingPolicy) { + DefaultHttpClient httpClient= new DefaultHttpClient(); try { - HttpResponse response = restClientService.doPost(restClientService.getUrl() + autoscalingPolicyDeploymentEndPoint, + HttpResponse response = restClientService.doPost(httpClient, restClientService.getUrl() + autoscalingPolicyDeploymentEndPoint, autoScalingPolicy, restClientService.getUsername(), restClientService.getPassword()); String responseCode = "" + response.getStatusLine().getStatusCode(); @@ -598,13 +631,16 @@ public class RestCommandLineService { } catch (Exception e) { e.printStackTrace(); + } finally { + httpClient.getConnectionManager().shutdown(); } } // This method helps to deploy deployment polices public void deployDeploymentPolicy (String deploymentPolicy) { + DefaultHttpClient httpClient = new DefaultHttpClient(); try { - HttpResponse response = restClientService.doPost(restClientService.getUrl() + deploymentPolicyDeploymentEndPoint, + HttpResponse response = restClientService.doPost(httpClient, restClientService.getUrl() + deploymentPolicyDeploymentEndPoint, deploymentPolicy, restClientService.getUsername(), restClientService.getPassword()); String responseCode = "" + response.getStatusLine().getStatusCode(); @@ -625,13 +661,16 @@ public class RestCommandLineService { } catch (Exception e) { e.printStackTrace(); + } finally { + httpClient.getConnectionManager().shutdown(); } } // This method list available partitons public void listPartitions() throws CommandException{ + DefaultHttpClient httpClient = new DefaultHttpClient(); try { - HttpResponse response = restClientService.doGet(restClientService.getUrl() + listParitionRestEndPoint, + HttpResponse response = restClientService.doGet(httpClient, restClientService.getUrl() + listParitionRestEndPoint, restClientService.getUsername(), restClientService.getPassword()); String responseCode = "" + response.getStatusLine().getStatusCode(); @@ -680,13 +719,16 @@ public class RestCommandLineService { } catch (Exception e) { e.printStackTrace(); + } finally { + httpClient.getConnectionManager().shutdown(); } } // This method list autoscale policies public void listAutoscalePolicies() throws CommandException { + DefaultHttpClient httpClient = new DefaultHttpClient(); try { - HttpResponse response = restClientService.doGet(restClientService.getUrl() + listAutoscalePolicyRestEndPoint, + HttpResponse response = restClientService.doGet(httpClient, restClientService.getUrl() + listAutoscalePolicyRestEndPoint, restClientService.getUsername(), restClientService.getPassword()); String responseCode = "" + response.getStatusLine().getStatusCode(); @@ -734,13 +776,16 @@ public class RestCommandLineService { } catch (Exception e) { e.printStackTrace(); + } finally { + httpClient.getConnectionManager().shutdown(); } } // This method list deployment policies public void listDeploymentPolicies() throws CommandException { + DefaultHttpClient httpClient = new DefaultHttpClient(); try { - HttpResponse response = restClientService.doGet(restClientService.getUrl() + listDeploymentPolicyRestEndPoint, + HttpResponse response = restClientService.doGet(httpClient, restClientService.getUrl() + listDeploymentPolicyRestEndPoint, restClientService.getUsername(), restClientService.getPassword()); String responseCode = "" + response.getStatusLine().getStatusCode(); @@ -783,9 +828,10 @@ public class RestCommandLineService { CommandLineUtils.printTable(policyArry, partitionMapper, "ID"); System.out.println(); - } catch (Exception e) { e.printStackTrace(); + } finally { + httpClient.getConnectionManager().shutdown(); } }
