You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@stratos.apache.org by ni...@apache.org on 2014/10/11 06:12:53 UTC

[07/50] [abbrv] git commit: Adding Kubernetes response handler and a kubernetes response class to handle http responses.

Adding Kubernetes response handler and a kubernetes response class to handle http responses.


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

Branch: refs/heads/master
Commit: 8635c6f261803268ab99b64c3fa274b9f95f59cf
Parents: 7d89126
Author: Nirmal Fernando <ni...@gmail.com>
Authored: Wed Oct 8 18:51:38 2014 +0530
Committer: Nirmal Fernando <ni...@gmail.com>
Committed: Sat Oct 11 09:30:55 2014 +0530

----------------------------------------------------------------------
 .../client/rest/KubernetesResponse.java         | 56 +++++++++++++++
 .../client/rest/KubernetesResponseHandler.java  | 71 ++++++++++++++++++++
 2 files changed, 127 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/stratos/blob/8635c6f2/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
new file mode 100644
index 0000000..e8f17c3
--- /dev/null
+++ b/components/org.apache.stratos.kubernetes.client/src/main/java/org/apache/stratos/kubernetes/client/rest/KubernetesResponse.java
@@ -0,0 +1,56 @@
+/*
+ *
+ * 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 KubernetesResponse {
+
+	private int statusCode;
+	private String content;
+	private String reason;
+	
+    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;
+    }
+    @Override
+    public String toString() {
+        return "KubernetesResponse [statusCode=" + statusCode + ", content=" + content
+                + ", reason=" + reason + "]";
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/stratos/blob/8635c6f2/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
new file mode 100644
index 0000000..da6aa90
--- /dev/null
+++ b/components/org.apache.stratos.kubernetes.client/src/main/java/org/apache/stratos/kubernetes/client/rest/KubernetesResponseHandler.java
@@ -0,0 +1,71 @@
+/*
+ *
+ * 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;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+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.HttpResponseException;
+import org.apache.http.client.ResponseHandler;
+
+/**
+ * Handles a HttpResponse and returns a {@link KubernetesResponse}
+ */
+public class KubernetesResponseHandler implements ResponseHandler<KubernetesResponse>{
+
+    @Override
+    public KubernetesResponse handleResponse(HttpResponse response) throws ClientProtocolException,
+            IOException {
+        StatusLine statusLine = response.getStatusLine();
+        HttpEntity entity = response.getEntity();
+        if (statusLine.getStatusCode() >= 300) {
+            throw new HttpResponseException(
+                    statusLine.getStatusCode(),
+                    statusLine.getReasonPhrase());
+        }
+        if (entity == null) {
+            throw new ClientProtocolException("Response contains no content");
+        }
+        
+        BufferedReader reader = new BufferedReader(new InputStreamReader(
+                (response.getEntity().getContent())));
+
+        String output;
+        String result = "";
+
+        while ((output = reader.readLine()) != null) {
+            result += output;
+        }
+        
+        KubernetesResponse kubResponse = new KubernetesResponse();
+        kubResponse.setStatusCode(statusLine.getStatusCode());
+        kubResponse.setContent(result);
+        kubResponse.setReason(statusLine.getReasonPhrase());
+        
+        return kubResponse;
+    }
+
+
+}