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:org.wso2.carbon.emm.integration.test.DeviceRegistrationTestCase.java

/**
 * Returns the response of a dummy Android device registration.
 * @param accessToken Current access token for admin user
 * @return Response of an Android device registration
 * @throws EMMIntegrationTestException Custom exception type for any EMM Integration Test
 *//*from   w w  w  . j a  v  a2 s  .co  m*/
@SuppressWarnings("unchecked")
private HttpResponse getAndroidRegistrationResponse(String accessToken) throws EMMIntegrationTestException {
    HttpPost request = new HttpPost(EMMIntegrationTestConstants.DEVICE_REGISTRATION_ENDPOINT);
    request.addHeader("content-type", "application/json");
    request.addHeader("Authorization", "Bearer " + accessToken);
    request.addHeader("User-Agent", "Mozilla/5.0(Linux; U; Android 2.2; en-gb; LG-P500 Build/FRF91)");

    JSONObject devicePayload = new JSONObject();

    devicePayload.put("regid", "969f0583b6382190");
    devicePayload.put("platform", "Android");
    devicePayload.put("username", "AndrewB");
    devicePayload.put("vendor", "samsung");
    devicePayload.put("mac", "48:5A:3F:08:19:60");
    devicePayload.put("properties",
            "{\"device\":\"hlte\",\"model\":\"SM-N9005\",\"imei\":\"351776060691383\"}");
    devicePayload.put("type", "BYOD");
    devicePayload.put("osversion", "4.4.2");

    StringEntity params;
    try {
        params = new StringEntity(devicePayload.toString());
    } catch (UnsupportedEncodingException e) {
        throw new EMMIntegrationTestException("Error converting android registration data into StringEntity",
                e);
    }

    request.setEntity(params);
    HttpResponse response;
    try {
        response = getHttpClient().execute(request);
    } catch (IOException e) {
        throw new EMMIntegrationTestException("Error executing Android registration request", e);
    }

    return response;
}

From source file:org.wso2.carbon.identity.authenticator.PushRestCall.java

public String invokePush() throws AuthenticationFailedException {
    String sessionId;/*from  w  w w .j  av a2 s . co  m*/
    if (log.isDebugEnabled()) {
        log.info("\nAsk push notification ");
    }
    PushAuthentication pushAuthentication = new PushAuthentication(serviceId, p12file, p12password);
    JSONObject result = pushAuthentication.pushAuthenticate(userId);
    if (log.isDebugEnabled()) {
        log.debug("Result: " + result.toJSONString());
    }
    sessionId = (String) result.get("sessionId");
    if (log.isDebugEnabled()) {
        log.debug("Session id: " + sessionId);
    }
    PushResult cr = new PushResult(serviceId, p12file, p12password);
    if (sessionId == null) {
        if (log.isDebugEnabled()) {
            log.debug("No session id: " + result.get(InweboConstants.RESULT));
        }
        return result.toString();
    }
    int retry = 0;
    while ((retry < waitTime)) {
        retry++;
        result = cr.checkPushResult(userId, sessionId);
        if (!result.get(InweboConstants.RESULT).equals(InweboConstants.CODEWAITTING))
            break;
        try {
            if (log.isDebugEnabled()) {
                log.debug("Request pending...  " + result);
            }
            Thread.sleep(retryInterval);
        } catch (InterruptedException e) {
            throw new AuthenticationFailedException("Error while getting response" + e.getMessage(), e);
        }
    }
    if (log.isDebugEnabled()) {
        log.info("Result:" + result.get(InweboConstants.RESULT));
    }
    return result.toString();
}

From source file:org.wso2.identity.integration.test.scim.SCIMComplexMultiAttributeUserTestCase.java

@Test
public void testCreateUser() throws Exception {
    HttpPost request = new HttpPost(getPath());
    request.addHeader(HttpHeaders.AUTHORIZATION, getAuthzHeader());
    request.addHeader(HttpHeaders.CONTENT_TYPE, "application/json");

    JSONObject rootObject = new JSONObject();

    JSONArray schemas = new JSONArray();
    rootObject.put(SCHEMAS_ATTRIBUTE, schemas);

    JSONObject names = new JSONObject();
    names.put(FAMILY_NAME_ATTRIBUTE, FAMILY_NAME_CLAIM_VALUE);
    names.put(GIVEN_NAME_ATTRIBUTE, GIVEN_NAME_CLAIM_VALUE);

    rootObject.put(NAME_ATTRIBUTE, names);
    rootObject.put(USER_NAME_ATTRIBUTE, USERNAME);

    JSONArray phoneNumbers = getSimpleMultivaluedPhoneNumbersAttribute();
    rootObject.put(PHONE_NUMBERS_ATTRIBUTE, phoneNumbers);

    JSONArray emails = getBasicComplexMultivaluedEmailsAttribute();
    rootObject.put(EMAILS_ATTRIBUTE, emails);

    JSONArray addresses = getAdvancedComplexMultivaluedAddressesAttribute();
    rootObject.put(ADDRESSES_ATTRIBUTE, addresses);

    rootObject.put(PASSWORD_ATTRIBUTE, PASSWORD);

    StringEntity entity = new StringEntity(rootObject.toString());
    request.setEntity(entity);/*  w w w  .j  av a 2  s. c o m*/

    HttpResponse response = client.execute(request);
    assertEquals(response.getStatusLine().getStatusCode(), 201, "User " + "has not been created successfully");

    Object responseObj = JSONValue.parse(EntityUtils.toString(response.getEntity()));
    EntityUtils.consume(response.getEntity());

    String usernameFromResponse = ((JSONObject) responseObj).get(USER_NAME_ATTRIBUTE).toString();
    assertEquals(usernameFromResponse, USERNAME);

    userId = ((JSONObject) responseObj).get(ID_ATTRIBUTE).toString();
    assertNotNull(userId);
}

From source file:org.wso2.identity.integration.test.scim2.SCIM2MultiAttributeUserFilterTestCase.java

private void createUser(String familyName, String givenName, String userName, String workEmail,
        String homeEmail, String password) throws IOException {

    HttpPost request = new HttpPost(getUserPath());
    request.addHeader(HttpHeaders.AUTHORIZATION, getAuthzHeader());
    request.addHeader(HttpHeaders.CONTENT_TYPE, "application/json");

    JSONObject rootObject = new JSONObject();

    JSONArray schemas = new JSONArray();
    rootObject.put(SCHEMAS_ATTRIBUTE, schemas);

    JSONObject names = new JSONObject();
    names.put(FAMILY_NAME_ATTRIBUTE, familyName);
    names.put(GIVEN_NAME_ATTRIBUTE, givenName);

    rootObject.put(NAME_ATTRIBUTE, names);
    rootObject.put(USER_NAME_ATTRIBUTE, userName);

    JSONObject emailWork = new JSONObject();
    emailWork.put(TYPE_PARAM, EMAIL_TYPE_WORK_ATTRIBUTE);
    emailWork.put(VALUE_PARAM, workEmail);

    JSONObject emailHome = new JSONObject();
    emailHome.put(TYPE_PARAM, EMAIL_TYPE_HOME_ATTRIBUTE);
    emailHome.put(VALUE_PARAM, homeEmail);

    JSONArray emails = new JSONArray();
    emails.add(emailWork);/*from  w ww. j a v  a 2  s .com*/
    emails.add(emailHome);

    rootObject.put(EMAILS_ATTRIBUTE, emails);

    rootObject.put(PASSWORD_ATTRIBUTE, password);

    StringEntity entity = new StringEntity(rootObject.toString());
    request.setEntity(entity);

    HttpResponse response = client.execute(request);
    assertEquals(response.getStatusLine().getStatusCode(), 201, "User " + "has not been created successfully");

    Object responseObj = JSONValue.parse(EntityUtils.toString(response.getEntity()));
    EntityUtils.consume(response.getEntity());

    String usernameFromResponse = ((JSONObject) responseObj).get(USER_NAME_ATTRIBUTE).toString();
    assertEquals(usernameFromResponse, userName);

    String userId;
    userId = ((JSONObject) responseObj).get(ID_ATTRIBUTE).toString();
    userIds.add(userId);
    assertNotNull(userId);
}

From source file:org.wso2.identity.integration.test.scim2.SCIM2MultiAttributeUserFilterTestCase.java

private void createGroup(List<String> userNames, List<String> userIDs, String groupName) throws IOException {

    HttpPost request = new HttpPost(getGroupPath());
    request.addHeader(HttpHeaders.AUTHORIZATION, getAuthzHeader());
    request.addHeader(HttpHeaders.CONTENT_TYPE, "application/json");

    JSONObject rootObject = new JSONObject();

    JSONArray members = new JSONArray();
    for (int i = 0; i < userNames.size(); i++) {
        JSONObject member = new JSONObject();
        member.put(SCIM2BaseTestCase.DISPLAY_ATTRIBUTE, userNames.get(i));
        member.put(VALUE_PARAM, userIDs.get(i));
        members.add(member);//from   w  ww .ja va2 s  .  c  o m
    }

    rootObject.put(SCIM2BaseTestCase.DISPLAY_NAME_ATTRIBUTE, groupName);

    rootObject.put(SCIM2BaseTestCase.MEMBERS_ATTRIBUTE, members);

    StringEntity entity = new StringEntity(rootObject.toString());
    request.setEntity(entity);

    HttpResponse response = client.execute(request);

    assertEquals(response.getStatusLine().getStatusCode(), 201, "User " + "has not been created successfully");

    Object responseObj = JSONValue.parse(EntityUtils.toString(response.getEntity()));
    EntityUtils.consume(response.getEntity());

    String groupNameFromResponse = ((JSONObject) responseObj).get(SCIM2BaseTestCase.DISPLAY_NAME_ATTRIBUTE)
            .toString();
    //TODO: groupsName comes from this is <Userstore_domain>/displayName
    assertTrue(groupNameFromResponse.contains(groupName));

    String groupId = ((JSONObject) responseObj).get(ID_ATTRIBUTE).toString();
    groupIds.add(groupId);
    assertNotNull(groupId);
}

From source file:org.wso2.identity.scenarios.commons.HTTPCommonClient.java

/**
 * Send POST request with a JSON payload.
 *
 * @param url        Request URL.//  w  w  w .  j  a v a 2 s . com
 * @param jsonObject JSON object for post request.
 * @param headers    Request headers.
 * @return HttpResponse containing the response.
 * @throws IOException If error occurs while sending the request.
 */
public HttpResponse sendPostRequestWithJSON(String url, JSONObject jsonObject, Header[] headers)
        throws IOException {

    HttpPost request = new HttpPost(url);
    if (headers != null) {
        request.setHeaders(headers);
    }
    request.setEntity(new StringEntity(jsonObject.toString()));

    return this.client.execute(request);
}

From source file:org.wso2.identity.scenarios.commons.HTTPCommonClient.java

/**
 * Send PUT request with a JSON payload.
 *
 * @param url        Request URL./*w ww. j  a v a2s . co m*/
 * @param jsonObject JSON object for post request.
 * @param headers    Request headers.
 * @return HttpResponse containing the response.
 * @throws IOException If error occurs while sending the request.
 */
public HttpResponse sendPutRequestWithJSON(String url, JSONObject jsonObject, Header[] headers)
        throws IOException {

    HttpPut request = new HttpPut(url);
    if (headers != null) {
        request.setHeaders(headers);
    }
    request.setEntity(new StringEntity(jsonObject.toString()));

    return this.client.execute(request);
}

From source file:org.wso2.identity.scenarios.commons.util.IdentityScenarioUtil.java

/**
 * Send POST request with a JSON payload.
 *
 * @param client     HttpClient to be used for request sending.
 * @param url        Request URL./*  w ww  .j a va2s.  com*/
 * @param jsonObject JSON object for post request.
 * @param headers    Request headers.
 * @return HttpResponse containing the response.
 * @throws IOException If error occurs while sending the request.
 */
public static HttpResponse sendPostRequestWithJSON(HttpClient client, String url, JSONObject jsonObject,
        Header[] headers) throws IOException {

    HttpPost request = new HttpPost(url);
    if (headers != null) {
        request.setHeaders(headers);
    }
    request.setEntity(new StringEntity(jsonObject.toString()));

    return client.execute(request);
}

From source file:org.wso2.identity.scenarios.commons.util.IdentityScenarioUtil.java

/**
 * Send PUT request to the given URL./*from  w w w .j  a v a2s.co  m*/
 *
 * @param client     HttpClient to be used for request sending.
 * @param jsonObject JSON object for put request
 * @param url        Request URL.
 * @param headers    Request headers.
 * @return HttpResponse containing the response.
 * @throws IOException If error occurs while sending the request.
 */
public static HttpResponse sendUpdateRequest(HttpClient client, JSONObject jsonObject, String url,
        Header[] headers) throws IOException {

    HttpPut updateRequest = new HttpPut(url);
    if (headers != null) {
        updateRequest.setHeaders(headers);
    }
    updateRequest.setEntity(new StringEntity(jsonObject.toString()));
    return client.execute(updateRequest);
}

From source file:org.wso2.identity.scenarios.test.scim2.AnonymousProvisioningTestCase.java

@Test(description = "1.1.2.1.2.15")
private void selfRegister() throws Exception {

    scimUsersEndpoint = backendURL + SEPERATOR + Constants.SCIMEndpoints.SCIM2_ENDPOINT + SEPERATOR
            + Constants.SCIMEndpoints.SCIM_ANONYMOUS_USER;

    HttpPost request = new HttpPost(scimUsersEndpoint);
    request.addHeader(HttpHeaders.AUTHORIZATION, getAuthzHeader());
    request.addHeader(HttpHeaders.CONTENT_TYPE, SCIMConstants.CONTENT_TYPE_APPLICATION_JSON);

    JSONObject rootObject = new JSONObject();
    JSONArray schemas = new JSONArray();
    rootObject.put(SCIMConstants.SCHEMAS_ATTRIBUTE, schemas);

    JSONObject names = new JSONObject();
    names.put(SCIMConstants.FAMILY_NAME_ATTRIBUTE, SCIMConstants.FAMILY_NAME_CLAIM_VALUE);
    names.put(SCIMConstants.GIVEN_NAME_ATTRIBUTE, SCIMConstants.GIVEN_NAME_CLAIM_VALUE);
    rootObject.put(SCIMConstants.NAME_ATTRIBUTE, names);
    rootObject.put(SCIMConstants.USER_NAME_ATTRIBUTE, SCIMConstants.USERNAME);
    rootObject.put(SCIMConstants.PASSWORD_ATTRIBUTE, SCIMConstants.PASSWORD);

    JSONObject emailWork = new JSONObject();
    emailWork.put(SCIMConstants.TYPE_PARAM, SCIMConstants.EMAIL_TYPE_WORK_ATTRIBUTE);
    emailWork.put(SCIMConstants.VALUE_PARAM, WORKEMAIL);

    JSONObject emailHome = new JSONObject();
    emailHome.put(SCIMConstants.PRIMARY_PARAM, PRIMARYSTATE);
    emailHome.put(SCIMConstants.TYPE_PARAM, SCIMConstants.EMAIL_TYPE_HOME_ATTRIBUTE);
    emailHome.put(SCIMConstants.VALUE_PARAM, HOMEEMAIL);

    JSONArray emails = new JSONArray();
    emails.add(emailWork);//from   ww  w  . j a  v a  2  s.  c o  m
    emails.add(emailHome);
    rootObject.put(SCIMConstants.EMAILS_ATTRIBUTE, emails);

    StringEntity entity = new StringEntity(rootObject.toString());
    request.setEntity(entity);

    HttpResponse response = client.execute(request);
    assertEquals(response.getStatusLine().getStatusCode(), HttpStatus.SC_CREATED,
            "User has not been created" + " successfully");

    Object responseObj = JSONValue.parse(EntityUtils.toString(response.getEntity()));
    EntityUtils.consume(response.getEntity());

    userNameResponse = ((JSONObject) responseObj).get(SCIMConstants.USER_NAME_ATTRIBUTE).toString();
    assertEquals(userNameResponse, SCIMConstants.USERNAME);

    userId = ((JSONObject) responseObj).get(SCIMConstants.ID_ATTRIBUTE).toString();
    assertNotNull(userId);
}