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

Java tutorial

Introduction

Here is the source code for org.onosproject.north.aaa.api.parser.impl.UserParser.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.User;

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

/**
 * Parse and build user class object from users.json file.
 */
public class UserParser implements JsonParserService<User> {
    private static final String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
            + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";

    @Override
    public Set<User> parseJson(InputStream stream) throws IOException, ParseException {
        // begin parsing JSON to Application class
        ObjectMapper mapper = new ObjectMapper();
        JsonNode jsonNode = mapper.readTree(stream);
        JsonParser jp = jsonNode.traverse();
        Set<User> userSet = 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) && "users".equals(jp.getCurrentName())) {
                token = jp.nextToken();
                if (!JsonToken.START_ARRAY.equals(token)) {
                    // bail out
                    throw new ParseException("expected ARRAY after users");
                }

                while (true) {
                    token = jp.nextToken();
                    if (JsonToken.END_ARRAY.equals(token)) {
                        // bail out
                        break;
                    }
                    if (JsonToken.START_OBJECT.equals(token)) {
                        User user = jsonToUser(jp);
                        userSet.add(user);
                    }
                }
            }
        }
        return userSet;
    }

    private User jsonToUser(JsonParser jp) throws ParseException, IOException {
        User.Builder builder = User.builder();

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

            if (JsonToken.FIELD_NAME.equals(token) && "username".equals(jp.getCurrentName())) {
                jp.nextToken();
                builder.buildUsername(jp.getText());
            } else if (JsonToken.FIELD_NAME.equals(token) && "password".equals(jp.getCurrentName())) {
                jp.nextToken();
                builder.buildPassword(jp.getText());
            } else if (JsonToken.FIELD_NAME.equals(token) && "roles".equals(jp.getCurrentName())) {
                jp.nextToken();
                String roles = jp.getText();
                if ("admin".equals(roles) || "user".equals(roles)) {
                    builder.buildRoles(roles);
                } else {
                    // bail out
                    throw new ParseException("roles must be set to either \"admin\" or \"user\"");
                }

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

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

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

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

            } 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) && "email".equals(jp.getCurrentName())) {
                jp.nextToken();
                String email = jp.getText();

                // verify email by email regex
                Pattern emailPattern = Pattern.compile(EMAIL_PATTERN);
                if (!emailPattern.matcher(email).matches()) {
                    // bail out
                    throw new ParseException("email is not valid");
                }
                builder.buildEmail(email);
            }
        }
        return builder.buildAll();
    }
}