org.aperte.lp.movies.OpeningConfigValidator.java Source code

Java tutorial

Introduction

Here is the source code for org.aperte.lp.movies.OpeningConfigValidator.java

Source

/**
 * Copyright 2013 Jeremy Handcock
 *
 * 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.aperte.lp.movies;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.aperte.lp.ConfigValidator;
import org.aperte.lp.ParseException;
import org.aperte.lp.model.ValidateConfigResponse;

import java.io.IOException;
import java.util.Collections;
import java.util.Set;

/**
 * Validates the configuration supplied by users for the opening movies
 * publication.
 * <p/>
 * See http://remote.bergcloud.com/developers/reference/validate_config
 */
public class OpeningConfigValidator implements ConfigValidator {

    /**
     * The error to display if the user does not select a country.
     */
    static final String SELECT_COUNTRY_ERROR = "Please select a country.";

    /**
     * The set of country codes are available to select.
     */
    private final Set<String> countries;

    /**
     * Creates a new configuration validator for the opening movies publication.
     *
     * @param countries the set of valid country codes.
     */
    public OpeningConfigValidator(Set<String> countries) {
        if (countries == null) {
            throw new IllegalArgumentException("Countries cannot be null.");
        }
        this.countries = countries;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ValidateConfigResponse validateConfig(String configJson) throws ParseException {
        if (configJson == null || configJson.isEmpty()) {
            return error(SELECT_COUNTRY_ERROR);
        }

        // Parse the configuration json
        ObjectMapper mapper = new ObjectMapper();
        JsonNode root = null;
        try {
            root = mapper.readTree(configJson);
        } catch (JsonProcessingException e) {
            throw new ParseException("Unable to parse JSON: " + configJson, e);
        } catch (IOException e) {
            throw new ParseException("Unable to parse JSON: " + configJson, e);
        }
        if (root == null) {
            return error(SELECT_COUNTRY_ERROR);
        }

        // Validate the country
        JsonNode countryNode = root.get("country");
        if (countryNode == null) {
            return error(SELECT_COUNTRY_ERROR);
        }
        if (!countryNode.isTextual()) {
            return error(SELECT_COUNTRY_ERROR);
        }
        String country = countryNode.textValue();
        if (country.isEmpty()) {
            return error(SELECT_COUNTRY_ERROR);
        }
        if (!countries.contains(country)) {
            return error(SELECT_COUNTRY_ERROR);
        }

        ValidateConfigResponse response = new ValidateConfigResponse();
        response.setValid(true);
        return response;
    }

    /**
     * Constructs a validation error response.
     *
     * @param msg The error message to display to the user.
     * @return the validation response
     */
    private ValidateConfigResponse error(String msg) {
        ValidateConfigResponse response = new ValidateConfigResponse();
        response.setValid(false);
        response.setErrors(Collections.singletonList(msg));

        return response;
    }

}