Example usage for org.apache.http.client ResponseHandler handleResponse

List of usage examples for org.apache.http.client ResponseHandler handleResponse

Introduction

In this page you can find the example usage for org.apache.http.client ResponseHandler handleResponse.

Prototype

T handleResponse(HttpResponse response) throws ClientProtocolException, IOException;

Source Link

Document

Processes an HttpResponse and returns some value corresponding to that response.

Usage

From source file:org.jenkinsci.plugins.appio.service.AppioService.java

/**
 * @param appId/* ww  w.jav  a  2 s  .c  om*/
 * @param urlUpload
 * @return
 * @throws Exception
 */
public AppioVersionObject addVersion(String appId, String urlUpload) throws Exception {
    DefaultHttpClient httpClient = new DefaultHttpClient();
    ResponseHandler<String> handler = new BasicResponseHandler();
    AppioVersion newVersion = new AppioVersion();
    AppioVersionObject versionObj = new AppioVersionObject();

    try {
        // Construct {"version": ... } message body
        versionObj.setApp_id(appId);
        versionObj.setBundle_url(urlUpload);
        newVersion.setVersion(versionObj);
        LOGGER.fine("AppioService.addVersion() Request: " + new Gson().toJson(newVersion));

        // Send new version REST call to Appio
        httpPostVersions.addHeader("Authorization", "Basic " + apiKey);
        httpPostVersions.addHeader("Content-Type", "application/json");
        httpPostVersions.addHeader("Accept", appio_v1);
        StringEntity reqBody = new StringEntity(new Gson().toJson(newVersion),
                ContentType.create("application/json", "UTF-8"));
        httpPostVersions.setEntity(reqBody);
        HttpResponse response = httpClient.execute(httpHost, httpPostVersions);

        String jsonAppioVersion = handler.handleResponse(response);
        LOGGER.fine("AppioService.addVersion() Response: " + jsonAppioVersion);
        newVersion = new Gson().fromJson(jsonAppioVersion, AppioVersion.class);

    } catch (Exception e) {
        e.getStackTrace();
        throw e;
    } finally {
        try {
            httpClient.getConnectionManager().shutdown();
        } catch (Exception ignore) {
        }
    }
    return newVersion.getVersion();
}

From source file:dataServer.StorageRESTClientManager.java

public String getAllMoulds() {
    // Default HTTP client and common properties for requests
    requestUrl = null;//from  w  w w.j a  v a2 s .co  m

    // Default HTTP response and common properties for responses
    HttpResponse response = null;
    ResponseHandler<String> handler = null;
    int status = 0;
    String body = null;

    // Query for mould list
    requestUrl = new StringBuilder("http://" + storageLocation + ":" + storagePortRegistryC
            + storageRegistryContext + "/query/mould/list");

    try {
        HttpGet query = new HttpGet(requestUrl.toString());
        query.setHeader("Content-type", "application/json");
        response = client.execute(query);

        // Check status code
        status = response.getStatusLine().getStatusCode();
        if (status != 200) {
            throw new RuntimeException("Failed! HTTP error code: " + status);
        }

        // Get body
        handler = new BasicResponseHandler();
        body = handler.handleResponse(response);

        //System.out.println("MOULD LIST: " + body);
        return body;
    } catch (Exception e) {
        System.out.println(e.getClass().getName() + ": " + e.getMessage());
        return null;
    }
}

From source file:dataServer.StorageRESTClientManager.java

public String getAllMachines() {
    // Default HTTP client and common properties for requests
    requestUrl = null;//from w  ww  .  jav a 2  s. com

    // Default HTTP response and common properties for responses
    HttpResponse response = null;
    ResponseHandler<String> handler = null;
    int status = 0;
    String body = null;

    // Query for machine list
    requestUrl = new StringBuilder("http://" + storageLocation + ":" + storagePortRegistryC
            + storageRegistryContext + "/query/machine/list");

    try {
        HttpGet query = new HttpGet(requestUrl.toString());
        query.setHeader("Content-type", "application/json");
        response = client.execute(query);

        // Check status code
        status = response.getStatusLine().getStatusCode();
        if (status != 200) {
            throw new RuntimeException("Failed! HTTP error code: " + status);
        }

        // Get body
        handler = new BasicResponseHandler();
        body = handler.handleResponse(response);

        //System.out.println("MACHINE LIST: " + body);
        return body;
    } catch (Exception e) {
        System.out.println(e.getClass().getName() + ": " + e.getMessage());
        return null;
    }
}

From source file:dataServer.StorageRESTClientManager.java

public String getAllProducts() {
    // Default HTTP client and common properties for requests
    requestUrl = null;//from  w  w w .  j av a  2 s.co  m

    // Default HTTP response and common properties for responses
    HttpResponse response = null;
    ResponseHandler<String> handler = null;
    int status = 0;
    String body = null;

    // Query for product list
    requestUrl = new StringBuilder("http://" + storageLocation + ":" + storagePortRegistryC
            + storageRegistryContext + "/query/product/list");

    try {
        HttpGet query = new HttpGet(requestUrl.toString());
        query.setHeader("Content-type", "application/json");
        response = client.execute(query);

        // Check status code
        status = response.getStatusLine().getStatusCode();
        if (status != 200) {
            throw new RuntimeException("Failed! HTTP error code: " + status);
        }

        // Get body
        handler = new BasicResponseHandler();
        body = handler.handleResponse(response);

        //System.out.println("PRODUCT LIST: " + body);
        return body;
    } catch (Exception e) {
        System.out.println(e.getClass().getName() + ": " + e.getMessage());
        return null;
    }
}

From source file:dataServer.StorageRESTClientManager.java

public String getSensorProprerties(String sensorId, String context) {
    // Default HTTP client and common properties for requests
    requestUrl = null;//from w w  w. j  ava2s.c o  m
    params = null;
    queryString = null;

    // Default HTTP response and common properties for responses
    HttpResponse response = null;
    ResponseHandler<String> handler = null;
    int status = 0;
    String body = null;

    // Query for sensor properties
    requestUrl = new StringBuilder("http://" + storageLocation + ":" + storagePortRegistryC
            + storageRegistryContext + "/query/sensor/properties?dataset=" + context + "&sensorId=" + sensorId);

    try {
        HttpGet query = new HttpGet(requestUrl.toString());
        query.setHeader("Content-type", "application/json");
        response = client.execute(query);

        // Check status code
        status = response.getStatusLine().getStatusCode();
        if (status != 200) {
            throw new RuntimeException("Failed! HTTP error code: " + status);
        }

        // Get body
        handler = new BasicResponseHandler();
        body = handler.handleResponse(response);

        //System.out.println("SENSOR PROPRIETIES: " + body);
        return body;
    } catch (Exception e) {
        System.out.println(e.getClass().getName() + ": " + e.getMessage());
        return null;
    }
}

From source file:org.jenkinsci.plugins.appio.service.AppioService.java

/**
* @param appName// w w w  . j a v a  2  s .  com
* @return AppioAppObject (org.jenkinsci.plugins.appio.model.AppioAppObject)
* @throws Exception
*/
public AppioAppObject createApp(String appName) throws Exception {
    DefaultHttpClient httpClient = new DefaultHttpClient();
    ResponseHandler<String> handler = new BasicResponseHandler();
    AppioAppObject theAppObject = new AppioAppObject();

    try {
        // App.io Authorization and Content-Type headers
        String appioAuth = "Basic " + apiKey;
        httpPost.addHeader("Authorization", appioAuth);
        httpPost.addHeader("Content-Type", "application/json");
        httpPost.addHeader("Accept", appio_v1);

        // Create App.io App object
        AppioAppObject appioAppObj = new AppioAppObject();
        appioAppObj.setName(appName);

        // We want to exclude all non-annotated fields
        Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();

        // Construct {"app": ... } message body
        AppioApp theApp = new AppioApp();
        theApp.setApp(appioAppObj);
        StringEntity postBody = new StringEntity(gson.toJson(theApp),
                ContentType.create("application/json", "UTF-8"));
        httpPost.setEntity(postBody);
        LOGGER.fine("AppioService.createApp() Request: " + gson.toJson(theApp));

        // Call App.io REST API to create the new app
        HttpResponse response = httpClient.execute(httpHost, httpPost);
        String jsonAppioApp = handler.handleResponse(response);
        LOGGER.fine("AppioService.createApp() Response: " + jsonAppioApp);

        // Get JSON data from the HTTP Response
        AppioApp appioApp = new Gson().fromJson(jsonAppioApp, AppioApp.class);
        theAppObject = appioApp.getApp();

    } catch (Exception e) {
        throw e;
    } finally {
        try {
            httpClient.getConnectionManager().shutdown();
        } catch (Exception ignore) {
        }
    }
    return theAppObject;
}

From source file:dataServer.StorageRESTClientManager.java

public String getAllSensors(String context) {
    // Default HTTP client and common properties for requests
    requestUrl = null;/*from   ww w.  j  a va  2  s.  c  o m*/

    // Default HTTP response and common properties for responses
    HttpResponse response = null;
    ResponseHandler<String> handler = null;
    int status = 0;
    String body = null;

    // Query for sensor list
    requestUrl = new StringBuilder("http://" + storageLocation + ":" + storagePortRegistryC
            + storageRegistryContext + "/query/sensor/list2?dataset=" + context);

    //System.out.println("Request URL: "+requestUrl);

    try {
        HttpGet query = new HttpGet(requestUrl.toString());
        query.setHeader("Content-type", "application/json");
        response = client.execute(query);

        // Check status code
        status = response.getStatusLine().getStatusCode();
        if (status != 200) {
            throw new RuntimeException("Failed! HTTP error code: " + status);
        }

        // Get body
        handler = new BasicResponseHandler();
        body = handler.handleResponse(response);

        //System.out.println("body: "+body);

        //System.out.println("SENSOR LIST: " + body);
        return body;
    } catch (Exception e) {
        System.out.println(e.getClass().getName() + ": " + e.getMessage());
        return null;
    }
}

From source file:dataServer.StorageRESTClientManager.java

public String getMouldProperties(String mouldId) {
    // Default HTTP client and common properties for requests
    requestUrl = null;/*from  ww  w .  j a  va 2  s .  c  o  m*/
    params = null;
    queryString = null;

    // Default HTTP response and common properties for responses
    HttpResponse response = null;
    ResponseHandler<String> handler = null;
    int status = 0;
    String body = null;

    // Query for mould properties
    requestUrl = new StringBuilder("http://" + storageLocation + ":" + storagePortRegistryC
            + storageRegistryContext + "/query/mould/properties");

    params = new LinkedList<NameValuePair>();
    params.add(new BasicNameValuePair("sensorId", mouldId));

    queryString = URLEncodedUtils.format(params, "utf-8");
    requestUrl.append("?");
    requestUrl.append(queryString);

    try {
        HttpGet query = new HttpGet(requestUrl.toString());
        query.setHeader("Content-type", "application/json");
        response = client.execute(query);

        // Check status code
        status = response.getStatusLine().getStatusCode();
        if (status != 200) {
            throw new RuntimeException("Failed! HTTP error code: " + status);
        }

        // Get body
        handler = new BasicResponseHandler();
        body = handler.handleResponse(response);

        //System.out.println("MOULD PROPRIETIES: " + body);
        return body;
    } catch (Exception e) {
        System.out.println(e.getClass().getName() + ": " + e.getMessage());
        return null;
    }
}

From source file:dataServer.StorageRESTClientManager.java

public String getMachineProperties(String machineId) {
    // Default HTTP client and common properties for requests
    requestUrl = null;/*from ww  w  .  j a  v a2 s .  co m*/
    params = null;
    queryString = null;

    // Default HTTP response and common properties for responses
    HttpResponse response = null;
    ResponseHandler<String> handler = null;
    int status = 0;
    String body = null;

    // Query for machine properties
    requestUrl = new StringBuilder("http://" + storageLocation + ":" + storagePortRegistryC
            + storageRegistryContext + "/query/machine/properties");

    params = new LinkedList<NameValuePair>();
    params.add(new BasicNameValuePair("machineId", machineId));

    queryString = URLEncodedUtils.format(params, "utf-8");
    requestUrl.append("?");
    requestUrl.append(queryString);

    try {
        HttpGet query = new HttpGet(requestUrl.toString());
        query.setHeader("Content-type", "application/json");
        response = client.execute(query);

        // Check status code
        status = response.getStatusLine().getStatusCode();
        if (status != 200) {
            throw new RuntimeException("Failed! HTTP error code: " + status);
        }

        // Get body
        handler = new BasicResponseHandler();
        body = handler.handleResponse(response);

        //System.out.println("MACHINE PROPRIETIES: " + body);
        return body;
    } catch (Exception e) {
        System.out.println(e.getClass().getName() + ": " + e.getMessage());
        return null;
    }
}

From source file:dataServer.StorageRESTClientManager.java

public String getProductProperties(String productId) {
    // Default HTTP client and common properties for requests
    requestUrl = null;/* w  ww  .j a v  a2s . c  om*/
    params = null;
    queryString = null;

    // Default HTTP response and common properties for responses
    HttpResponse response = null;
    ResponseHandler<String> handler = null;
    int status = 0;
    String body = null;

    // Query for product properties
    requestUrl = new StringBuilder("http://" + storageLocation + ":" + storagePortRegistryC
            + storageRegistryContext + "/query/product/properties");

    params = new LinkedList<NameValuePair>();
    params.add(new BasicNameValuePair("productId", productId));

    queryString = URLEncodedUtils.format(params, "utf-8");
    requestUrl.append("?");
    requestUrl.append(queryString);

    try {
        HttpGet query = new HttpGet(requestUrl.toString());
        query.setHeader("Content-type", "application/json");
        response = client.execute(query);

        // Check status code
        status = response.getStatusLine().getStatusCode();
        if (status != 200) {
            throw new RuntimeException("Failed! HTTP error code: " + status);
        }

        // Get body
        handler = new BasicResponseHandler();
        body = handler.handleResponse(response);

        //System.out.println("PRODUCT PROPRIETIES: " + body);
        return body;
    } catch (Exception e) {
        System.out.println(e.getClass().getName() + ": " + e.getMessage());
        return null;
    }
}