You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@stratos.apache.org by ma...@apache.org on 2013/12/11 09:31:57 UTC

[05/21] git commit: RESTful unsubscribe command implementation in CLI

RESTful unsubscribe command implementation in CLI


Project: http://git-wip-us.apache.org/repos/asf/incubator-stratos/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-stratos/commit/adf766c8
Tree: http://git-wip-us.apache.org/repos/asf/incubator-stratos/tree/adf766c8
Diff: http://git-wip-us.apache.org/repos/asf/incubator-stratos/diff/adf766c8

Branch: refs/heads/master
Commit: adf766c83ffe0053d06b1dcc576b338bc37d84ae
Parents: 7bf4824
Author: Manula Thantriwatte <ma...@apache.org>
Authored: Fri Dec 6 19:38:48 2013 +0530
Committer: Manula Thantriwatte <ma...@apache.org>
Committed: Fri Dec 6 19:38:48 2013 +0530

----------------------------------------------------------------------
 .../java/org/apache/stratos/cli/RestClient.java |  3 ++
 .../stratos/cli/RestCommandLineService.java     | 44 +++++++++++++-------
 .../cli/commands/UnsubscribeCommand.java        |  4 +-
 3 files changed, 36 insertions(+), 15 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/adf766c8/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 eadd031..bb91d09 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
@@ -59,6 +59,9 @@ public class RestClient implements GenericRestClient{
             httpClient = (DefaultHttpClient) WebClientWrapper.wrapClient(httpClient);
             HttpResponse response = httpClient.execute(postRequest);
 
+            if (response.getStatusLine().getStatusCode() == 204) {
+                return "";
+            }
             if (response.getStatusLine().getStatusCode() != 200) {
                 throw new RuntimeException("Failed : HTTP error code : " + response.getStatusLine().getStatusCode());
             }

http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/adf766c8/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 62d78d0..608338c 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
@@ -29,6 +29,8 @@ import org.apache.commons.lang3.StringUtils;
 import org.apache.stratos.cli.exception.CommandException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+
+import java.rmi.RemoteException;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
@@ -47,6 +49,7 @@ public class RestCommandLineService {
     private final String listSubscribedCartridgesRestEndpoint = "/stratos/admin/cartridge/list/subscribed";
     private final String subscribCartridgeRestEndpoint = "/stratos/admin/cartridge/subscribe";
     private final String addTenantEndPoint = "/stratos/admin/tenant";
+    private final String unsubscribeTenantEndPoint = "/stratos/admin/cartridge/unsubscribe";
 
     private static class SingletonHolder {
 		private final static RestCommandLineService INSTANCE = new RestCommandLineService();
@@ -369,24 +372,37 @@ public class RestCommandLineService {
     }
 
     public void addTenant(String admin, String firstName, String lastaName, String password, String domain, String email, String active) {
-        TenantInfoBean tenantInfo = new TenantInfoBean();
-        tenantInfo.setAdmin(admin);
-        tenantInfo.setFirstname(firstName);
-        tenantInfo.setLastname(lastaName);
-        tenantInfo.setAdminPassword(password);
-        tenantInfo.setTenantDomain(domain);
-        tenantInfo.setEmail(email);
-        tenantInfo.setActive(active);
+        try {
+            TenantInfoBean tenantInfo = new TenantInfoBean();
+            tenantInfo.setAdmin(admin);
+            tenantInfo.setFirstname(firstName);
+            tenantInfo.setLastname(lastaName);
+            tenantInfo.setAdminPassword(password);
+            tenantInfo.setTenantDomain(domain);
+            tenantInfo.setEmail(email);
+            tenantInfo.setActive(active);
 
-        GsonBuilder gsonBuilder = new GsonBuilder();
-        Gson gson = gsonBuilder.create();
+            GsonBuilder gsonBuilder = new GsonBuilder();
+            Gson gson = gsonBuilder.create();
+
+            String jsonString = gson.toJson(tenantInfo, TenantInfoBean.class);
+            String completeJsonString = "{\"tenantInfoBean\":" + jsonString + "}";
 
-        String jsonString = gson.toJson(tenantInfo, TenantInfoBean.class);
-        String completeJsonString = "{\"tenantInfoBean\":" + jsonString + "}";
+            String result = restClientService.doPost(restClientService.getUrl() + addTenantEndPoint, completeJsonString, restClientService.getUsername(), restClientService.getPassword());
 
-        String result = restClientService.doPost(restClientService.getUrl() + addTenantEndPoint, completeJsonString, restClientService.getUsername(), restClientService.getPassword());
+            System.out.println(result);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
 
-        System.out.println(result);
+    public void unsubscribe(String alias) throws CommandException {
+        try {
+            restClientService.doPost(restClientService.getUrl() + unsubscribeTenantEndPoint, alias, restClientService.getUsername(), restClientService.getPassword());
+            System.out.println("You have successfully unsubscribed " + alias);
+        } catch ( Exception e) {
+            e.printStackTrace();
+        }
     }
 
     private class CartridgeList  {

http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/adf766c8/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/commands/UnsubscribeCommand.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/commands/UnsubscribeCommand.java b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/commands/UnsubscribeCommand.java
index 9979340..8ef7d69 100644
--- a/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/commands/UnsubscribeCommand.java
+++ b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/commands/UnsubscribeCommand.java
@@ -24,6 +24,7 @@ import org.apache.commons.cli.GnuParser;
 import org.apache.commons.cli.Option;
 import org.apache.commons.cli.Options;
 import org.apache.commons.cli.ParseException;
+import org.apache.stratos.cli.RestCommandLineService;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.apache.stratos.cli.Command;
@@ -106,7 +107,8 @@ public class UnsubscribeCommand implements Command<StratosCommandContext> {
 				}
 				if (force || context.getApplication().getConfirmation("Are you sure you want to unsubscribe?")) {
 					System.out.format("Unsubscribing the cartridge %s%n", alias);
-					CommandLineService.getInstance().unsubscribe(alias);
+					//CommandLineService.getInstance().unsubscribe(alias);
+                    RestCommandLineService.getInstance().unsubscribe(alias);
 				}
 				return CliConstants.SUCCESSFUL_CODE;
 			} catch (ParseException e) {