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/4dae7f22 Tree: http://git-wip-us.apache.org/repos/asf/stratos/tree/4dae7f22 Diff: http://git-wip-us.apache.org/repos/asf/stratos/diff/4dae7f22 Branch: refs/heads/container-autoscaling Commit: 4dae7f2231a2f8126dde26042b95ca9f2f6e98b8 Parents: 8b5048a Author: Nirmal Fernando <[email protected]> Authored: Wed Oct 8 18:51:38 2014 +0530 Committer: Nirmal Fernando <[email protected]> Committed: Wed Oct 8 22:21:29 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/4dae7f22/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/4dae7f22/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; + } + + +}
