Example usage for javax.json JsonReader close

List of usage examples for javax.json JsonReader close

Introduction

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

Prototype

@Override
void close();

Source Link

Document

Closes this reader and frees any resources associated with the reader.

Usage

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();

    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();

    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//  w  w w  .  j  a va 2  s .c  o m
                    .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();

    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();

    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 {// w w  w .  j a  v a 2 s .com
            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 va 2s .  c o m*/
        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();

    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 {//from   w  w  w .j ava2 s  . co  m
            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:jp.co.yahoo.yconnect.core.oidc.IdTokenDecoder.java

/**
 * IdToken?//from  w  w  w . j av a2  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:org.grogshop.services.endpoints.impl.ShopUserProfileServiceImpl.java

public Response setInterests(@NotNull @PathParam("user_id") Long user_id,
        @FormParam("interests") String interests) throws ServiceException {
    log.info("Storing from the database: (" + user_id + ") " + interests);
    if (interests != null) {
        JsonReader reader = Json.createReader(new ByteArrayInputStream(interests.getBytes()));
        JsonArray array = reader.readArray();
        reader.close();

        List<Interest> interestsList = new ArrayList<Interest>(array.size());

        if (array != null) {

            for (int i = 0; i < array.size(); i++) {
                log.info("Interest[" + i + "]: " + array.getJsonObject(i).getString("name"));
                interestsList.add(interestService.get(array.getJsonObject(i).getString("name")));
            }/*from w  w w.  java2s  . c  o  m*/

        }

        profileService.setInterests(user_id, interestsList);
    }

    return Response.ok().build();
}

From source file:org.grogshop.services.endpoints.impl.ShopItemsServiceImpl.java

@Override
public Response newItem(@NotNull @FormParam("user_id") Long userId, @NotNull @FormParam("club_id") Long clubId,
        @NotNull @FormParam("type") String type, @NotNull @NotEmpty @FormParam("name") String name,
        @NotNull @NotEmpty @FormParam("description") String description,
        @NotNull @NotEmpty @FormParam("tags") String tags, @FormParam("minPrice") String minPrice,
        @FormParam("maxPrice") String maxPrice) throws ServiceException {

    JsonReader reader = Json.createReader(new ByteArrayInputStream(tags.getBytes()));
    JsonArray array = reader.readArray();
    reader.close();

    List<String> interestsList = new ArrayList<String>();

    if (array != null) {

        for (int i = 0; i < array.size(); i++) {
            log.info("Tag[" + i + "]: " + array.getJsonObject(i).getString("text"));
            interestsList.add(array.getJsonObject(i).getString("text"));
        }/*from  w w w  . ja v  a  2  s  .  c  o  m*/

    }
    Long newItem = itemsService.newItem(userId, clubId, type, name, description, interestsList,
            (minPrice == null) ? new BigDecimal(0) : new BigDecimal(minPrice),
            (maxPrice == null) ? new BigDecimal(0) : new BigDecimal(maxPrice));
    return Response.ok(newItem).build();

}

From source file:at.porscheinformatik.sonarqube.licensecheck.maven.MavenDependencyScanner.java

@Override
public List<Dependency> scan(File moduleDir, String mavenProjectDependencies) {
    JsonReader jsonReader = Json.createReader(new StringReader(mavenProjectDependencies));
    Set<Dependency> dependencies = new HashSet<>();

    parseDependencyJson(dependencies, jsonReader.readArray());

    loadLicenses(dependencies);/*w w w. ja v a  2  s.  c  om*/

    mapAdditionalLicenses(dependencies);

    jsonReader.close();

    return new ArrayList<>(dependencies);
}