Example usage for com.google.gson JsonObject toString

List of usage examples for com.google.gson JsonObject toString

Introduction

In this page you can find the example usage for com.google.gson JsonObject toString.

Prototype

@Override
public String toString() 

Source Link

Document

Returns a String representation of this element.

Usage

From source file:com.baomidou.springwind.util.yuntongxin.CCPRestSDK.java

License:Open Source License

/**
 * ??/*from  ww w .j  a v  a 2  s  .c om*/
 * 
 * @param to
 *            ? ???
 * @param mediaName
 *            ?? ??? wavmediaTxt???mediaTxt
 * @param mediaTxt
 *            ?? 
 * @param displayNum
 *            ?? ??????
 * @param playTimes
 *            ?? 1?31
 * @param respUrl
 *            ?? ????Url????
 * @param userData
 *            ?? ??
 * @param txtSpeed
 *            ?? ???-500500mediaTxt?0
 * @param txtVolume
 *            ?? ????-2020mediaTxt?0
 * @param txtPitch
 *            ?? ??-500500mediaTxt?0
 * @param txtBgsound
 *            ?? ?????6?16??0????mediaTxt? 
 * @param playMode
 *            ?? ?? , 0?? 1?0
 * @return
 */
public HashMap<String, Object> landingCall(String to, String mediaName, String mediaTxt, String displayNum,
        String playTimes, String respUrl, String userData, String txtSpeed, String txtVolume, String txtPitch,
        String txtBgsound, String playMode) {
    HashMap<String, Object> validate = accountValidate();
    if (validate != null)
        return validate;
    if (isEmpty(to))
        throw new IllegalArgumentException("?:" + (isEmpty(to) ? " ??? " : "") + "");
    if ((isEmpty(mediaName)) && (isEmpty(mediaTxt)))
        throw new IllegalArgumentException(
                "??????");
    CcopHttpClient chc = new CcopHttpClient();
    DefaultHttpClient httpclient = null;
    try {
        httpclient = chc.registerSSL(SERVER_IP, "TLS", Integer.parseInt(SERVER_PORT), "https");
    } catch (Exception e1) {
        e1.printStackTrace();
        throw new RuntimeException("?httpclient" + e1.getMessage());
    }
    String result = "";
    try {
        HttpPost httppost = (HttpPost) getHttpRequestBase(1, LandingCalls);
        String requsetbody = "";
        if (BODY_TYPE == BodyType.Type_JSON) {
            JsonObject json = new JsonObject();
            json.addProperty("appId", App_ID);
            json.addProperty("to", to);

            if (!(isEmpty(mediaName)))
                json.addProperty("mediaName", mediaName);

            if (!(isEmpty(mediaTxt)))
                json.addProperty("mediaTxt", mediaTxt);

            if (!(isEmpty(displayNum)))
                json.addProperty("displayNum", displayNum);
            if (!(isEmpty(playTimes)))
                json.addProperty("playTimes", playTimes);

            if (!(isEmpty(respUrl)))
                json.addProperty("respUrl", respUrl);
            if (!(isEmpty(userData)))
                json.addProperty("userData", userData);
            if (!(isEmpty(txtSpeed)))
                json.addProperty("txtSpeed", txtSpeed);
            if (!(isEmpty(txtVolume)))
                json.addProperty("txtVolume", txtVolume);
            if (!(isEmpty(txtPitch)))
                json.addProperty("txtPitch", txtPitch);
            if (!(isEmpty(txtBgsound)))
                json.addProperty("txtBgsound", txtBgsound);
            if (!(isEmpty(playMode)))
                json.addProperty("playMode", playMode);

            requsetbody = json.toString();
        } else {
            StringBuilder sb = new StringBuilder("<?xml version='1.0' encoding='utf-8'?><LandingCall>");
            sb.append("<appId>").append(App_ID).append("</appId>").append("<to>").append(to).append("</to>");
            if (!(isEmpty(mediaName)))
                sb.append("<mediaName>").append(mediaName).append("</mediaName>");
            else if (!(isEmpty(mediaName)))
                sb.append("<mediaName>").append(mediaName).append("</mediaName>");

            if (!(isEmpty(mediaTxt)))
                sb.append("<mediaTxt>").append(mediaTxt).append("</mediaTxt>");

            if (!(isEmpty(displayNum)))
                sb.append("<displayNum>").append(displayNum).append("</displayNum>");
            if (!(isEmpty(playTimes)))
                sb.append("<playTimes>").append(playTimes).append("</playTimes>");

            if (!(isEmpty(respUrl)))
                sb.append("<respUrl>").append(respUrl).append("</respUrl>");
            if (!(isEmpty(userData)))
                sb.append("<userData>").append(userData).append("</userData>");
            if (!(isEmpty(txtSpeed)))
                sb.append("<txtSpeed>").append(txtSpeed).append("</txtSpeed>");
            if (!(isEmpty(txtVolume)))
                sb.append("<txtVolume>").append(txtVolume).append("</txtVolume>");
            if (!(isEmpty(txtPitch)))
                sb.append("<txtPitch>").append(txtPitch).append("</txtPitch>");
            if (!(isEmpty(txtBgsound)))
                sb.append("<txtBgsound>").append(txtBgsound).append("</txtBgsound>");
            if (!(isEmpty(playMode)))
                sb.append("<playMode>").append(playMode).append("</playMode>");

            sb.append("</LandingCall>").toString();
            requsetbody = sb.toString();
        }
        logger.info("landingCalls Request body = : " + requsetbody);

        System.out.println("" + requsetbody);
        BasicHttpEntity requestBody = new BasicHttpEntity();
        requestBody.setContent(new ByteArrayInputStream(requsetbody.getBytes("UTF-8")));
        requestBody.setContentLength(requsetbody.getBytes("UTF-8").length);
        httppost.setEntity(requestBody);
        HttpResponse response = httpclient.execute(httppost);
        status = response.getStatusLine().getStatusCode();
        System.out.println("Https??" + status);

        HttpEntity entity = response.getEntity();
        if (entity != null)
            result = EntityUtils.toString(entity, "UTF-8");

        EntityUtils.consume(entity);
    } catch (IOException e) {
        e.printStackTrace();
        logger.error(e.getMessage());
        return getMyError("172001", "");
    } catch (Exception e) {
        e.printStackTrace();
        logger.error(e.getMessage());
        return getMyError("172002", "");
    } finally {
        if (httpclient != null)
            httpclient.getConnectionManager().shutdown();
    }

    logger.info("landingCall response body = " + result);

    try {
        if (BODY_TYPE == BodyType.Type_JSON) {
            return jsonToMap(result);
        } else {
            return xmlToMap(result);
        }
    } catch (Exception e) {

        return getMyError("172003", "");
    }
}

From source file:com.baomidou.springwind.util.yuntongxin.CCPRestSDK.java

License:Open Source License

/**
 * ???/*  w w w .  j  av a2s. c  o m*/
 * 
 * @param to
 *            ? ??????????100
 * @param templateId
 *            ? ?Id
 * @param datas
 *            ?? ???{??}
 * @return
 */
public HashMap<String, Object> sendTemplateSMS(String to, String templateId, String[] datas) {
    HashMap<String, Object> validate = accountValidate();
    if (validate != null)
        return validate;
    if ((isEmpty(to)) || (isEmpty(App_ID)) || (isEmpty(templateId)))
        throw new IllegalArgumentException("?:" + (isEmpty(to) ? " ?? " : "")
                + (isEmpty(templateId) ? " ?Id " : "") + "");
    CcopHttpClient chc = new CcopHttpClient();
    DefaultHttpClient httpclient = null;
    try {
        httpclient = chc.registerSSL(SERVER_IP, "TLS", Integer.parseInt(SERVER_PORT), "https");
    } catch (Exception e1) {
        e1.printStackTrace();
        throw new RuntimeException("?httpclient" + e1.getMessage());
    }
    String result = "";

    try {
        HttpPost httppost = (HttpPost) getHttpRequestBase(1, TemplateSMS);
        String requsetbody = "";
        if (BODY_TYPE == BodyType.Type_JSON) {
            JsonObject json = new JsonObject();
            json.addProperty("appId", App_ID);
            json.addProperty("to", to);
            json.addProperty("templateId", templateId);
            if (datas != null) {
                StringBuilder sb = new StringBuilder("[");
                for (String s : datas) {
                    sb.append("\"" + s + "\"" + ",");
                }
                sb.replace(sb.length() - 1, sb.length(), "]");
                JsonParser parser = new JsonParser();
                JsonArray Jarray = parser.parse(sb.toString()).getAsJsonArray();
                json.add("datas", Jarray);
            }
            requsetbody = json.toString();
        } else {
            StringBuilder sb = new StringBuilder("<?xml version='1.0' encoding='utf-8'?><TemplateSMS>");
            sb.append("<appId>").append(App_ID).append("</appId>").append("<to>").append(to).append("</to>")
                    .append("<templateId>").append(templateId).append("</templateId>");
            if (datas != null) {
                sb.append("<datas>");
                for (String s : datas) {
                    sb.append("<data>").append(s).append("</data>");
                }
                sb.append("</datas>");
            }
            sb.append("</TemplateSMS>").toString();
            requsetbody = sb.toString();
        }
        //?
        System.out.println("" + requsetbody);
        logger.info("sendTemplateSMS Request body =  " + requsetbody);
        BasicHttpEntity requestBody = new BasicHttpEntity();
        requestBody.setContent(new ByteArrayInputStream(requsetbody.getBytes("UTF-8")));
        requestBody.setContentLength(requsetbody.getBytes("UTF-8").length);
        httppost.setEntity(requestBody);

        HttpResponse response = httpclient.execute(httppost);

        //???

        status = response.getStatusLine().getStatusCode();

        System.out.println("Https??" + status);

        HttpEntity entity = response.getEntity();
        if (entity != null)
            result = EntityUtils.toString(entity, "UTF-8");

        EntityUtils.consume(entity);
    } catch (IOException e) {
        e.printStackTrace();
        logger.error(e.getMessage());
        return getMyError("172001", "" + "Https?" + status);
    } catch (Exception e) {
        e.printStackTrace();
        logger.error(e.getMessage());
        return getMyError("172002", "");
    } finally {
        if (httpclient != null)
            httpclient.getConnectionManager().shutdown();
    }

    logger.info("sendTemplateSMS response body = " + result);

    try {
        if (BODY_TYPE == BodyType.Type_JSON) {
            return jsonToMap(result);
        } else {
            return xmlToMap(result);
        }
    } catch (Exception e) {

        return getMyError("172003", "");
    }
}

From source file:com.baomidou.springwind.util.yuntongxin.CCPRestSDK.java

License:Open Source License

/**
 * ?????/*from w  ww.  ja v  a 2  s  . c o  m*/
 * 
 * @param appId
 *            ? Id
 * @param friendlyName
 *            ? ?????
 * @return
 */
public HashMap<String, Object> querySubAccount(String friendlyName) {
    HashMap<String, Object> validate = accountValidate();
    if (validate != null)
        return validate;
    if ((isEmpty(friendlyName))) {
        logger.debug("?: ????? ");
        throw new IllegalArgumentException("?: ????? ");
    }
    CcopHttpClient chc = new CcopHttpClient();
    DefaultHttpClient httpclient = null;
    try {
        httpclient = chc.registerSSL(SERVER_IP, "TLS", Integer.parseInt(SERVER_PORT), "https");
    } catch (Exception e1) {
        e1.printStackTrace();
        throw new RuntimeException("?httpclient" + e1.getMessage());
    }
    String result = "";
    try {
        HttpPost httppost = (HttpPost) getHttpRequestBase(1, Query_SubAccountByName);
        String requsetbody = "";
        if (BODY_TYPE == BodyType.Type_JSON) {
            JsonObject json = new JsonObject();
            json.addProperty("appId", App_ID);
            json.addProperty("friendlyName", friendlyName);
            requsetbody = json.toString();
        } else {
            requsetbody = "<?xml version='1.0' encoding='utf-8'?><SubAccount>" + "<appId>" + App_ID + "</appId>"
                    + "<friendlyName>" + friendlyName + "</friendlyName>" + "</SubAccount>";
        }
        logger.info("querySubAccountByName Request body =  " + requsetbody);
        System.out.println("" + requsetbody);

        BasicHttpEntity requestBody = new BasicHttpEntity();
        requestBody.setContent(new ByteArrayInputStream(requsetbody.getBytes("UTF-8")));
        requestBody.setContentLength(requsetbody.getBytes("UTF-8").length);
        httppost.setEntity(requestBody);
        HttpResponse response = httpclient.execute(httppost);
        status = response.getStatusLine().getStatusCode();
        System.out.println("Https??" + status);

        HttpEntity entity = response.getEntity();
        if (entity != null)
            result = EntityUtils.toString(entity, "UTF-8");

        EntityUtils.consume(entity);
    } catch (IOException e) {
        e.printStackTrace();
        logger.error(e.getMessage());
        return getMyError("172001", "");
    } catch (Exception e) {
        e.printStackTrace();
        logger.error(e.getMessage());
        return getMyError("172002", "");
    } finally {
        if (httpclient != null)
            httpclient.getConnectionManager().shutdown();
    }

    logger.info("querySubAccount result " + result);

    try {
        if (BODY_TYPE == BodyType.Type_JSON) {
            return jsonToMap(result);
        } else {
            return xmlToMap(result);
        }
    } catch (Exception e) {

        return getMyError("172003", "");
    }

}

From source file:com.baomidou.springwind.util.yuntongxin.CCPRestSDK.java

License:Open Source License

/**
 * ????//from w  w  w  .  ja  va2s  . c  o  m
 * 
 * @param startNo
 *            ?? ??0
 * @param offset
 *            ?? ??1?100?
 * @return
 */
public HashMap<String, Object> getSubAccounts(String startNo, String offset) {
    HashMap<String, Object> validate = accountValidate();
    if (validate != null)
        return validate;
    CcopHttpClient chc = new CcopHttpClient();
    DefaultHttpClient httpclient = null;
    try {
        httpclient = chc.registerSSL(SERVER_IP, "TLS", Integer.parseInt(SERVER_PORT), "https");
    } catch (Exception e1) {
        e1.printStackTrace();
        throw new RuntimeException("?httpclient" + e1.getMessage());
    }
    String result = "";
    try {
        HttpPost httppost = (HttpPost) getHttpRequestBase(1, Get_SubAccounts);
        String requsetbody = "";
        if (BODY_TYPE == BodyType.Type_JSON) {
            JsonObject json = new JsonObject();
            json.addProperty("appId", App_ID);
            if (!(isEmpty(startNo)))
                json.addProperty("startNo", startNo);
            if (!(isEmpty(offset)))
                json.addProperty("offset", offset);
            requsetbody = json.toString();
        } else {
            StringBuilder sb = new StringBuilder("<?xml version='1.0' encoding='utf-8'?><SubAccount>");
            sb.append("<appId>").append(App_ID).append("</appId>");
            if (!(isEmpty(startNo)))
                sb.append("<startNo>").append(startNo).append("</startNo>");
            if (!(isEmpty(offset)))
                sb.append("<offset>").append(offset).append("</offset>");
            sb.append("</SubAccount>").toString();
            requsetbody = sb.toString();
        }
        logger.info("GetSubAccounts Request body =  " + requsetbody);
        System.out.println("" + requsetbody);

        BasicHttpEntity requestBody = new BasicHttpEntity();
        requestBody.setContent(new ByteArrayInputStream(requsetbody.getBytes("UTF-8")));
        requestBody.setContentLength(requsetbody.getBytes("UTF-8").length);
        httppost.setEntity(requestBody);
        HttpResponse response = httpclient.execute(httppost);
        status = response.getStatusLine().getStatusCode();
        System.out.println("Https??" + status);

        HttpEntity entity = response.getEntity();
        if (entity != null)
            result = EntityUtils.toString(entity, "UTF-8");

        EntityUtils.consume(entity);
    } catch (IOException e) {
        e.printStackTrace();
        logger.error(e.getMessage());
        return getMyError("172001", "");
    } catch (Exception e) {
        e.printStackTrace();
        logger.error(e.getMessage());
        return getMyError("172002", "");
    } finally {
        if (httpclient != null)
            httpclient.getConnectionManager().shutdown();
    }
    logger.info("getSubAccounts result " + result);

    try {
        if (BODY_TYPE == BodyType.Type_JSON) {
            return jsonToMap(result);
        } else {
            return xmlToMap(result);
        }
    } catch (Exception e) {

        return getMyError("172003", "");
    }
}

From source file:com.baomidou.springwind.util.yuntongxin.CCPRestSDK.java

License:Open Source License

/**
 * ???// w  w  w .  ja  va2  s. c  om
 * 
 * @param friendlyName
 *            ? ?????????????????
 * @return
 */
public HashMap<String, Object> createSubAccount(String friendlyName) {
    HashMap<String, Object> validate = accountValidate();
    if (validate != null)
        return validate;
    if (isEmpty(friendlyName)) {
        logger.debug("?: ????? ");
        throw new IllegalArgumentException("?: ????? ");
    }

    CcopHttpClient chc = new CcopHttpClient();
    DefaultHttpClient httpclient = null;
    try {
        httpclient = chc.registerSSL(SERVER_IP, "TLS", Integer.parseInt(SERVER_PORT), "https");
    } catch (Exception e1) {
        e1.printStackTrace();
        logger.error(e1.getMessage());
        throw new RuntimeException("?httpclient" + e1.getMessage());
    }
    String result = "";
    try {
        HttpPost httppost = (HttpPost) getHttpRequestBase(1, Create_SubAccount);
        String requsetbody = "";

        if (BODY_TYPE == BodyType.Type_JSON) {
            JsonObject json = new JsonObject();
            json.addProperty("appId", App_ID);
            json.addProperty("friendlyName", friendlyName);
            requsetbody = json.toString();
        } else {
            requsetbody = "<?xml version='1.0' encoding='utf-8'?><SubAccount>" + "<appId>" + App_ID + "</appId>"
                    + "<friendlyName>" + friendlyName + "</friendlyName>" + "</SubAccount>";
        }
        logger.info("CreateSubAccount Request body =  " + requsetbody);
        System.out.println("" + requsetbody);

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

        HttpResponse response = httpclient.execute(httppost);
        status = response.getStatusLine().getStatusCode();

        System.out.println("Https??" + status);

        HttpEntity entity = response.getEntity();

        if (entity != null) {
            result = EntityUtils.toString(entity, "UTF-8");
        }

        EntityUtils.consume(entity);
    } catch (IOException e) {
        e.printStackTrace();
        logger.error(e.getMessage());
        return getMyError("172001", "");
    } catch (Exception e) {
        e.printStackTrace();
        logger.error(e.getMessage());
        return getMyError("172002", "");
    } finally {
        if (httpclient != null)
            httpclient.getConnectionManager().shutdown();
    }
    logger.info("createSubAccount response body = " + result);
    try {
        if (BODY_TYPE == BodyType.Type_JSON) {
            return jsonToMap(result);
        } else {
            return xmlToMap(result);
        }
    } catch (Exception e) {

        return getMyError("172003", "");
    }
}

From source file:com.baomidou.springwind.util.yuntongxin.CCPRestSDK.java

License:Open Source License

/**
 * ?//from   w  w  w  .  ja  va  2  s . c om
 * 
 * @param templateId
 *            ?? ?Id????
 * @return
 */
public HashMap<String, Object> QuerySMSTemplate(String templateId) {
    HashMap<String, Object> validate = accountValidate();
    if (validate != null)
        return validate;

    CcopHttpClient chc = new CcopHttpClient();
    DefaultHttpClient httpclient = null;
    try {
        httpclient = chc.registerSSL(SERVER_IP, "TLS", Integer.parseInt(SERVER_PORT), "https");
    } catch (Exception e1) {
        e1.printStackTrace();
        logger.error(e1.getMessage());
        throw new RuntimeException("?httpclient" + e1.getMessage());
    }
    String result = "";
    try {
        HttpPost httppost = (HttpPost) getHttpRequestBase(1, Query_SMSTemplate);
        String requsetbody = "";

        if (BODY_TYPE == BodyType.Type_JSON) {
            JsonObject json = new JsonObject();
            json.addProperty("appId", App_ID);
            json.addProperty("templateId", templateId);
            requsetbody = json.toString();
        } else {
            requsetbody = "<?xml version='1.0' encoding='utf-8'?><Request>" + "<appId>" + App_ID + "</appId>"
                    + "<templateId>" + templateId + "</templateId>" + "</Request>";
        }
        logger.info("QuerySMSTemplate Request body =  " + requsetbody);
        //?
        System.out.println("" + requsetbody);
        BasicHttpEntity requestBody = new BasicHttpEntity();
        requestBody.setContent(new ByteArrayInputStream(requsetbody.getBytes("UTF-8")));
        requestBody.setContentLength(requsetbody.getBytes("UTF-8").length);
        httppost.setEntity(requestBody);

        HttpResponse response = httpclient.execute(httppost);

        //???

        status = response.getStatusLine().getStatusCode();

        System.out.println("Https??" + status);

        HttpEntity entity = response.getEntity();

        if (entity != null) {
            result = EntityUtils.toString(entity, "UTF-8");
        }

        EntityUtils.consume(entity);
    } catch (IOException e) {
        e.printStackTrace();
        logger.error(e.getMessage());
        return getMyError("172001", "");
    } catch (Exception e) {
        e.printStackTrace();
        logger.error(e.getMessage());
        return getMyError("172002", "");
    } finally {
        if (httpclient != null)
            httpclient.getConnectionManager().shutdown();
    }
    logger.info("QuerySMSTemplate response body = " + result);
    try {
        if (BODY_TYPE == BodyType.Type_JSON) {
            return jsonToMap(result);
        } else {
            return xmlToMap(result);
        }
    } catch (Exception e) {

        return getMyError("172003", "");
    }
}

From source file:com.baomidou.springwind.util.yuntongxin.CCPRestSDK.java

License:Open Source License

/**
 * ??/*  w ww .  j  a  v a  2s . c om*/
 * 
 * @param callid
 *            ? ?Id
 * @param action
 *            ?? url?
 * @return
 */
public HashMap<String, Object> QueryCallState(String callid, String action) {

    HashMap<String, Object> validate = accountValidate();
    if (validate != null)
        return validate;
    if ((isEmpty(callid))) {
        logger.debug("?: callid  ");
        throw new IllegalArgumentException("?: callid ");
    }
    Callsid = callid;
    CcopHttpClient chc = new CcopHttpClient();
    DefaultHttpClient httpclient = null;
    try {
        httpclient = chc.registerSSL(SERVER_IP, "TLS", Integer.parseInt(SERVER_PORT), "https");
    } catch (Exception e1) {
        e1.printStackTrace();
        logger.error(e1.getMessage());
        throw new RuntimeException("?httpclient" + e1.getMessage());
    }
    String result = "";
    try {
        HttpPost httppost = (HttpPost) getHttpRequestBase(1, queryCallState);
        String requsetbody = "";
        if (BODY_TYPE == BodyType.Type_JSON) {
            JsonObject json = new JsonObject();
            JsonObject json2 = new JsonObject();
            json.addProperty("Appid", App_ID);
            json2.addProperty("callid", callid);
            if (!(isEmpty(action)))
                json2.addProperty("action", action);
            json.addProperty("QueryCallState", json2.toString());
            requsetbody = json.toString();
        } else {
            StringBuilder sb = new StringBuilder("<?xml version='1.0' encoding='utf-8'?><Request>");
            sb.append("<Appid>").append(App_ID).append("</Appid>").append("<QueryCallState callid=")
                    .append("\"").append(callid).append("\"");
            if (action != null) {
                sb.append(" action=").append("\"").append(action).append("\"").append("/");
            }

            sb.append("></Request>").toString();
            requsetbody = sb.toString();
        }
        logger.info("queryCallState Request body = : " + requsetbody);
        System.out.println("" + requsetbody);

        BasicHttpEntity requestBody = new BasicHttpEntity();
        requestBody.setContent(new ByteArrayInputStream(requsetbody.getBytes("UTF-8")));
        requestBody.setContentLength(requsetbody.getBytes("UTF-8").length);
        httppost.setEntity(requestBody);
        HttpResponse response = httpclient.execute(httppost);
        status = response.getStatusLine().getStatusCode();
        System.out.println("Https??" + status);

        HttpEntity entity = response.getEntity();
        if (entity != null)
            result = EntityUtils.toString(entity, "UTF-8");

        EntityUtils.consume(entity);
    } catch (IOException e) {
        e.printStackTrace();
        logger.error(e.getMessage());
        return getMyError("172001", "");
    } catch (Exception e) {
        e.printStackTrace();
        logger.error(e.getMessage());
        return getMyError("172002", "");
    } finally {
        if (httpclient != null)
            httpclient.getConnectionManager().shutdown();
    }
    logger.info("billRecords response body = " + result);
    try {
        if (BODY_TYPE == BodyType.Type_JSON) {
            return jsonToMap(result);
        } else {
            return xmlToMap(result);
        }
    } catch (Exception e) {

        return getMyError("172003", "");
    }
}

From source file:com.basho.riakcs.client.impl.RiakCSClientImpl.java

License:Open Source License

public JsonObject createUser(String fullname, String emailAddress) throws RiakCSException {
    if (endpointIsS3())
        throw new RiakCSException("Not Supported on AWS S3");

    JsonObject result = null;// w w  w  . ja  v  a 2 s . c o  m

    try {
        JsonObject postData = new JsonObject();
        postData.addProperty("email", emailAddress);
        postData.addProperty("name", fullname);

        InputStream dataInputStream = new ByteArrayInputStream(postData.toString().getBytes("UTF-8"));

        Map<String, String> headers = new HashMap<String, String>();
        headers.put("Content-Type", "application/json");
        headers.put("Content-Length", String.valueOf(dataInputStream.available()));

        CommunicationLayer comLayer = getCommunicationLayer();

        URL url = comLayer.generateCSUrl("", "user", EMPTY_STRING_MAP);
        HttpURLConnection connection = comLayer.makeCall(CommunicationLayer.HttpMethod.POST, url,
                dataInputStream, headers);
        InputStreamReader inputStreamReader = new InputStreamReader(connection.getInputStream(), "UTF-8");

        result = new JsonParser().parse(inputStreamReader).getAsJsonObject();

    } catch (Exception e) {
        throw new RiakCSException(e);
    }

    return result;
}

From source file:com.basho.riakcs.client.impl.RiakCSClientImpl.java

License:Open Source License

private void enableDisableUser(String key_id, boolean enable) throws RiakCSException {
    if (endpointIsS3())
        throw new RiakCSException("Not Supported on AWS S3");

    try {/*from  w w w.j  a  v  a2  s  .c o  m*/
        JsonObject postData = new JsonObject();
        if (enable)
            postData.addProperty("status", "enabled");
        else
            postData.addProperty("status", "disabled");

        InputStream dataInputStream = new ByteArrayInputStream(postData.toString().getBytes("UTF-8"));

        Map<String, String> headers = new HashMap<String, String>();
        headers.put("Content-Type", "application/json");
        headers.put("Content-Length", String.valueOf(dataInputStream.available()));

        CommunicationLayer comLayer = getCommunicationLayer();

        URL url = comLayer.generateCSUrl("/riak-cs/user/" + key_id);
        comLayer.makeCall(CommunicationLayer.HttpMethod.PUT, url, dataInputStream, headers);

    } catch (Exception e) {
        throw new RiakCSException(e);
    }
}

From source file:com.betafase.mcmanager.utils.spiget.ServerRequest.java

private String requestFromServer() {
    try {//from  w  ww.  jav a 2s. c om
        URL bf = new URL("https://api.spiget.org/v2/" + path);
        HttpURLConnection con = (HttpURLConnection) bf.openConnection();
        con.addRequestProperty("User-Agent", "MCManager");
        con.setDoInput(true);
        if (body != null) {
            con.setDoOutput(true);
            con.setRequestMethod("POST");
            BufferedWriter w = new BufferedWriter(new OutputStreamWriter(con.getOutputStream()));
            w.write(body);
            w.flush();
        }
        int code = con.getResponseCode();
        switch (code) {
        case 200:
            BufferedReader r = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
            String line = null;
            StringBuilder b = new StringBuilder();
            while ((line = r.readLine()) != null) {
                b.append(line);
            }
            return b.toString();
        case 401: {
            JsonObject o = new JsonObject();
            o.addProperty("result", "error");
            o.addProperty("code", code);
            o.addProperty("description", "Authentication failed!");
            return o.toString();
        }
        default: {
            JsonObject o = new JsonObject();
            o.addProperty("result", "error");
            o.addProperty("code", code);
            o.addProperty("description", con.getResponseMessage());
            return o.toString();
        }
        }
    } catch (Exception ex) {
        Logger.getLogger(ServerRequest.class.getName()).log(Level.SEVERE, null, ex);
    }
    return null;
}