Example usage for org.apache.http.entity BasicHttpEntity setContent

List of usage examples for org.apache.http.entity BasicHttpEntity setContent

Introduction

In this page you can find the example usage for org.apache.http.entity BasicHttpEntity setContent.

Prototype

public void setContent(InputStream inputStream) 

Source Link

Usage

From source file:com.tmount.business.cloopen.restAPI.RestAPI.java

public String getSubAccountSid(String accountSid, String authToken, String appId)
        throws NoSuchAlgorithmException, KeyManagementException {
    // ?/*from   www  . j a v  a 2s .  com*/
    String result = "";
    // HttpClient
    CcopHttpClient chc = new CcopHttpClient();
    DefaultHttpClient httpclient = chc.registerSSL("app.cloopen.com", "TLS", 8883, "https");
    try {
        // URL
        String timestamp = DateUtil.dateToStr(new Date(), DateUtil.DATE_TIME_NO_SLASH);
        // md5(Id +? + )
        String sig = accountSid + authToken + timestamp;
        // MD5
        EncryptUtil eu = new EncryptUtil();
        String signature = eu.md5Digest(sig);
        String url = (new StringBuilder(hostname)).append("/").append(softVer).append("/Accounts/")
                .append(accountSid).append("/GetSubAccounts?sig=").append(signature).toString();
        // HttpPost
        System.out.println("url:" + url);
        HttpPost httppost = new HttpPost(url);
        setHttpHeader(httppost);
        String src = accountSid + ":" + timestamp;
        // base64(Id + ? +)
        String auth = eu.base64Encoder(src);
        httppost.setHeader("Authorization", auth);

        String body = null;
        if (!JsonUtils.JSON_ENABLE) {
            // Body
            body = (new StringBuilder("<?xml version='1.0' encoding='utf-8'?><SubAccount>").append("<appId>"))
                    .append(appId).append("</appId>").append("<startNo>1</startNo>")
                    .append("<offset>100</offset></SubAccount>").toString();
        }
        System.out.println("CreateSubAccount : request _" + body);

        BasicHttpEntity requestBody = new BasicHttpEntity();
        requestBody.setContent(new ByteArrayInputStream(body.getBytes("UTF-8")));
        requestBody.setContentLength(body.getBytes("UTF-8").length);
        httppost.setEntity(requestBody);

        // 
        HttpResponse response = httpclient.execute(httppost);

        // ???
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            result = EntityUtils.toString(entity, "UTF-8");
        }
        // ?HTTP??
        EntityUtils.consume(entity);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        // 
        httpclient.getConnectionManager().shutdown();
    }
    return result;
}

From source file:com.tmount.business.cloopen.restAPI.RestAPI.java

/**
* @brief               ??//from   ww  w.j  a  v a2s  .  c o m
* @param accountSid         ?
* @param authToken         ?
* @param appId            id
* @param friendlyName      
* @param status            ?
* @return Httppost          ???
* @throws NoSuchAlgorithmException
* @throws KeyManagementException  jdbc.properties
*/
public String CreateSubAccount(String accountSid, String authToken, String appId, String friendlyName,
        String status) throws NoSuchAlgorithmException, KeyManagementException {
    // ?
    String result = "";
    // HttpClient
    CcopHttpClient chc = new CcopHttpClient();
    DefaultHttpClient httpclient = chc.registerSSL(domain, "TLS", 8883, "https");
    try {
        // URL
        String timestamp = DateUtil.dateToStr(new Date(), DateUtil.DATE_TIME_NO_SLASH);
        // md5(Id +? + )
        String sig = accountSid + authToken + timestamp;
        // MD5
        EncryptUtil eu = new EncryptUtil();
        String signature = eu.md5Digest(sig);
        String url = (new StringBuilder(hostname)).append(":").append(port).append("/").append(softVer)
                .append("/Accounts/").append(accountSid).append("/SubAccounts?sig=").append(signature)
                .toString();
        // HttpPost
        HttpPost httppost = new HttpPost(url);
        setHttpHeader(httppost);
        String src = accountSid + ":" + timestamp;
        // base64(Id + ? +)
        String auth = eu.base64Encoder(src);
        httppost.setHeader("Authorization", auth);
        String body = null;
        if (!JsonUtils.JSON_ENABLE) {
            // Body
            body = (new StringBuilder("<?xml version='1.0' encoding='utf-8'?><SubAccount>").append("<appId>"))
                    .append(appId).append("</appId>").append("<friendlyName>").append(friendlyName)
                    .append("</friendlyName>").append("</SubAccount>").toString();
        } else {
            SubAccountCreate subAccount = new SubAccountCreate(appId, friendlyName, accountSid);
            Gson gson = new Gson();
            body = gson.toJson(subAccount);
        }
        System.out.println("CreateSubAccount : request _" + body);
        BasicHttpEntity requestBody = new BasicHttpEntity();
        requestBody.setContent(new ByteArrayInputStream(body.getBytes("UTF-8")));
        requestBody.setContentLength(body.getBytes("UTF-8").length);
        httppost.setEntity(requestBody);
        // 
        HttpResponse response = httpclient.execute(httppost);
        // ???
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            result = EntityUtils.toString(entity, "UTF-8");
        }
        // ?HTTP??
        EntityUtils.consume(entity);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        // 
        httpclient.getConnectionManager().shutdown();
    }
    return result;
}

From source file:com.tmount.business.cloopen.restAPI.RestAPI.java

/**
 * ??//from   w w w  .jav a2s  .  c om
 * @param accountSid
 * @param authToken
 * @param appId
 * @param subAccountSid
 * @return
 * @throws NoSuchAlgorithmException
 * @throws KeyManagementException
 */
public String CloseSubAccount(String accountSid, String authToken, String appId, String subAccountSid)
        throws NoSuchAlgorithmException, KeyManagementException {
    // ?
    String result = "";
    // HttpClient
    CcopHttpClient chc = new CcopHttpClient();
    DefaultHttpClient httpclient = chc.registerSSL("app.cloopen.com", "TLS", 8883, "https");
    try {
        // URL
        String timestamp = DateUtil.dateToStr(new Date(), DateUtil.DATE_TIME_NO_SLASH);
        // md5(Id +? + )
        String sig = accountSid + authToken + timestamp;
        // MD5
        EncryptUtil eu = new EncryptUtil();
        String signature = eu.md5Digest(sig);
        String url = (new StringBuilder(hostname)).append("/").append(softVer).append("/Accounts/")
                .append(accountSid).append("/CloseSubAccount?sig=").append(signature).toString();
        // HttpPost
        System.out.println("url:" + url);
        HttpPost httppost = new HttpPost(url);
        setHttpHeader(httppost);
        String src = accountSid + ":" + timestamp;
        // base64(Id + ? +)
        String auth = eu.base64Encoder(src);
        httppost.setHeader("Authorization", auth);

        String body = null;
        if (!JsonUtils.JSON_ENABLE) {
            // Body
            body = (new StringBuilder("<?xml version='1.0' encoding='utf-8'?><SubAccount>").append("<appId>"))
                    .append(appId).append("</appId>")
                    .append("<subAccountSid>" + subAccountSid + "</subAccountSid> </SubAccount>").toString();
        }
        System.out.println("CloseSubAccount : request _" + body);

        BasicHttpEntity requestBody = new BasicHttpEntity();
        requestBody.setContent(new ByteArrayInputStream(body.getBytes("UTF-8")));
        requestBody.setContentLength(body.getBytes("UTF-8").length);
        httppost.setEntity(requestBody);

        // 
        HttpResponse response = httpclient.execute(httppost);

        // ???
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            result = EntityUtils.toString(entity, "UTF-8");
        }
        // ?HTTP??
        EntityUtils.consume(entity);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        // 
        httpclient.getConnectionManager().shutdown();
    }
    return result;
}

From source file:com.tmount.business.cloopen.restAPI.RestAPI.java

/**
* @brief            ??/*from ww w.  jav a  2 s  . c  o m*/
* @param subAccountSid   ??   
* @param subToken      ??
* @param voipAccount   VoIP?
* @param from         ??
* @param to            ??
* @return Httppost       ???
* @throws NoSuchAlgorithmException
* @throws KeyManagementException
*/
public String CallBack(String subAccountSid, String subToken, String voipAccount, String from, String to)
        throws NoSuchAlgorithmException, KeyManagementException {
    // ?
    String result = "";
    // HttpClient
    CcopHttpClient chc = new CcopHttpClient();
    DefaultHttpClient httpclient = chc.registerSSL("sandboxapp.cloopen.com", "TLS", 8883, "https");
    try {
        // URL
        String timestamp = DateUtil.dateToStr(new Date(), DateUtil.DATE_TIME_NO_SLASH);// 
        // md5(?Id + ?? + )
        String sig = subAccountSid + subToken + timestamp;
        // MD5
        EncryptUtil eu = new EncryptUtil();
        String signature = eu.md5Digest(sig);

        String url = (new StringBuilder(hostname)).append(":").append(port).append("/").append(softVer)
                .append("/SubAccounts/").append(subAccountSid).append("/Calls/Callback?sig=").append(signature)
                .toString();
        // HttpPost
        HttpPost httppost = new HttpPost(url);
        setHttpHeader(httppost);
        String src = subAccountSid + ":" + timestamp;
        String auth = eu.base64Encoder(src);
        httppost.setHeader("Authorization", auth);// base64(?Id + ? + )

        String body = null;
        if (!JsonUtils.JSON_ENABLE) {

            // Body
            body = (new StringBuilder("<?xml version='1.0' encoding='utf-8'?><CallBack>").append("<from>")
                    .append(from).append("</from>").append("<to>").append(to).append("</to></CallBack>")
                    .toString());
        } else {
            CallBackRequest callBackRequest = new CallBackRequest(subAccountSid, voipAccount, from, to);
            Gson gson = new Gson();
            body = gson.toJson(callBackRequest);
        }
        System.out.println("CreateSubAccount : request _" + body);

        BasicHttpEntity requestBody = new BasicHttpEntity();
        requestBody.setContent(new ByteArrayInputStream(body.getBytes("UTF-8")));
        requestBody.setContentLength(body.getBytes("UTF-8").length);
        httppost.setEntity(requestBody);

        // 
        HttpResponse response = httpclient.execute(httppost);

        // ???
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            result = EntityUtils.toString(entity, "UTF-8");
        }
        // ?HTTP??
        EntityUtils.consume(entity);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        // 
        httpclient.getConnectionManager().shutdown();
    }
    return result;
}

From source file:com.tmount.business.cloopen.restAPI.RestAPI.java

/**
* @brief            ??// w  w  w  . j ava 2 s.  c om
* @param accountSid      ?
* @param authToken      ?
* @param appId         id
* @param verifyCode      ?????4-20?
* @param playTimes      1?3
* @param to            ??
*/
public String VoiceVerifyCode(String accountSid, String authToken, String appId, String verifyCode,
        String playTimes, String to) throws NoSuchAlgorithmException, KeyManagementException {
    // ?
    String result = "";
    // HttpClient
    CcopHttpClient chc = new CcopHttpClient();
    DefaultHttpClient httpclient = chc.registerSSL("sandboxapp.cloopen.com", "TLS", 8883, "https");
    try {
        // URL
        String timestamp = DateUtil.dateToStr(new Date(), DateUtil.DATE_TIME_NO_SLASH);
        // md5(Id +? + )
        String sig = accountSid + authToken + timestamp;
        // MD5
        EncryptUtil eu = new EncryptUtil();
        String signature = eu.md5Digest(sig);
        String url = (new StringBuilder(hostname)).append(":").append(port).append("/").append(softVer)
                .append("/Accounts/").append(accountSid).append("/Calls/VoiceVerify?sig=").append(signature)
                .toString();
        // HttpPost
        HttpPost httppost = new HttpPost(url);
        setHttpHeader(httppost);
        String src = accountSid + ":" + timestamp;
        // base64(Id + ? +)
        String auth = eu.base64Encoder(src);
        httppost.setHeader("Authorization", auth);

        String body = null;
        if (!JsonUtils.JSON_ENABLE) {
            // Body
            body = (new StringBuilder("<?xml version='1.0' encoding='utf-8'?><VoiceVerify>").append("<appId>"))
                    .append(appId).append("</appId>").append("<verifyCode>").append(verifyCode)
                    .append("</verifyCode>").append("<playTimes>").append(playTimes).append("</playTimes>")
                    .append("<to>").append(to).append("</to></VoiceVerify>").toString();
        } else {

            VoiceVerifyCodeRequest verifyCodeRequest = new VoiceVerifyCodeRequest(appId, verifyCode, playTimes,
                    to);
            Gson gson = new Gson();
            body = gson.toJson(verifyCodeRequest);
        }
        System.out.println("CreateSubAccount : request _" + body);

        BasicHttpEntity requestBody = new BasicHttpEntity();
        requestBody.setContent(new ByteArrayInputStream(body.getBytes("UTF-8")));
        requestBody.setContentLength(body.getBytes("UTF-8").length);
        httppost.setEntity(requestBody);

        // 
        HttpResponse response = httpclient.execute(httppost);

        // ???
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            result = EntityUtils.toString(entity, "UTF-8");
        }
        // ?HTTP??
        EntityUtils.consume(entity);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        // 
        httpclient.getConnectionManager().shutdown();
    }
    return result;
}

From source file:com.tmount.business.cloopen.restAPI.RestAPI.java

/**
* @brief            ?/*from  w w w  . j a va2s.  c  om*/
* @param accountSid      ?
* @param authToken      ?
* @param appId         id
* @param mediaName      ??
* @param mediaTxt      
* @param playTimes      
* @param to            ???
*/
public String LandingCalls(String accountSid, String authToken, String appId, String mediaName,
        String playTimes, String to) throws NoSuchAlgorithmException, KeyManagementException {
    // ?
    String result = "";
    // HttpClient
    CcopHttpClient chc = new CcopHttpClient();
    DefaultHttpClient httpclient = chc.registerSSL("sandboxapp.cloopen.com", "TLS", 8883, "https");
    try {
        // URL
        String timestamp = DateUtil.dateToStr(new Date(), DateUtil.DATE_TIME_NO_SLASH);
        // md5(Id +? + )
        String sig = accountSid + authToken + timestamp;
        // MD5
        EncryptUtil eu = new EncryptUtil();
        String signature = eu.md5Digest(sig);

        String url = (new StringBuilder(hostname)).append(":").append(port).append("/").append(softVer)
                .append("/Accounts/").append(accountSid).append("/Calls/LandingCalls?sig=").append(signature)
                .toString();
        // HttpPost
        HttpPost httppost = new HttpPost(url);
        setHttpHeader(httppost);
        String src = accountSid + ":" + timestamp;
        // base64(Id + ? +)
        String auth = eu.base64Encoder(src);
        httppost.setHeader("Authorization", auth);

        String body = null;
        if (!JsonUtils.JSON_ENABLE) {
            // Body
            StringBuilder bodyBuilder = new StringBuilder(
                    "<?xml version='1.0' encoding='utf-8'?><LandingCall>");
            bodyBuilder.append("<appId>").append(appId).append("</appId>");
            bodyBuilder.append("<mediaName>").append(mediaName).append("</mediaName>");
            bodyBuilder.append("<playTimes>").append(playTimes).append("</playTimes>");
            bodyBuilder.append("<to>").append(to).append("</to></LandingCall>");
            body = bodyBuilder.toString();

        } else {
            LandingCallsRequest landingCallsRequest = new LandingCallsRequest(appId, mediaName, playTimes, to);
            Gson gson = new Gson();
            body = gson.toJson(landingCallsRequest);
        }
        System.out.println("CreateSubAccount : request _" + body);

        BasicHttpEntity requestBody = new BasicHttpEntity();
        requestBody.setContent(new ByteArrayInputStream(body.getBytes("UTF-8")));
        requestBody.setContentLength(body.getBytes("UTF-8").length);
        httppost.setEntity(requestBody);

        // 
        HttpResponse response = httpclient.execute(httppost);

        // ???
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            result = EntityUtils.toString(entity, "UTF-8");
        }
        // ?HTTP??
        EntityUtils.consume(entity);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        // 
        httpclient.getConnectionManager().shutdown();
    }
    return result;
}

From source file:com.tmount.business.cloopen.restAPI.RestAPI.java

/**
 * ??/*  w w w . j  a  v  a 2s.  co m*/
 * @param to  ?
 * @param body 
 * @param msgType             0:     1:65  
 * @param sms_type  ?          0:   1:  2???  3tomcat
 * @return
 * @throws NoSuchAlgorithmException
 * @throws KeyManagementException
 */
public String SendSMS(String to, String body, String msgType, String sms_type)
        throws NoSuchAlgorithmException, KeyManagementException {
    String filePath = this.getClass().getClassLoader().getResource("config.properties").toString().substring(6);
    filePath = "/" + filePath;
    AgentSysInfo agentSysInfo = new AgentSysInfo();
    agentSysInfo = PropertiesUtil.getAgentSysInfo(filePath);
    String accountSid = agentSysInfo.getAccountSid();
    String authToken = agentSysInfo.getAuthToken();
    String appId = agentSysInfo.getAppId();
    String subAccountSid = agentSysInfo.getSub_account();
    String app_name = agentSysInfo.getApp_name();
    String reserve_date = agentSysInfo.getReserve_date();
    String reserve_address = agentSysInfo.getReserve_address();
    String sms_time = agentSysInfo.getSms_time();
    String reslut = "";
    // HttpClient
    CcopHttpClient chc = new CcopHttpClient();
    DefaultHttpClient httpclient = chc.registerSSL("app.cloopen.com", "TLS", 8883, "https");
    try {
        // URL
        String timestamp = DateUtil.dateToStr(new Date(), DateUtil.DATE_TIME_NO_SLASH);// 
        // md5(Id + ? + )
        String sig = accountSid + authToken + timestamp;
        // MD5
        EncryptUtil eu = new EncryptUtil();
        String signature = eu.md5Digest(sig);
        String url = (new StringBuilder(hostname)).append(":").append(port).append("/").append(softVer)
                .append("/Accounts/").append(accountSid).append("/SMS/TemplateSMS?sig=")//Messages   TemplateSMS
                .append(signature).toString();
        // HttpPost
        System.out.println("url:" + url);
        HttpPost httppost = new HttpPost(url);
        setHttpHeader(httppost);
        String src = accountSid + ":" + timestamp;
        String auth = eu.base64Encoder(src);
        httppost.setHeader("Authorization", auth);// base64(Id + ? + )
        String bodyData = null;
        logger.info("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~SMS is bigining  sms_type:"
                + sms_type);
        if (!JsonUtils.JSON_ENABLE) {
            // Body
            if (sms_type.equalsIgnoreCase("0"))//?
            {
                bodyData = (new StringBuilder("<?xml version='1.0' encoding='utf-8'?>")).append("<TemplateSMS>")
                        .append("<to>").append(to).append("</to>").append("<appId>").append(appId)
                        .append("</appId>").append("<templateId>4106</templateId>").append("<datas>")
                        .append("<data>" + app_name + "</data>").append("<data>" + body + "</data>")
                        /*.append("<data>"+sms_time+"</data>")*/
                        .append("</datas>").append("</TemplateSMS>").toString();
            } else if (sms_type.equalsIgnoreCase("1"))//
            {
                bodyData = (new StringBuilder("<?xml version='1.0' encoding='utf-8'?>")).append("<TemplateSMS>")
                        .append("<to>").append(to).append("</to>").append("<appId>").append(appId)
                        .append("</appId>").append("<templateId>1519</templateId>").append("<datas>")
                        .append("<data>" + body + "</data>").append("<data>" + sms_time + "</data>")
                        .append("</datas>").append("</TemplateSMS>").toString();
            } else if (sms_type.equalsIgnoreCase("2"))//????
            {
                bodyData = (new StringBuilder("<?xml version='1.0' encoding='utf-8'?>")).append("<TemplateSMS>")
                        .append("<to>").append(to).append("</to>").append("<appId>").append(appId)
                        .append("</appId>").append("<templateId>1520</templateId>").append("<datas>")
                        .append("<data>" + DatatimeUtil.getCurrentDate().toString() + "</data>")//?
                        .append("<data>" + body + "</data>")//??
                        .append("<data>" + reserve_date + "</data>")//?
                        .append("<data>" + reserve_address + "</data>")//??
                        .append("</datas>").append("</TemplateSMS>").toString();
            } else if (sms_type.equalsIgnoreCase("3"))//tomcat 
            {
                bodyData = (new StringBuilder("<?xml version='1.0' encoding='utf-8'?>")).append("<TemplateSMS>")
                        .append("<to>").append(to).append("</to>").append("<appId>").append(appId)
                        .append("</appId>").append("<templateId>1758</templateId>").append("<datas>")
                        .append("<data>" + body + "</data>")//?
                        .append("</datas>").append("</TemplateSMS>").toString();
            }
        } else {
            SendSMSRequest sendSMS = new SendSMSRequest(to, bodyData, msgType, appId, subAccountSid);
            Gson gson = new Gson();
            bodyData = gson.toJson(sendSMS);
        }
        System.out.println("?? : request _" + bodyData);
        BasicHttpEntity requestBody = new BasicHttpEntity();
        requestBody.setContent(new ByteArrayInputStream(bodyData.getBytes("UTF-8")));
        requestBody.setContentLength(bodyData.getBytes("UTF-8").length);
        httppost.setEntity(requestBody);
        // 
        HttpResponse response = httpclient.execute(httppost);
        // ???
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            reslut = EntityUtils.toString(entity, "UTF-8");
        }
        // ?HTTP??
        EntityUtils.consume(entity);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        // 
        httpclient.getConnectionManager().shutdown();
    }
    //???
    return reslut;
}

From source file:org.restcomm.camelgateway.slee.CamelGatewaySbb.java

private void pushContent(HttpUriRequest request, String contentType, String contentEncoding, byte[] content) {

    // TODO: check other preconditions?
    if (contentType != null && content != null && request instanceof HttpEntityEnclosingRequest) {
        BasicHttpEntity entity = new BasicHttpEntity();
        entity.setContent(new ByteArrayInputStream(content));
        entity.setContentLength(content.length);
        entity.setChunked(false);/*from   w w  w.j  a  va  2 s.c o m*/
        if (contentEncoding != null)
            entity.setContentEncoding(contentEncoding);
        entity.setContentType(contentType);
        HttpEntityEnclosingRequest rr = (HttpEntityEnclosingRequest) request;
        rr.setEntity(entity);
    }

}