Repository: stratos Updated Branches: refs/heads/master ef816f063 -> ce277f44d
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/ce277f44 Tree: http://git-wip-us.apache.org/repos/asf/stratos/tree/ce277f44 Diff: http://git-wip-us.apache.org/repos/asf/stratos/diff/ce277f44 Branch: refs/heads/master Commit: ce277f44d58ddd9f3a299514cabd65fedff1003b Parents: ef816f0 Author: Imesh Gunaratne <[email protected]> Authored: Fri Oct 10 23:35:56 2014 +0530 Committer: Imesh Gunaratne <[email protected]> Committed: Fri Oct 10 23:35:56 2014 +0530 ---------------------------------------------------------------------- .../stratos/cli/RestCommandLineService.java | 535 ++++++++++--------- 1 file changed, 268 insertions(+), 267 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/stratos/blob/ce277f44/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 631510f..a02effe 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); } @@ -319,15 +319,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; } @@ -379,8 +379,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); } @@ -396,8 +396,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); } @@ -427,15 +427,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; } @@ -471,10 +471,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; } @@ -484,16 +484,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); @@ -524,7 +524,7 @@ public class RestCommandLineService { data[10] = cartridge.getRepoURL() != null ? cartridge.getRepoURL() : ""; } return data; - + } }; @@ -559,18 +559,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; } @@ -588,8 +588,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()); @@ -602,22 +602,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); @@ -625,9 +625,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>(); @@ -643,8 +643,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); @@ -682,16 +682,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; } @@ -706,25 +706,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(); @@ -739,9 +739,9 @@ public class RestCommandLineService { break; } - printLBs(resultStringCluster); - } - + printLBs(resultStringCluster); + } + } catch (Exception e) { handleException("Exception in listing subscribe cartridges", e); } finally { @@ -749,18 +749,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; } @@ -773,37 +773,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"); @@ -820,21 +820,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(); @@ -918,7 +918,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, @@ -950,7 +950,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, @@ -1021,8 +1021,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(); @@ -1030,9 +1030,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) { @@ -1062,7 +1062,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(); @@ -1086,8 +1086,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(); @@ -1115,7 +1115,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(); @@ -1142,8 +1142,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(); @@ -1170,8 +1170,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(); @@ -1198,8 +1198,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); @@ -1242,8 +1242,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); } @@ -1267,7 +1267,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); @@ -1308,7 +1308,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); } @@ -1338,13 +1338,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) { @@ -1414,8 +1414,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); @@ -1443,8 +1443,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(); @@ -1471,8 +1471,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(); @@ -1499,8 +1499,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(); @@ -1527,8 +1527,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(); @@ -1595,8 +1595,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(); @@ -1623,8 +1623,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); @@ -1665,8 +1665,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); } @@ -1689,8 +1689,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(); @@ -1732,10 +1732,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; @@ -1755,8 +1755,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(); @@ -1797,8 +1797,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); } @@ -1821,8 +1821,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(); @@ -1869,8 +1869,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(); @@ -1911,8 +1911,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(); @@ -1941,11 +1941,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..."); @@ -2115,21 +2115,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; @@ -2185,30 +2186,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 { @@ -2343,19 +2344,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()))); @@ -2394,73 +2395,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();
