Refactor REST client to use a resource URI.
Project: http://git-wip-us.apache.org/repos/asf/stratos/repo Commit: http://git-wip-us.apache.org/repos/asf/stratos/commit/f5491ff0 Tree: http://git-wip-us.apache.org/repos/asf/stratos/tree/f5491ff0 Diff: http://git-wip-us.apache.org/repos/asf/stratos/diff/f5491ff0 Branch: refs/heads/container-autoscaling Commit: f5491ff0ffbca2ddeb981b249a7af27834aeb946 Parents: f4011b4 Author: Nirmal Fernando <[email protected]> Authored: Mon Oct 6 16:02:37 2014 +0530 Committer: Nirmal Fernando <[email protected]> Committed: Wed Oct 8 22:21:28 2014 +0530 ---------------------------------------------------------------------- .../kubernetes/client/rest/RestClient.java | 76 ++++---------------- 1 file changed, 12 insertions(+), 64 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/stratos/blob/f5491ff0/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 2d0b950..272d04b 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 @@ -18,36 +18,20 @@ */ package org.apache.stratos.kubernetes.client.rest; -import java.io.IOException; -import java.net.ConnectException; +import java.net.URI; 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; import org.apache.http.client.methods.HttpPut; 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; -@SuppressWarnings("deprecation") public class RestClient { - private String baseURL; private DefaultHttpClient httpClient; - private final int TIME_OUT_PARAM = 6000000; - - public RestClient(String baseURL) { - this.baseURL = baseURL; - } - - public String getBaseURL() { - return baseURL; - } - /** * Handle http post request. Return String * @@ -62,13 +46,13 @@ public class RestClient { * @param password * Password for basic auth * @return The HttpResponse - * @throws org.apache.http.client.ClientProtocolException and IOException + * @throws Exception * if any errors occur when executing the request */ - public HttpResponse doPost(String resourcePath, String jsonParamString) throws Exception{ + public HttpResponse doPost(URI resourcePath, String jsonParamString) throws Exception{ try { httpClient = new DefaultHttpClient(); - HttpPost postRequest = new HttpPost(baseURL+resourcePath); + HttpPost postRequest = new HttpPost(resourcePath); StringEntity input = new StringEntity(jsonParamString); input.setContentType("application/json"); @@ -77,14 +61,6 @@ public class RestClient { HttpResponse response = httpClient.execute(postRequest); return response; - } catch (ClientProtocolException e) { - throw new ClientProtocolException(); - } catch (ConnectException e) { - throw new ConnectException(); - } - catch (IOException e) { - e.printStackTrace(); - return null; } finally { httpClient.getConnectionManager().shutdown(); } @@ -105,63 +81,41 @@ public class RestClient { * @throws org.apache.http.client.ClientProtocolException and IOException * if any errors occur when executing the request */ - public HttpResponse doGet(String resourcePath) throws Exception{ + public HttpResponse doGet(URI resourcePath) throws Exception{ try { httpClient = new DefaultHttpClient(); - HttpGet getRequest = new HttpGet(baseURL+resourcePath); + HttpGet getRequest = new HttpGet(resourcePath); + System.out.println(getRequest.getRequestLine().getUri()); getRequest.addHeader("Content-Type", "application/json"); - HttpParams params = httpClient.getParams(); - HttpConnectionParams.setConnectionTimeout(params, TIME_OUT_PARAM); - HttpConnectionParams.setSoTimeout(params, TIME_OUT_PARAM); - HttpResponse response = httpClient.execute(getRequest); return response; - } catch (ClientProtocolException e) { - throw new ClientProtocolException(); - } catch (ConnectException e) { - throw new ConnectException(); - } catch (IOException e) { - e.printStackTrace(); - return null; } finally { httpClient.getConnectionManager().shutdown(); } } - public HttpResponse doDelete(String resourcePath) throws Exception { + public HttpResponse doDelete(URI resourcePath) throws Exception { try { httpClient = new DefaultHttpClient(); - HttpDelete httpDelete = new HttpDelete(baseURL+resourcePath); + 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); - HttpResponse response = httpClient.execute(httpDelete); return response; - } catch (ClientProtocolException e) { - e.printStackTrace(); - return null; - } catch (IOException e) { - e.printStackTrace(); - return null; } finally { httpClient.getConnectionManager().shutdown(); } } - public HttpResponse doPut(String resourcePath, String jsonParamString) throws Exception { + public HttpResponse doPut(URI resourcePath, String jsonParamString) throws Exception { try { httpClient = new DefaultHttpClient(); - System.out.println(baseURL + resourcePath); - System.out.println(jsonParamString); - HttpPut putRequest = new HttpPut(baseURL + resourcePath); + HttpPut putRequest = new HttpPut(resourcePath); StringEntity input = new StringEntity(jsonParamString); input.setContentType("application/json"); @@ -170,13 +124,7 @@ public class RestClient { HttpResponse response = httpClient.execute(putRequest); return response; - } catch (ClientProtocolException e) { - throw new ClientProtocolException(); - } catch (ConnectException e) { - throw new ConnectException(); - } catch (IOException e) { - e.printStackTrace(); - return null; + } finally { httpClient.getConnectionManager().shutdown(); }
