it.openprj.jValidator.spring.ExtraCheckCustomCodeValidator.java Source code

Java tutorial

Introduction

Here is the source code for it.openprj.jValidator.spring.ExtraCheckCustomCodeValidator.java

Source

/*
 jValidator is a Data Quality middleware for managing data streams and Open Data.
 jValidator is a cutting edge technology Data Quality Firewall, Data QualityMonitor and ETL tool.
 Copyright (C) 2010-2013 OpenPRJ srl
 All rights reserved
    
  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU Affero General Public License as
  published by the Free Software Foundation, either version 3 of the
  License, or (at your option) any later version.
    
  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 Affero General Public License for more details.
    
  You should have received a copy of the GNU Affero General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
    
    
 Go to jValidator site, at http://www.jvalidator.com, for further information.
 Installation, customization, training and support services are available with OpenPRJ srl
 Site: http://www.openprj.it
 Contact:  info@openprj.it
 */

package it.openprj.jValidator.spring;

import it.openprj.jValidator.eventtrigger.DynamicClassLoader;
import it.openprj.jValidator.jpa.ReadList;
import it.openprj.jValidator.jpa.dao.DaoSet;
import it.openprj.jValidator.utils.generic.I18n;
import it.openprj.jValidator.validation.CustomCodeValidator;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;

public class ExtraCheckCustomCodeValidator implements Controller, DaoSet {

    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String code = request.getParameter("value");
        String name = request.getParameter("name");
        String addReq = request.getParameter("addReq");

        //TODO: take the result message from "locale"
        String result = null;
        ObjectMapper mapper = new ObjectMapper();
        ServletOutputStream out = null;
        response.setContentType("application/json");
        out = response.getOutputStream();
        if (StringUtils.isEmpty(code) || StringUtils.isEmpty(name)) {
            result = I18n.getMessage("error.extracheck.invaliddata");
        } else {
            name = name.trim();
            if (addReq.equalsIgnoreCase("true")) {
                ReadList list = checksTypeDao.findCustomCodeByName(name);
                if (list != null && CollectionUtils.isNotEmpty(list.getResults())) {
                    result = I18n.getMessage("error.extracheck.name.alreadyexist");
                    out.write(mapper.writeValueAsBytes(result));
                    out.flush();
                    out.close();
                    return null;
                }
            }
            try {
                File sourceDir = new File(System.getProperty("java.io.tmpdir"), "jValidator/src");
                sourceDir.mkdirs();
                String classNamePack = name.replace('.', File.separatorChar);
                String srcFilePath = sourceDir + "" + File.separatorChar + classNamePack + ".java";
                File sourceFile = new File(srcFilePath);
                if (sourceFile.exists()) {
                    sourceFile.delete();
                }
                FileUtils.writeStringToFile(new File(srcFilePath), code);
                DynamicClassLoader dynacode = DynamicClassLoader.getInstance();
                dynacode.addSourceDir(sourceDir);
                CustomCodeValidator customCodeValidator = (CustomCodeValidator) dynacode
                        .newProxyInstance(CustomCodeValidator.class, name);
                boolean isValid = false;
                if (customCodeValidator != null) {
                    Class clazz = dynacode.getLoadedClass(name);
                    if (clazz != null) {
                        Class[] interfaces = clazz.getInterfaces();
                        if (ArrayUtils.isNotEmpty(interfaces)) {
                            for (Class clz : interfaces) {
                                if ((clz.getName().equalsIgnoreCase(
                                        "it.openprj.jValidator.utils.validation.SingleValidation"))) {
                                    isValid = true;
                                }
                            }
                        }
                    }
                }
                if (isValid) {
                    result = "Success";
                } else {
                    result = I18n.getMessage("error.extracheck.wrongimpl");
                }
            } catch (Exception e) {
                result = "Failed. Reason:" + e.getMessage();
            }
        }
        out.write(mapper.writeValueAsBytes(result));
        out.flush();
        out.close();
        return null;
    }
}