stratos-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From im...@apache.org
Subject [08/12] stratos git commit: Introducing kubernetes error response message parser for extracting error messages
Date Wed, 24 Dec 2014 17:36:31 GMT
Introducing kubernetes error response message parser for extracting error messages


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

Branch: refs/heads/master
Commit: 025a59ceff67b597643e4e81330c204750f0c4a2
Parents: e195f2f
Author: Imesh Gunaratne <imesh@apache.org>
Authored: Wed Dec 24 18:40:46 2014 +0530
Committer: Imesh Gunaratne <imesh@apache.org>
Committed: Wed Dec 24 18:40:46 2014 +0530

----------------------------------------------------------------------
 .../kubernetes/client/rest/HttpResponse.java    | 63 ++++++++++++++++
 .../client/rest/KubernetesResponse.java         | 79 ++++++++++++++------
 .../client/rest/KubernetesResponseHandler.java  | 56 ++++++++++----
 .../kubernetes/client/rest/RestClient.java      |  8 +-
 4 files changed, 165 insertions(+), 41 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/stratos/blob/025a59ce/components/org.apache.stratos.kubernetes.client/src/main/java/org/apache/stratos/kubernetes/client/rest/HttpResponse.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.kubernetes.client/src/main/java/org/apache/stratos/kubernetes/client/rest/HttpResponse.java
b/components/org.apache.stratos.kubernetes.client/src/main/java/org/apache/stratos/kubernetes/client/rest/HttpResponse.java
new file mode 100644
index 0000000..1a8ab86
--- /dev/null
+++ b/components/org.apache.stratos.kubernetes.client/src/main/java/org/apache/stratos/kubernetes/client/rest/HttpResponse.java
@@ -0,0 +1,63 @@
+/*
+ * 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.kubernetes.client.rest;
+
+/**
+ * Holds the data extracted from a HttpResponse.
+ */
+public class HttpResponse {
+
+    private int statusCode;
+    private String content;
+    private String reason;
+    private KubernetesResponse kubernetesResponse;
+
+    public int getStatusCode() {
+        return statusCode;
+    }
+    public void setStatusCode(int statusCode) {
+        this.statusCode = statusCode;
+    }
+    public String getContent() {
+        return content;
+    }
+    public void setContent(String content) {
+        this.content = content;
+    }
+    public String getReason() {
+        return reason;
+    }
+    public void setReason(String reason) {
+        this.reason = reason;
+    }
+
+    public KubernetesResponse getKubernetesResponse() {
+        return kubernetesResponse;
+    }
+
+    public void setKubernetesResponse(KubernetesResponse kubernetesResponse) {
+        this.kubernetesResponse = kubernetesResponse;
+    }
+
+    @Override
+    public String toString() {
+        return "HttpResponse [statusCode=" + statusCode + ", content=" + content
+                + ", reason=" + reason + ", kubernetesResponse=" + kubernetesResponse + "]";
+    }
+}

http://git-wip-us.apache.org/repos/asf/stratos/blob/025a59ce/components/org.apache.stratos.kubernetes.client/src/main/java/org/apache/stratos/kubernetes/client/rest/KubernetesResponse.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.kubernetes.client/src/main/java/org/apache/stratos/kubernetes/client/rest/KubernetesResponse.java
b/components/org.apache.stratos.kubernetes.client/src/main/java/org/apache/stratos/kubernetes/client/rest/KubernetesResponse.java
index e8f17c3..e700a1c 100644
--- a/components/org.apache.stratos.kubernetes.client/src/main/java/org/apache/stratos/kubernetes/client/rest/KubernetesResponse.java
+++ b/components/org.apache.stratos.kubernetes.client/src/main/java/org/apache/stratos/kubernetes/client/rest/KubernetesResponse.java
@@ -1,5 +1,4 @@
 /*
- *
  * 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
@@ -16,41 +15,75 @@
  * KIND, either express or implied.  See the License for the
  * specific language governing permissions and limitations
  * under the License.
- *
  */
+
 package org.apache.stratos.kubernetes.client.rest;
 
 /**
- * Holds the data extracted from a HttpResponse.
+ * Kubernetes response.
  */
 public class KubernetesResponse {
+    private String kind;
+    private String creationTimestamp;
+    private String selfLink;
+    private String apiVersion;
+    private String status;
+    private String message;
+    private int code;
+
+    public String getKind() {
+        return kind;
+    }
+
+    public void setKind(String kind) {
+        this.kind = kind;
+    }
+
+    public String getCreationTimestamp() {
+        return creationTimestamp;
+    }
+
+    public void setCreationTimestamp(String creationTimestamp) {
+        this.creationTimestamp = creationTimestamp;
+    }
 
-	private int statusCode;
-	private String content;
-	private String reason;
-	
-    public int getStatusCode() {
-        return statusCode;
+    public String getSelfLink() {
+        return selfLink;
     }
-    public void setStatusCode(int statusCode) {
-        this.statusCode = statusCode;
+
+    public void setSelfLink(String selfLink) {
+        this.selfLink = selfLink;
     }
-    public String getContent() {
-        return content;
+
+    public String getApiVersion() {
+        return apiVersion;
     }
-    public void setContent(String content) {
-        this.content = content;
+
+    public void setApiVersion(String apiVersion) {
+        this.apiVersion = apiVersion;
     }
-    public String getReason() {
-        return reason;
+
+    public String getStatus() {
+        return status;
     }
-    public void setReason(String reason) {
-        this.reason = reason;
+
+    public void setStatus(String status) {
+        this.status = status;
     }
-    @Override
-    public String toString() {
-        return "KubernetesResponse [statusCode=" + statusCode + ", content=" + content
-                + ", reason=" + reason + "]";
+
+    public String getMessage() {
+        return message;
     }
 
+    public void setMessage(String message) {
+        this.message = message;
+    }
+
+    public int getCode() {
+        return code;
+    }
+
+    public void setCode(int code) {
+        this.code = code;
+    }
 }

http://git-wip-us.apache.org/repos/asf/stratos/blob/025a59ce/components/org.apache.stratos.kubernetes.client/src/main/java/org/apache/stratos/kubernetes/client/rest/KubernetesResponseHandler.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.kubernetes.client/src/main/java/org/apache/stratos/kubernetes/client/rest/KubernetesResponseHandler.java
b/components/org.apache.stratos.kubernetes.client/src/main/java/org/apache/stratos/kubernetes/client/rest/KubernetesResponseHandler.java
index fa2442a..35a15be 100644
--- a/components/org.apache.stratos.kubernetes.client/src/main/java/org/apache/stratos/kubernetes/client/rest/KubernetesResponseHandler.java
+++ b/components/org.apache.stratos.kubernetes.client/src/main/java/org/apache/stratos/kubernetes/client/rest/KubernetesResponseHandler.java
@@ -20,10 +20,13 @@
  */
 package org.apache.stratos.kubernetes.client.rest;
 
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import com.google.gson.JsonSyntaxException;
+import org.apache.commons.lang3.StringUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.http.HttpEntity;
-import org.apache.http.HttpResponse;
 import org.apache.http.StatusLine;
 import org.apache.http.client.ClientProtocolException;
 import org.apache.http.client.ResponseHandler;
@@ -33,20 +36,20 @@ import java.io.IOException;
 import java.io.InputStreamReader;
 
 /**
- * Handles a HttpResponse and returns a {@link KubernetesResponse}
+ * Handles a HttpResponse and returns a {@link HttpResponse}
  */
-public class KubernetesResponseHandler implements ResponseHandler<KubernetesResponse>{
+public class KubernetesResponseHandler implements ResponseHandler<HttpResponse>{
     private static final Log log = LogFactory.getLog(KubernetesResponseHandler.class);
 
     @Override
-    public KubernetesResponse handleResponse(HttpResponse response) throws ClientProtocolException,
+    public HttpResponse handleResponse(org.apache.http.HttpResponse response) throws ClientProtocolException,
             IOException {
         StatusLine statusLine = response.getStatusLine();
         HttpEntity entity = response.getEntity();
         if (entity == null) {
             throw new ClientProtocolException("Response contains no content");
         }
-        
+
         BufferedReader reader = new BufferedReader(new InputStreamReader(
                 (response.getEntity().getContent())));
 
@@ -56,16 +59,41 @@ public class KubernetesResponseHandler implements ResponseHandler<KubernetesResp
         while ((output = reader.readLine()) != null) {
             result += output;
         }
-        
-        KubernetesResponse kubResponse = new KubernetesResponse();
-        kubResponse.setStatusCode(statusLine.getStatusCode());
-        kubResponse.setContent(result);
-        kubResponse.setReason(statusLine.getReasonPhrase());
-        
+
+        HttpResponse httpResponse = new HttpResponse();
+        httpResponse.setStatusCode(statusLine.getStatusCode());
+        httpResponse.setContent(result);
+        if(StringUtils.isNotBlank(result) && (isJson(result))) {
+            httpResponse.setKubernetesResponse(parseKubernetesResponse(result));
+        }
+        httpResponse.setReason(statusLine.getReasonPhrase());
+
         if (log.isDebugEnabled()) {
-            log.debug("Extracted Kubernetes Response: "+kubResponse.toString());
+            log.debug("Extracted Kubernetes Response: "+httpResponse.toString());
+        }
+
+        return httpResponse;
+    }
+
+    private boolean isJson(String content) {
+        try {
+            GsonBuilder gsonBuilder = new GsonBuilder();
+            Gson gson = gsonBuilder.create();
+            gson.fromJson(content, Object.class);
+            return true;
+        } catch (JsonSyntaxException ignore) {
+            return false;
+        }
+    }
+
+    private KubernetesResponse parseKubernetesResponse(String result) {
+        try {
+            GsonBuilder gsonBuilder = new GsonBuilder();
+            Gson gson = gsonBuilder.create();
+            return gson.fromJson(result, KubernetesResponse.class);
+        } catch (Exception e) {
+            log.error("Could not parse kubernetes api response", e);
+            return null;
         }
-        
-        return kubResponse;
     }
 }

http://git-wip-us.apache.org/repos/asf/stratos/blob/025a59ce/components/org.apache.stratos.kubernetes.client/src/main/java/org/apache/stratos/kubernetes/client/rest/RestClient.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.kubernetes.client/src/main/java/org/apache/stratos/kubernetes/client/rest/RestClient.java
b/components/org.apache.stratos.kubernetes.client/src/main/java/org/apache/stratos/kubernetes/client/rest/RestClient.java
index 3af3203..090dae0 100644
--- a/components/org.apache.stratos.kubernetes.client/src/main/java/org/apache/stratos/kubernetes/client/rest/RestClient.java
+++ b/components/org.apache.stratos.kubernetes.client/src/main/java/org/apache/stratos/kubernetes/client/rest/RestClient.java
@@ -51,7 +51,7 @@ public class RestClient {
      * @return The HttpResponse
      * @throws Exception if any errors occur when executing the request
      */
-    public KubernetesResponse doPost(URI resourcePath, String jsonParamString) throws Exception{
+    public HttpResponse doPost(URI resourcePath, String jsonParamString) throws Exception{
         HttpPost postRequest = null;
         try {
             postRequest = new HttpPost(resourcePath);
@@ -74,7 +74,7 @@ public class RestClient {
      * @throws org.apache.http.client.ClientProtocolException and IOException
      *             if any errors occur when executing the request
      */
-    public KubernetesResponse doGet(URI resourcePath) throws Exception{
+    public HttpResponse doGet(URI resourcePath) throws Exception{
         HttpGet getRequest = null;
         try {
             getRequest = new HttpGet(resourcePath);
@@ -86,7 +86,7 @@ public class RestClient {
         }
     }
     
-    public KubernetesResponse doDelete(URI resourcePath) throws Exception {
+    public HttpResponse doDelete(URI resourcePath) throws Exception {
         HttpDelete httpDelete = null;
         try {
             httpDelete = new HttpDelete(resourcePath);
@@ -98,7 +98,7 @@ public class RestClient {
         }
     }
 
-    public KubernetesResponse doPut(URI resourcePath, String jsonParamString) throws Exception
{
+    public HttpResponse doPut(URI resourcePath, String jsonParamString) throws Exception
{
 
         HttpPut putRequest = null;
 		try {


Mime
View raw message