Example usage for com.google.gson JsonObject equals

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

Introduction

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

Prototype

@Override
    public boolean equals(Object o) 

Source Link

Usage

From source file:org.openbaton.sdk.api.util.RestRequest.java

License:Apache License

private void getAccessToken() throws IOException, SDKException {

    HttpPost httpPost = new HttpPost(provider);

    httpPost.setHeader("Authorization", "Basic " + encoding);
    List<BasicNameValuePair> parametersBody = new ArrayList<>();
    parametersBody.add(new BasicNameValuePair("grant_type", "password"));
    parametersBody.add(new BasicNameValuePair("username", this.username));
    parametersBody.add(new BasicNameValuePair("password", this.password));

    log.debug("Username is: " + username);
    log.debug("Password is: " + password);

    httpPost.setEntity(new UrlEncodedFormEntity(parametersBody, StandardCharsets.UTF_8));

    CloseableHttpResponse response = null;
    log.debug("httpPost is: " + httpPost.toString());
    response = httpClient.execute(httpPost);
    String responseString = null;
    responseString = EntityUtils.toString(response.getEntity());
    int statusCode = response.getStatusLine().getStatusCode();
    response.close();/*from   w w  w  . j a  v a 2 s .  c  o  m*/
    httpPost.releaseConnection();
    log.trace(statusCode + ": " + responseString);

    Gson gson = new GsonBuilder().setPrettyPrinting().create();
    if (statusCode != 200) {
        JsonObject error = gson.fromJson(responseString, JsonObject.class);

        JsonElement detailMessage = error.get("detailMessage");
        if (detailMessage == null)
            detailMessage = error.get("errorMessage");
        if (detailMessage == null)
            detailMessage = error.get("message");
        if (detailMessage == null)
            detailMessage = error.get("description");
        if (detailMessage == null)
            detailMessage = error.get("errorDescription");

        log.error("Status Code [" + statusCode + "]: Error signing-in ["
                + (detailMessage != null ? detailMessage.getAsString() : "no error description") + "]");

        if (detailMessage == null)
            log.error("Got Error from server: \n" + gson.toJson(error));
        throw new SDKException("Status Code [" + statusCode + "]: Error signing-in ["
                + (detailMessage != null ? detailMessage.getAsString() : "no error description") + "]");
    }
    JsonObject jobj = new Gson().fromJson(responseString, JsonObject.class);
    log.trace("JsonTokeAccess is: " + jobj.toString());
    try {
        String token = jobj.get("value").getAsString();
        log.trace(token);
        bearerToken = "Bearer " + token;
        this.token = token;
    } catch (NullPointerException e) {
        String error = jobj.get("error").getAsString();
        if (error.equals("invalid_grant")) {
            throw new SDKException(
                    "Error during authentication: " + jobj.get("error_description").getAsString(), e);
        }
    }
}

From source file:org.wso2.iot.integration.common.AssertUtil.java

License:Open Source License

/**
 * This can be used to compare if to json strings are matched or not.
 *
 * @param expectedJsonPayload the expected json string.
 * @param realPayload         real json string.
 * @param mustMatch           If the real and expected must match, in order to become the test successful or not.
 *//*from   w ww.  j  a v a  2  s. c  o  m*/
public static void jsonPayloadCompare(String expectedJsonPayload, String realPayload, boolean mustMatch) {
    JsonElement jsonElement = new JsonParser().parse(expectedJsonPayload);
    JsonObject expectedPayloadObject = jsonElement.getAsJsonObject();
    jsonElement = new JsonParser().parse(realPayload);
    JsonObject realPayloadObject = jsonElement.getAsJsonObject();
    if (mustMatch) {
        Assert.assertTrue(realPayloadObject.equals(expectedPayloadObject));
    } else {
        Assert.assertFalse(realPayloadObject.equals(expectedPayloadObject));
    }
}