org.onosproject.north.aaa.api.parser.impl.ClientParser.java Source code

Java tutorial

Introduction

Here is the source code for org.onosproject.north.aaa.api.parser.impl.ClientParser.java

Source

/*
 * Copyright 2014-2016 Open Networking Laboratory
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.onosproject.north.aaa.api.parser.impl;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.onosproject.north.aaa.api.JsonParserService;
import org.onosproject.north.aaa.api.ParseException;
import org.onosproject.north.aaa.store.ClientCredential;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Pattern;

/**
 * Parse and build client credential class object from clients.json file.
 */
public class ClientParser implements JsonParserService<ClientCredential> {
    private static final String URI_PATTERN = "^(https?|ftp|file)://[-a-zA-Z0-9+&@#/"
            + "%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]";

    @Override
    public Set<ClientCredential> parseJson(InputStream stream) throws IOException, ParseException {
        ObjectMapper mapper = new ObjectMapper();
        JsonNode jsonNode = mapper.readTree(stream);
        JsonParser jp = jsonNode.traverse();
        Set<ClientCredential> clientSet = new HashSet<>();

        // continue parsing the token till the end of input is reached
        while (!jp.isClosed()) {
            // get the token
            JsonToken token = jp.nextToken();
            // if its the last token then we are done
            if (token == null) {
                break;
            }

            if (JsonToken.FIELD_NAME.equals(token) && "clients".equals(jp.getCurrentName())) {
                token = jp.nextToken();
                if (!JsonToken.START_ARRAY.equals(token)) {
                    // bail out
                    throw new ParseException("expected ARRAY after clients");
                }

                while (true) {
                    token = jp.nextToken();
                    if (JsonToken.END_ARRAY.equals(token)) {
                        // bail out
                        break;
                    }

                    if (JsonToken.START_OBJECT.equals(token)) {
                        ClientCredential client = jsonToClientCredential(jp);
                        clientSet.add(client);
                    }
                }
            }
        }
        return clientSet;
    }

    public ClientCredential jsonToClientCredential(JsonParser jp) throws ParseException, IOException {
        ClientCredential.Builder builder = ClientCredential.builder();

        while (true) {
            JsonToken token = jp.nextToken();
            if (JsonToken.END_OBJECT.equals(token)) {
                // bail out
                break;
            }

            if (JsonToken.FIELD_NAME.equals(token) && "appId".equals(jp.getCurrentName())) {
                jp.nextToken();
                builder.buildAppId(jp.getText());
            } else if (JsonToken.FIELD_NAME.equals(token) && "grants".equals(jp.getCurrentName())) {
                token = jp.nextToken();
                if (!JsonToken.START_ARRAY.equals(token)) {
                    // bail out
                    throw new ParseException("expected ARRAY after grants");
                }
                Set<String> grants = new HashSet<>();

                while (true) {
                    token = jp.nextToken();
                    if (JsonToken.END_ARRAY.equals(token)) {
                        // bail out
                        break;
                    }
                    grants.add(jp.getText());
                }
                builder.buildGrants(grants);

            } else if (JsonToken.FIELD_NAME.equals(token) && "scopes".equals(jp.getCurrentName())) {
                token = jp.nextToken();
                if (!JsonToken.START_ARRAY.equals(token)) {
                    // bail out
                    throw new ParseException("expected ARRAY after scopes");
                }
                Set<String> scopes = new HashSet<>();

                while (true) {
                    token = jp.nextToken();
                    if (JsonToken.END_ARRAY.equals(token)) {
                        // bail out
                        break;
                    }
                    scopes.add(jp.getText());
                }
                builder.buildScopes(scopes);

            } else if (JsonToken.FIELD_NAME.equals(token) && "redirectUri".equals(jp.getCurrentName())) {
                jp.nextToken();
                String uri = jp.getText();

                // verify uri by uri regex
                Pattern emailPattern = Pattern.compile(URI_PATTERN);
                if (!emailPattern.matcher(uri).matches()) {
                    // bail out
                    throw new ParseException("uri is not valid");
                }
                builder.buildRedirectUri(uri);
            } else if (JsonToken.FIELD_NAME.equals(token) && "clientId".equals(jp.getCurrentName())) {
                jp.nextToken();
                builder.buildClientId(jp.getText());
            } else if (JsonToken.FIELD_NAME.equals(token) && "clientSecret".equals(jp.getCurrentName())) {
                jp.nextToken();
                builder.buildClientSecret(jp.getText());
            }
        }
        return builder.buildAll();
    }
}