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:ch.newscron.encryption.Encryption.java

/**
 * Given a String, it is decoded and the result is returned as a String as well.
 * @param encodedUrl is a String that have the full data encrypted
 * @return decoded String//from   w  w w . ja  va  2s  .  c o m
 */
public static String decode(String encodedUrl) {

    try {

        //Decode URL
        final SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
        cipher.init(Cipher.DECRYPT_MODE, secretKey,
                new IvParameterSpec(initializationVector.getBytes("UTF-8")));

        String result = new String(cipher.doFinal(Base64.decodeBase64(encodedUrl)));

        //Extract and remove hash from JSONObject
        JSONParser parser = new JSONParser();
        JSONObject receivedData = (JSONObject) parser.parse(result);
        String receivedHash = (String) receivedData.get("hash");
        receivedData.remove("hash");

        //Compare received hash with newly computed hash
        if (checkDataValidity(receivedData)) {
            byte[] hashOfData = createMD5Hash(receivedData);

            if (receivedHash.equals(new String(hashOfData, "UTF-8"))) { //Valid data
                return receivedData.toString();
            }
        }
    } catch (Exception e) {
    } //Invalid data (including encryption algorithm exceptions

    return null;
}

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

/**
 * Role??.//from w  ww  .j  a v a  2s .c  o  m
 * @param cellName ??
 * @param token 
 * @param boxName ??
 * @param roleName ??
 * @param code ?
 * @return ?
 */
@SuppressWarnings("unchecked")
public static TResponse create(final String cellName, final String token, final String boxName,
        final String roleName, final int code) {
    JSONObject body = new JSONObject();
    body.put("Name", roleName);
    if (boxName != null) {
        body.put("_Box.Name", boxName);
    }

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

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

/**
 * Role??.//from   ww  w. ja  va  2 s .co m
 * @param cellName ??
 * @param token 
 * @param roleName ??
 * @param boxName ??
 * @param code ?
 * @return ?
 */
@SuppressWarnings("unchecked")
public static TResponse create(final String cellName, final String token, final String roleName,
        final String boxName, final int code) {
    JSONObject body = new JSONObject();
    body.put("Name", roleName);
    if (boxName != null) {
        body.put("_Box.Name", boxName);
    }

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

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

/**
 * ???????ExtRole??.//from w w w.j  a  v  a2s.c  o m
 * @param cellName ??
 * @param testExtRoleName ??
 * @param relationName ??
 * @param relationBoxName ??
 * @param relationNameEmpty _Relation.Name???
 * @param relationBoxNameEmpty _Relation._Box.Name???
 */
@SuppressWarnings({ "unchecked", "unused" })
public static void createExtRole(String cellName, String testExtRoleName, String relationName,
        String relationBoxName, boolean relationNameEmpty, boolean relationBoxNameEmpty) {
    JSONObject body = new JSONObject();
    body.put("ExtRole", testExtRoleName);
    if (!relationNameEmpty) {
        body.put("_Relation.Name", relationName);
    }
    if (!relationBoxNameEmpty) {
        body.put("_Relation._Box.Name", relationBoxName);
    }
    TResponse response = Http.request("cell/extRole/extRole-create.txt")
            .with("token", AbstractCase.MASTER_TOKEN_NAME).with("cellPath", cellName)
            .with("body", body.toString()).returns().statusCode(HttpStatus.SC_CREATED);
}

From source file:com.avinashbehera.sabera.network.HttpClient.java

public static JSONObject SendHttpPostUsingHttpClient(String URL, JSONObject jsonObjSend) {

    try {/* w w  w  .  ja v a  2 s  . c  o m*/
        HttpParams httpParameters = new BasicHttpParams();
        int timeoutConnection = 60 * 1000;
        HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
        int timeoutSocket = 60 * 1000;
        HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

        DefaultHttpClient httpclient = new DefaultHttpClient(httpParameters);
        HttpPost httpPostRequest = new HttpPost(URL);

        StringEntity se;
        se = new StringEntity(jsonObjSend.toString());

        // Set HTTP parameters
        httpPostRequest.setEntity(se);
        //httpPostRequest.setHeader("Accept", "application/json");
        httpPostRequest.setHeader("Content-type", "application/json");
        //httpPostRequest.setHeader("Accept-Encoding", "gzip"); // only set this parameter if you would like to use gzip compression

        long t = System.currentTimeMillis();
        Log.d(TAG, "httpPostReuest = " + httpPostRequest.toString());
        HttpResponse response = (HttpResponse) httpclient.execute(httpPostRequest);
        Log.i(TAG, "HTTPResponse received in [" + (System.currentTimeMillis() - t) + "ms]");

        // Get hold of the response entity (-> the data):
        HttpEntity entity = response.getEntity();

        if (entity != null) {
            // Read the content stream
            InputStream instream = entity.getContent();
            Header contentEncoding = response.getFirstHeader("Content-Encoding");
            if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
                instream = new GZIPInputStream(instream);
            }

            // convert content stream to a String
            String resultString = convertStreamToString(instream);
            instream.close();
            resultString = resultString.substring(1, resultString.length() - 1); // remove wrapping "[" and "]"

            // Transform the String into a JSONObject
            JSONParser parser = new JSONParser();
            JSONObject jsonObjRecv = (JSONObject) parser.parse(resultString);
            // Raw DEBUG output of our received JSON object:
            Log.i(TAG, "<JSONObject>\n" + jsonObjRecv.toString() + "\n</JSONObject>");

            return jsonObjRecv;
        }

    } catch (Exception e) {
        Log.d(TAG, "catch block");
        // More about HTTP exception handling in another tutorial.
        // For now we just print the stack trace.
        e.printStackTrace();
    }
    Log.d(TAG, "SendHttpPostUsingHttpClient returning null");
    return null;
}

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

/**
 * ./*  w  w  w . ja  va2s. com*/
 * @param token ?
 * @param cellName ??
 * @param roleName ??
 * @param newRoleName ??
 * @param boxName ??
 * @param sc 
 * @return ?
 */
@SuppressWarnings("unchecked")
public static TResponse update(String token, String cellName, String roleName, String newRoleName,
        String boxName, int sc) {
    JSONObject body = new JSONObject();
    body.put("Name", newRoleName);
    String boxNameStr = null;
    if (boxName != null) {
        body.put("_Box.Name", boxName);
        boxNameStr = "'" + boxName + "'";
    } else {
        boxNameStr = "null";
    }
    TResponse res = Http.request("cell/role-update.txt").with("cellPath", cellName).with("token", token)
            .with("rolename", roleName).with("boxname", boxNameStr).with("body", body.toString()).returns()
            .statusCode(sc);
    return res;
}

From source file:com.fujitsu.dc.test.jersey.box.acl.AclTest.java

/**
     * Role??./*from   www.j  a v a2  s  .com*/
     * @param cellName ??
     * @param token 
     * @param roleName ??
     * @param code ?
     */
    @SuppressWarnings("unchecked")
    public static void createRole(final String cellName, final String token, final String roleName,
            final int code) {
        JSONObject body = new JSONObject();
        body.put("Name", roleName);

        TResponse res = Http.request("role-create.txt").with("token", token).with("cellPath", cellName)
                .with("body", body.toString()).returns();

        assertEquals(code, res.getStatusCode());

    }

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

/**
 * NP?.//www.  ja v  a 2s.c  o  m
 * @param method ??
 * @param cell ??
 * @param entityType ??
 * @param id ID
 * @param navPropName navPropName
 * @param body 
 * @param token ?
 * @param sc ?
 * @return ?
 */
public static TResponse createNp(String method, String cell, String entityType, String id, String navPropName,
        JSONObject body, String token, int sc) {
    TResponse res = Http.request("cell/createNP.txt").with("method", method).with("cell", cell)
            .with("entityType", entityType).with("id", id).with("navPropName", navPropName)
            .with("accept", MediaType.APPLICATION_JSON).with("contentType", MediaType.APPLICATION_JSON)
            .with("token", token).with("body", body.toString()).returns().statusCode(sc);
    return res;
}

From source file:clientpaxos.ClientPaxos.java

public static void getClientAddress() throws Exception {
    String json;/*from  w ww  .jav  a  2  s . co  m*/
    JSONObject jsonObject = new JSONObject();
    jsonObject.put("method", "client_address");
    json = jsonObject.toString();
    System.out.println("Send to server : " + json);
    sendToServer(json);
}

From source file:com.opensoc.tagging.adapters.RegexTagger.java

/**
 * @param raw_message telemetry message to be tagged
 *///from  w ww . j  a  v  a  2  s .co  m
@SuppressWarnings("unchecked")
public JSONArray tag(JSONObject raw_message) {

    JSONArray ja = new JSONArray();
    String message_as_string = raw_message.toString();

    for (String rule : _rules.keySet()) {
        if (message_as_string.matches(rule)) {
            ja.add(_rules.get(rule));
        }
    }

    return ja;
}