You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@stratos.apache.org by im...@apache.org on 2014/10/13 10:22:29 UTC

git commit: Fixing unsubscribe logic in CLI

Repository: stratos
Updated Branches:
  refs/heads/master be94843f7 -> 61e7c922c


Fixing unsubscribe logic in CLI


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

Branch: refs/heads/master
Commit: 61e7c922cfe0b1edeea8b1e9db33aec7a18d52ee
Parents: be94843
Author: Imesh Gunaratne <im...@apache.org>
Authored: Mon Oct 13 13:52:15 2014 +0530
Committer: Imesh Gunaratne <im...@apache.org>
Committed: Mon Oct 13 13:52:15 2014 +0530

----------------------------------------------------------------------
 .../java/org/apache/stratos/cli/RestClient.java | 34 +-------
 .../stratos/cli/RestCommandLineService.java     | 89 +++++++++-----------
 .../org/apache/stratos/cli/utils/CliUtils.java  | 63 +++++++++++---
 3 files changed, 96 insertions(+), 90 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/stratos/blob/61e7c922/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 3228f0f..a2560eb 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
@@ -232,32 +232,6 @@ public class RestClient implements GenericRestClient {
         }
     }
 
-    private void printError(HttpResponse response) {
-        String resultString = CliUtils.getHttpResponseString(response);
-        if (StringUtils.isNotBlank(resultString)) {
-            // Response body found, try to extract exception information
-            boolean exceptionMapperInstanceFound = false;
-            try {
-                GsonBuilder gsonBuilder = new GsonBuilder();
-                Gson gson = gsonBuilder.create();
-                ExceptionMapper exception = gson.fromJson(resultString, ExceptionMapper.class);
-                if (exception != null) {
-                    System.out.println(exception);
-                    exceptionMapperInstanceFound = true;
-                }
-            } catch (Exception ignore) {
-                // Could not find an ExceptionMapper instance
-            } finally {
-                if (!exceptionMapperInstanceFound) {
-                    System.out.println(response.getStatusLine().toString());
-                }
-            }
-        } else {
-            // No response body found
-            System.out.println(response.getStatusLine().toString());
-        }
-    }
-
     private int executePost(String serviceEndpoint, String postBody) throws IOException {
         DefaultHttpClient httpClient = new DefaultHttpClient();
         try {
@@ -265,7 +239,7 @@ public class RestClient implements GenericRestClient {
 
             int responseCode = response.getStatusLine().getStatusCode();
             if (responseCode < 200 || responseCode >= 300) {
-                printError(response);
+                CliUtils.printError(response);
             }
             return responseCode;
         } finally {
@@ -285,7 +259,7 @@ public class RestClient implements GenericRestClient {
                 // Entity not found
                 return null;
             } else if (responseCode < 200 || responseCode >= 300) {
-                printError(response);
+                CliUtils.printError(response);
                 return null;
             } else {
                 String resultString = CliUtils.getHttpResponseString(response);
@@ -305,7 +279,7 @@ public class RestClient implements GenericRestClient {
 
             int responseCode = response.getStatusLine().getStatusCode();
             if (responseCode < 200 || responseCode >= 300) {
-                printError(response);
+                CliUtils.printError(response);
             }
             return responseCode;
         } finally {
@@ -320,7 +294,7 @@ public class RestClient implements GenericRestClient {
 
             int responseCode = response.getStatusLine().getStatusCode();
             if (responseCode < 200 || responseCode >= 300) {
-                printError(response);
+                CliUtils.printError(response);
             }
             return responseCode;
         } finally {

http://git-wip-us.apache.org/repos/asf/stratos/blob/61e7c922/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 03a050a..e089128 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
@@ -69,7 +69,7 @@ import com.google.gson.GsonBuilder;
 
 public class RestCommandLineService {
 
-    private static final Logger logger = LoggerFactory.getLogger(RestCommandLineService.class);
+    private static final Logger log = LoggerFactory.getLogger(RestCommandLineService.class);
 
     private RestClient restClient;
 
@@ -168,8 +168,8 @@ public class RestCommandLineService {
         try {
             initializeRestClient(serverURL, username, password);
 
-            if (logger.isDebugEnabled()) {
-                logger.debug("Initialized REST Client for user {}", username);
+            if (log.isDebugEnabled()) {
+                log.debug("Initialized REST Client for user {}", username);
             }
         } catch (AxisFault e) {
             System.out.println("Error connecting to the back-end");
@@ -197,17 +197,17 @@ public class RestCommandLineService {
         } catch (ConnectException e) {
             String message = "Could not connect to stratos manager";
             System.out.println(message);
-            logger.error(message, e);
+            log.error(message, e);
             return false;
         } catch (java.lang.NoSuchMethodError e) {
             String message = "Authentication failed!";
             System.out.println(message);
-            logger.error(message, e);
+            log.error(message, e);
             return false;
         } catch (Exception e) {
             String message = "An unknown error occurred: " + e.getMessage();
             System.out.println(message);
-            logger.error(message, e);
+            log.error(message, e);
             return false;
         } finally {
             httpClient.getConnectionManager().shutdown();
@@ -251,7 +251,7 @@ public class RestCommandLineService {
         } catch (Exception e) {
             String message = "Error in getting cartridge";
             System.out.println(message);
-            logger.error(message, e);
+            log.error(message, e);
         }
         return null;
     }
@@ -273,7 +273,7 @@ public class RestCommandLineService {
         } catch (Exception e) {
             String message = "Error in listing cartridges";
             System.out.println(message);
-            logger.error(message, e);
+            log.error(message, e);
             return null;
         }
     }
@@ -313,7 +313,7 @@ public class RestCommandLineService {
         } catch (Exception e) {
             String message = "Error in listing cartridges";
             System.out.println(message);
-            logger.error(message, e);
+            log.error(message, e);
         }
     }
 
@@ -340,7 +340,7 @@ public class RestCommandLineService {
         } catch (Exception e) {
             String message = "Error in describing cartridge: " + cartridgeType;
             System.out.println(message);
-            logger.error(message, e);
+            log.error(message, e);
         }
     }
 
@@ -411,7 +411,7 @@ public class RestCommandLineService {
         } catch (Exception e) {
             String message = "Error in listing cartridge subscriptions";
             System.out.println(message);
-            logger.error(message, e);
+            log.error(message, e);
         }
     }
 
@@ -467,7 +467,7 @@ public class RestCommandLineService {
         } catch (Exception e) {
             String message = "Error in getting cartridge subscription";
             System.out.println(message);
-            logger.error(message, e);
+            log.error(message, e);
         }
     }
 
@@ -526,7 +526,7 @@ public class RestCommandLineService {
         } catch (Exception e) {
             String message = "Error in getting load balancer ip list";
             System.out.println(message);
-            logger.error(message, e);
+            log.error(message, e);
             return null;
         }
     }
@@ -543,8 +543,8 @@ public class RestCommandLineService {
             }
 
             if (members.length == 0) {
-                if (logger.isDebugEnabled()) {
-                    logger.debug("No members found");
+                if (log.isDebugEnabled()) {
+                    log.debug("No members found");
                 }
                 System.out.println("No members found for the corresponding cluster for type " + cartridgeType
                         + ", alias " + alias);
@@ -592,7 +592,7 @@ public class RestCommandLineService {
         } catch (Exception e) {
             String message = "Error in listing members";
             System.out.println(message);
-            logger.error(message, e);
+            log.error(message, e);
         } finally {
             httpClient.getConnectionManager().shutdown();
         }
@@ -627,7 +627,7 @@ public class RestCommandLineService {
         } catch (Exception e) {
             String message = "Error in listing members";
             System.out.println(message);
-            logger.error(message, e);
+            log.error(message, e);
             return null;
         }
     }
@@ -665,8 +665,8 @@ public class RestCommandLineService {
         members = cluster.getMember().toArray(members);
 
         if (members.length == 0) {
-            if (logger.isDebugEnabled()) {
-                logger.debug("No subscribed cartridges found");
+            if (log.isDebugEnabled()) {
+                log.debug("No subscribed cartridges found");
             }
             System.out.println("There are no subscribed cartridges");
             return;
@@ -1138,7 +1138,7 @@ public class RestCommandLineService {
         } catch (Exception e) {
             String message = "Error in listing users";
             System.out.println(message);
-            logger.error(message, e);
+            log.error(message, e);
         }
     }
 
@@ -1171,7 +1171,7 @@ public class RestCommandLineService {
         } catch (Exception e) {
             String message = "Error in listing users";
             System.out.println(message);
-            logger.error(message, e);
+            log.error(message, e);
         }
     }
 
@@ -1180,25 +1180,18 @@ public class RestCommandLineService {
         DefaultHttpClient httpClient = new DefaultHttpClient();
         try {
             HttpResponse response = restClient.doPost(httpClient, restClient.getBaseURL() + ENDPOINT_UNSUBSCRIBE_CARTRIDGE_OF_TENANT, alias);
+            int responseCode = response.getStatusLine().getStatusCode();
 
-            String responseCode = "" + response.getStatusLine().getStatusCode();
-
-            GsonBuilder gsonBuilder = new GsonBuilder();
-            Gson gson = gsonBuilder.create();
-
-            if (responseCode.equals(CliConstants.RESPONSE_OK)) {
-                System.out.println("You have successfully unsubscribed " + alias + " cartridge");
+            if ((responseCode >= 200) || (responseCode < 300)) {
+                System.out.println("You have successfully unsubscribed from cartridge: " + alias);
                 return;
             } else {
-                String resultString = CliUtils.getHttpResponseString(response);
-                ExceptionMapper exception = gson.fromJson(resultString, ExceptionMapper.class);
-                System.out.println(exception);
+                CliUtils.printError(response);
             }
-
         } catch (Exception e) {
-            String message = "Error in un-subscribing cartridge";
+            String message = "Error in unsubscribing cartridge: " + alias;
             System.out.println(message);
-            logger.error(message, e);
+            log.error(message, e);
         } finally {
             httpClient.getConnectionManager().shutdown();
         }
@@ -1268,7 +1261,7 @@ public class RestCommandLineService {
         } catch (Exception e) {
             String message = "Error in listing services";
             System.out.println(message);
-            logger.error(message, e);
+            log.error(message, e);
         }
     }
 
@@ -1307,7 +1300,7 @@ public class RestCommandLineService {
         } catch (Exception e) {
             String message = "Error in listing partitions";
             System.out.println(message);
-            logger.error(message, e);
+            log.error(message, e);
         }
     }
 
@@ -1339,7 +1332,7 @@ public class RestCommandLineService {
         } catch (Exception e) {
             String message = "Error in listing autoscaling policies";
             System.out.println(message);
-            logger.error(message, e);
+            log.error(message, e);
         }
     }
 
@@ -1371,7 +1364,7 @@ public class RestCommandLineService {
         } catch (Exception e) {
             String message = "Error in listing deployment policies";
             System.out.println(message);
-            logger.error(message, e);
+            log.error(message, e);
         }
     }
 
@@ -1396,7 +1389,7 @@ public class RestCommandLineService {
         } catch (Exception e) {
             String message = "Error in describing deployment policy: " + id;
             System.out.println(message);
-            logger.error(message, e);
+            log.error(message, e);
         }
     }
 
@@ -1421,7 +1414,7 @@ public class RestCommandLineService {
         } catch (Exception e) {
             String message = "Error in describing partition: " + id;
             System.out.println(message);
-            logger.error(message, e);
+            log.error(message, e);
         }
     }
 
@@ -1446,7 +1439,7 @@ public class RestCommandLineService {
         } catch (Exception e) {
             String message = "Error in describing autoscaling policy: " + id;
             System.out.println(message);
-            logger.error(message, e);
+            log.error(message, e);
         }
     }
 
@@ -1478,7 +1471,7 @@ public class RestCommandLineService {
         } catch (Exception e) {
             String message = "Error in listing kubernetes groups";
             System.out.println(message);
-            logger.error(message, e);
+            log.error(message, e);
         }
     }
 
@@ -1516,7 +1509,7 @@ public class RestCommandLineService {
         } catch (Exception e) {
             String message = "Error in listing kubernetes hosts";
             System.out.println(message);
-            logger.error(message, e);
+            log.error(message, e);
         }
     }
 
@@ -1552,7 +1545,7 @@ public class RestCommandLineService {
         } catch (Exception e) {
             String message = "Error in synchronizing artifacts for cartridge subscription alias: " + cartridgeAlias;
             System.out.println(message);
-            logger.error(message, e);
+            log.error(message, e);
         } finally {
             httpClient.getConnectionManager().shutdown();
         }
@@ -1710,14 +1703,14 @@ public class RestCommandLineService {
 
     // This is for handle exception
     private void handleException(String key, Exception e, Object... args) throws CommandException {
-        if (logger.isDebugEnabled()) {
-            logger.debug("Displaying message for {}. Exception thrown is {}", key, e.getClass());
+        if (log.isDebugEnabled()) {
+            log.debug("Displaying message for {}. Exception thrown is {}", key, e.getClass());
         }
 
         String message = CliUtils.getMessage(key, args);
 
-        if (logger.isErrorEnabled()) {
-            logger.error(message);
+        if (log.isErrorEnabled()) {
+            log.error(message);
         }
 
         System.out.println(message);

http://git-wip-us.apache.org/repos/asf/stratos/blob/61e7c922/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/utils/CliUtils.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/utils/CliUtils.java b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/utils/CliUtils.java
index 1733f22..1304b69 100644
--- a/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/utils/CliUtils.java
+++ b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/utils/CliUtils.java
@@ -18,7 +18,13 @@
  */
 package org.apache.stratos.cli.utils;
 
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 import org.apache.http.HttpResponse;
+import org.apache.stratos.cli.exception.ExceptionMapper;
 
 import java.io.BufferedReader;
 import java.io.FileReader;
@@ -29,8 +35,9 @@ import java.text.MessageFormat;
 import java.util.ResourceBundle;
 
 public class CliUtils {
-	
-	private static final ResourceBundle BUNDLE = ResourceBundle.getBundle("Resources");
+    private static Log log = LogFactory.getLog(CliUtils.class);
+
+    private static final ResourceBundle BUNDLE = ResourceBundle.getBundle("Resources");
 
 	public static <T> void printTable(T[] data, RowMapper<T> mapper, String... headers) {
 		if (data == null) {
@@ -123,25 +130,57 @@ public class CliUtils {
      */
     public static String getHttpResponseString (HttpResponse response) {
         try {
-            BufferedReader reader = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));
-
             String output;
             String result = "";
 
-            while ((output = reader.readLine()) != null) {
-                result += output;
+            if((response != null) && (response.getEntity() != null) && (response.getEntity().getContent() != null)) {
+                BufferedReader reader = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));
+                while ((output = reader.readLine()) != null) {
+                    result += output;
+                }
             }
-
             return result;
         } catch (SocketException e) {
-            System.out.println("Connection problem");
-            return null;
-        } catch (NullPointerException e) {
-            System.out.println("Null value return from server");
+            String message = "A connection error occurred while reading response message: " + e.getMessage();
+            System.out.println(message);
+            log.error(message, e);
             return null;
         } catch (IOException e) {
-            System.out.println("IO error");
+            String message = "An IO error occurred while reading response message: " + e.getMessage();
+            System.out.println(message);
+            log.error(message, e);
             return null;
+        } catch(Exception e) {
+            String message = "An unknown error occurred while reading response message: " + e.getMessage();
+            System.out.println(message);
+            log.error(message, e);
+            return null;
+        }
+    }
+
+    public static void printError(HttpResponse response) {
+        String resultString = CliUtils.getHttpResponseString(response);
+        if (StringUtils.isNotBlank(resultString)) {
+            // Response body found, try to extract exception information
+            boolean exceptionMapperInstanceFound = false;
+            try {
+                GsonBuilder gsonBuilder = new GsonBuilder();
+                Gson gson = gsonBuilder.create();
+                ExceptionMapper exception = gson.fromJson(resultString, ExceptionMapper.class);
+                if (exception != null) {
+                    System.out.println(exception);
+                    exceptionMapperInstanceFound = true;
+                }
+            } catch (Exception ignore) {
+                // Could not find an ExceptionMapper instance
+            } finally {
+                if (!exceptionMapperInstanceFound) {
+                    System.out.println(response.getStatusLine().toString());
+                }
+            }
+        } else {
+            // No response body found
+            System.out.println(response.getStatusLine().toString());
         }
     }
 }