com.seajas.search.profiler.validator.FeedValidator.java Source code

Java tutorial

Introduction

Here is the source code for com.seajas.search.profiler.validator.FeedValidator.java

Source

/**
 * Copyright (C) 2013 Seajas, the Netherlands.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3, as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.seajas.search.profiler.validator;

import com.seajas.search.profiler.model.command.FeedCommand;
import com.seajas.search.profiler.service.profiler.ProfilerService;
import com.seajas.search.profiler.service.task.TaskService;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

/**
 * Feed validator.
 * 
 * @author Jasper van Veghel <jasper@seajas.com>
 */
@Component("feedValidator")
public class FeedValidator implements Validator {
    /**
     * Profiler service.
     */
    @Autowired
    private ProfilerService profilerService;

    /**
     * Task service.
     */
    @Autowired
    private TaskService taskService;

    /**
     * Determine whether this validator supports the command object.
     * 
     * @param klass
     * @return boolean
     */
    @Override
    public boolean supports(final Class<?> klass) {
        return klass.equals(FeedCommand.class);
    }

    /**
     * Validate the given command object.
     * 
     * @param command
     * @param errors
     */
    @Override
    public void validate(final Object command, final Errors errors) {
        FeedCommand feed = (FeedCommand) command;

        if (feed.getAction().equals("add") || feed.getAction().equals("edit")) {
            if (!StringUtils.hasText(feed.getName()))
                errors.rejectValue("name", "feeds.errors.no.name");

            if (!StringUtils.hasText(feed.getCollection()))
                errors.rejectValue("collection", "feeds.errors.no.collection");
            else if (!profilerService.getJobNames().containsKey(feed.getCollection()))
                errors.rejectValue("collection", "feeds.errors.invalid.collection");

            if (StringUtils.hasText(feed.getFeedEncodingOverride())
                    && !Charset.isSupported(feed.getFeedEncodingOverride()))
                errors.rejectValue("feedEncodingOverride", "feeds.errors.invalid.feed.encoding.override");
            if (StringUtils.hasText(feed.getResultEncodingOverride())
                    && !Charset.isSupported(feed.getResultEncodingOverride()))
                errors.rejectValue("resultEncodingOverride", "feeds.errors.invalid.result.encoding.override");

            if (StringUtils.hasText(feed.getLanguage())
                    && !profilerService.getAvailableSearchLanguages().contains(feed.getLanguage()))
                errors.rejectValue("language", "feeds.errors.invalid.language");

            for (String feedUrl : feed.getFeedUrls())
                try {
                    if (StringUtils.hasText(feedUrl))
                        new URI(feedUrl);
                } catch (URISyntaxException e) {
                    errors.rejectValue("feedUrls", "feeds.errors.invalid.feed.urls");

                    break;
                }

            if (StringUtils.hasText(feed.getQueue())
                    && !taskService.getFeedInjectionTriggers().containsKey(feed.getQueue()))
                errors.rejectValue("queue", "feeds.errors.invalid.queue");
            if (StringUtils.hasText(feed.getStrategy())
                    && !profilerService.getAuthenticationStrategies().containsKey(feed.getStrategy()))
                errors.rejectValue("strategy", "feeds.errors.invalid.strategy");

            if (feed.getFeedParameterKeys() != null && feed.getFeedParameterKeys().size() > 0) {
                if (feed.getFeedParameterValues() == null
                        || feed.getFeedParameterKeys().size() != feed.getFeedParameterValues().size())
                    errors.rejectValue("feedParameterKeys", "feeds.errors.invalid.feed.parameters");
                else {
                    for (String feedParameterKey : feed.getFeedParameterKeys())
                        if (!StringUtils.hasText(feedParameterKey))
                            errors.rejectValue("feedParameterKeys", "feeds.errors.invalid.feed.parameters");

                    for (String feedParameterValue : feed.getFeedParameterValues())
                        if (!StringUtils.hasText(feedParameterValue))
                            errors.rejectValue("feedParameterKeys", "feeds.errors.invalid.feed.parameters");
                }
            }
        }
    }
}