Example usage for org.apache.commons.httpclient.methods PutMethod setRequestBody

List of usage examples for org.apache.commons.httpclient.methods PutMethod setRequestBody

Introduction

In this page you can find the example usage for org.apache.commons.httpclient.methods PutMethod setRequestBody.

Prototype

public void setRequestBody(InputStream paramInputStream) 

Source Link

Usage

From source file:ServiceController.java

@RequestMapping("/service")
public @ResponseBody Service service(
        @RequestParam(value = "authentication", required = false, defaultValue = "Error") String authentication,
        @RequestParam(value = "hostid", required = false, defaultValue = "") String hostid,
        @RequestParam(value = "metricType", required = false, defaultValue = "") String name)
        throws FileNotFoundException, UnsupportedEncodingException, IOException {

    Properties props = new Properties();
    FileInputStream fis = new FileInputStream("properties.xml");
    //loading properites from properties file
    props.loadFromXML(fis);//ww  w .  j  ava  2s. c o  m

    String server_ip = props.getProperty("server_ip");
    String ZABBIX_API_URL = "http://" + server_ip + "/api_jsonrpc.php"; // 1.2.3.4 is your zabbix_server_ip

    PutMethod putMethod = new PutMethod(ZABBIX_API_URL);

    // content-type is controlled in api_jsonrpc.php, so set it like this
    putMethod.setRequestHeader("Content-Type", "application/json-rpc");
    String request = setRequest("net.tcp.service", authentication, hostid);

    putMethod.setRequestBody(request); // put the json object as input stream into request body 

    return serviceResponse(hostid, putMethod, name);
}

From source file:com.serena.rlc.jenkins.plugins.rlcnotifier.RLCSite.java

public String executeJSONPut(URI uri, String putContents) throws Exception {
    String result = null;//from   ww w. j av a  2s .c  om
    HttpClient httpClient = new HttpClient();

    if ("https".equalsIgnoreCase(uri.getScheme())) {
        ProtocolSocketFactory socketFactory = new OpenSSLProtocolSocketFactory();
        Protocol https = new Protocol("https", socketFactory, 443);
        Protocol.registerProtocol("https", https);
    }

    PutMethod method = new PutMethod(uri.toString());
    if (putContents != null)
        method.setRequestBody(putContents);
    method.setRequestHeader("Content-Type", "application/json");
    method.setRequestHeader("charset", "utf-8");
    try {
        HttpClientParams params = httpClient.getParams();
        params.setAuthenticationPreemptive(true);

        UsernamePasswordCredentials clientCredentials = new UsernamePasswordCredentials(user, password);
        httpClient.getState().setCredentials(AuthScope.ANY, clientCredentials);

        int responseCode = httpClient.executeMethod(method);

        //if (responseCode < 200 || responseCode < 300) {
        if (responseCode != 200 && responseCode != 204) {
            throw new Exception("Serena RLC returned error code: " + responseCode);
        } else {
            result = method.getResponseBodyAsString();
        }
    } catch (Exception e) {
        throw new Exception("Error connecting to Serena RLC: " + e.getMessage());
    } finally {
        method.releaseConnection();
    }

    return result;
}

From source file:com.cellbots.eyes.EyesActivity.java

public void surfaceDestroyed(SurfaceHolder holder) {
    mCamera.stopPreview();//from  w  ww. ja  v  a 2 s . co m
    mCamera.release();
    mCamera = null;

    try {
        PutMethod put = new PutMethod(putUrl);
        put.setRequestBody(new ByteArrayInputStream(new byte[0]));
        put.execute(mHttpState, mConnection);
    } catch (NoHttpResponseException e) {
        // Silently ignore this.
    } catch (IOException e) {
        e.printStackTrace();
        resetConnection();
    }
}

From source file:com.cellbots.local.EyesView.java

public void surfaceDestroyed(SurfaceHolder holder) {
    mCamera.stopPreview();/*from  w  w  w  .  ja va2  s  . c o m*/
    mCamera.release();
    mCamera = null;
    // If putUrl is null, it means that only personas overlay was requested.
    if (putUrl == null) {
        return;
    }
    if (isLocalUrl) {
        mParent.setRemoteEyesImage(new byte[0]);
        return;
    }
    try {
        PutMethod put = new PutMethod(putUrl);
        put.setRequestBody(new ByteArrayInputStream(new byte[0]));
        put.execute(mHttpState, mConnection);
    } catch (NoHttpResponseException e) {
        // Silently ignore this.
    } catch (IOException e) {
        e.printStackTrace();
        resetConnection();
    }
}

From source file:com.cellbots.eyes.EyesActivity.java

private void uploadImage(byte[] imageData) {
    try {/*from  ww w .  ja va  2s.  c  o  m*/
        YuvImage yuvImage = new YuvImage(imageData, previewFormat, previewWidth, previewHeight, null);
        yuvImage.compressToJpeg(r, 20, out); // Tweak the quality here - 20
        // seems pretty decent for quality + size.
        if (putUrl.contains("127.0.0.1") || putUrl.contains("localhost")) {
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            resetConnection();
        }
        PutMethod put = new PutMethod(putUrl);
        put.setRequestBody(new ByteArrayInputStream(out.toByteArray()));
        int result = put.execute(mHttpState, mConnection);
        //Log.e("result", result + "");
    } catch (UnsupportedEncodingException e) {
        Log.e(TAG, "UnsupportedEncodingException: Error uploading image: " + e.getMessage());
    } catch (IllegalStateException e) {
        Log.e(TAG, "IllegalStateException: Error uploading image: " + e.getMessage());
        resetConnection();
    } catch (ClientProtocolException e) {
        Log.e(TAG, "ClientProtocolException: Error uploading image: " + e.getMessage());
        resetConnection();
    } catch (UnknownHostException e) {
        Log.e(TAG, "UnknownHostException: Error uploading image: " + e.getMessage());
        resetConnection();
    } catch (NoHttpResponseException e) {
        // Silently ignore this.
    } catch (IOException e) {
        Log.e(TAG, "IOException: Error uploading image: " + e.getMessage());
        resetConnection();
    } finally {
        out.reset();
        if (mCamera != null) {
            mCamera.addCallbackBuffer(mCallbackBuffer);
        }
        isUploading = false;
    }

}

From source file:com.cellbots.local.EyesView.java

private void uploadImage(byte[] imageData) {
    try {/*from   w  w  w  .  j  a va  2 s.co  m*/
        YuvImage yuvImage = new YuvImage(imageData, previewFormat, previewWidth, previewHeight, null);
        yuvImage.compressToJpeg(r, 20, out); // Tweak the quality here - 20
        // seems pretty decent for quality + size.
        if (isLocalUrl) {
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            mParent.setRemoteEyesImage(out.toByteArray());
        } else {
            PutMethod put = new PutMethod(putUrl);
            put.setRequestBody(new ByteArrayInputStream(out.toByteArray()));
            int result = put.execute(mHttpState, mConnection);
        }
        //Log.e("result", result + "");
    } catch (UnsupportedEncodingException e) {
        Log.e(TAG, "UnsupportedEncodingException: Error uploading image: " + e.getMessage());
    } catch (IllegalStateException e) {
        Log.e(TAG, "IllegalStateException: Error uploading image: " + e.getMessage());
        resetConnection();
    } catch (ClientProtocolException e) {
        Log.e(TAG, "ClientProtocolException: Error uploading image: " + e.getMessage());
        resetConnection();
    } catch (UnknownHostException e) {
        Log.e(TAG, "UnknownHostException: Error uploading image: " + e.getMessage());
        resetConnection();
    } catch (NoHttpResponseException e) {
        // Silently ignore this.
    } catch (IOException e) {
        Log.e(TAG, "IOException: Error uploading image: " + e.getMessage());
        resetConnection();
    } finally {
        out.reset();
        if (mCamera != null) {
            mCamera.addCallbackBuffer(mCallbackBuffer);
        }
        isUploading = false;
    }

}

From source file:com.urbancode.ds.jenkins.plugins.serenarapublisher.UrbanDeploySite.java

public String executeJSONPut(URI uri, String putContents) throws Exception {
    String result = null;/*from  w  w w . java  2 s .c o  m*/
    HttpClient httpClient = new HttpClient();

    if ("https".equalsIgnoreCase(uri.getScheme())) {
        ProtocolSocketFactory socketFactory = new OpenSSLProtocolSocketFactory();
        Protocol https = new Protocol("https", socketFactory, 443);
        Protocol.registerProtocol("https", https);
    }

    PutMethod method = new PutMethod(uri.toString());
    setDirectSsoInteractionHeader(method);
    method.setRequestBody(putContents);
    method.setRequestHeader("Content-Type", "application/json");
    method.setRequestHeader("charset", "utf-8");
    try {
        HttpClientParams params = httpClient.getParams();
        params.setAuthenticationPreemptive(true);

        UsernamePasswordCredentials clientCredentials = new UsernamePasswordCredentials(user, password);
        httpClient.getState().setCredentials(AuthScope.ANY, clientCredentials);

        int responseCode = httpClient.executeMethod(method);

        //if (responseCode < 200 || responseCode < 300) {
        if (responseCode != 200 && responseCode != 204) {
            throw new Exception("Serena DA returned error code: " + responseCode);
        } else {
            result = method.getResponseBodyAsString();
        }
    } catch (Exception e) {
        throw new Exception("Error connecting to SerenaRA: " + e.getMessage());
    } finally {
        method.releaseConnection();
    }

    return result;
}

From source file:AuthenticationController.java

@RequestMapping("/authentication")
public @ResponseBody Authentication authentication(
        @RequestParam(value = "username", required = true, defaultValue = "") String username,
        @RequestParam(value = "password", required = true, defaultValue = "") String password)
        throws JSONException, FileNotFoundException, UnsupportedEncodingException, IOException {

    Properties props = new Properties();
    FileInputStream fis = new FileInputStream("properties.xml");
    // loading properites from properties file
    props.loadFromXML(fis);//from  w ww. j av a  2  s  .c o m

    String server_ip = props.getProperty("server_ip");
    String ZABBIX_API_URL = "http://" + server_ip + "/api_jsonrpc.php"; // 1.2.3.4 is your zabbix_server_ip

    HttpClient client = new HttpClient();

    PutMethod putMethod = new PutMethod(ZABBIX_API_URL);
    putMethod.setRequestHeader("Content-Type", "application/json-rpc"); // content-type is controlled in api_jsonrpc.php, so set it like this

    // create json object for apiinfo.version 
    JSONObject jsonObj = new JSONObject(
            "{\"jsonrpc\":\"2.0\",\"method\":\"user.authenticate\",\"params\":{\"user\":\"" + username
                    + "\",\"password\":\"" + password + "\"},\"auth\": null,\"id\":0}");

    putMethod.setRequestBody(jsonObj.toString()); // put the json object as input stream into request body 

    String loginResponse = "";

    try {
        client.executeMethod(putMethod); // send to request to the zabbix api

        loginResponse = putMethod.getResponseBodyAsString(); // read the result of the response

        // Work with the data using methods like...
        JSONObject obj = new JSONObject(loginResponse);
        String result = obj.getString("result");
        System.out.println(result);

        return new Authentication(String.format(result));

    } catch (HttpException e) {
        e.printStackTrace();

    } catch (IOException e) {
        e.printStackTrace();
    }
    return new Authentication();
}

From source file:NetworkController.java

@RequestMapping("/network")
public @ResponseBody Network network(
        @RequestParam(value = "authentication", required = false, defaultValue = "Error") String authentication,
        @RequestParam(value = "hostid", required = false, defaultValue = "") String hostid,
        @RequestParam(value = "metricType", required = false, defaultValue = "") String name)
        throws FileNotFoundException, UnsupportedEncodingException, IOException {

    Properties props = new Properties();
    FileInputStream fis = new FileInputStream("properties.xml");
    //loading properites from properties file
    props.loadFromXML(fis);/*from  www.  j a v  a 2s  .  com*/

    String server_ip = props.getProperty("server_ip");
    String ZABBIX_API_URL = "http://" + server_ip + "/api_jsonrpc.php"; // 1.2.3.4 is your zabbix_server_ip

    PutMethod putMethod = new PutMethod(ZABBIX_API_URL);

    // content-type is controlled in api_jsonrpc.php, so set it like this
    putMethod.setRequestHeader("Content-Type", "application/json-rpc");

    String request = "";

    if (name.equals("latency"))
        request = setRequest("icmppingsec", authentication, hostid);
    else if (name.equals("packetloss"))
        request = setRequest("icmppingloss", authentication, hostid);
    else
        request = setRequest("net", authentication, hostid);

    putMethod.setRequestBody(request); // put the json object as input stream into request body 

    return networkResponse(hostid, putMethod, name);
}

From source file:HostController.java

@RequestMapping("/hosts")
public @ResponseBody Host host(
        @RequestParam(value = "authentication", required = false, defaultValue = "") String authentication)
        throws FileNotFoundException, UnsupportedEncodingException, IOException {

    Properties props = new Properties();
    FileInputStream fis = new FileInputStream("properties.xml");
    //loading properites from properties file
    props.loadFromXML(fis);/*w  ww. j a v a  2 s .com*/

    String server_ip = props.getProperty("server_ip");
    String ZABBIX_API_URL = "http://" + server_ip + "/api_jsonrpc.php"; // 1.2.3.4 is your zabbix_server_ip

    HttpClient client = new HttpClient();

    PutMethod putMethod = new PutMethod(ZABBIX_API_URL);
    // content-type is controlled in api_jsonrpc.php, so set it like this
    putMethod.setRequestHeader("Content-Type", "application/json-rpc");
    // create json object for apiinfo.version 
    JSONParser parser = new JSONParser();
    JSONObject jsonObj = new JSONObject();
    JSONArray list = new JSONArray();
    jsonObj.put("jsonrpc", "2.0");
    jsonObj.put("method", "host.get");
    JSONObject params = new JSONObject();
    params.put("output", "extend");
    jsonObj.put("params", params);
    jsonObj.put("auth", authentication);// todo
    jsonObj.put("id", new Integer(1));

    putMethod.setRequestBody(jsonObj.toString()); // put the json object as input stream into request body 

    String loginResponse = "";

    try {
        client.executeMethod(putMethod); // send to request to the zabbix api

        loginResponse = putMethod.getResponseBodyAsString(); // read the result of the response

        Object obj = parser.parse(loginResponse);
        JSONObject obj2 = (JSONObject) obj;
        String jsonrpc = (String) obj2.get("jsonrpc");
        JSONArray array = (JSONArray) obj2.get("result");

        for (int i = 0; i < array.size(); i++) {
            JSONObject tobj = (JSONObject) array.get(i);

            JSONObject objret = new JSONObject();

            objret.put("hostid", tobj.get("hostid"));
            objret.put("hostName", tobj.get("host"));

            list.add(objret);
        }
        return new Host(list);

    } catch (HttpException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ParseException pe) {
        pe.printStackTrace();
    }
    return new Host(
            "Error: please provide the appropriate input parameters of the metric you are looking for its corresponding monitoring data:");
}