Example usage for org.json JSONObject put

List of usage examples for org.json JSONObject put

Introduction

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

Prototype

public JSONObject put(String key, Object value) throws JSONException 

Source Link

Document

Put a key/value pair in the JSONObject.

Usage

From source file:com.shampan.services.PageService.java

public static String getCategorySubCategory() {
    JSONObject json = new JSONObject();
    json.put("categoryList", pageObject.getCategories());
    json.put("subCategoryList", pageObject.getSubCategories());
    return json.toString();
}

From source file:com.shampan.services.PageService.java

public static String getTimelinePhotos(String pageId, String userId) {
    JSONObject json = new JSONObject();
    json.put("pageInfo", pageObject.getPageInfo(pageId));
    json.put("pageMemberInfo", pageObject.getMemeberInfo(pageId, userId));
    json.put("photoList", pageObject.getTimelinePhotos(pageId));
    return json.toString();
}

From source file:com.shampan.services.PageService.java

public static String getAlbums(String pageId) {
    JSONObject albums = new JSONObject();
    albums.put("albumList", pageObject.getAlbums(pageId));
    return albums.toString();
}

From source file:com.shampan.services.PageService.java

public static String getPhotos(String userId, String mappingId, String albumId) {
    JSONObject photos = new JSONObject();
    photos.put("albumInfo", pageObject.getAlbum(userId, mappingId, albumId));
    photos.put("photoList", pageObject.getPhotos(mappingId, albumId));
    return photos.toString();
}

From source file:com.shampan.services.PageService.java

public static String getPageInfo(String pageId, String userId) {
    JSONObject json = new JSONObject();
    json.put("pageInfo", pageObject.getPageInfo(pageId));
    json.put("pageMemberInfo", pageObject.getMemeberInfo(pageId, userId));
    return json.toString();
}

From source file:org.wso2.carbon.connector.integration.test.amazonsqs.AmazonSQSAuthConnector.java

/**
 * Connect method which is generating authentication of the connector for each request.
 *
 * @param messageContext ESB messageContext.
 * @throws java.io.UnsupportedEncodingException
 * @throws IllegalStateException /*from   ww w. j a v  a  2  s . co  m*/
 * @throws java.security.NoSuchAlgorithmException
 * @throws java.security.InvalidKeyException
 * @throws JSONException 
 */
public final Map<String, String> getRequestPayload(final JSONObject signatureRequestObject)
        throws InvalidKeyException, NoSuchAlgorithmException, IllegalStateException,
        UnsupportedEncodingException, JSONException {

    final StringBuilder canonicalRequest = new StringBuilder();
    final StringBuilder stringToSign = new StringBuilder();
    final StringBuilder payloadBuilder = new StringBuilder();
    final StringBuilder payloadStrBuilder = new StringBuilder();
    final StringBuilder authHeader = new StringBuilder();
    init(signatureRequestObject);

    // Generate time-stamp which will be sent to API and to be used in Signature
    final Date date = new Date();
    final TimeZone timeZone = TimeZone.getTimeZone(AmazonSQSConstants.GMT);
    final DateFormat dateFormat = new SimpleDateFormat(AmazonSQSConstants.ISO8601_BASIC_DATE_FORMAT);
    dateFormat.setTimeZone(timeZone);
    final String amzDate = dateFormat.format(date);

    final DateFormat shortDateFormat = new SimpleDateFormat(AmazonSQSConstants.SHORT_DATE_FORMAT);
    shortDateFormat.setTimeZone(timeZone);
    final String shortDate = shortDateFormat.format(date);

    signatureRequestObject.put(AmazonSQSConstants.AMZ_DATE, amzDate);
    final Map<String, String> parameterNamesMap = getParameterNamesMap();
    final Map<String, String> parametersMap = getSortedParametersMap(signatureRequestObject, parameterNamesMap);
    canonicalRequest.append(signatureRequestObject.get(AmazonSQSConstants.HTTP_METHOD));
    canonicalRequest.append(AmazonSQSConstants.NEW_LINE);
    final String charSet = Charset.defaultCharset().toString();
    if (signatureRequestObject.has(AmazonSQSConstants.URL_QUEUE_NAME)
            && !("").equals(signatureRequestObject.get(AmazonSQSConstants.URL_QUEUE_NAME))
            && signatureRequestObject.has(AmazonSQSConstants.QUEUE_ID)
            && !("").equals(signatureRequestObject.get(AmazonSQSConstants.QUEUE_ID))) {
        // queue ID and queue name should be encoded twise to match the Signature being generated by API,
        // Note that API it looks encodes the incoming URL once before creating the signature, SInce
        // we send url encoded URLs, API signatures are twise encoded
        final String encodedQueueID = URLEncoder
                .encode(signatureRequestObject.get(AmazonSQSConstants.QUEUE_ID).toString(), charSet);
        final String encodedQueueName = URLEncoder
                .encode(signatureRequestObject.get(AmazonSQSConstants.URL_QUEUE_NAME).toString(), charSet);
        canonicalRequest.append((AmazonSQSConstants.FORWARD_SLASH + URLEncoder.encode(encodedQueueID, charSet)
                + AmazonSQSConstants.FORWARD_SLASH + URLEncoder.encode(encodedQueueName, charSet)
                + AmazonSQSConstants.FORWARD_SLASH).replaceAll(AmazonSQSConstants.REGEX_ASTERISK,
                        AmazonSQSConstants.URL_ENCODED_ASTERISK));
        // Sets the http request Uri to message context 
        signatureRequestObject.put(AmazonSQSConstants.HTTP_REQUEST_URI,
                AmazonSQSConstants.FORWARD_SLASH + encodedQueueID + AmazonSQSConstants.FORWARD_SLASH
                        + encodedQueueName + AmazonSQSConstants.FORWARD_SLASH);
    } else {
        canonicalRequest.append(AmazonSQSConstants.FORWARD_SLASH);
    }

    canonicalRequest.append(AmazonSQSConstants.NEW_LINE);

    final Set<String> keySet = parametersMap.keySet();
    for (String key : keySet) {
        payloadBuilder.append(URLEncoder.encode(key, charSet));
        payloadBuilder.append(AmazonSQSConstants.EQUAL);
        payloadBuilder.append(URLEncoder.encode(parametersMap.get(key), charSet));
        payloadBuilder.append(AmazonSQSConstants.AMPERSAND);
        payloadStrBuilder.append(AmazonSQSConstants.QUOTE);
        payloadStrBuilder.append(key);
        payloadStrBuilder.append(AmazonSQSConstants.QUOTE);
        payloadStrBuilder.append(AmazonSQSConstants.COLON);
        payloadStrBuilder.append(AmazonSQSConstants.QUOTE);
        payloadStrBuilder.append(parametersMap.get(key));
        payloadStrBuilder.append(AmazonSQSConstants.QUOTE);
        payloadStrBuilder.append(AmazonSQSConstants.COMMA);

    }
    // Adds authorization header to message context, removes additionally appended comma at the end
    if (payloadStrBuilder.length() > 0) {
        signatureRequestObject.put(AmazonSQSConstants.REQUEST_PAYLOAD,
                payloadStrBuilder.substring(0, payloadStrBuilder.length() - 1));
    }
    // Appends empty string since no URL parameters are used in POST API requests
    canonicalRequest.append("");
    canonicalRequest.append(AmazonSQSConstants.NEW_LINE);
    final Map<String, String> headersMap = getSortedHeadersMap(signatureRequestObject, parameterNamesMap);
    final StringBuilder canonicalHeaders = new StringBuilder();
    final StringBuilder signedHeader = new StringBuilder();
    final Set<String> keysSet = headersMap.keySet();
    for (String key : keysSet) {
        canonicalHeaders.append(key);
        canonicalHeaders.append(AmazonSQSConstants.COLON);
        canonicalHeaders.append(headersMap.get(key));
        canonicalHeaders.append(AmazonSQSConstants.NEW_LINE);
        signedHeader.append(key);
        signedHeader.append(AmazonSQSConstants.SEMI_COLON);
    }
    canonicalRequest.append(canonicalHeaders.toString());
    canonicalRequest.append(AmazonSQSConstants.NEW_LINE);
    // Remove unwanted semi-colon at the end of the signedHeader string
    String signedHeaders = "";
    if (signedHeader.length() > 0) {
        signedHeaders = signedHeader.substring(0, signedHeader.length() - 1);
    }
    canonicalRequest.append(signedHeaders);
    canonicalRequest.append(AmazonSQSConstants.NEW_LINE);
    // HashedPayload = HexEncode(Hash(requestPayload))
    String requestPayload = "";
    if (payloadBuilder.length() > 0) {
        /*
         * First removes the additional ampersand appended to the end of the payloadBuilder, then o
         * further modifications to preserve unreserved characters as per the API guide
         * (http://docs.aws.amazon.com/general/latest/gr/sigv4-create-canonical-request.html)
         */
        requestPayload = payloadBuilder.substring(0, payloadBuilder.length() - 1).toString()
                .replace(AmazonSQSConstants.PLUS, AmazonSQSConstants.URL_ENCODED_PLUS)
                .replace(AmazonSQSConstants.URL_ENCODED_TILT, AmazonSQSConstants.TILT)
                .replace(AmazonSQSConstants.ASTERISK, AmazonSQSConstants.URL_ENCODED_ASTERISK);
    }
    canonicalRequest.append(bytesToHex(hash(requestPayload)).toLowerCase());
    stringToSign.append(AmazonSQSConstants.AWS4_HMAC_SHA_256);
    stringToSign.append(AmazonSQSConstants.NEW_LINE);
    stringToSign.append(amzDate);
    stringToSign.append(AmazonSQSConstants.NEW_LINE);
    stringToSign.append(shortDate);
    stringToSign.append(AmazonSQSConstants.FORWARD_SLASH);
    stringToSign.append(signatureRequestObject.get(AmazonSQSConstants.REGION));
    stringToSign.append(AmazonSQSConstants.FORWARD_SLASH);
    stringToSign.append(signatureRequestObject.get(AmazonSQSConstants.SERVICE));
    stringToSign.append(AmazonSQSConstants.FORWARD_SLASH);
    stringToSign.append(signatureRequestObject.get(AmazonSQSConstants.TERMINATION_STRING));
    stringToSign.append(AmazonSQSConstants.NEW_LINE);
    stringToSign.append(bytesToHex(hash(canonicalRequest.toString())).toLowerCase());
    final byte[] signingKey = getSignatureKey(signatureRequestObject,
            signatureRequestObject.get(AmazonSQSConstants.SECRET_ACCESS_KEY).toString(), shortDate,
            signatureRequestObject.get(AmazonSQSConstants.REGION).toString(),
            signatureRequestObject.get(AmazonSQSConstants.SERVICE).toString());

    // Construction of authorization header value to be in cluded in API request
    authHeader.append(AmazonSQSConstants.AWS4_HMAC_SHA_256);
    authHeader.append(AmazonSQSConstants.COMMA);
    authHeader.append(AmazonSQSConstants.CREDENTIAL);
    authHeader.append(AmazonSQSConstants.EQUAL);
    authHeader.append(signatureRequestObject.get(AmazonSQSConstants.ACCESS_KEY_ID));
    authHeader.append(AmazonSQSConstants.FORWARD_SLASH);
    authHeader.append(shortDate);
    authHeader.append(AmazonSQSConstants.FORWARD_SLASH);
    authHeader.append(signatureRequestObject.get(AmazonSQSConstants.REGION));
    authHeader.append(AmazonSQSConstants.FORWARD_SLASH);
    authHeader.append(signatureRequestObject.get(AmazonSQSConstants.SERVICE));
    authHeader.append(AmazonSQSConstants.FORWARD_SLASH);
    authHeader.append(signatureRequestObject.get(AmazonSQSConstants.TERMINATION_STRING));
    authHeader.append(AmazonSQSConstants.COMMA);
    authHeader.append(AmazonSQSConstants.SIGNED_HEADERS);
    authHeader.append(AmazonSQSConstants.EQUAL);
    authHeader.append(signedHeaders);
    authHeader.append(AmazonSQSConstants.COMMA);
    authHeader.append(AmazonSQSConstants.API_SIGNATURE);
    authHeader.append(AmazonSQSConstants.EQUAL);
    authHeader.append(bytesToHex(hmacSHA256(signingKey, stringToSign.toString())).toLowerCase());
    // Adds authorization header to message context
    signatureRequestObject.put(AmazonSQSConstants.AUTHORIZATION_HEADER, authHeader.toString());

    Map<String, String> responseMap = new HashMap<String, String>();
    responseMap.put(AmazonSQSConstants.AUTHORIZATION_HEADER, authHeader.toString());
    responseMap.put(AmazonSQSConstants.AMZ_DATE, amzDate);
    responseMap.put(AmazonSQSConstants.REQUEST_PAYLOAD, requestPayload);
    return responseMap;
}

From source file:org.wso2.carbon.connector.integration.test.amazonsqs.AmazonSQSAuthConnector.java

/**
 * @param signatureRequestObject// ww  w.ja  va  2  s  .c o  m
 * @throws JSONException
 */
private void init(JSONObject signatureRequestObject) throws JSONException {
    signatureRequestObject.put(AmazonSQSConstants.SERVICE, "sqs");
    signatureRequestObject.put(AmazonSQSConstants.SIGNATURE_METHOD, "HmacSHA256");
    signatureRequestObject.put(AmazonSQSConstants.SIGNATURE_VERSION, "4");
    signatureRequestObject.put(AmazonSQSConstants.CONTENT_TYPE, "application/x-www-form-urlencoded");
    signatureRequestObject.put(AmazonSQSConstants.HTTP_METHOD, "POST");
    signatureRequestObject.put(AmazonSQSConstants.TERMINATION_STRING, "aws4_request");
    signatureRequestObject.put(AmazonSQSConstants.HOST,
            "sqs." + signatureRequestObject.get(AmazonSQSConstants.REGION) + ".amazonaws.com");
}

From source file:org.digitalcampus.oppia.application.Tracker.java

public void saveTracker(int courseId, String digest, JSONObject data, boolean completed) {
    // add tracker UUID
    UUID guid = java.util.UUID.randomUUID();
    try {//  w  w  w .  ja va 2s  .co m
        data.put("uuid", guid.toString());
    } catch (JSONException e) {
        e.printStackTrace();
    }
    DbHelper db = new DbHelper(this.ctx);
    db.insertTracker(courseId, digest, data.toString(), completed);
    DatabaseManager.getInstance().closeDatabase();

}

From source file:com.liferay.mobile.android.v7.assetentry.AssetEntryService.java

public JSONArray getEntries(JSONObjectWrapper entryQuery) throws Exception {
    JSONObject _command = new JSONObject();

    try {/*from   w w  w  . j a va 2  s. co  m*/
        JSONObject _params = new JSONObject();

        mangleWrapper(_params, "entryQuery", "com.liferay.asset.kernel.service.persistence.AssetEntryQuery",
                entryQuery);

        _command.put("/assetentry/get-entries", _params);
    } catch (JSONException _je) {
        throw new Exception(_je);
    }

    JSONArray _result = session.invoke(_command);

    if (_result == null) {
        return null;
    }

    return _result.getJSONArray(0);
}

From source file:com.liferay.mobile.android.v7.assetentry.AssetEntryService.java

public JSONObject getEntry(long entryId) throws Exception {
    JSONObject _command = new JSONObject();

    try {//from w  ww. j a v a  2 s. co  m
        JSONObject _params = new JSONObject();

        _params.put("entryId", entryId);

        _command.put("/assetentry/get-entry", _params);
    } catch (JSONException _je) {
        throw new Exception(_je);
    }

    JSONArray _result = session.invoke(_command);

    if (_result == null) {
        return null;
    }

    return _result.getJSONObject(0);
}