Example usage for javax.json JsonReader readObject

List of usage examples for javax.json JsonReader readObject

Introduction

In this page you can find the example usage for javax.json JsonReader readObject.

Prototype

JsonObject readObject();

Source Link

Document

Returns a JSON object that is represented in the input source.

Usage

From source file:uk.ac.ebi.ep.data.service.BioPortalService.java

@Transactional
public String getDiseaseDescription(String term) {
    String definition = "";

    // Reader reader = new StringReader(get(REST_URL + "/search?q=" + term+"&ontology={EFO,MeSH,OMIM}&exact_match=true"));
    Reader reader = new StringReader(get(REST_URL + "/search?q=" + term));

    JsonReader jsonReader = Json.createReader(reader);

    JsonObject jo = jsonReader.readObject();

    JsonArray jsonArray = jo.getJsonArray("collection");
    for (JsonObject obj : jsonArray.getValuesAs(JsonObject.class)) {

        for (Map.Entry<String, JsonValue> entry : obj.entrySet()) {

            if (entry.getKey().equalsIgnoreCase(DEFINITION)) {

                definition = entry.getValue().toString().replace("\"", "").replaceAll("\\[", "")
                        .replaceAll("\\]", "");
            }/*from  ww  w.  j  a v a2s.c  o m*/

        }
    }

    return definition;
}

From source file:jp.co.yahoo.yconnect.core.oauth2.RefreshTokenClient.java

public void fetch() throws TokenException, Exception {

    HttpParameters parameters = new HttpParameters();
    parameters.put("grant_type", OAuth2GrantType.REFRESH_TOKEN);
    parameters.put("refresh_token", refreshToken);

    String credential = clientId + ":" + clientSecret;
    String basic = new String(Base64.encodeBase64(credential.getBytes()));

    HttpHeaders requestHeaders = new HttpHeaders();
    requestHeaders.put("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
    requestHeaders.put("Authorization", "Basic " + basic);

    client = new YHttpClient();
    client.requestPost(endpointUrl, parameters, requestHeaders);

    YConnectLogger.debug(TAG, client.getResponseHeaders().toString());
    YConnectLogger.debug(TAG, client.getResponseBody().toString());

    String json = client.getResponseBody();
    JsonReader jsonReader = Json.createReader(new StringReader(json));
    JsonObject jsonObject = jsonReader.readObject();
    jsonReader.close();//from  w ww .  j av  a 2s.  com

    int statusCode = client.getStatusCode();

    checkErrorResponse(statusCode, jsonObject);

    String accessTokenString = (String) jsonObject.getString("access_token");
    long expiresIn = Long.parseLong((String) jsonObject.getString("expires_in"));
    accessToken = new BearerToken(accessTokenString, expiresIn, refreshToken);

}

From source file:at.porscheinformatik.sonarqube.licensecheck.webservice.mavenlicense.MavenLicenseEditAction.java

@Override
public void handle(Request request, Response response) throws Exception {
    JsonReader jsonReader = Json.createReader(new StringReader(request.param(MavenLicenseConfiguration.PARAM)));
    JsonObject jsonObject = jsonReader.readObject();
    jsonReader.close();//from ww w .  j  a va  2s .c o m

    boolean oldRegexIsNotBlank = StringUtils
            .isNotBlank(jsonObject.getString(MavenLicenseConfiguration.PROPERTY_OLD_REGEX));
    boolean newRegexIsNotBlank = StringUtils
            .isNotBlank(jsonObject.getString(MavenLicenseConfiguration.PROPERTY_NEW_REGEX));
    boolean newKeyIsNotBlank = StringUtils
            .isNotBlank(jsonObject.getString(MavenLicenseConfiguration.PROPERTY_NEW_KEY));

    if (oldRegexIsNotBlank && newRegexIsNotBlank && newKeyIsNotBlank) {
        MavenLicense newMavenLicense = new MavenLicense(
                jsonObject.getString(MavenLicenseConfiguration.PROPERTY_NEW_REGEX),
                jsonObject.getString(MavenLicenseConfiguration.PROPERTY_NEW_KEY));

        if (!mavenLicenseSettingsService.checkIfListContains(newMavenLicense)) {
            mavenLicenseSettingsService
                    .deleteMavenLicense(jsonObject.getString(MavenLicenseConfiguration.PROPERTY_OLD_REGEX));
            mavenLicenseSettingsService.addMavenLicense(newMavenLicense);
            mavenLicenseSettingsService.sortMavenLicenses();
            response.stream().setStatus(HTTPConfiguration.HTTP_STATUS_OK);
            LOGGER.info(MavenLicenseConfiguration.INFO_EDIT_SUCCESS + jsonObject.toString());
        } else {
            LOGGER.error(MavenLicenseConfiguration.ERROR_EDIT_ALREADY_EXISTS + jsonObject.toString());
            response.stream().setStatus(HTTPConfiguration.HTTP_STATUS_NOT_MODIFIED);
        }

    } else {
        LOGGER.error(MavenLicenseConfiguration.ERROR_EDIT_INVALID_INPUT + jsonObject.toString());
        response.stream().setStatus(HTTPConfiguration.HTTP_STATUS_NOT_MODIFIED);
    }
}

From source file:jp.co.yahoo.yconnect.core.oauth2.TokenClient.java

@Override
public void fetch() throws TokenException, Exception {

    HttpParameters parameters = new HttpParameters();
    parameters.put("grant_type", OAuth2GrantType.AUTHORIZATION_CODE);
    parameters.put("code", authorizationCode);
    parameters.put("redirect_uri", redirectUri);

    String credential = clientId + ":" + clientSecret;
    String basic = new String(Base64.encodeBase64(credential.getBytes()));

    HttpHeaders requestHeaders = new HttpHeaders();
    requestHeaders.put("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
    requestHeaders.put("Authorization", "Basic " + basic);

    client = new YHttpClient();
    client.requestPost(endpointUrl, parameters, requestHeaders);

    YConnectLogger.debug(TAG, client.getResponseHeaders().toString());
    YConnectLogger.debug(TAG, client.getResponseBody().toString());

    String json = client.getResponseBody();
    JsonReader jsonReader = Json.createReader(new StringReader(json));

    JsonObject jsonObject = jsonReader.readObject();
    jsonReader.close();/*ww  w .  j ava  2s.c  om*/

    int statusCode = client.getStatusCode();

    checkErrorResponse(statusCode, jsonObject);

    String accessTokenString = (String) jsonObject.getString("access_token");
    long expiresIn = Long.parseLong((String) jsonObject.getString("expires_in"));
    String refreshToken = (String) jsonObject.getString("refresh_token");
    accessToken = new BearerToken(accessTokenString, expiresIn, refreshToken);
    idToken = (String) jsonObject.getString("id_token");

}

From source file:at.porscheinformatik.sonarqube.licensecheck.webservice.mavendependency.MavenDependencyAddAction.java

@Override
public void handle(Request request, Response response) throws Exception {
    JsonReader jsonReader = Json
            .createReader(new StringReader(request.param(MavenDependencyConfiguration.PARAM)));
    JsonObject jsonObject = jsonReader.readObject();
    jsonReader.close();/*from ww w  . ja  va2s . c  o  m*/

    boolean keyIsNotBlank = StringUtils
            .isNotBlank(jsonObject.getString(MavenDependencyConfiguration.PROPERTY_KEY));
    boolean licenseIsNotBlank = StringUtils
            .isNotBlank(jsonObject.getString(MavenDependencyConfiguration.PROPERTY_LICENSE));

    if (keyIsNotBlank && licenseIsNotBlank) {
        boolean success = mavenDependencySettingsService.addMavenDependency(
                jsonObject.getString(MavenDependencyConfiguration.PROPERTY_KEY),
                jsonObject.getString(MavenDependencyConfiguration.PROPERTY_LICENSE));

        if (success) {
            LOGGER.info(MavenDependencyConfiguration.INFO_ADD_SUCCESS + jsonObject.toString());
            response.stream().setStatus(HTTPConfiguration.HTTP_STATUS_OK);
        } else {
            LOGGER.error(MavenDependencyConfiguration.ERROR_ADD_ALREADY_EXISTS + jsonObject.toString());
            response.stream().setStatus(HTTPConfiguration.HTTP_STATUS_NOT_MODIFIED);
        }
    } else {
        LOGGER.error(MavenDependencyConfiguration.INFO_ADD_SUCCESS + jsonObject.toString());
        response.stream().setStatus(HTTPConfiguration.HTTP_STATUS_NOT_MODIFIED);
    }
}

From source file:GUI_The_Code_Book.ParserAPIStackEx.java

private void parseStackExchange(String jsonStr) {
    JsonReader reader = null;
    try {/*from w  ww . j a  v  a  2  s  .  c om*/
        reader = Json.createReader(new StringReader(jsonStr));
        JsonObject jsonObject = reader.readObject();
        reader.close();
        JsonArray array = jsonObject.getJsonArray("items");
        for (JsonObject result : array.getValuesAs(JsonObject.class)) {
            urlList.add(new URLlist(result.getInt("view_count"), result.getInt("answer_count"),
                    result.getString("title"), result.getString("link")));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:at.porscheinformatik.sonarqube.licensecheck.webservice.mavendependency.MavenDependencyEditAction.java

@Override
public void handle(Request request, Response response) throws Exception {
    JsonReader jsonReader = Json
            .createReader(new StringReader(request.param(MavenDependencyConfiguration.PARAM)));
    JsonObject jsonObject = jsonReader.readObject();
    jsonReader.close();//from  w  ww  . j  av  a  2 s  .c om

    boolean oldKeyIsNotBlank = StringUtils
            .isNotBlank(jsonObject.getString(MavenDependencyConfiguration.PROPERTY_OLD_KEY));
    boolean newKeyIsNotBlank = StringUtils
            .isNotBlank(jsonObject.getString(MavenDependencyConfiguration.PROPERTY_NEW_KEY));
    boolean newLicenseIsNotBlank = StringUtils
            .isNotBlank(jsonObject.getString(MavenDependencyConfiguration.PROPERTY_NEW_LICENSE));

    if (oldKeyIsNotBlank && newKeyIsNotBlank && newLicenseIsNotBlank) {
        MavenDependency newMavenDependency = new MavenDependency(
                jsonObject.getString(MavenDependencyConfiguration.PROPERTY_NEW_KEY),
                jsonObject.getString(MavenDependencyConfiguration.PROPERTY_NEW_LICENSE));

        if (!mavenDependencySettingsService.hasDependency(newMavenDependency)) {
            mavenDependencySettingsService
                    .deleteMavenDependency(jsonObject.getString(MavenDependencyConfiguration.PROPERTY_OLD_KEY));
            mavenDependencySettingsService.addMavenDependency(newMavenDependency);
            response.stream().setStatus(HTTPConfiguration.HTTP_STATUS_OK);
            LOGGER.info(MavenDependencyConfiguration.INFO_EDIT_SUCCESS + jsonObject.toString());
        } else {
            LOGGER.error(MavenDependencyConfiguration.ERROR_EDIT_ALREADY_EXISTS + jsonObject.toString());
            response.stream().setStatus(HTTPConfiguration.HTTP_STATUS_NOT_MODIFIED);
        }

    } else {
        LOGGER.error(MavenDependencyConfiguration.ERROR_ADD_INVALID_INPUT + jsonObject.toString());
        response.stream().setStatus(HTTPConfiguration.HTTP_STATUS_NOT_MODIFIED);
    }
}

From source file:uk.ac.ebi.ep.adapter.bioportal.BioPortalService.java

@Override
public Disease getDisease(String term) throws BioportalAdapterException {
    String definition = "";

    // Reader reader = new StringReader(get(REST_URL + "/search?q=" + term+"&ontology={EFO,MeSH,OMIM}&exact_match=true"));
    Reader reader = new StringReader(get(REST_URL + "/search?q=" + term));

    JsonReader jsonReader = Json.createReader(reader);

    JsonObject jo = jsonReader.readObject();

    JsonArray jsonArray = jo.getJsonArray("collection");
    Disease disease = null;/* ww  w  .  ja va  2 s.c  om*/
    for (JsonObject obj : jsonArray.getValuesAs(JsonObject.class)) {
        disease = new Disease();
        disease.setId(term);
        for (Map.Entry<String, JsonValue> entry : obj.entrySet()) {

            if (entry.getKey().equalsIgnoreCase(LABEL)) {
                String name = entry.getValue().toString();
                disease.setName(name);

            }
            if (entry.getKey().equalsIgnoreCase(DEFINITION)) {

                definition = entry.getValue().toString().replace("\"", "").replaceAll("\\[", "")
                        .replaceAll("\\]", "");
                disease.setDescription(definition);
            }

        }
    }

    return disease;
}

From source file:jp.co.yahoo.yconnect.core.oidc.IdTokenDecoder.java

/**
 * IdToken?// w w  w . j  av a 2 s  .com
 * 
 * @return IdTokenObject
 * @throws DataFormatException
 */
public IdTokenObject decode() throws DataFormatException {

    HashMap<String, String> idToken = this.splitIdToken();

    // Header
    String jsonHeader = idToken.get("header");

    JsonReader jsonHeaderReader = Json.createReader(new StringReader(jsonHeader));
    JsonObject rootHeader = jsonHeaderReader.readObject();
    jsonHeaderReader.close();

    JsonString typeString = rootHeader.getJsonString("typ");
    String type = typeString.getString();

    JsonString algorithmString = rootHeader.getJsonString("alg");
    String algorithm = algorithmString.getString();

    // Payload
    String jsonPayload = idToken.get("payload");

    JsonReader jsonPayloadReader = Json.createReader(new StringReader(jsonPayload));

    JsonObject rootPayload = jsonPayloadReader.readObject();
    jsonPayloadReader.close();

    JsonString issString = rootPayload.getJsonString("iss");
    String iss = issString.getString();

    JsonString userIdString = rootPayload.getJsonString("user_id");
    String userId = userIdString.getString();

    JsonString audString = rootPayload.getJsonString("aud");
    ArrayList<String> aud = new ArrayList<String>();
    aud.add(audString.getString());

    JsonNumber expString = rootPayload.getJsonNumber("exp");
    int exp = expString.intValue();

    JsonNumber iatString = rootPayload.getJsonNumber("iat");
    int iat = iatString.intValue();

    JsonString nonceString = rootPayload.getJsonString("nonce");
    String nonce = nonceString.getString();

    // signature
    String signature = idToken.get("signature");

    // ???
    return new IdTokenObject(type, algorithm, iss, userId, aud, nonce, exp, iat, signature);
}

From source file:uk.ac.ebi.ep.adapter.bioportal.BioPortalService.java

@Override
public Disease getDiseaseByName(String term, String diseaseId) throws BioportalAdapterException {
    String definition = "";

    // Reader reader = new StringReader(get(REST_URL + "/search?q=" + term+"&ontology={EFO,MeSH,OMIM}&exact_match=true"));
    Reader reader = new StringReader(get(REST_URL + "/search?q=" + term));

    JsonReader jsonReader = Json.createReader(reader);

    JsonObject jo = jsonReader.readObject();

    JsonArray jsonArray = jo.getJsonArray("collection");
    Disease disease = null;/*from ww w.j av a2s .com*/
    for (JsonObject obj : jsonArray.getValuesAs(JsonObject.class)) {
        disease = new Disease();
        disease.setId(diseaseId);
        for (Map.Entry<String, JsonValue> entry : obj.entrySet()) {

            switch (entry.getKey()) {
            case LABEL:
                if (entry.getKey().equalsIgnoreCase(LABEL)) {
                    String name = entry.getValue().toString();
                    name = name.replaceAll("^\"|\"$", "");
                    if (name.equalsIgnoreCase(term)) {
                        disease.setName(name);

                    }

                }
                break;
            case DEFINITION:
                if (entry.getKey().equalsIgnoreCase(DEFINITION)) {
                    definition = entry.getValue().toString().replace("\"", "").replaceAll("\\[", "")
                            .replaceAll("\\]", "");

                    disease.setDescription(definition);
                    return disease;
                }
                break;

            }

        }
    }

    return disease;
}