Repository: stratos Updated Branches: refs/heads/container-autoscaling 04ddbb794 -> 9b6b3f831
Improving error handling in RestCommandLineService.executeList() Project: http://git-wip-us.apache.org/repos/asf/stratos/repo Commit: http://git-wip-us.apache.org/repos/asf/stratos/commit/9b6b3f83 Tree: http://git-wip-us.apache.org/repos/asf/stratos/tree/9b6b3f83 Diff: http://git-wip-us.apache.org/repos/asf/stratos/diff/9b6b3f83 Branch: refs/heads/container-autoscaling Commit: 9b6b3f831758f19a06142fe0ba2a4e20ae634f4a Parents: 04ddbb7 Author: Imesh Gunaratne <[email protected]> Authored: Fri Oct 10 23:35:56 2014 +0530 Committer: Imesh Gunaratne <[email protected]> Committed: Fri Oct 10 23:36:32 2014 +0530 ---------------------------------------------------------------------- .../stratos/cli/RestCommandLineService.java | 535 ++++++++++--------- 1 file changed, 268 insertions(+), 267 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/stratos/blob/9b6b3f83/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 904a783..346c1af 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 @@ -74,7 +74,7 @@ import com.google.gson.GsonBuilder; public class RestCommandLineService { - private static final Logger logger = LoggerFactory.getLogger(RestCommandLineService.class); + private static final Logger logger = LoggerFactory.getLogger(RestCommandLineService.class); private RestClient restClient; @@ -119,12 +119,12 @@ public class RestCommandLineService { private final String getKubernetesMaster = "/stratos/admin/kubernetes/master/{id}"; private static class SingletonHolder { - private final static RestCommandLineService INSTANCE = new RestCommandLineService(); - } + private final static RestCommandLineService INSTANCE = new RestCommandLineService(); + } - public static RestCommandLineService getInstance() { - return SingletonHolder.INSTANCE; - } + public static RestCommandLineService getInstance() { + return SingletonHolder.INSTANCE; + } // Loing method. This will authenticate the user public boolean login(String serverURL, String username, String password, boolean validateLogin) throws Exception { @@ -162,7 +162,7 @@ public class RestCommandLineService { // Initialized client try { initializeRestClient(serverURL, username, password); - + if (logger.isDebugEnabled()) { logger.debug("Initialized REST Client for user {}", username); } @@ -320,15 +320,15 @@ public class RestCommandLineService { String responseCode = "" + response.getStatusLine().getStatusCode(); String resultString = getHttpResponseString(response); if (resultString == null) { - return; + return; } - + GsonBuilder gsonBuilder = new GsonBuilder(); Gson gson = gsonBuilder.create(); - + if ( ! responseCode.equals(CliConstants.RESPONSE_OK)) { - ExceptionMapper exception = gson.fromJson(resultString, ExceptionMapper.class); - System.out.println(exception); + ExceptionMapper exception = gson.fromJson(resultString, ExceptionMapper.class); + System.out.println(exception); return; } @@ -380,8 +380,8 @@ public class RestCommandLineService { }; if (multiTenetCartridge.size() == 0) { - String message = "Cannot find any deployed multi-tenant Cartridge. " - + "Please deploy a Cartridge using [" + CliConstants.CARTRIDGE_DEPLOYMENT + "] command."; + String message = "Cannot find any deployed multi-tenant Cartridge. " + + "Please deploy a Cartridge using [" + CliConstants.CARTRIDGE_DEPLOYMENT + "] command."; if (logger.isDebugEnabled()) { logger.debug(message); } @@ -397,8 +397,8 @@ public class RestCommandLineService { } if (singleTentCartridge.size() == 0) { - String message = "Cannot find any deployed single-tenant Cartridge. " - + "Please deploy a Cartridge using [" + CliConstants.CARTRIDGE_DEPLOYMENT + "] command."; + String message = "Cannot find any deployed single-tenant Cartridge. " + + "Please deploy a Cartridge using [" + CliConstants.CARTRIDGE_DEPLOYMENT + "] command."; if (logger.isDebugEnabled()) { logger.debug(message); } @@ -428,15 +428,15 @@ public class RestCommandLineService { String responseCode = "" + response.getStatusLine().getStatusCode(); String resultString = getHttpResponseString(response); if (resultString == null) { - return; + return; } - + GsonBuilder gsonBuilder = new GsonBuilder(); Gson gson = gsonBuilder.create(); - + if ( ! responseCode.equals(CliConstants.RESPONSE_OK)) { - ExceptionMapper exception = gson.fromJson(resultString, ExceptionMapper.class); - System.out.println(exception); + ExceptionMapper exception = gson.fromJson(resultString, ExceptionMapper.class); + System.out.println(exception); return; } @@ -472,10 +472,10 @@ public class RestCommandLineService { String resultString = getHttpResponseString(response); GsonBuilder gsonBuilder = new GsonBuilder(); Gson gson = gsonBuilder.create(); - + if ( ! responseCode.equals(CliConstants.RESPONSE_OK)) { - ExceptionMapper exception = gson.fromJson(resultString, ExceptionMapper.class); - System.out.println(exception); + ExceptionMapper exception = gson.fromJson(resultString, ExceptionMapper.class); + System.out.println(exception); return; } @@ -485,16 +485,16 @@ public class RestCommandLineService { System.out.println("Subscribe cartridge list is null"); return; } - + CartridgeList applicationCartridgeList = new CartridgeList(); - + // Filter out LB cartridges List<Cartridge> allCartridges = cartridgeList.getCartridge(); for (Cartridge cartridge : allCartridges) { - if( ! cartridge.isLoadBalancer()) { - applicationCartridgeList.getCartridge().add(cartridge); - } - } + if( ! cartridge.isLoadBalancer()) { + applicationCartridgeList.getCartridge().add(cartridge); + } + } Cartridge[] cartridges = new Cartridge[applicationCartridgeList.getCartridge().size()]; cartridges = applicationCartridgeList.getCartridge().toArray(cartridges); @@ -525,7 +525,7 @@ public class RestCommandLineService { data[10] = cartridge.getRepoURL() != null ? cartridge.getRepoURL() : ""; } return data; - + } }; @@ -560,18 +560,18 @@ public class RestCommandLineService { public void listSubscribedCartridgeInfo(String alias) throws CommandException { DefaultHttpClient httpClient = new DefaultHttpClient(); try { - HttpResponse response = restClient.doGet(httpClient, restClient.getBaseURL() - + listSubscribedCartridgeInfoRestEndpoint + alias); + HttpResponse response = restClient.doGet(httpClient, restClient.getBaseURL() + + listSubscribedCartridgeInfoRestEndpoint + alias); String responseCode = "" + response.getStatusLine().getStatusCode(); String resultString = getHttpResponseString(response); GsonBuilder gsonBuilder = new GsonBuilder(); Gson gson = gsonBuilder.create(); - + if ( !responseCode.equals(CliConstants.RESPONSE_OK)) { - ExceptionMapper exception = gson.fromJson(resultString, ExceptionMapper.class); - System.out.println(exception); + ExceptionMapper exception = gson.fromJson(resultString, ExceptionMapper.class); + System.out.println(exception); return; } @@ -589,8 +589,8 @@ public class RestCommandLineService { final Set<String> lbFloatingIpSet = lbIpMap.get("floating"); Cartridge[] cartridges = new Cartridge[1]; cartridges[0] = cartridge; - - + + System.out.println("\nSubscribed Cartridges Info\n"); System.out.println("\tType : " + cartridge.getCartridgeType()); System.out.println("\tName : " + cartridge.getDisplayName()); @@ -603,22 +603,22 @@ public class RestCommandLineService { String instanceCount = String.valueOf(cartridge.getActiveInstances()); System.out.println("\tRunning Instances : " + instanceCount); System.out.println("\tAccess URL(s) : " + getAccessURLs(cartridge)); - if (cartridge.getRepoURL() != null) { - System.out.println("\tRepo URL : " + cartridge.getRepoURL()); - } - System.out.println("\tLB Private IP : " + lbPrivateIpSet.toString()); - if (lbFloatingIpSet != null) { - System.out.println("\tLB Floating IP : " + lbFloatingIpSet.toString()); - } - if (cartridge.getProvider().equals("data")) { - System.out.println("\tDB-username : " +cartridge.getDbUserName()); - System.out.println("\tDB-password : " +cartridge.getPassword()); - System.out.println("\tDB-Host IP (private) : " +cartridge.getIp()); - if (cartridge.getPublicIp() != null) { - System.out.println("\tDB-Host IP (floating) : " - + cartridge.getPublicIp()); - } - } + if (cartridge.getRepoURL() != null) { + System.out.println("\tRepo URL : " + cartridge.getRepoURL()); + } + System.out.println("\tLB Private IP : " + lbPrivateIpSet.toString()); + if (lbFloatingIpSet != null) { + System.out.println("\tLB Floating IP : " + lbFloatingIpSet.toString()); + } + if (cartridge.getProvider().equals("data")) { + System.out.println("\tDB-username : " +cartridge.getDbUserName()); + System.out.println("\tDB-password : " +cartridge.getPassword()); + System.out.println("\tDB-Host IP (private) : " +cartridge.getIp()); + if (cartridge.getPublicIp() != null) { + System.out.println("\tDB-Host IP (floating) : " + + cartridge.getPublicIp()); + } + } System.out.println(); } catch (Exception e) { handleException("Exception in listing subscribe cartridges", e); @@ -626,9 +626,9 @@ public class RestCommandLineService { httpClient.getConnectionManager().shutdown(); } } - + private Map<String, Set<String>> getLbIpList(Cartridge cartridge, DefaultHttpClient httpClient) throws Exception{ - try { + try { Map<String, Set<String>> privateFloatingLBIPMap = new HashMap<String, Set<String>>(); Set<String> lbFloatingIpSet = new HashSet<String>(); Set<String> lbPrivateIpSet = new HashSet<String>(); @@ -644,8 +644,8 @@ public class RestCommandLineService { // Invoke cluster/{clusterId} for (String clusterId : lbClusterIdSet) { - HttpResponse responseCluster = restClient.doGet(httpClient, restClient.getBaseURL() - + listClusterRestEndpoint + "lb"); + HttpResponse responseCluster = restClient.doGet(httpClient, restClient.getBaseURL() + + listClusterRestEndpoint + "lb"); String responseCode = "" + responseCluster.getStatusLine().getStatusCode(); String resultStringCluster = getHttpResponseString(responseCluster); @@ -683,16 +683,16 @@ public class RestCommandLineService { handleException("Exception in get LB ip list", e); return null; } - } + } - public void listMembersOfCluster(String cartridgeType, String alias) throws CommandException { + public void listMembersOfCluster(String cartridgeType, String alias) throws CommandException { DefaultHttpClient httpClient = new DefaultHttpClient(); try { - Member[] members = getMembers(cartridgeType, alias, httpClient); + Member[] members = getMembers(cartridgeType, alias, httpClient); if (members == null) { - // these conditions are handled in the getMembers method + // these conditions are handled in the getMembers method return; } @@ -707,25 +707,25 @@ public class RestCommandLineService { System.out.println("\nList of members in the [cluster]: " + alias); for (Member member : members) { - System.out.println("\n\tServiceName : "+member.getServiceName()); - System.out.println("\tClusterId : "+member.getClusterId()); - System.out.println("\tNewtworkPartitionId : "+member.getNetworkPartitionId()); - System.out.println("\tPartitionId : "+member.getPartitionId()); - System.out.println("\tStatus : "+member.getStatus()); - if(member.getLbClusterId() != null) { - System.out.println("\tLBCluster : "+member.getLbClusterId()); - } - System.out.println("\tMemberPrivateIp : "+member.getMemberIp()); - System.out.println("\tMemberFloatingIp : "+member.getMemberPublicIp()); - System.out.println("\t-----------------------"); - } + System.out.println("\n\tServiceName : "+member.getServiceName()); + System.out.println("\tClusterId : "+member.getClusterId()); + System.out.println("\tNewtworkPartitionId : "+member.getNetworkPartitionId()); + System.out.println("\tPartitionId : "+member.getPartitionId()); + System.out.println("\tStatus : "+member.getStatus()); + if(member.getLbClusterId() != null) { + System.out.println("\tLBCluster : "+member.getLbClusterId()); + } + System.out.println("\tMemberPrivateIp : "+member.getMemberIp()); + System.out.println("\tMemberFloatingIp : "+member.getMemberPublicIp()); + System.out.println("\t-----------------------"); + } System.out.println("=================================================="); System.out.println("List of LB members for the [cluster]: " + alias ); - + // Invoke cluster/{clusterId} for (Member m : members) { - HttpResponse responseCluster = restClient.doGet(httpClient, restClient.getBaseURL() + listClusterRestEndpoint + HttpResponse responseCluster = restClient.doGet(httpClient, restClient.getBaseURL() + listClusterRestEndpoint +"clusterId/"+ m.getLbClusterId()); String responseCode = "" + responseCluster.getStatusLine().getStatusCode(); @@ -740,9 +740,9 @@ public class RestCommandLineService { break; } - printLBs(resultStringCluster); - } - + printLBs(resultStringCluster); + } + } catch (Exception e) { handleException("Exception in listing subscribe cartridges", e); } finally { @@ -750,18 +750,18 @@ public class RestCommandLineService { } } - private Member[] getMembers(String cartridgeType, String alias, DefaultHttpClient httpClient) throws Exception{ + private Member[] getMembers(String cartridgeType, String alias, DefaultHttpClient httpClient) throws Exception{ try { - HttpResponse response = restClient.doGet(httpClient, restClient.getBaseURL() - + listClusterRestEndpoint + cartridgeType + "/" + alias); + HttpResponse response = restClient.doGet(httpClient, restClient.getBaseURL() + + listClusterRestEndpoint + cartridgeType + "/" + alias); String responseCode = "" + response.getStatusLine().getStatusCode(); - + Gson gson = new Gson(); if ( ! responseCode.equals(CliConstants.RESPONSE_OK)) { - String resultString = getHttpResponseString(response); - ExceptionMapper exception = gson.fromJson(resultString, ExceptionMapper.class); - System.out.println(exception); + String resultString = getHttpResponseString(response); + ExceptionMapper exception = gson.fromJson(resultString, ExceptionMapper.class); + System.out.println(exception); return null; } @@ -774,37 +774,37 @@ public class RestCommandLineService { Member[] members = new Member[cluster.getMember().size()]; members = cluster.getMember().toArray(members); - - return members; + + return members; } catch (Exception e) { handleException("Exception in get member", e); return null; } - } + } - private Cluster getClusterObjectFromString(String resultString) { - String tmp; - if(resultString.startsWith("{\"cluster\"")) { - tmp = resultString.substring("{\"cluster\"".length() + 1, resultString.length()-1); - resultString = tmp; - } - GsonBuilder gsonBuilder = new GsonBuilder(); - Gson gson = gsonBuilder.create(); + private Cluster getClusterObjectFromString(String resultString) { + String tmp; + if(resultString.startsWith("{\"cluster\"")) { + tmp = resultString.substring("{\"cluster\"".length() + 1, resultString.length()-1); + resultString = tmp; + } + GsonBuilder gsonBuilder = new GsonBuilder(); + Gson gson = gsonBuilder.create(); return gson.fromJson(resultString, Cluster.class); - } - - private ArrayList<Cluster> getClusterListObjectFromString(String resultString) { - GsonBuilder gsonBuilder = new GsonBuilder(); - Gson gson = gsonBuilder.create(); + } - ClusterList clusterlist = gson.fromJson(resultString, ClusterList.class); - return clusterlist.getCluster(); - } + private ArrayList<Cluster> getClusterListObjectFromString(String resultString) { + GsonBuilder gsonBuilder = new GsonBuilder(); + Gson gson = gsonBuilder.create(); + + ClusterList clusterlist = gson.fromJson(resultString, ClusterList.class); + return clusterlist.getCluster(); + } private void printLBs(String resultString) { - - Cluster cluster = getClusterObjectFromString(resultString); + + Cluster cluster = getClusterObjectFromString(resultString); if (cluster == null) { System.out.println("Subscribe cartridge list is null"); @@ -821,21 +821,21 @@ public class RestCommandLineService { System.out.println("There are no subscribed cartridges"); return; } - + for (Member member : members) { - System.out.println("\n\tServiceName : "+member.getServiceName()); - System.out.println("\tClusterId : "+member.getClusterId()); - System.out.println("\tNewtworkPartitionId : "+member.getNetworkPartitionId()); - System.out.println("\tPartitionId : "+member.getPartitionId()); - System.out.println("\tStatus : "+member.getStatus()); - if(member.getLbClusterId() != null) { - System.out.println("\tLBCluster : "+member.getLbClusterId()); - } - System.out.println("\tMemberPrivateIp : "+member.getMemberIp()); - System.out.println("\tMemberFloatingIp : "+member.getMemberPublicIp()); - System.out.println("\t-----------------------"); - } - } + System.out.println("\n\tServiceName : "+member.getServiceName()); + System.out.println("\tClusterId : "+member.getClusterId()); + System.out.println("\tNewtworkPartitionId : "+member.getNetworkPartitionId()); + System.out.println("\tPartitionId : "+member.getPartitionId()); + System.out.println("\tStatus : "+member.getStatus()); + if(member.getLbClusterId() != null) { + System.out.println("\tLBCluster : "+member.getLbClusterId()); + } + System.out.println("\tMemberPrivateIp : "+member.getMemberIp()); + System.out.println("\tMemberFloatingIp : "+member.getMemberPublicIp()); + System.out.println("\t-----------------------"); + } + } private String getAsPolicyFromServiceDefinition(String cartridgeType) throws CommandException{ DefaultHttpClient httpClient = new DefaultHttpClient(); @@ -919,7 +919,7 @@ public class RestCommandLineService { } } - // This method does the cartridge subscription + // This method does the cartridge subscription public void subscribe(String cartridgeType, String alias, String externalRepoURL, boolean privateRepo, String username, String password,String asPolicy, String depPolicy, String size, boolean remoOnTermination, boolean persistanceMapping, @@ -951,7 +951,7 @@ public class RestCommandLineService { cartridgeInfoBean.setPersistanceRequired(persistanceMapping); cartridgeInfoBean.setCommitsEnabled(enableCommits); cartridgeInfoBean.setVolumeId(volumeId); - + String jsonSubscribeString = gson.toJson(cartridgeInfoBean, CartridgeInfoBean.class); HttpResponse response = restClient.doPost(httpClient, restClient.getBaseURL() + subscribCartridgeRestEndpoint, @@ -1022,8 +1022,8 @@ public class RestCommandLineService { String jsonString = gson.toJson(tenantInfo, TenantInfoBean.class); - HttpResponse response = restClient.doPost(httpClient, restClient.getBaseURL() - + addTenantEndPoint, jsonString); + HttpResponse response = restClient.doPost(httpClient, restClient.getBaseURL() + + addTenantEndPoint, jsonString); String responseCode = "" + response.getStatusLine().getStatusCode(); @@ -1031,9 +1031,9 @@ public class RestCommandLineService { System.out.println("Tenant added successfully"); return; } else { - String resultString = getHttpResponseString(response); - ExceptionMapper exception = gson.fromJson(resultString, ExceptionMapper.class); - System.out.println(exception); + String resultString = getHttpResponseString(response); + ExceptionMapper exception = gson.fromJson(resultString, ExceptionMapper.class); + System.out.println(exception); } } catch (Exception e) { @@ -1063,7 +1063,7 @@ public class RestCommandLineService { String jsonString = gson.toJson(userInfoBean, UserInfoBean.class); HttpResponse response = restClient.doPost(httpClient, restClient.getBaseURL() - + addUserEndPoint, jsonString); + + addUserEndPoint, jsonString); String responseCode = "" + response.getStatusLine().getStatusCode(); @@ -1087,8 +1087,8 @@ public class RestCommandLineService { public void deleteTenant(String tenantDomain) throws CommandException{ DefaultHttpClient httpClient = new DefaultHttpClient(); try { - HttpResponse response = restClient.doDelete(httpClient, restClient.getBaseURL() - + addTenantEndPoint + "/" + tenantDomain); + HttpResponse response = restClient.doDelete(httpClient, restClient.getBaseURL() + + addTenantEndPoint + "/" + tenantDomain); String responseCode = "" + response.getStatusLine().getStatusCode(); @@ -1116,7 +1116,7 @@ public class RestCommandLineService { DefaultHttpClient httpClient = new DefaultHttpClient(); try { HttpResponse response = restClient.doDelete(httpClient, restClient.getBaseURL() - + addUserEndPoint + "/" + userName); + + addUserEndPoint + "/" + userName); String responseCode = "" + response.getStatusLine().getStatusCode(); @@ -1143,8 +1143,8 @@ public class RestCommandLineService { public void deactivateTenant(String tenantDomain) throws CommandException { DefaultHttpClient httpClient = new DefaultHttpClient(); try { - HttpResponse response = restClient.doPost(httpClient, restClient.getBaseURL() - + deactivateTenantRestEndPoint + "/" + tenantDomain, ""); + HttpResponse response = restClient.doPost(httpClient, restClient.getBaseURL() + + deactivateTenantRestEndPoint + "/" + tenantDomain, ""); String responseCode = "" + response.getStatusLine().getStatusCode(); @@ -1171,8 +1171,8 @@ public class RestCommandLineService { public void activateTenant(String tenantDomain) throws CommandException { DefaultHttpClient httpClient = new DefaultHttpClient(); try { - HttpResponse response = restClient.doPost(httpClient, restClient.getBaseURL() - + activateTenantRestEndPoint + "/" + tenantDomain, ""); + HttpResponse response = restClient.doPost(httpClient, restClient.getBaseURL() + + activateTenantRestEndPoint + "/" + tenantDomain, ""); String responseCode = "" + response.getStatusLine().getStatusCode(); @@ -1199,8 +1199,8 @@ public class RestCommandLineService { public void listAllTenants() throws CommandException { DefaultHttpClient httpClient = new DefaultHttpClient(); try { - HttpResponse response = restClient.doGet(httpClient, restClient.getBaseURL() - + listAllTenantRestEndPoint); + HttpResponse response = restClient.doGet(httpClient, restClient.getBaseURL() + + listAllTenantRestEndPoint); String responseCode = "" + response.getStatusLine().getStatusCode(); String resultString = getHttpResponseString(response); @@ -1243,8 +1243,8 @@ public class RestCommandLineService { tenants = tenantInfoList.getTenantInfoBean().toArray(tenants); if (tenants.length == 0) { - String message = "Cannot find any Tenant. " - + "Please create a new tenant using [" + CliConstants.ADD_TENANT + "] command."; + String message = "Cannot find any Tenant. " + + "Please create a new tenant using [" + CliConstants.ADD_TENANT + "] command."; if (logger.isDebugEnabled()) { logger.debug(message); } @@ -1268,7 +1268,7 @@ public class RestCommandLineService { DefaultHttpClient httpClient = new DefaultHttpClient(); try { HttpResponse response = restClient.doGet(httpClient, restClient.getBaseURL() - + listAllUserRestEndPoint); + + listAllUserRestEndPoint); String responseCode = "" + response.getStatusLine().getStatusCode(); String resultString = getHttpResponseString(response); @@ -1309,7 +1309,7 @@ public class RestCommandLineService { if (users.length == 0) { String message = "Cannot find any User. " - + "Please create a new user using [" + CliConstants.ADD_USER + "] command."; + + "Please create a new user using [" + CliConstants.ADD_USER + "] command."; if (logger.isDebugEnabled()) { logger.debug(message); } @@ -1339,13 +1339,13 @@ public class RestCommandLineService { GsonBuilder gsonBuilder = new GsonBuilder(); Gson gson = gsonBuilder.create(); - if (responseCode.equals(CliConstants.RESPONSE_OK)) { + if (responseCode.equals(CliConstants.RESPONSE_OK)) { System.out.println("You have successfully unsubscribed " + alias + " cartridge"); return; } else { - String resultString = getHttpResponseString(response); - ExceptionMapper exception = gson.fromJson(resultString, ExceptionMapper.class); - System.out.println(exception); + String resultString = getHttpResponseString(response); + ExceptionMapper exception = gson.fromJson(resultString, ExceptionMapper.class); + System.out.println(exception); } } catch ( Exception e) { @@ -1415,8 +1415,8 @@ public class RestCommandLineService { public void deployPartition (String partitionDefinition) throws CommandException{ DefaultHttpClient httpClient = new DefaultHttpClient(); try { - HttpResponse response = restClient.doPost(httpClient, restClient.getBaseURL() - + partitionDeploymentEndPoint, partitionDefinition); + HttpResponse response = restClient.doPost(httpClient, restClient.getBaseURL() + + partitionDeploymentEndPoint, partitionDefinition); String responseCode = "" + response.getStatusLine().getStatusCode(); String resultString = getHttpResponseString(response); @@ -1444,8 +1444,8 @@ public class RestCommandLineService { public void deployAutoscalingPolicy (String autoScalingPolicy) throws CommandException{ DefaultHttpClient httpClient= new DefaultHttpClient(); try { - HttpResponse response = restClient.doPost(httpClient, restClient.getBaseURL() - + autoscalingPolicyDeploymentEndPoint, autoScalingPolicy); + HttpResponse response = restClient.doPost(httpClient, restClient.getBaseURL() + + autoscalingPolicyDeploymentEndPoint, autoScalingPolicy); String responseCode = "" + response.getStatusLine().getStatusCode(); @@ -1472,8 +1472,8 @@ public class RestCommandLineService { public void deployService (String deployService) throws CommandException{ DefaultHttpClient httpClient= new DefaultHttpClient(); try { - HttpResponse response = restClient.doPost(httpClient, restClient.getBaseURL() - + deployServiceEndPoint, deployService); + HttpResponse response = restClient.doPost(httpClient, restClient.getBaseURL() + + deployServiceEndPoint, deployService); String responseCode = "" + response.getStatusLine().getStatusCode(); @@ -1500,8 +1500,8 @@ public class RestCommandLineService { public void undeployService(String id) throws CommandException{ DefaultHttpClient httpClient = new DefaultHttpClient(); try { - HttpResponse response = restClient.doDelete(httpClient, restClient.getBaseURL() - + deployServiceEndPoint + "/" + id); + HttpResponse response = restClient.doDelete(httpClient, restClient.getBaseURL() + + deployServiceEndPoint + "/" + id); String responseCode = "" + response.getStatusLine().getStatusCode(); @@ -1528,8 +1528,8 @@ public class RestCommandLineService { public void listDeployServices() throws CommandException { DefaultHttpClient httpClient = new DefaultHttpClient(); try { - HttpResponse response = restClient.doGet(httpClient, restClient.getBaseURL() - + listDeployServicesRestEndPoint); + HttpResponse response = restClient.doGet(httpClient, restClient.getBaseURL() + + listDeployServicesRestEndPoint); String responseCode = "" + response.getStatusLine().getStatusCode(); @@ -1596,8 +1596,8 @@ public class RestCommandLineService { public void deployDeploymentPolicy (String deploymentPolicy) throws CommandException{ DefaultHttpClient httpClient = new DefaultHttpClient(); try { - HttpResponse response = restClient.doPost(httpClient, restClient.getBaseURL() - + deploymentPolicyDeploymentEndPoint, deploymentPolicy); + HttpResponse response = restClient.doPost(httpClient, restClient.getBaseURL() + + deploymentPolicyDeploymentEndPoint, deploymentPolicy); String responseCode = "" + response.getStatusLine().getStatusCode(); @@ -1624,8 +1624,8 @@ public class RestCommandLineService { public void listPartitions() throws CommandException{ DefaultHttpClient httpClient = new DefaultHttpClient(); try { - HttpResponse response = restClient.doGet(httpClient, restClient.getBaseURL() - + listParitionRestEndPoint); + HttpResponse response = restClient.doGet(httpClient, restClient.getBaseURL() + + listParitionRestEndPoint); String responseCode = "" + response.getStatusLine().getStatusCode(); String resultString = getHttpResponseString(response); @@ -1666,8 +1666,8 @@ public class RestCommandLineService { partitions = partitionList.getPartition().toArray(partitions); if (partitions.length == 0) { - String message = "Cannot find any deployed Partition. " - + "Please deploy a Partition using [" + CliConstants.PARTITION_DEPLOYMENT + "] command."; + String message = "Cannot find any deployed Partition. " + + "Please deploy a Partition using [" + CliConstants.PARTITION_DEPLOYMENT + "] command."; if (logger.isDebugEnabled()) { logger.debug(message); } @@ -1690,8 +1690,8 @@ public class RestCommandLineService { public void listAutoscalePolicies() throws CommandException { DefaultHttpClient httpClient = new DefaultHttpClient(); try { - HttpResponse response = restClient.doGet(httpClient, restClient.getBaseURL() - + listAutoscalePolicyRestEndPoint); + HttpResponse response = restClient.doGet(httpClient, restClient.getBaseURL() + + listAutoscalePolicyRestEndPoint); String responseCode = "" + response.getStatusLine().getStatusCode(); @@ -1733,10 +1733,10 @@ public class RestCommandLineService { policyArry = policyList.getAutoscalePolicy().toArray(policyArry); if (policyArry.length == 0) { - String message = "Cannot find any deployed auto-scaling policy. " - + "Please deploy a policy using [" + CliConstants.AUTOSCALING_POLICY_DEPLOYMENT + "] command."; + String message = "Cannot find any deployed auto-scaling policy. " + + "Please deploy a policy using [" + CliConstants.AUTOSCALING_POLICY_DEPLOYMENT + "] command."; if (logger.isDebugEnabled()) { - logger.debug(message); + logger.debug(message); } System.out.println(message); return; @@ -1756,8 +1756,8 @@ public class RestCommandLineService { public void listDeploymentPolicies() throws CommandException { DefaultHttpClient httpClient = new DefaultHttpClient(); try { - HttpResponse response = restClient.doGet(httpClient, restClient.getBaseURL() - + listDeploymentPolicyRestEndPoint); + HttpResponse response = restClient.doGet(httpClient, restClient.getBaseURL() + + listDeploymentPolicyRestEndPoint); String responseCode = "" + response.getStatusLine().getStatusCode(); @@ -1798,8 +1798,8 @@ public class RestCommandLineService { policyArry = policyList.getDeploymentPolicy().toArray(policyArry); if (policyArry.length == 0) { - String message = "Cannot find any deployed deployment policy. " - + "Please deploy a policy using [" + CliConstants.DEPLOYMENT_POLICY_DEPLOYMENT + "] command."; + String message = "Cannot find any deployed deployment policy. " + + "Please deploy a policy using [" + CliConstants.DEPLOYMENT_POLICY_DEPLOYMENT + "] command."; if (logger.isDebugEnabled()) { logger.debug(message); } @@ -1822,8 +1822,8 @@ public class RestCommandLineService { public void describeDeploymentPolicies(String id) throws CommandException { DefaultHttpClient httpClient = new DefaultHttpClient(); try { - HttpResponse response = restClient.doGet(httpClient, restClient.getBaseURL() - + listDeploymentPolicyRestEndPoint); + HttpResponse response = restClient.doGet(httpClient, restClient.getBaseURL() + + listDeploymentPolicyRestEndPoint); String responseCode = "" + response.getStatusLine().getStatusCode(); @@ -1870,8 +1870,8 @@ public class RestCommandLineService { public void describePartition(String id) throws CommandException { DefaultHttpClient httpClient = new DefaultHttpClient(); try { - HttpResponse response = restClient.doGet(httpClient, restClient.getBaseURL() - + listParitionRestEndPoint); + HttpResponse response = restClient.doGet(httpClient, restClient.getBaseURL() + + listParitionRestEndPoint); String responseCode = "" + response.getStatusLine().getStatusCode(); @@ -1912,8 +1912,8 @@ public class RestCommandLineService { public void describeAutoScalingPolicy(String id) throws CommandException { DefaultHttpClient httpClient = new DefaultHttpClient(); try { - HttpResponse response = restClient.doGet(httpClient, restClient.getBaseURL() - + listAutoscalePolicyRestEndPoint); + HttpResponse response = restClient.doGet(httpClient, restClient.getBaseURL() + + listAutoscalePolicyRestEndPoint); String responseCode = "" + response.getStatusLine().getStatusCode(); @@ -1942,11 +1942,11 @@ public class RestCommandLineService { } for(AutoscalePolicy policy : policyList.getAutoscalePolicy()) { - if(policy.getId().equalsIgnoreCase(id)) { - System.out.println("Autoscale policy is:"); - System.out.println(gson.toJson(policy)); - return; - } + if(policy.getId().equalsIgnoreCase(id)) { + System.out.println("Autoscale policy is:"); + System.out.println(gson.toJson(policy)); + return; + } } System.out.println("No matching Autoscale Policy found..."); @@ -2116,21 +2116,22 @@ public class RestCommandLineService { try { response = restClient.doGet(httpClient, restClient.getBaseURL() + serviceEndpoint); int responseCode = response.getStatusLine().getStatusCode(); - String resultString = getHttpResponseString(response); - - if (resultString == null) { - return null; - } - - GsonBuilder gsonBuilder = new GsonBuilder(); - Gson gson = gsonBuilder.create(); if (responseCode != 200) { - ExceptionMapper exception = gson.fromJson(resultString, ExceptionMapper.class); - System.out.println(exception); - return null; + String resultString = getHttpResponseString(response); + if(StringUtils.isBlank(resultString)) { + return null; + } else { + GsonBuilder gsonBuilder = new GsonBuilder(); + Gson gson = gsonBuilder.create(); + ExceptionMapper exception = gson.fromJson(resultString, ExceptionMapper.class); + if(exception != null) { + System.out.println(exception); + } + return gson.fromJson(resultString, _class); + } } - return gson.fromJson(resultString, _class); + return null; } catch (Exception e) { handleException(String.format("Error in listing %s", entityName), e); return null; @@ -2186,30 +2187,30 @@ public class RestCommandLineService { } } - public void sync(String alias) throws CommandException { - DefaultHttpClient httpClient = new DefaultHttpClient(); - try { - HttpResponse response = restClient.doPost(httpClient, restClient.getBaseURL() + syncEndPoint, alias); + public void sync(String alias) throws CommandException { + DefaultHttpClient httpClient = new DefaultHttpClient(); + try { + HttpResponse response = restClient.doPost(httpClient, restClient.getBaseURL() + syncEndPoint, alias); - String responseCode = "" + response.getStatusLine().getStatusCode(); + String responseCode = "" + response.getStatusLine().getStatusCode(); - if (responseCode.equals(CliConstants.RESPONSE_OK)) { - System.out.format("Synchronizing repository for alias: %s%n", alias); - return; - } else { - GsonBuilder gsonBuilder = new GsonBuilder(); - Gson gson = gsonBuilder.create(); - String resultString = getHttpResponseString(response); - ExceptionMapper exception = gson.fromJson(resultString, ExceptionMapper.class); - System.out.println(exception); - } + if (responseCode.equals(CliConstants.RESPONSE_OK)) { + System.out.format("Synchronizing repository for alias: %s%n", alias); + return; + } else { + GsonBuilder gsonBuilder = new GsonBuilder(); + Gson gson = gsonBuilder.create(); + String resultString = getHttpResponseString(response); + ExceptionMapper exception = gson.fromJson(resultString, ExceptionMapper.class); + System.out.println(exception); + } - } catch (Exception e) { - handleException("Exception when synchronizing repository for alias: " + alias, e); - } finally { - httpClient.getConnectionManager().shutdown(); - } - } + } catch (Exception e) { + handleException("Exception when synchronizing repository for alias: " + alias, e); + } finally { + httpClient.getConnectionManager().shutdown(); + } + } // This class convert JSON string to deploymentpolicylist object private class DeploymentPolicyList { @@ -2344,19 +2345,19 @@ public class RestCommandLineService { // This will return access url from a given cartridge private String getAccessURLs(Cartridge cartridge) { - PortMapping[] portMappings = cartridge.getPortMappings(); - StringBuilder urlBuilder = new StringBuilder(); + PortMapping[] portMappings = cartridge.getPortMappings(); + StringBuilder urlBuilder = new StringBuilder(); for (PortMapping portMapping : portMappings) { - String url = portMapping.getProtocol()+"://"+ cartridge.getHostName() + ":" + portMapping.getProxyPort() + "/"; - urlBuilder.append(url).append(", "); - } + String url = portMapping.getProtocol()+"://"+ cartridge.getHostName() + ":" + portMapping.getProxyPort() + "/"; + urlBuilder.append(url).append(", "); + } return urlBuilder.toString(); } // This method gives the HTTP response string - private String getHttpResponseString (HttpResponse response) { + private String getHttpResponseString (HttpResponse response) { try { BufferedReader reader = new BufferedReader(new InputStreamReader((response.getEntity().getContent()))); @@ -2395,73 +2396,73 @@ public class RestCommandLineService { System.out.println(message); throw new CommandException(message, e); } - + /** * To map RestApiException of back-end. * @author nirmal * */ public class ExceptionMapper { - @Override - public String toString() { - return Error.toString(); - } + @Override + public String toString() { + return Error.toString(); + } + + private ErrorWrapper Error; - private ErrorWrapper Error; + public ErrorWrapper getError() { + return Error; + } - public ErrorWrapper getError() { - return Error; - } + public void setError(ErrorWrapper error) { + Error = error; + } - public void setError(ErrorWrapper error) { - Error = error; - } - } - + public class ErrorWrapper { - private String errorCode; - private String errorMessage; + private String errorCode; + private String errorMessage; public String getErrorCode() { - return errorCode; - } + return errorCode; + } public void setErrorCode(String errorCode) { - this.errorCode = errorCode; - } + this.errorCode = errorCode; + } public String getErrorMessage() { - return errorMessage; - } + return errorMessage; + } public void setErrorMessage(String errorMessage) { - this.errorMessage = errorMessage; - } + this.errorMessage = errorMessage; + } @Override - public String toString() { - return "Exception [errorCode=" + errorCode - + ", errorMessage=" + errorMessage + "]"; - } - - } - - // This class is to convert JSON string to Cartridge object - public class CartridgeWrapper { - private Cartridge cartridge; - - public Cartridge getCartridge() { - return cartridge; - } - - public void setCartridge(Cartridge cartridge) { - this.cartridge = cartridge; - } - - public CartridgeWrapper() { - } - } + public String toString() { + return "Exception [errorCode=" + errorCode + + ", errorMessage=" + errorMessage + "]"; + } + + } + + // This class is to convert JSON string to Cartridge object + public class CartridgeWrapper { + private Cartridge cartridge; + + public Cartridge getCartridge() { + return cartridge; + } + + public void setCartridge(Cartridge cartridge) { + this.cartridge = cartridge; + } + + public CartridgeWrapper() { + } + } public boolean isMultiTenant(String type) throws CommandException { DefaultHttpClient httpClient = new DefaultHttpClient();
