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/11 04:48:46 UTC

git commit: Improving error handling in CLI with respect to HTTP status codes

Repository: stratos
Updated Branches:
  refs/heads/master 73fdb984e -> 869f818ff


Improving error handling in CLI with respect to HTTP status codes


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

Branch: refs/heads/master
Commit: 869f818ff354a44f0f80607b7541ca8eb4285ca9
Parents: 73fdb98
Author: Imesh Gunaratne <im...@apache.org>
Authored: Sat Oct 11 08:18:36 2014 +0530
Committer: Imesh Gunaratne <im...@apache.org>
Committed: Sat Oct 11 08:18:36 2014 +0530

----------------------------------------------------------------------
 .../apache/stratos/cli/GenericRestClient.java   |   2 +-
 .../java/org/apache/stratos/cli/RestClient.java | 313 +++++++++++-----
 .../stratos/cli/RestCommandLineService.java     | 353 ++++---------------
 .../stratos/cli/exception/ErrorWrapper.java     |  48 +++
 .../stratos/cli/exception/ExceptionMapper.java  |  43 +++
 .../stratos/cli/utils/CommandLineUtils.java     |  33 ++
 6 files changed, 411 insertions(+), 381 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/stratos/blob/869f818f/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/GenericRestClient.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/GenericRestClient.java b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/GenericRestClient.java
index 5b843a3..0c87c19 100644
--- a/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/GenericRestClient.java
+++ b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/GenericRestClient.java
@@ -49,7 +49,7 @@ public interface GenericRestClient {
      */
     public HttpResponse doGet(DefaultHttpClient httpClient, String resourcePath) throws Exception;
 
-    public HttpResponse doDelete(DefaultHttpClient httpClient, String resourcePath);
+    public HttpResponse doDelete(DefaultHttpClient httpClient, String resourcePath) throws IOException;
 
     public HttpResponse doPut(DefaultHttpClient httpClient, String resourcePath, String jsonParamString) throws IOException;
 }

http://git-wip-us.apache.org/repos/asf/stratos/blob/869f818f/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 899d666..48488e5 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
@@ -18,12 +18,10 @@
  */
 package org.apache.stratos.cli;
 
-import java.io.IOException;
-import java.io.UnsupportedEncodingException;
-import java.net.ConnectException;
-
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import org.apache.commons.lang3.StringUtils;
 import org.apache.http.HttpResponse;
-import org.apache.http.client.ClientProtocolException;
 import org.apache.http.client.methods.HttpDelete;
 import org.apache.http.client.methods.HttpGet;
 import org.apache.http.client.methods.HttpPost;
@@ -32,8 +30,16 @@ import org.apache.http.entity.StringEntity;
 import org.apache.http.impl.client.DefaultHttpClient;
 import org.apache.http.params.HttpConnectionParams;
 import org.apache.http.params.HttpParams;
+import org.apache.stratos.cli.exception.ExceptionMapper;
+import org.apache.stratos.cli.utils.CommandLineUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+
+public class RestClient implements GenericRestClient {
 
-public class RestClient implements GenericRestClient{
+    private static final Logger logger = LoggerFactory.getLogger(RestClient.class);
 
     private String baseURL;
     private String username;
@@ -48,120 +54,83 @@ public class RestClient implements GenericRestClient{
     }
 
     public String getBaseURL() {
-		return baseURL;
-	}
+        return baseURL;
+    }
 
-	/**
+    /**
      * Handle http post request. Return String
      *
-     * @param  httpClient
-     *              This should be httpClient which used to connect to rest endpoint
-     * @param resourcePath
-     *              This should be REST endpoint
-     * @param jsonParamString
-     *              The json string which should be executed from the post request
+     * @param httpClient      This should be httpClient which used to connect to rest endpoint
+     * @param resourcePath    This should be REST endpoint
+     * @param jsonParamString The json string which should be executed from the post request
      * @return The HttpResponse
-     * @throws org.apache.http.client.ClientProtocolException and IOException
-     *             if any errors occur when executing the request
+     * @throws IOException if any errors occur when executing the request
      */
     public HttpResponse doPost(DefaultHttpClient httpClient, String resourcePath, String jsonParamString)
-            throws ClientProtocolException, ConnectException {
-        try {
-            HttpPost postRequest = new HttpPost(resourcePath);
+            throws IOException {
+        HttpPost postRequest = new HttpPost(resourcePath);
 
-            StringEntity input = new StringEntity(jsonParamString);
-            input.setContentType("application/json");
-            postRequest.setEntity(input);
-
-            String userPass = username + ":" + password;
-            String basicAuth = "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary(userPass.getBytes("UTF-8"));
-            postRequest.addHeader("Authorization", basicAuth);
+        StringEntity input = new StringEntity(jsonParamString);
+        input.setContentType("application/json");
+        postRequest.setEntity(input);
 
-            httpClient = (DefaultHttpClient) WebClientWrapper.wrapClient(httpClient);
+        String userPass = username + ":" + password;
+        String basicAuth = "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary(userPass.getBytes("UTF-8"));
+        postRequest.addHeader("Authorization", basicAuth);
 
-            HttpParams params = httpClient.getParams();
-            HttpConnectionParams.setConnectionTimeout(params, TIME_OUT_PARAM);
-            HttpConnectionParams.setSoTimeout(params, TIME_OUT_PARAM);
+        httpClient = (DefaultHttpClient) WebClientWrapper.wrapClient(httpClient);
 
-            HttpResponse response = httpClient.execute(postRequest);
+        HttpParams params = httpClient.getParams();
+        HttpConnectionParams.setConnectionTimeout(params, TIME_OUT_PARAM);
+        HttpConnectionParams.setSoTimeout(params, TIME_OUT_PARAM);
 
-            return response;
-        } catch (ClientProtocolException e) {
-            throw new ClientProtocolException();
-        } catch (ConnectException e) {
-            throw new ConnectException();
-        }
-        catch (IOException e) {
-            e.printStackTrace();
-            return null;
-        }
+        HttpResponse response = httpClient.execute(postRequest);
+        return response;
     }
 
     /**
      * Handle http get request. Return String
      *
-     * @param  httpClient
-     *              This should be httpClient which used to connect to rest endpoint
-     * @param resourcePath
-     *              This should be REST endpoint
+     * @param httpClient   This should be httpClient which used to connect to rest endpoint
+     * @param resourcePath This should be REST endpoint
      * @return The HttpResponse
      * @throws org.apache.http.client.ClientProtocolException and IOException
-     *             if any errors occur when executing the request
+     *                                                        if any errors occur when executing the request
      */
-    public HttpResponse doGet(DefaultHttpClient httpClient, String resourcePath) throws Exception{
-        try {
-            HttpGet getRequest = new HttpGet(resourcePath);
-            getRequest.addHeader("Content-Type", "application/json");
-
-            String userPass = username + ":" + password;
-            String basicAuth = "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary(userPass.getBytes("UTF-8"));
-            getRequest.addHeader("Authorization", basicAuth);
+    public HttpResponse doGet(DefaultHttpClient httpClient, String resourcePath) throws IOException {
+        HttpGet getRequest = new HttpGet(resourcePath);
+        getRequest.addHeader("Content-Type", "application/json");
 
-            httpClient = (DefaultHttpClient) WebClientWrapper.wrapClient(httpClient);
+        String userPass = username + ":" + password;
+        String basicAuth = "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary(userPass.getBytes("UTF-8"));
+        getRequest.addHeader("Authorization", basicAuth);
 
-            HttpParams params = httpClient.getParams();
-            HttpConnectionParams.setConnectionTimeout(params, TIME_OUT_PARAM);
-            HttpConnectionParams.setSoTimeout(params, TIME_OUT_PARAM);
+        httpClient = (DefaultHttpClient) WebClientWrapper.wrapClient(httpClient);
 
-            HttpResponse response = httpClient.execute(getRequest);
+        HttpParams params = httpClient.getParams();
+        HttpConnectionParams.setConnectionTimeout(params, TIME_OUT_PARAM);
+        HttpConnectionParams.setSoTimeout(params, TIME_OUT_PARAM);
 
-            return response;
-        } catch (ClientProtocolException e) {
-            throw new ClientProtocolException();
-        } catch (ConnectException e) {
-            throw new ConnectException();
-        } catch (IOException e) {
-            e.printStackTrace();
-            return null;
-        }
+        HttpResponse response = httpClient.execute(getRequest);
+        return response;
     }
 
-    public HttpResponse doDelete(DefaultHttpClient httpClient, String resourcePath) {
-        try {
-            HttpDelete httpDelete = new HttpDelete(resourcePath);
-            httpDelete.addHeader("Content-Type", "application/json");
-
-            String userPass = username + ":" + password;
-            String basicAuth = "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary(userPass.getBytes("UTF-8"));
-            httpDelete.addHeader("Authorization", basicAuth);
-
-            httpClient = (DefaultHttpClient) WebClientWrapper.wrapClient(httpClient);
+    public HttpResponse doDelete(DefaultHttpClient httpClient, String resourcePath) throws IOException {
+        HttpDelete httpDelete = new HttpDelete(resourcePath);
+        httpDelete.addHeader("Content-Type", "application/json");
 
-            HttpParams params = httpClient.getParams();
-            HttpConnectionParams.setConnectionTimeout(params, TIME_OUT_PARAM);
-            HttpConnectionParams.setSoTimeout(params, TIME_OUT_PARAM);
+        String userPass = username + ":" + password;
+        String basicAuth = "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary(userPass.getBytes("UTF-8"));
+        httpDelete.addHeader("Authorization", basicAuth);
 
-            HttpResponse response = httpClient.execute(httpDelete);
+        httpClient = (DefaultHttpClient) WebClientWrapper.wrapClient(httpClient);
 
-            return  response;
+        HttpParams params = httpClient.getParams();
+        HttpConnectionParams.setConnectionTimeout(params, TIME_OUT_PARAM);
+        HttpConnectionParams.setSoTimeout(params, TIME_OUT_PARAM);
 
-        } catch (ClientProtocolException e) {
-            e.printStackTrace();
-            return null;
-        } catch (IOException e) {
-            e.printStackTrace();
-            return null;
-        }
+        HttpResponse response = httpClient.execute(httpDelete);
+        return response;
     }
 
     public HttpResponse doPut(DefaultHttpClient httpClient, String resourcePath, String jsonParamString) throws IOException {
@@ -184,4 +153,166 @@ public class RestClient implements GenericRestClient{
         HttpResponse response = httpClient.execute(httpPutRequest);
         return response;
     }
+
+    public void deployEntity(String serviceEndpoint, String entityBody, String entityName) {
+        try {
+            int responseCode = executePost(serviceEndpoint, entityBody);
+            if (responseCode == 201) {
+                System.out.println(String.format("Successfully deployed %s", entityName));
+            }
+        } catch (Exception e) {
+            String message = String.format("Error in deploying %s", entityName);
+            System.out.println(message);
+            logger.error(message, e);
+        }
+    }
+
+    public void undeployEntity(String serviceEndpoint, String entityName, String entityId) {
+        try {
+            int responseCode = executeDelete(serviceEndpoint, entityId);
+            if (responseCode == 404) {
+                System.out.println(String.format("%s not found", StringUtils.capitalize(entityName)));
+            } else if (responseCode == 204) {
+                System.out.println(String.format("Successfully un-deployed %s", entityName));
+            }
+        } catch (Exception e) {
+            String message = String.format("Error in un-deploying %s", entityName);
+            System.out.println(message);
+            logger.error(message, e);
+        }
+    }
+
+    public void updateEntity(String serviceEndpoint, String entityBody, String entityName) {
+        try {
+            int responseCode = executePut(serviceEndpoint, entityBody);
+            if (responseCode == 404) {
+                System.out.println(String.format("%s not found", StringUtils.capitalize(entityName)));
+            } else if (responseCode == 200) {
+                System.out.println(String.format("Successfully updated %s", entityName));
+            }
+        } catch (Exception e) {
+            String message = String.format("Error in updating %s", entityName);
+            System.out.println(message);
+            logger.error(message, e);
+        }
+    }
+
+    public void deleteEntity(String serviceEndpoint, String identifier, String entityName) {
+        try {
+            int responseCode = executeDelete(serviceEndpoint, identifier);
+            if (responseCode == 200) {
+                System.out.println(String.format("Successfully deleted %s", entityName));
+            }
+        } catch (Exception e) {
+            String message = String.format("Error in deleting %s", entityName);
+            System.out.println(message);
+            logger.error(message, e);
+        }
+    }
+
+    public Object listEntity(String serviceEndpoint, Class responseJsonClass, String entityName) {
+        try {
+            return executeList(serviceEndpoint, responseJsonClass, entityName);
+        } catch (Exception e) {
+            String message = String.format("Error in listing %s", entityName);
+            System.out.println(message);
+            logger.error(message, e);
+            return null;
+        }
+    }
+
+    private void printError(HttpResponse response) {
+        String resultString = CommandLineUtils.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 {
+            HttpResponse response = doPost(httpClient, getBaseURL()
+                    + serviceEndpoint, postBody);
+
+            int responseCode = response.getStatusLine().getStatusCode();
+            if (responseCode < 200 || responseCode >= 300) {
+                printError(response);
+            }
+            return responseCode;
+        } finally {
+            httpClient.getConnectionManager().shutdown();
+        }
+    }
+
+    private Object executeList(String serviceEndpoint, Class responseJsonClass, String entityName) throws Exception {
+        DefaultHttpClient httpClient = new DefaultHttpClient();
+        HttpResponse response = null;
+
+        try {
+            response = doGet(httpClient, getBaseURL() + serviceEndpoint);
+            int responseCode = response.getStatusLine().getStatusCode();
+
+            if (responseCode < 200 || responseCode >= 300) {
+                printError(response);
+                return null;
+            } else {
+                String resultString = CommandLineUtils.getHttpResponseString(response);
+                GsonBuilder gsonBuilder = new GsonBuilder();
+                Gson gson = gsonBuilder.create();
+                return gson.fromJson(resultString, responseJsonClass);
+            }
+        } finally {
+            httpClient.getConnectionManager().shutdown();
+        }
+    }
+
+    private int executePut(String serviceEndpoint, String postBody) throws IOException {
+        DefaultHttpClient httpClient = new DefaultHttpClient();
+        try {
+            HttpResponse response = doPut(httpClient, getBaseURL()
+                    + serviceEndpoint, postBody);
+
+            int responseCode = response.getStatusLine().getStatusCode();
+            if (responseCode < 200 || responseCode >= 300) {
+                printError(response);
+            }
+            return responseCode;
+        } finally {
+            httpClient.getConnectionManager().shutdown();
+        }
+    }
+
+    private int executeDelete(String serviceEndpoint, String identifier) throws IOException {
+        DefaultHttpClient httpClient = new DefaultHttpClient();
+        try {
+            HttpResponse response = doDelete(httpClient, getBaseURL() + serviceEndpoint.replace("{id}", identifier));
+
+            int responseCode = response.getStatusLine().getStatusCode();
+            if (responseCode < 200 || responseCode >= 300) {
+                printError(response);
+            }
+            return responseCode;
+        } finally {
+            httpClient.getConnectionManager().shutdown();
+        }
+    }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/stratos/blob/869f818f/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 b98575d..fb5c2ff 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
@@ -63,6 +63,7 @@ import org.apache.stratos.cli.beans.kubernetes.KubernetesHostList;
 import org.apache.stratos.cli.beans.topology.Cluster;
 import org.apache.stratos.cli.beans.topology.Member;
 import org.apache.stratos.cli.exception.CommandException;
+import org.apache.stratos.cli.exception.ExceptionMapper;
 import org.apache.stratos.cli.utils.CliConstants;
 import org.apache.stratos.cli.utils.CommandLineUtils;
 import org.apache.stratos.cli.utils.RowMapper;
@@ -241,7 +242,7 @@ public class RestCommandLineService {
             response = restClient.doGet(httpClient, endpoint);
 
             String responseCode = "" + response.getStatusLine().getStatusCode();
-            String resultString = getHttpResponseString(response);
+            String resultString = CommandLineUtils.getHttpResponseString(response);
             if (resultString == null) {
                 return null;
             }
@@ -275,7 +276,7 @@ public class RestCommandLineService {
             response = restClient.doGet(httpClient, restClient.getBaseURL() + listAvailableCartridgesRestEndpoint);
 
             String responseCode = "" + response.getStatusLine().getStatusCode();
-            String resultString = getHttpResponseString(response);
+            String resultString = CommandLineUtils.getHttpResponseString(response);
 
             if (resultString == null) {
                 return null;
@@ -317,7 +318,7 @@ public class RestCommandLineService {
             HttpResponse response = restClient.doGet(httpClient, restClient.getBaseURL() + listAvailableCartridgesRestEndpoint);
 
             String responseCode = "" + response.getStatusLine().getStatusCode();
-            String resultString = getHttpResponseString(response);
+            String resultString = CommandLineUtils.getHttpResponseString(response);
             if (resultString == null) {
                 return;
             }
@@ -425,7 +426,7 @@ public class RestCommandLineService {
             HttpResponse response = restClient.doGet(httpClient, restClient.getBaseURL() + listAvailableCartridgesRestEndpoint);
 
             String responseCode = "" + response.getStatusLine().getStatusCode();
-            String resultString = getHttpResponseString(response);
+            String resultString = CommandLineUtils.getHttpResponseString(response);
             if (resultString == null) {
                 return;
             }
@@ -468,7 +469,7 @@ public class RestCommandLineService {
             HttpResponse response = restClient.doGet(httpClient, restClient.getBaseURL() + listSubscribedCartridgesRestEndpoint);
 
             String responseCode = "" + response.getStatusLine().getStatusCode();
-            String resultString = getHttpResponseString(response);
+            String resultString = CommandLineUtils.getHttpResponseString(response);
             GsonBuilder gsonBuilder = new GsonBuilder();
             Gson gson = gsonBuilder.create();
 
@@ -563,7 +564,7 @@ public class RestCommandLineService {
                     + listSubscribedCartridgeInfoRestEndpoint + alias);
 
             String responseCode = "" + response.getStatusLine().getStatusCode();
-            String resultString = getHttpResponseString(response);
+            String resultString = CommandLineUtils.getHttpResponseString(response);
 
             GsonBuilder gsonBuilder = new GsonBuilder();
             Gson gson = gsonBuilder.create();
@@ -647,7 +648,7 @@ public class RestCommandLineService {
                         + listClusterRestEndpoint + "lb");
 
                 String responseCode = "" + responseCluster.getStatusLine().getStatusCode();
-                String resultStringCluster = getHttpResponseString(responseCluster);
+                String resultStringCluster = CommandLineUtils.getHttpResponseString(responseCluster);
 
                 GsonBuilder gsonBuilder = new GsonBuilder();
                 Gson gson = gsonBuilder.create();
@@ -728,7 +729,7 @@ public class RestCommandLineService {
                         +"clusterId/"+ m.getLbClusterId());
 
                 String responseCode = "" + responseCluster.getStatusLine().getStatusCode();
-                String resultStringCluster = getHttpResponseString(responseCluster);
+                String resultStringCluster = CommandLineUtils.getHttpResponseString(responseCluster);
 
                 GsonBuilder gsonBuilder = new GsonBuilder();
                 Gson gson = gsonBuilder.create();
@@ -758,13 +759,13 @@ public class RestCommandLineService {
 
             Gson gson = new Gson();
             if ( ! responseCode.equals(CliConstants.RESPONSE_OK)) {
-                String resultString = getHttpResponseString(response);
+                String resultString = CommandLineUtils.getHttpResponseString(response);
                 ExceptionMapper exception = gson.fromJson(resultString, ExceptionMapper.class);
                 System.out.println(exception);
                 return null;
             }
 
-            Cluster cluster = getClusterObjectFromString(getHttpResponseString(response));
+            Cluster cluster = getClusterObjectFromString(CommandLineUtils.getHttpResponseString(response));
 
             if (cluster == null) {
                 System.out.println("No existing subscriptions found for alias " + alias);
@@ -848,13 +849,13 @@ public class RestCommandLineService {
             Gson gson = gsonBuilder.create();
 
             if ( ! responseCode.equals(CliConstants.RESPONSE_OK)) {
-                String resultString = getHttpResponseString(response);
+                String resultString = CommandLineUtils.getHttpResponseString(response);
                 ExceptionMapper exception = gson.fromJson(resultString, ExceptionMapper.class);
                 System.out.println(exception);
                 return null;
             }
 
-            String resultString = getHttpResponseString(response);
+            String resultString = CommandLineUtils.getHttpResponseString(response);
             if (resultString == null) {
                 System.out.println("Response content is empty");
                 return null;
@@ -889,13 +890,13 @@ public class RestCommandLineService {
             Gson gson = gsonBuilder.create();
 
             if ( ! responseCode.equals(CliConstants.RESPONSE_OK)) {
-                String resultString = getHttpResponseString(response);
+                String resultString = CommandLineUtils.getHttpResponseString(response);
                 ExceptionMapper exception = gson.fromJson(resultString, ExceptionMapper.class);
                 System.out.println(exception);
                 return null;
             }
 
-            String resultString = getHttpResponseString(response);
+            String resultString = CommandLineUtils.getHttpResponseString(response);
             if (resultString == null) {
                 System.out.println("Response content is empty");
                 return null;
@@ -959,13 +960,13 @@ public class RestCommandLineService {
             String responseCode = "" + response.getStatusLine().getStatusCode();
 
             if ( ! responseCode.equals(CliConstants.RESPONSE_OK)) {
-                String resultString = getHttpResponseString(response);
+                String resultString = CommandLineUtils.getHttpResponseString(response);
                 ExceptionMapper exception = gson.fromJson(resultString, ExceptionMapper.class);
                 System.out.println(exception);
                 return;
             }
 
-            String subscriptionOutput = getHttpResponseString(response);
+            String subscriptionOutput = CommandLineUtils.getHttpResponseString(response);
 
             if (subscriptionOutput == null) {
                 System.out.println("Error in response");
@@ -1030,7 +1031,7 @@ public class RestCommandLineService {
                 System.out.println("Tenant added successfully");
                 return;
             } else {
-                String resultString = getHttpResponseString(response);
+                String resultString = CommandLineUtils.getHttpResponseString(response);
                 ExceptionMapper exception = gson.fromJson(resultString, ExceptionMapper.class);
                 System.out.println(exception);
             }
@@ -1070,7 +1071,7 @@ public class RestCommandLineService {
                 System.out.println("User added successfully");
                 return;
             } else {
-                String resultString = getHttpResponseString(response);
+                String resultString = CommandLineUtils.getHttpResponseString(response);
                 ExceptionMapper exception = gson.fromJson(resultString, ExceptionMapper.class);
                 System.out.println(exception);
             }
@@ -1098,7 +1099,7 @@ public class RestCommandLineService {
                 System.out.println("You have succesfully delete " + tenantDomain + " tenant");
                 return;
             } else {
-                String resultString = getHttpResponseString(response);
+                String resultString = CommandLineUtils.getHttpResponseString(response);
                 ExceptionMapper exception = gson.fromJson(resultString, ExceptionMapper.class);
                 System.out.println(exception);
             }
@@ -1126,7 +1127,7 @@ public class RestCommandLineService {
                 System.out.println("You have succesfully deleted " + userName + " user");
                 return;
             } else {
-                String resultString = getHttpResponseString(response);
+                String resultString = CommandLineUtils.getHttpResponseString(response);
                 ExceptionMapper exception = gson.fromJson(resultString, ExceptionMapper.class);
                 System.out.println(exception);
             }
@@ -1154,7 +1155,7 @@ public class RestCommandLineService {
                 System.out.println("You have succesfully deactivate " + tenantDomain + " tenant");
                 return;
             } else {
-                String resultString = getHttpResponseString(response);
+                String resultString = CommandLineUtils.getHttpResponseString(response);
                 ExceptionMapper exception = gson.fromJson(resultString, ExceptionMapper.class);
                 System.out.println(exception);
             }
@@ -1182,7 +1183,7 @@ public class RestCommandLineService {
                 System.out.println("You have succesfully activate " + tenantDomain + " tenant");
                 return;
             } else {
-                String resultString = getHttpResponseString(response);
+                String resultString = CommandLineUtils.getHttpResponseString(response);
                 ExceptionMapper exception = gson.fromJson(resultString, ExceptionMapper.class);
                 System.out.println(exception);
             }
@@ -1202,7 +1203,7 @@ public class RestCommandLineService {
                     + listAllTenantRestEndPoint);
 
             String responseCode = "" + response.getStatusLine().getStatusCode();
-            String resultString = getHttpResponseString(response);
+            String resultString = CommandLineUtils.getHttpResponseString(response);
 
             GsonBuilder gsonBuilder = new GsonBuilder();
             Gson gson = gsonBuilder.create();
@@ -1270,7 +1271,7 @@ public class RestCommandLineService {
                     + listAllUserRestEndPoint);
 
             String responseCode = "" + response.getStatusLine().getStatusCode();
-            String resultString = getHttpResponseString(response);
+            String resultString = CommandLineUtils.getHttpResponseString(response);
 
             GsonBuilder gsonBuilder = new GsonBuilder();
             Gson gson = gsonBuilder.create();
@@ -1342,7 +1343,7 @@ public class RestCommandLineService {
                 System.out.println("You have successfully unsubscribed " + alias + " cartridge");
                 return;
             } else {
-                String resultString = getHttpResponseString(response);
+                String resultString = CommandLineUtils.getHttpResponseString(response);
                 ExceptionMapper exception = gson.fromJson(resultString, ExceptionMapper.class);
                 System.out.println(exception);
             }
@@ -1370,7 +1371,7 @@ public class RestCommandLineService {
                 System.out.println("You have successfully deployed the cartridge");
                 return;
             } else {
-                String resultString = getHttpResponseString(response);
+                String resultString = CommandLineUtils.getHttpResponseString(response);
                 ExceptionMapper exception = gson.fromJson(resultString, ExceptionMapper.class);
                 System.out.println(exception);
             }
@@ -1398,7 +1399,7 @@ public class RestCommandLineService {
                 System.out.println("You have succesfully undeploy " + id + " cartridge");
                 return;
             } else {
-                String resultString = getHttpResponseString(response);
+                String resultString = CommandLineUtils.getHttpResponseString(response);
                 ExceptionMapper exception = gson.fromJson(resultString, ExceptionMapper.class);
                 System.out.println(exception);
             }
@@ -1418,7 +1419,7 @@ public class RestCommandLineService {
                     + partitionDeploymentEndPoint, partitionDefinition);
 
             String responseCode = "" + response.getStatusLine().getStatusCode();
-            String resultString = getHttpResponseString(response);
+            String resultString = CommandLineUtils.getHttpResponseString(response);
 
             GsonBuilder gsonBuilder = new GsonBuilder();
             Gson gson = gsonBuilder.create();
@@ -1455,7 +1456,7 @@ public class RestCommandLineService {
                 System.out.println("You have successfully deployed the autoscaling policy");
                 return;
             } else {
-                String resultString = getHttpResponseString(response);
+                String resultString = CommandLineUtils.getHttpResponseString(response);
                 ExceptionMapper exception = gson.fromJson(resultString, ExceptionMapper.class);
                 System.out.println(exception);
             }
@@ -1483,7 +1484,7 @@ public class RestCommandLineService {
                 System.out.println("You have succesfully deploy the multi-tenant service cluster");
                 return;
             } else {
-                String resultString = getHttpResponseString(response);
+                String resultString = CommandLineUtils.getHttpResponseString(response);
                 ExceptionMapper exception = gson.fromJson(resultString, ExceptionMapper.class);
                 System.out.println(exception);
             }
@@ -1511,7 +1512,7 @@ public class RestCommandLineService {
                 System.out.println("You have succesfully undeploy multi-tenant service cluster");
                 return;
             } else {
-                String resultString = getHttpResponseString(response);
+                String resultString = CommandLineUtils.getHttpResponseString(response);
                 ExceptionMapper exception = gson.fromJson(resultString, ExceptionMapper.class);
                 System.out.println(exception);
             }
@@ -1536,13 +1537,13 @@ public class RestCommandLineService {
             Gson gson = gsonBuilder.create();
 
             if ( ! responseCode.equals(CliConstants.RESPONSE_OK)) {
-                String resultString = getHttpResponseString(response);
+                String resultString = CommandLineUtils.getHttpResponseString(response);
                 ExceptionMapper exception = gson.fromJson(resultString, ExceptionMapper.class);
                 System.out.println(exception);
                 return;
             }
 
-            String resultString = getHttpResponseString(response);
+            String resultString = CommandLineUtils.getHttpResponseString(response);
             if (resultString == null) {
                 System.out.println("Response content is empty");
                 return;
@@ -1607,7 +1608,7 @@ public class RestCommandLineService {
                 System.out.println("You have successfully deployed the deployment policy");
                 return;
             } else {
-                String resultString = getHttpResponseString(response);
+                String resultString = CommandLineUtils.getHttpResponseString(response);
                 ExceptionMapper exception = gson.fromJson(resultString, ExceptionMapper.class);
                 System.out.println(exception);
             }
@@ -1627,7 +1628,7 @@ public class RestCommandLineService {
                     + listParitionRestEndPoint);
 
             String responseCode = "" + response.getStatusLine().getStatusCode();
-            String resultString = getHttpResponseString(response);
+            String resultString = CommandLineUtils.getHttpResponseString(response);
 
             GsonBuilder gsonBuilder = new GsonBuilder();
             Gson gson = gsonBuilder.create();
@@ -1698,13 +1699,13 @@ public class RestCommandLineService {
             Gson gson = gsonBuilder.create();
 
             if ( ! responseCode.equals(CliConstants.RESPONSE_OK)) {
-                String resultString = getHttpResponseString(response);
+                String resultString = CommandLineUtils.getHttpResponseString(response);
                 ExceptionMapper exception = gson.fromJson(resultString, ExceptionMapper.class);
                 System.out.println(exception);
                 return;
             }
 
-            String resultString = getHttpResponseString(response);
+            String resultString = CommandLineUtils.getHttpResponseString(response);
 
             if (resultString == null) {
                 System.out.println("Response content is empty");
@@ -1764,13 +1765,13 @@ public class RestCommandLineService {
             Gson gson = gsonBuilder.create();
 
             if ( ! responseCode.equals(CliConstants.RESPONSE_OK)) {
-                String resultString = getHttpResponseString(response);
+                String resultString = CommandLineUtils.getHttpResponseString(response);
                 ExceptionMapper exception = gson.fromJson(resultString, ExceptionMapper.class);
                 System.out.println(exception);
                 return;
             }
 
-            String resultString = getHttpResponseString(response);
+            String resultString = CommandLineUtils.getHttpResponseString(response);
             if (resultString == null) {
                 System.out.println("Response content is empty");
                 return;
@@ -1830,13 +1831,13 @@ public class RestCommandLineService {
             Gson gson = gsonBuilder.create();
 
             if ( ! responseCode.equals(CliConstants.RESPONSE_OK)) {
-                String resultString = getHttpResponseString(response);
+                String resultString = CommandLineUtils.getHttpResponseString(response);
                 ExceptionMapper exception = gson.fromJson(resultString, ExceptionMapper.class);
                 System.out.println(exception);
                 return;
             }
 
-            String resultString = getHttpResponseString(response);
+            String resultString = CommandLineUtils.getHttpResponseString(response);
             if (resultString == null) {
                 System.out.println("Response content is empty");
                 return;
@@ -1878,13 +1879,13 @@ public class RestCommandLineService {
             Gson gson = gsonBuilder.create();
 
             if ( ! responseCode.equals(CliConstants.RESPONSE_OK)) {
-                String resultString = getHttpResponseString(response);
+                String resultString = CommandLineUtils.getHttpResponseString(response);
                 ExceptionMapper exception = gson.fromJson(resultString, ExceptionMapper.class);
                 System.out.println(exception);
                 return;
             }
 
-            String resultString = getHttpResponseString(response);
+            String resultString = CommandLineUtils.getHttpResponseString(response);
             if (resultString == null) {
                 System.out.println("Response content is empty");
                 return;
@@ -1921,13 +1922,13 @@ public class RestCommandLineService {
             Gson gson = gsonBuilder.create();
 
             if ( ! responseCode.equals(CliConstants.RESPONSE_OK)) {
-                String resultString = getHttpResponseString(response);
+                String resultString = CommandLineUtils.getHttpResponseString(response);
                 ExceptionMapper exception = gson.fromJson(resultString, ExceptionMapper.class);
                 System.out.println(exception);
                 return;
             }
 
-            String resultString = getHttpResponseString(response);
+            String resultString = CommandLineUtils.getHttpResponseString(response);
             if (resultString == null) {
                 System.out.println("Response content is empty");
                 return;
@@ -1957,12 +1958,12 @@ public class RestCommandLineService {
     }
 
     public void deployKubernetesGroup(String entityBody) {
-        deployEntity(deployKubernetesGroup, entityBody, "kubernetes group");
+        restClient.deployEntity(deployKubernetesGroup, entityBody, "kubernetes group");
     }
 
     public void listKubernetesGroups() {
         try {
-            KubernetesGroupList list = (KubernetesGroupList) executeList(listKubernetesGroup, KubernetesGroupList.class, "kubernetes group");
+            KubernetesGroupList list = (KubernetesGroupList) restClient.listEntity(listKubernetesGroup, KubernetesGroupList.class, "kubernetes group");
             if((list != null) && (list.getKubernetesGroup() != null) && (list.getKubernetesGroup().size() > 0)) {
                 RowMapper<KubernetesGroup> partitionMapper = new RowMapper<KubernetesGroup>() {
                     public String[] getData(KubernetesGroup kubernetesGroup) {
@@ -1978,26 +1979,27 @@ public class RestCommandLineService {
                 System.out.println("Available kubernetes groups:" );
                 CommandLineUtils.printTable(array, partitionMapper, "Group ID", "Description");
             } else {
-                String message = "No kubernetes groups found.";
-                System.out.println(message);
+                System.out.println("No kubernetes groups found");
                 return;
             }
         } catch (Exception e) {
-            logger.error("Error in listing kubernetes groups");
+            String message = "Error in listing kubernetes groups";
+            System.out.println(message);
+            logger.error(message, e);
         }
     }
 
     public void undeployKubernetesGroup(String groupId) {
-        undeployEntity(undeployKubernetesGroup, "kubernetes group", groupId);
+        restClient.undeployEntity(undeployKubernetesGroup, "kubernetes group", groupId);
     }
 
     public void deployKubernetesHost(String entityBody) {
-        deployEntity(deployKubernetesHost, entityBody, "kubernetes host");
+        restClient.deployEntity(deployKubernetesHost, entityBody, "kubernetes host");
     }
 
     public void listKubernetesHosts(String groupId) {
         try {
-            KubernetesHostList list = (KubernetesHostList) executeList(listKubernetesHost.replace("{groupId}", groupId),
+            KubernetesHostList list = (KubernetesHostList) restClient.listEntity(listKubernetesHost.replace("{groupId}", groupId),
                     KubernetesHostList.class, "kubernetes host");
             if((list != null) && (list.getKubernetesHost() != null) && (list.getKubernetesHost().size() > 0)) {
                 RowMapper<KubernetesHost> partitionMapper = new RowMapper<KubernetesHost>() {
@@ -2015,177 +2017,26 @@ public class RestCommandLineService {
                 System.out.println("Available kubernetes hosts:" );
                 CommandLineUtils.printTable(array, partitionMapper, "Host ID", "Hostname", "IP Address");
             } else {
-                String message = "No kubernetes hosts found.";
-                System.out.println(message);
+                System.out.println("No kubernetes hosts found");
                 return;
             }
         } catch (Exception e) {
-            logger.error("Error in listing kubernetes hosts");
+            String message = "Error in listing kubernetes hosts";
+            System.out.println(message);
+            logger.error(message, e);
         }
     }
 
     public void undeployKubernetesHost(String hostId) {
-        undeployEntity(undeployKubernetesHost, "kubernetes host", hostId);
+        restClient.undeployEntity(undeployKubernetesHost, "kubernetes host", hostId);
     }
 
     public void updateKubernetesMaster(String entityBody) {
-        updateEntity(updateKubernetesMaster, entityBody, "kubernetes master");
+        restClient.updateEntity(updateKubernetesMaster, entityBody, "kubernetes master");
     }
 
     public void updateKubernetesHost(String entityBody) {
-        updateEntity(updateKubernetesHost, entityBody, "kubernetes host");
-    }
-
-    private void deployEntity(String serviceEndpoint, String entityBody, String entityName) {
-        try {
-            int responseCode = executePost(serviceEndpoint, entityBody);
-            if(responseCode == 201) {
-                System.out.println(String.format("Successfully deployed %s", entityName));
-            }
-        } catch (Exception e) {
-            System.out.println(String.format("Error in deploying %s", entityName));
-        }
-    }
-
-    private void undeployEntity(String serviceEndpoint, String entityName, String entityId) {
-        try {
-            int responseCode = executeDelete(serviceEndpoint, entityId);
-            if(responseCode == 404) {
-                System.out.println(String.format("%s not found", StringUtils.capitalize(entityName)));
-            } else if(responseCode == 204) {
-                System.out.println(String.format("Successfully un-deployed %s", entityName));
-            }
-        } catch (Exception e) {
-            System.out.println(String.format("Error in un-deploying %s", entityName));
-        }
-    }
-
-    private void updateEntity(String serviceEndpoint, String entityBody, String entityName) {
-        try {
-            int responseCode = executePut(serviceEndpoint, entityBody);
-            if(responseCode == 404) {
-                System.out.println(String.format("%s not found", StringUtils.capitalize(entityName)));
-            } else if(responseCode == 200) {
-                System.out.println(String.format("Successfully updated %s", entityName));
-            }
-        } catch (Exception e) {
-            System.out.println(String.format("Error in updating %s", entityName));
-        }
-    }
-
-    private void deleteEntity(String serviceEndpoint, String identifier, String entityName) {
-        try {
-            int responseCode = executeDelete(serviceEndpoint, identifier);
-            if(responseCode == 200) {
-                System.out.println(String.format("Successfully deleted %s", entityName));
-            }
-        } catch (Exception e) {
-            System.out.println(String.format("Error in deleting %s", entityName));
-        }
-    }
-
-    public int executePost(String serviceEndpoint, String postBody) throws ClientProtocolException, ConnectException {
-        DefaultHttpClient httpClient= new DefaultHttpClient();
-        try {
-            HttpResponse response = restClient.doPost(httpClient, restClient.getBaseURL()
-                    + serviceEndpoint, postBody);
-
-            int responseCode = response.getStatusLine().getStatusCode();
-            if (responseCode != 200) {
-                String resultString = getHttpResponseString(response);
-                if(StringUtils.isNotBlank(resultString)) {
-                    GsonBuilder gsonBuilder = new GsonBuilder();
-                    Gson gson = gsonBuilder.create();
-                    ExceptionMapper exception = gson.fromJson(resultString, ExceptionMapper.class);
-                    if(exception != null) {
-                        System.out.println(exception);
-                    }
-                }
-            }
-            return responseCode;
-        } finally {
-            httpClient.getConnectionManager().shutdown();
-        }
-    }
-
-    public Object executeList(String serviceEndpoint, Class _class, String entityName) throws CommandException{
-        DefaultHttpClient httpClient = new DefaultHttpClient();
-        HttpResponse response = null;
-
-        try {
-            response = restClient.doGet(httpClient, restClient.getBaseURL() + serviceEndpoint);
-            int responseCode = response.getStatusLine().getStatusCode();
-            String resultString = getHttpResponseString(response);
-
-            GsonBuilder gsonBuilder = new GsonBuilder();
-            Gson gson = gsonBuilder.create();
-
-            if (responseCode == 200) {
-                return gson.fromJson(resultString, _class);
-            } else {
-                if(StringUtils.isBlank(resultString)) {
-                    return null;
-                } else {
-                    ExceptionMapper exception = gson.fromJson(resultString, ExceptionMapper.class);
-                    if(exception != null) {
-                        System.out.println(exception);
-                    }
-                    return gson.fromJson(resultString, _class);
-                }
-            }
-        } catch (Exception e) {
-            handleException(String.format("Error in listing %s", entityName), e);
-            return null;
-        } finally {
-            httpClient.getConnectionManager().shutdown();
-        }
-    }
-
-    public int executePut(String serviceEndpoint, String postBody) throws IOException {
-        DefaultHttpClient httpClient= new DefaultHttpClient();
-        try {
-            HttpResponse response = restClient.doPut(httpClient, restClient.getBaseURL()
-                    + serviceEndpoint, postBody);
-
-            int responseCode = response.getStatusLine().getStatusCode();
-            if (responseCode != 200) {
-                String resultString = getHttpResponseString(response);
-                if(StringUtils.isNotBlank(resultString)) {
-                    GsonBuilder gsonBuilder = new GsonBuilder();
-                    Gson gson = gsonBuilder.create();
-                    ExceptionMapper exception = gson.fromJson(resultString, ExceptionMapper.class);
-                    if(exception != null) {
-                        System.out.println(exception);
-                    }
-                }
-            }
-            return responseCode;
-        } finally {
-            httpClient.getConnectionManager().shutdown();
-        }
-    }
-
-    public int executeDelete(String serviceEndpoint, String identifier) throws IOException {
-        DefaultHttpClient httpClient= new DefaultHttpClient();
-        try {
-            HttpResponse response = restClient.doDelete(httpClient, restClient.getBaseURL() + serviceEndpoint.replace("{id}", identifier));
-
-            int responseCode = response.getStatusLine().getStatusCode();
-            if (responseCode != 200) {
-                String resultString = getHttpResponseString(response);
-                if(StringUtils.isNotBlank(resultString)) {
-                    GsonBuilder gsonBuilder = new GsonBuilder();
-                    Gson gson = gsonBuilder.create();
-                    ExceptionMapper exception = gson.fromJson(resultString, ExceptionMapper.class);
-                    if(exception != null) {
-                        System.out.println(exception);
-                    }
-                }
-            }
-            return responseCode;
-        } finally {
-            httpClient.getConnectionManager().shutdown();
-        }
+        restClient.updateEntity(updateKubernetesHost, entityBody, "kubernetes host");
     }
 
     public void sync(String alias) throws CommandException {
@@ -2201,7 +2052,7 @@ public class RestCommandLineService {
             } else {
                 GsonBuilder gsonBuilder = new GsonBuilder();
                 Gson gson = gsonBuilder.create();
-                String resultString = getHttpResponseString(response);
+                String resultString = CommandLineUtils.getHttpResponseString(response);
                 ExceptionMapper exception = gson.fromJson(resultString, ExceptionMapper.class);
                 System.out.println(exception);
             }
@@ -2357,31 +2208,6 @@ public class RestCommandLineService {
         return urlBuilder.toString();
     }
 
-    // This method gives the HTTP response string
-    private 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;
-            }
-
-            return result;
-        } catch (SocketException e) {
-            System.out.println("Connection problem");
-            return null;
-        } catch (NullPointerException e) {
-            System.out.println("Null value return from server");
-            return null;
-        } catch (IOException e) {
-            System.out.println("IO error");
-            return null;
-        }
-    }
-
     // This is for handle exception
     private void handleException(String key, Exception e, Object... args) throws CommandException {
         if (logger.isDebugEnabled()) {
@@ -2398,57 +2224,6 @@ public class RestCommandLineService {
         throw new CommandException(message, e);
     }
 
-    /**
-     * To map RestApiException of back-end.
-     * @author nirmal
-     *
-     */
-    public class ExceptionMapper {
-        @Override
-        public String toString() {
-            return Error.toString();
-        }
-
-        private ErrorWrapper Error;
-
-        public ErrorWrapper getError() {
-            return Error;
-        }
-
-        public void setError(ErrorWrapper error) {
-            Error = error;
-        }
-
-    }
-
-    public class ErrorWrapper {
-        private String errorCode;
-        private String errorMessage;
-
-        public String getErrorCode() {
-            return errorCode;
-        }
-
-        public void setErrorCode(String errorCode) {
-            this.errorCode = errorCode;
-        }
-
-        public String getErrorMessage() {
-            return errorMessage;
-        }
-
-        public void setErrorMessage(String 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;
@@ -2471,7 +2246,7 @@ public class RestCommandLineService {
             HttpResponse response = restClient.doGet(httpClient, restClient.getBaseURL() + listAvailableCartridgesRestEndpoint);
 
             String responseCode = "" + response.getStatusLine().getStatusCode();
-            String resultString = getHttpResponseString(response);
+            String resultString = CommandLineUtils.getHttpResponseString(response);
             if (resultString == null) {
                 return false;
             }

http://git-wip-us.apache.org/repos/asf/stratos/blob/869f818f/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/exception/ErrorWrapper.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/exception/ErrorWrapper.java b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/exception/ErrorWrapper.java
new file mode 100644
index 0000000..01376f3
--- /dev/null
+++ b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/exception/ErrorWrapper.java
@@ -0,0 +1,48 @@
+/**
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+
+ *  http://www.apache.org/licenses/LICENSE-2.0
+
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+
+package org.apache.stratos.cli.exception;
+
+public class ErrorWrapper {
+    private String errorCode;
+    private String errorMessage;
+
+    public String getErrorCode() {
+        return errorCode;
+    }
+
+    public void setErrorCode(String errorCode) {
+        this.errorCode = errorCode;
+    }
+
+    public String getErrorMessage() {
+        return errorMessage;
+    }
+
+    public void setErrorMessage(String errorMessage) {
+        this.errorMessage = errorMessage;
+    }
+
+    @Override
+    public String toString() {
+        return "Exception [errorCode=" + errorCode
+                + ", errorMessage=" + errorMessage + "]";
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/stratos/blob/869f818f/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/exception/ExceptionMapper.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/exception/ExceptionMapper.java b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/exception/ExceptionMapper.java
new file mode 100644
index 0000000..ebbfb59
--- /dev/null
+++ b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/exception/ExceptionMapper.java
@@ -0,0 +1,43 @@
+/**
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+
+ *  http://www.apache.org/licenses/LICENSE-2.0
+
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+
+package org.apache.stratos.cli.exception;
+
+/**
+ * To map RestApiException of back-end.
+ * @author nirmal
+ *
+ */
+public class ExceptionMapper {
+    @Override
+    public String toString() {
+        return Error.toString();
+    }
+
+    private ErrorWrapper Error;
+
+    public ErrorWrapper getError() {
+        return Error;
+    }
+
+    public void setError(ErrorWrapper error) {
+        Error = error;
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/stratos/blob/869f818f/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/utils/CommandLineUtils.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/utils/CommandLineUtils.java b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/utils/CommandLineUtils.java
index 77a0636..ed00a0d 100644
--- a/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/utils/CommandLineUtils.java
+++ b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/utils/CommandLineUtils.java
@@ -18,9 +18,13 @@
  */
 package org.apache.stratos.cli.utils;
 
+import org.apache.http.HttpResponse;
+
 import java.io.BufferedReader;
 import java.io.FileReader;
 import java.io.IOException;
+import java.io.InputStreamReader;
+import java.net.SocketException;
 import java.text.MessageFormat;
 import java.util.ResourceBundle;
 
@@ -111,4 +115,33 @@ public class CommandLineUtils {
             br.close();
         }
     }
+
+    /**
+     * Extract HTTP response body as a string
+     * @param response
+     * @return
+     */
+    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;
+            }
+
+            return result;
+        } catch (SocketException e) {
+            System.out.println("Connection problem");
+            return null;
+        } catch (NullPointerException e) {
+            System.out.println("Null value return from server");
+            return null;
+        } catch (IOException e) {
+            System.out.println("IO error");
+            return null;
+        }
+    }
 }