Example usage for org.json.simple JSONObject toString

List of usage examples for org.json.simple JSONObject toString

Introduction

In this page you can find the example usage for org.json.simple JSONObject toString.

Prototype

@Override
    public String toString() 

Source Link

Usage

From source file:com.cbmapi.CbmAPI.java

private static String convertArrayToJsonString(String[] infoArray) {
    //Depending on prefered formating, use of temp is not necessary.
    JSONObject temp = new JSONObject();
    JSONObject jObj = new JSONObject();
    int length = infoArray.length;
    for (int i = 1; i < length - 1; i += 2) {
        int y = i + 1;
        temp.put(infoArray[i].trim(), infoArray[y].trim());
    }/*from w w  w  .  j  a v a 2 s.  c o  m*/
    //Name of the cpu is always located first in the array.
    jObj.put(infoArray[0], temp);
    return jObj.toString();
}

From source file:io.personium.test.jersey.cell.ctl.CellCtlUtils.java

/**
 * ??????????.//w w w . ja v  a 2s .c  o m
 * @param cellName ??
 * @param testRoleName ??
 * @return ?
 */
@SuppressWarnings("unchecked")
public static TResponse createRole(String cellName, String testRoleName) {

    JSONObject body = new JSONObject();
    body.put("Name", testRoleName);
    body.put("_Box.Name", null);

    return Http.request("role-create.txt").with("token", AbstractCase.MASTER_TOKEN_NAME)
            .with("cellPath", cellName).with("body", body.toString()).returns()
            .statusCode(HttpStatus.SC_CREATED);
}

From source file:io.personium.test.jersey.cell.ctl.CellCtlUtils.java

/**
 * ?????????./*w w w . j  a  va  2 s. c  o m*/
 * @param cellName ??
 * @param testRoleName ??
 * @param boxname ??
 * @return ?
 */
@SuppressWarnings("unchecked")
public static TResponse createRole(String cellName, String testRoleName, String boxname) {
    // Role?
    JSONObject body = new JSONObject();
    body.put("Name", testRoleName);
    body.put("_Box.Name", boxname);
    return Http.request("role-create.txt").with("token", AbstractCase.MASTER_TOKEN_NAME)
            .with("cellPath", cellName).with("body", body.toString()).returns()
            .statusCode(HttpStatus.SC_CREATED);
}

From source file:io.personium.test.jersey.cell.ctl.CellCtlUtils.java

/**
 * ????????Relation??.//w  w  w .j  ava  2 s. c  o m
 * @param cellName ??
 * @param testRelationName ??
 * @return ?
 */
@SuppressWarnings("unchecked")
public static TResponse createRelation(String cellName, String testRelationName) {
    JSONObject body = new JSONObject();
    body.put("Name", testRelationName);
    body.put("_Box.Name", null);

    return Http.request("relation-create.txt").with("token", AbstractCase.BEARER_MASTER_TOKEN)
            .with("cellPath", cellName).with("body", body.toString()).returns()
            .statusCode(HttpStatus.SC_CREATED);
}

From source file:io.personium.test.jersey.cell.ctl.CellCtlUtils.java

/**
 * ???????Relation??./*from   w  w w .  j av  a  2 s  .  c o m*/
 * @param cellName ??
 * @param testRelationName ??
 * @param boxname ??
 * @return ?
 */
@SuppressWarnings("unchecked")
public static TResponse createRelation(String cellName, String testRelationName, String boxname) {
    JSONObject body = new JSONObject();
    body.put("Name", testRelationName);
    body.put("_Box.Name", boxname);

    return Http.request("relation-create.txt").with("token", AbstractCase.BEARER_MASTER_TOKEN)
            .with("cellPath", cellName).with("body", body.toString()).returns()
            .statusCode(HttpStatus.SC_CREATED);
}

From source file:JavaCloud.Utils.java

public static Object request(String address, String function, final JSONObject params) throws CoreException {
    try {/*from  w  w  w . j  a v a  2 s . c o m*/
        GenericUrl url = new GenericUrl(address + "/" + function);
        NetHttpTransport transport = new NetHttpTransport();
        HttpRequest request = transport.createRequestFactory().buildPostRequest(url, new HttpContent() {
            @Override
            public long getLength() throws IOException {
                return params.toString().length();
            }

            @Override
            public String getType() {
                return "text/json";
            }

            @Override
            public boolean retrySupported() {
                return false;
            }

            @Override
            public void writeTo(OutputStream outputStream) throws IOException {
                outputStream.write(params.toString().getBytes());
            }
        });
        HttpResponse response = request.execute();
        JSONParser parser = new JSONParser();
        JSONObject object = (JSONObject) parser.parse(response.parseAsString());

        if (!object.get("status").equals("ok")) {
            throw new CoreException(object.get("status").toString());
        }

        if (object.containsKey("data") && object.get("data") != null)
            return parser.parse(object.get("data").toString());
        else
            return null;
    } catch (ParseException e) {
        System.out.println("Error parsing response: " + e.toString());
        throw new CoreException("json_malformed");
    } catch (IOException e) {
        System.out.println("Error connecting to server: " + e.toString());
        throw new CoreException("connection_failed");
    }
}

From source file:com.fujitsu.dc.test.utils.RelationUtils.java

/**
 * NP?Relation??.// w  w  w.  j a  v  a2  s.  c o  m
 * @param cellName ??
 * @param token 
 * @param boxName ??
 * @param relationName ??
 * @param code ?
 * @return ?
 */
@SuppressWarnings("unchecked")
public static TResponse createViaNP(final String cellName, final String token, final String boxName,
        final String relationName, final int code) {
    JSONObject body = new JSONObject();
    body.put("Name", relationName);

    TResponse res = Http.request("createNP.txt").with("token", token).with("accept", MediaType.APPLICATION_JSON)
            .with("contentType", MediaType.APPLICATION_JSON).with("cell", cellName).with("entityType", "Box")
            .with("id", boxName).with("navPropName", "_Relation").with("body", body.toString()).returns();

    assertEquals(code, res.getStatusCode());

    return res;
}

From source file:io.personium.test.utils.RoleUtils.java

/**
 * create role utility.//from   ww w  .  j  a v a 2 s .  co m
 * @param cellName cell name
 * @param token token
 * @param body body
 * @param code response code
 * @return response
 */
public static TResponse create(final String cellName, final String token, final JSONObject body,
        final int code) {
    return Http.request("role-create.txt").with("token", token).with("cellPath", cellName)
            .with("body", body.toString()).returns().statusCode(code);
}

From source file:com.sforce.cd.apexUnit.client.codeCoverage.WebServiceInvoker.java

public static JSONObject doGet(String relativeServiceURL, String accessToken) {

    LOG.debug("relativeServiceURL in doGet method:" + relativeServiceURL);
    HttpClient httpclient = new HttpClient();
    GetMethod get = null;/* ww w .  j a v  a2 s  .  c  om*/

    String authorizationServerURL = CommandLineArguments.getOrgUrl() + relativeServiceURL;
    get = new GetMethod(authorizationServerURL);
    get.addRequestHeader("Content-Type", "application/json");
    get.setRequestHeader("Authorization", "Bearer " + accessToken);
    LOG.debug("Start GET operation for the url..." + authorizationServerURL);
    InputStream instream = null;
    try {
        instream = executeHTTPMethod(httpclient, get, authorizationServerURL);
        LOG.debug("done with get operation");

        JSONObject json = (JSONObject) JSONValue.parse(new InputStreamReader(instream));
        LOG.debug("is json null? :" + json == null ? "true" : "false");
        if (json != null) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("ToolingApi.get response: " + json.toString());
                Set<String> keys = castSet(String.class, json.keySet());
                Iterator<String> jsonKeyIter = keys.iterator();
                LOG.debug("Response for the GET method: ");
                while (jsonKeyIter.hasNext()) {
                    String key = jsonKeyIter.next();
                    LOG.debug("key : " + key + ". Value :  " + json.get(key) + "\n");
                    // TODO if query results are too large, only 1st batch
                    // of results
                    // are returned. Need to use the identifier in an
                    // additional query
                    // to retrieve rest of the next batch of results

                    if (key.equals("nextRecordsUrl")) {
                        // fire query to the value for this key
                        // doGet((String) json.get(key), accessToken);
                        try {
                            authorizationServerURL = CommandLineArguments.getOrgUrl() + (String) json.get(key);
                            get.setURI(new URI(authorizationServerURL, false));
                            instream = executeHTTPMethod(httpclient, get, authorizationServerURL);
                            JSONObject newJson = (JSONObject) JSONValue.parse(new InputStreamReader(instream));
                            if (newJson != null) {
                                Set<String> newKeys = castSet(String.class, json.keySet());
                                Iterator<String> newJsonKeyIter = newKeys.iterator();
                                while (newJsonKeyIter.hasNext()) {
                                    String newKey = newJsonKeyIter.next();
                                    json.put(newKey, newJson.get(newKey));
                                    LOG.debug("newkey : " + newKey + ". NewValue :  " + newJson.get(newKey)
                                            + "\n");
                                }
                            }

                        } catch (URIException e) {
                            ApexUnitUtils.shutDownWithDebugLog(e,
                                    "URI exception while fetching subsequent batch of result");
                        }
                    }

                }
            }
        }
        return json;
    } finally {
        get.releaseConnection();
        try {
            if (instream != null) {
                instream.close();

            }
        } catch (IOException e) {
            ApexUnitUtils.shutDownWithDebugLog(e,
                    "Encountered IO exception when closing the stream after reading response from the get method. The error says: "
                            + e.getMessage());
        }
    }
}

From source file:ch.newscron.encryption.Encryption.java

/**
 * Given a JSONObject, and maybe an hash, it is encoded and returned as a String.
 * @param inviteData is a JSONObject having the data with the keys "customerId", "rew1", "rew2" and "val"
 * @param md5Hash is a String that substitute the hash computed using md5 algorithm, in case it is not null and not empty
 * @return encoded string // www  .  j  av a 2s. c  om
 */
protected static String encode(JSONObject inviteData, String md5Hash) {

    try {
        //Check in case we want to add a given hash (having at the end a corrupt data)
        if (md5Hash != null && !md5Hash.isEmpty()) {
            //Add md5Hash to JSONObject
            inviteData.put("hash", md5Hash);
        } else {
            return null;
        }

        //Encode inviteData (with hash) JSONObject
        String params = inviteData.toString();
        final SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey,
                new IvParameterSpec(initializationVector.getBytes("UTF-8")));
        return Base64.encodeBase64URLSafeString(cipher.doFinal(params.getBytes())); //to ensure valid URL characters
    } catch (Exception e) {
    }

    return null;
}