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

Java tutorial

Introduction

Here is the source code for com.seajas.search.profiler.validator.ModifierValidator.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 java.io.StringReader;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamSource;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import com.seajas.search.profiler.model.command.ModifierCommand;

/**
 * Modifier validator.
 * 
 * @author Jasper van Veghel <jasper@seajas.com>
 */
@Component("modifierValidator")
public class ModifierValidator implements Validator {
    /**
     * The logger.
     */
    private static final Logger logger = LoggerFactory.getLogger(ModifierValidator.class);

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

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

        if (modifier.getAction() != null
                && (modifier.getAction().equals("add") || modifier.getAction().equals("edit"))) {
            if (StringUtils.isEmpty(modifier.getUrlExpression()))
                errors.rejectValue("urlExpression", "modifiers.validator.error.url.expression.empty");
            else
                try {
                    Pattern.compile(modifier.getUrlExpression());
                } catch (PatternSyntaxException e) {
                    errors.rejectValue("urlExpression", "modifiers.validator.error.url.expression.invalid");
                }

            if (StringUtils.isEmpty(modifier.getUrlType()))
                errors.rejectValue("urlType", "modifiers.validator.error.url.type.empty");
        } else if (modifier.getFilterAction() != null && (modifier.getFilterAction().equals("add-filter")
                || modifier.getFilterAction().equals("edit-filter"))) {
            if (StringUtils.isEmpty(modifier.getFilterStart()))
                errors.rejectValue("filterStart", "modifiers.validator.error.filter.start.empty");
            else if (modifier.getFilterAsExpression())
                try {
                    Pattern.compile(modifier.getFilterStart());
                } catch (PatternSyntaxException e) {
                    errors.rejectValue("filterStart", "modifiers.validator.error.filter.start.invalid");
                }

            if (StringUtils.isEmpty(modifier.getFilterEnd()))
                errors.rejectValue("filterEnd", "modifiers.validator.error.filter.end.empty");
            else if (modifier.getFilterAsExpression())
                try {
                    Pattern.compile(modifier.getFilterEnd());
                } catch (PatternSyntaxException e) {
                    errors.rejectValue("filterEnd", "modifiers.validator.error.filter.end.invalid");
                }
        } else if (modifier.getScriptAction() != null && (modifier.getScriptAction().equals("add-script")
                || modifier.getScriptAction().equals("edit-script"))) {
            if (StringUtils.isEmpty(modifier.getScriptLanguage()))
                errors.rejectValue("scriptLanguage", "modifiers.validator.error.script.language.invalid");

            if (StringUtils.isEmpty(modifier.getScriptContent()))
                errors.rejectValue("scriptContent", "modifiers.validator.error.script.content.empty");
            else {
                if (modifier.getScriptLanguage().equalsIgnoreCase("xslt")) {
                    StreamSource source = new StreamSource(new StringReader(modifier.getScriptContent()));

                    try {
                        TransformerFactory.newInstance().newTransformer(source);
                    } catch (Exception e) {
                        errors.rejectValue("scriptContent",
                                "modifiers.validator.error.script.content.invalid.transformation");
                    }
                } else {
                    ScriptEngineManager engineManager = new ScriptEngineManager();
                    ScriptEngine engine = engineManager.getEngineByName(modifier.getScriptLanguage().toLowerCase());

                    engine.put("input", "");
                    engine.put("inputUrl", "");
                    engine.put("inputMaximumContentLength", 0L);

                    try {
                        engine.eval(modifier.getScriptContent());
                    } catch (ScriptException e) {
                        errors.rejectValue("scriptContent",
                                "modifiers.validator.error.script.content.syntax.error");

                        logger.warn("Script validation failed", e);
                    }
                }
            }
        }
    }
}