Example usage for com.google.gson.stream JsonReader peek

List of usage examples for com.google.gson.stream JsonReader peek

Introduction

In this page you can find the example usage for com.google.gson.stream JsonReader peek.

Prototype

public JsonToken peek() throws IOException 

Source Link

Document

Returns the type of the next token without consuming it.

Usage

From source file:org.mitre.openid.connect.service.impl.MITREidDataService_1_0.java

License:Apache License

/**
 * @param reader//from w  w  w . j  av a2  s.c  om
 * @throws IOException
 */
private void readAuthenticationHolders(JsonReader reader) throws IOException {
    reader.beginArray();
    while (reader.hasNext()) {
        AuthenticationHolderEntity ahe = new AuthenticationHolderEntity();
        reader.beginObject();
        Long currentId = null;
        while (reader.hasNext()) {
            switch (reader.peek()) {
            case END_OBJECT:
                continue;
            case NAME:
                String name = reader.nextName();
                if (reader.peek() == JsonToken.NULL) {
                    reader.skipValue();
                } else if (name.equals("id")) {
                    currentId = reader.nextLong();
                } else if (name.equals("ownerId")) {
                    //not needed
                    reader.skipValue();
                } else if (name.equals("authentication")) {
                    OAuth2Request clientAuthorization = null;
                    Authentication userAuthentication = null;
                    reader.beginObject();
                    while (reader.hasNext()) {
                        switch (reader.peek()) {
                        case END_OBJECT:
                            continue;
                        case NAME:
                            String subName = reader.nextName();
                            if (reader.peek() == JsonToken.NULL) {
                                reader.skipValue();
                            } else if (subName.equals("clientAuthorization")) {
                                clientAuthorization = readAuthorizationRequest(reader);
                            } else if (subName.equals("userAuthentication")) {
                                // skip binary encoded version
                                reader.skipValue();

                            } else if (subName.equals("savedUserAuthentication")) {
                                userAuthentication = readSavedUserAuthentication(reader);

                            } else {
                                logger.debug("Found unexpected entry");
                                reader.skipValue();
                            }
                            break;
                        default:
                            logger.debug("Found unexpected entry");
                            reader.skipValue();
                            continue;
                        }
                    }
                    reader.endObject();
                    OAuth2Authentication auth = new OAuth2Authentication(clientAuthorization,
                            userAuthentication);
                    ahe.setAuthentication(auth);
                } else {
                    logger.debug("Found unexpected entry");
                    reader.skipValue();
                }
                break;
            default:
                logger.debug("Found unexpected entry");
                reader.skipValue();
                continue;
            }
        }
        reader.endObject();
        Long newId = authHolderRepository.save(ahe).getId();
        maps.getAuthHolderOldToNewIdMap().put(currentId, newId);
        logger.debug("Read authentication holder {}", currentId);
    }
    reader.endArray();
    logger.info("Done reading authentication holders");
}

From source file:org.mitre.openid.connect.service.impl.MITREidDataService_1_0.java

License:Apache License

private OAuth2Request readAuthorizationRequest(JsonReader reader) throws IOException {
    Set<String> scope = new LinkedHashSet<>();
    Set<String> resourceIds = new HashSet<>();
    boolean approved = false;
    Collection<GrantedAuthority> authorities = new HashSet<>();
    Map<String, String> authorizationParameters = new HashMap<>();
    Set<String> responseTypes = new HashSet<>();
    String redirectUri = null;/*ww  w. j  av  a 2s  .c o  m*/
    String clientId = null;
    reader.beginObject();
    while (reader.hasNext()) {
        switch (reader.peek()) {
        case END_OBJECT:
            continue;
        case NAME:
            String name = reader.nextName();
            if (reader.peek() == JsonToken.NULL) {
                reader.skipValue();
            } else if (name.equals("authorizationParameters")) {
                authorizationParameters = readMap(reader);
            } else if (name.equals("approvalParameters")) {
                reader.skipValue();
            } else if (name.equals("clientId")) {
                clientId = reader.nextString();
            } else if (name.equals("scope")) {
                scope = readSet(reader);
            } else if (name.equals("resourceIds")) {
                resourceIds = readSet(reader);
            } else if (name.equals("authorities")) {
                Set<String> authorityStrs = readSet(reader);
                authorities = new HashSet<>();
                for (String s : authorityStrs) {
                    GrantedAuthority ga = new SimpleGrantedAuthority(s);
                    authorities.add(ga);
                }
            } else if (name.equals("approved")) {
                approved = reader.nextBoolean();
            } else if (name.equals("denied")) {
                if (approved == false) {
                    approved = !reader.nextBoolean();
                }
            } else if (name.equals("redirectUri")) {
                redirectUri = reader.nextString();
            } else if (name.equals("responseTypes")) {
                responseTypes = readSet(reader);
            } else {
                reader.skipValue();
            }
            break;
        default:
            logger.debug("Found unexpected entry");
            reader.skipValue();
            continue;
        }
    }
    reader.endObject();
    return new OAuth2Request(authorizationParameters, clientId, authorities, approved, scope, resourceIds,
            redirectUri, responseTypes, null);
}

From source file:org.mitre.openid.connect.service.impl.MITREidDataService_1_0.java

License:Apache License

/**
 * @param reader//  w w w .j  a v  a  2  s. c  o m
 * @return
 * @throws IOException
 */
private SavedUserAuthentication readSavedUserAuthentication(JsonReader reader) throws IOException {
    SavedUserAuthentication savedUserAuth = new SavedUserAuthentication();
    reader.beginObject();

    while (reader.hasNext()) {
        switch (reader.peek()) {
        case END_OBJECT:
            continue;
        case NAME:
            String name = reader.nextName();
            if (reader.peek() == JsonToken.NULL) {
                reader.skipValue();
            } else if (name.equals("name")) {
                savedUserAuth.setName(reader.nextString());
            } else if (name.equals("sourceClass")) {
                savedUserAuth.setSourceClass(reader.nextString());
            } else if (name.equals("authenticated")) {
                savedUserAuth.setAuthenticated(reader.nextBoolean());
            } else if (name.equals("authorities")) {
                Set<String> authorityStrs = readSet(reader);
                Set<GrantedAuthority> authorities = new HashSet<GrantedAuthority>();
                for (String s : authorityStrs) {
                    GrantedAuthority ga = new SimpleGrantedAuthority(s);
                    authorities.add(ga);
                }
                savedUserAuth.setAuthorities(authorities);
            } else {
                logger.debug("Found unexpected entry");
                reader.skipValue();
            }
            break;
        default:
            logger.debug("Found unexpected entry");
            reader.skipValue();
            continue;
        }
    }

    reader.endObject();
    return savedUserAuth;
}

From source file:org.mitre.openid.connect.service.impl.MITREidDataService_1_0.java

License:Apache License

/**
 * @param reader/*  w  w w.ja  v a 2  s.com*/
 * @throws IOException
 */
private void readGrants(JsonReader reader) throws IOException {
    reader.beginArray();
    while (reader.hasNext()) {
        ApprovedSite site = new ApprovedSite();
        Long currentId = null;
        Long whitelistedSiteId = null;
        Set<Long> tokenIds = null;
        reader.beginObject();
        while (reader.hasNext()) {
            switch (reader.peek()) {
            case END_OBJECT:
                continue;
            case NAME:
                String name = reader.nextName();
                if (reader.peek() == JsonToken.NULL) {
                    reader.skipValue();
                } else if (name.equals("id")) {
                    currentId = reader.nextLong();
                } else if (name.equals("accessDate")) {
                    Date date = utcToDate(reader.nextString());
                    site.setAccessDate(date);
                } else if (name.equals("clientId")) {
                    site.setClientId(reader.nextString());
                } else if (name.equals("creationDate")) {
                    Date date = utcToDate(reader.nextString());
                    site.setCreationDate(date);
                } else if (name.equals("timeoutDate")) {
                    Date date = utcToDate(reader.nextString());
                    site.setTimeoutDate(date);
                } else if (name.equals("userId")) {
                    site.setUserId(reader.nextString());
                } else if (name.equals("allowedScopes")) {
                    Set<String> allowedScopes = readSet(reader);
                    site.setAllowedScopes(allowedScopes);
                } else if (name.equals("whitelistedSiteId")) {
                    whitelistedSiteId = reader.nextLong();
                } else if (name.equals("approvedAccessTokens")) {
                    tokenIds = readSet(reader);
                } else {
                    logger.debug("Found unexpected entry");
                    reader.skipValue();
                }
                break;
            default:
                logger.debug("Found unexpected entry");
                reader.skipValue();
                continue;
            }
        }
        reader.endObject();
        Long newId = approvedSiteRepository.save(site).getId();
        maps.getGrantOldToNewIdMap().put(currentId, newId);
        if (whitelistedSiteId != null) {
            logger.debug("Ignoring whitelisted site marker on approved site.");
        }
        if (tokenIds != null) {
            maps.getGrantToAccessTokensRefs().put(currentId, tokenIds);
        }
        logger.debug("Read grant {}", currentId);
    }
    reader.endArray();
    logger.info("Done reading grants");
}

From source file:org.mitre.openid.connect.service.impl.MITREidDataService_1_0.java

License:Apache License

/**
 * @param reader//ww  w.  j  av a 2s  .c o  m
 * @throws IOException
 */
private void readWhitelistedSites(JsonReader reader) throws IOException {
    reader.beginArray();
    while (reader.hasNext()) {
        WhitelistedSite wlSite = new WhitelistedSite();
        Long currentId = null;
        reader.beginObject();
        while (reader.hasNext()) {
            switch (reader.peek()) {
            case END_OBJECT:
                continue;
            case NAME:
                String name = reader.nextName();
                if (name.equals("id")) {
                    currentId = reader.nextLong();
                } else if (name.equals("clientId")) {
                    wlSite.setClientId(reader.nextString());
                } else if (name.equals("creatorUserId")) {
                    wlSite.setCreatorUserId(reader.nextString());
                } else if (name.equals("allowedScopes")) {
                    Set<String> allowedScopes = readSet(reader);
                    wlSite.setAllowedScopes(allowedScopes);
                } else {
                    logger.debug("Found unexpected entry");
                    reader.skipValue();
                }
                break;
            default:
                logger.debug("Found unexpected entry");
                reader.skipValue();
                continue;
            }
        }
        reader.endObject();
        Long newId = wlSiteRepository.save(wlSite).getId();
        maps.getWhitelistedSiteOldToNewIdMap().put(currentId, newId);
    }
    reader.endArray();
    logger.info("Done reading whitelisted sites");
}

From source file:org.mitre.openid.connect.service.impl.MITREidDataService_1_0.java

License:Apache License

/**
 * @param reader//from   ww  w . ja v  a 2  s .  c om
 * @throws IOException
 */
private void readBlacklistedSites(JsonReader reader) throws IOException {
    reader.beginArray();
    while (reader.hasNext()) {
        BlacklistedSite blSite = new BlacklistedSite();
        reader.beginObject();
        while (reader.hasNext()) {
            switch (reader.peek()) {
            case END_OBJECT:
                continue;
            case NAME:
                String name = reader.nextName();
                if (name.equals("id")) {
                    reader.skipValue();
                } else if (name.equals("uri")) {
                    blSite.setUri(reader.nextString());
                } else {
                    logger.debug("Found unexpected entry");
                    reader.skipValue();
                }
                break;
            default:
                logger.debug("Found unexpected entry");
                reader.skipValue();
                continue;
            }
        }
        reader.endObject();
        blSiteRepository.save(blSite);
    }
    reader.endArray();
    logger.info("Done reading blacklisted sites");
}

From source file:org.mitre.openid.connect.service.impl.MITREidDataService_1_0.java

License:Apache License

/**
 * @param reader//from  w  w  w.jav a 2 s  . c o m
 * @throws IOException
 */
private void readClients(JsonReader reader) throws IOException {
    reader.beginArray();
    while (reader.hasNext()) {
        ClientDetailsEntity client = new ClientDetailsEntity();
        reader.beginObject();
        while (reader.hasNext()) {
            switch (reader.peek()) {
            case END_OBJECT:
                continue;
            case NAME:
                String name = reader.nextName();
                if (reader.peek() == JsonToken.NULL) {
                    reader.skipValue();
                } else if (name.equals("clientId")) {
                    client.setClientId(reader.nextString());
                } else if (name.equals("resourceIds")) {
                    Set<String> resourceIds = readSet(reader);
                    client.setResourceIds(resourceIds);
                } else if (name.equals("secret")) {
                    client.setClientSecret(reader.nextString());
                } else if (name.equals("scope")) {
                    Set<String> scope = readSet(reader);
                    client.setScope(scope);
                } else if (name.equals("authorities")) {
                    Set<String> authorityStrs = readSet(reader);
                    Set<GrantedAuthority> authorities = new HashSet<>();
                    for (String s : authorityStrs) {
                        GrantedAuthority ga = new SimpleGrantedAuthority(s);
                        authorities.add(ga);
                    }
                    client.setAuthorities(authorities);
                } else if (name.equals("accessTokenValiditySeconds")) {
                    client.setAccessTokenValiditySeconds(reader.nextInt());
                } else if (name.equals("refreshTokenValiditySeconds")) {
                    client.setRefreshTokenValiditySeconds(reader.nextInt());
                } else if (name.equals("redirectUris")) {
                    Set<String> redirectUris = readSet(reader);
                    client.setRedirectUris(redirectUris);
                } else if (name.equals("name")) {
                    client.setClientName(reader.nextString());
                } else if (name.equals("uri")) {
                    client.setClientUri(reader.nextString());
                } else if (name.equals("logoUri")) {
                    client.setLogoUri(reader.nextString());
                } else if (name.equals("contacts")) {
                    Set<String> contacts = readSet(reader);
                    client.setContacts(contacts);
                } else if (name.equals("tosUri")) {
                    client.setTosUri(reader.nextString());
                } else if (name.equals("tokenEndpointAuthMethod")) {
                    AuthMethod am = AuthMethod.getByValue(reader.nextString());
                    client.setTokenEndpointAuthMethod(am);
                } else if (name.equals("grantTypes")) {
                    Set<String> grantTypes = readSet(reader);
                    client.setGrantTypes(grantTypes);
                } else if (name.equals("responseTypes")) {
                    Set<String> responseTypes = readSet(reader);
                    client.setResponseTypes(responseTypes);
                } else if (name.equals("policyUri")) {
                    client.setPolicyUri(reader.nextString());
                } else if (name.equals("applicationType")) {
                    AppType appType = AppType.getByValue(reader.nextString());
                    client.setApplicationType(appType);
                } else if (name.equals("sectorIdentifierUri")) {
                    client.setSectorIdentifierUri(reader.nextString());
                } else if (name.equals("subjectType")) {
                    SubjectType st = SubjectType.getByValue(reader.nextString());
                    client.setSubjectType(st);
                } else if (name.equals("jwks_uri")) {
                    client.setJwksUri(reader.nextString());
                } else if (name.equals("requestObjectSigningAlg")) {
                    JWSAlgorithm alg = JWSAlgorithm.parse(reader.nextString());
                    client.setRequestObjectSigningAlg(alg);
                } else if (name.equals("userInfoEncryptedResponseAlg")) {
                    JWEAlgorithm alg = JWEAlgorithm.parse(reader.nextString());
                    client.setUserInfoEncryptedResponseAlg(alg);
                } else if (name.equals("userInfoEncryptedResponseEnc")) {
                    EncryptionMethod alg = EncryptionMethod.parse(reader.nextString());
                    client.setUserInfoEncryptedResponseEnc(alg);
                } else if (name.equals("userInfoSignedResponseAlg")) {
                    JWSAlgorithm alg = JWSAlgorithm.parse(reader.nextString());
                    client.setUserInfoSignedResponseAlg(alg);
                } else if (name.equals("idTokenSignedResonseAlg")) {
                    JWSAlgorithm alg = JWSAlgorithm.parse(reader.nextString());
                    client.setIdTokenSignedResponseAlg(alg);
                } else if (name.equals("idTokenEncryptedResponseAlg")) {
                    JWEAlgorithm alg = JWEAlgorithm.parse(reader.nextString());
                    client.setIdTokenEncryptedResponseAlg(alg);
                } else if (name.equals("idTokenEncryptedResponseEnc")) {
                    EncryptionMethod alg = EncryptionMethod.parse(reader.nextString());
                    client.setIdTokenEncryptedResponseEnc(alg);
                } else if (name.equals("tokenEndpointAuthSigningAlg")) {
                    JWSAlgorithm alg = JWSAlgorithm.parse(reader.nextString());
                    client.setTokenEndpointAuthSigningAlg(alg);
                } else if (name.equals("defaultMaxAge")) {
                    client.setDefaultMaxAge(reader.nextInt());
                } else if (name.equals("requireAuthTime")) {
                    client.setRequireAuthTime(reader.nextBoolean());
                } else if (name.equals("defaultACRValues")) {
                    Set<String> defaultACRvalues = readSet(reader);
                    client.setDefaultACRvalues(defaultACRvalues);
                } else if (name.equals("initiateLoginUri")) {
                    client.setInitiateLoginUri(reader.nextString());
                } else if (name.equals("postLogoutRedirectUri")) {
                    HashSet<String> postLogoutUris = Sets.newHashSet(reader.nextString());
                    client.setPostLogoutRedirectUris(postLogoutUris);
                } else if (name.equals("requestUris")) {
                    Set<String> requestUris = readSet(reader);
                    client.setRequestUris(requestUris);
                } else if (name.equals("description")) {
                    client.setClientDescription(reader.nextString());
                } else if (name.equals("allowIntrospection")) {
                    client.setAllowIntrospection(reader.nextBoolean());
                } else if (name.equals("reuseRefreshToken")) {
                    client.setReuseRefreshToken(reader.nextBoolean());
                } else if (name.equals("dynamicallyRegistered")) {
                    client.setDynamicallyRegistered(reader.nextBoolean());
                } else {
                    logger.debug("Found unexpected entry");
                    reader.skipValue();
                }
                break;
            default:
                logger.debug("Found unexpected entry");
                reader.skipValue();
                continue;
            }
        }
        reader.endObject();
        clientRepository.saveClient(client);
    }
    reader.endArray();
    logger.info("Done reading clients");
}

From source file:org.mitre.openid.connect.service.impl.MITREidDataService_1_0.java

License:Apache License

/**
 * Read the list of system scopes from the reader and insert them into the
 * scope repository./*from www . jav  a 2s .  c o  m*/
 *
 * @param reader
 * @throws IOException
 */
private void readSystemScopes(JsonReader reader) throws IOException {
    reader.beginArray();
    while (reader.hasNext()) {
        SystemScope scope = new SystemScope();
        reader.beginObject();
        while (reader.hasNext()) {
            switch (reader.peek()) {
            case END_OBJECT:
                continue;
            case NAME:
                String name = reader.nextName();
                if (reader.peek() == JsonToken.NULL) {
                    reader.skipValue();
                } else if (name.equals("value")) {
                    scope.setValue(reader.nextString());
                } else if (name.equals("description")) {
                    scope.setDescription(reader.nextString());
                } else if (name.equals("allowDynReg")) {
                    // previously "allowDynReg" scopes are now tagged as "not restricted" and vice versa
                    scope.setRestricted(!reader.nextBoolean());
                } else if (name.equals("defaultScope")) {
                    scope.setDefaultScope(reader.nextBoolean());
                } else if (name.equals("icon")) {
                    scope.setIcon(reader.nextString());
                } else {
                    logger.debug("found unexpected entry");
                    reader.skipValue();
                }
                break;
            default:
                logger.debug("Found unexpected entry");
                reader.skipValue();
                continue;
            }
        }
        reader.endObject();
        sysScopeRepository.save(scope);
    }
    reader.endArray();
    logger.info("Done reading system scopes");
}

From source file:org.mitre.openid.connect.service.impl.MITREidDataService_1_1.java

License:Apache License

@Override
public void importData(JsonReader reader) throws IOException {

    logger.info("Reading configuration for 1.1");

    // this *HAS* to start as an object
    reader.beginObject();/*w w  w . java 2s. c  om*/

    while (reader.hasNext()) {
        JsonToken tok = reader.peek();
        switch (tok) {
        case NAME:
            String name = reader.nextName();
            // find out which member it is
            if (name.equals(CLIENTS)) {
                readClients(reader);
            } else if (name.equals(GRANTS)) {
                readGrants(reader);
            } else if (name.equals(WHITELISTEDSITES)) {
                readWhitelistedSites(reader);
            } else if (name.equals(BLACKLISTEDSITES)) {
                readBlacklistedSites(reader);
            } else if (name.equals(AUTHENTICATIONHOLDERS)) {
                readAuthenticationHolders(reader);
            } else if (name.equals(ACCESSTOKENS)) {
                readAccessTokens(reader);
            } else if (name.equals(REFRESHTOKENS)) {
                readRefreshTokens(reader);
            } else if (name.equals(SYSTEMSCOPES)) {
                readSystemScopes(reader);
            } else {
                for (MITREidDataServiceExtension extension : extensions) {
                    if (extension.supportsVersion(THIS_VERSION)) {
                        if (extension.supportsVersion(THIS_VERSION)) {
                            extension.importExtensionData(name, reader);
                            break;
                        }
                    }
                }
                // unknown token, skip it
                reader.skipValue();
            }
            break;
        case END_OBJECT:
            // the object ended, we're done here
            reader.endObject();
            continue;
        default:
            logger.debug("Found unexpected entry");
            reader.skipValue();
            continue;
        }
    }
    fixObjectReferences();
    for (MITREidDataServiceExtension extension : extensions) {
        if (extension.supportsVersion(THIS_VERSION)) {
            extension.fixExtensionObjectReferences(maps);
            break;
        }
    }
    maps.clearAll();
}

From source file:org.mitre.openid.connect.service.impl.MITREidDataService_1_1.java

License:Apache License

/**
 * @param reader/* ww w.  ja  va 2 s.  co m*/
 * @throws IOException
 */
private void readAuthenticationHolders(JsonReader reader) throws IOException {
    reader.beginArray();
    while (reader.hasNext()) {
        AuthenticationHolderEntity ahe = new AuthenticationHolderEntity();
        reader.beginObject();
        Long currentId = null;
        while (reader.hasNext()) {
            switch (reader.peek()) {
            case END_OBJECT:
                continue;
            case NAME:
                String name = reader.nextName();
                if (reader.peek() == JsonToken.NULL) {
                    reader.skipValue();
                } else if (name.equals("id")) {
                    currentId = reader.nextLong();
                } else if (name.equals("ownerId")) {
                    //not needed
                    reader.skipValue();
                } else if (name.equals("authentication")) {
                    OAuth2Request clientAuthorization = null;
                    Authentication userAuthentication = null;
                    reader.beginObject();
                    while (reader.hasNext()) {
                        switch (reader.peek()) {
                        case END_OBJECT:
                            continue;
                        case NAME:
                            String subName = reader.nextName();
                            if (reader.peek() == JsonToken.NULL) {
                                reader.skipValue(); // skip null values
                            } else if (subName.equals("clientAuthorization")) {
                                clientAuthorization = readAuthorizationRequest(reader);
                            } else if (subName.equals("userAuthentication")) {
                                // skip binary encoded version
                                reader.skipValue();

                            } else if (subName.equals("savedUserAuthentication")) {
                                userAuthentication = readSavedUserAuthentication(reader);

                            } else {
                                logger.debug("Found unexpected entry");
                                reader.skipValue();
                            }
                            break;
                        default:
                            logger.debug("Found unexpected entry");
                            reader.skipValue();
                            continue;
                        }
                    }
                    reader.endObject();
                    OAuth2Authentication auth = new OAuth2Authentication(clientAuthorization,
                            userAuthentication);
                    ahe.setAuthentication(auth);
                } else {
                    logger.debug("Found unexpected entry");
                    reader.skipValue();
                }
                break;
            default:
                logger.debug("Found unexpected entry");
                reader.skipValue();
                continue;
            }
        }
        reader.endObject();
        Long newId = authHolderRepository.save(ahe).getId();
        maps.getAuthHolderOldToNewIdMap().put(currentId, newId);
        logger.debug("Read authentication holder {}", currentId);
    }
    reader.endArray();
    logger.info("Done reading authentication holders");
}