Search in sources :

Example 1 with ConfigException

use of org.jaffa.applications.jaffa.modules.admin.exceptions.ConfigException in project jaffa-framework by jaffa-projects.

the class ValidationRulesEditorComponent method performSave.

/**
 * This will save the contents of the current file.
 * @throws ApplicationExceptions if any error occurs while writing the file.
 * @throws FrameworkException if any error occurs.
 */
void performSave() throws FrameworkException, ApplicationExceptions {
    BufferedWriter writer = null;
    try {
        if (m_fileUpdateable) {
            URL url = URLHelper.newExtendedURL(m_validationRulesFile);
            File f = new File(url.getPath());
            writer = new BufferedWriter(new FileWriter(f, false));
            writer.write(getFileContents());
            writer.flush();
            if (log.isDebugEnabled())
                log.debug("Saved contents to the file " + m_validationRulesFile);
            // Clear cache so new rules are loaded.
            RulesMetaDataService.clearCache();
        } else {
            if (log.isDebugEnabled())
                log.debug("File cannot be updated since it cannot be read using File I/O. It is probably part of a JAR: " + m_validationRulesFile);
        }
    } catch (MalformedURLException e) {
        ApplicationExceptions appExps = new ApplicationExceptions();
        appExps.add(new ConfigException(ConfigException.PROP_FILEREAD_ERROR, StringHelper.convertToHTML(e.getMessage())));
        throw appExps;
    } catch (IOException e) {
        ApplicationExceptions appExps = new ApplicationExceptions();
        appExps.add(new ConfigException(ConfigException.PROP_FILEREAD_ERROR, StringHelper.convertToHTML(e.getMessage())));
        throw appExps;
    } finally {
        if (writer != null) {
            try {
                writer.close();
            } catch (IOException e) {
                String str = "Exception thrown while closing the Writer Stream";
                log.error(str, e);
                ApplicationExceptions appExps = new ApplicationExceptions();
                appExps.add(new ConfigException(ConfigException.PROP_FILEREAD_ERROR, StringHelper.convertToHTML(e.getMessage())));
                throw appExps;
            }
        }
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) ApplicationExceptions(org.jaffa.exceptions.ApplicationExceptions) ConfigException(org.jaffa.applications.jaffa.modules.admin.exceptions.ConfigException) URL(java.net.URL)

Example 2 with ConfigException

use of org.jaffa.applications.jaffa.modules.admin.exceptions.ConfigException in project jaffa-framework by jaffa-projects.

the class ValidationRulesEditorComponent method loadFileContents.

/**
 * Load a file from a URL, which could be an Extended URL
 * @throws ApplicationExceptions if any error occurs while reading the file.
 * @throws FrameworkException if any error occurs.
 */
void loadFileContents() throws FrameworkException, ApplicationExceptions {
    Reader reader = null;
    try {
        URL url = URLHelper.newExtendedURL(m_validationRulesFile);
        File f = new File(url.getPath());
        m_fileUpdateable = f.exists() && f.isFile();
        reader = new BufferedReader(new InputStreamReader(url.openStream()));
        StringBuffer buf = new StringBuffer();
        int i;
        while ((i = reader.read()) != -1) buf.append((char) i);
        m_fileContents = buf.toString();
        if (log.isDebugEnabled())
            log.debug("Obtained contents from the file " + m_validationRulesFile);
        // remove the EditBoxModel from the WidgetCache
        getUserSession().getWidgetCache(getComponentId()).removeModel("fileContents");
    } catch (MalformedURLException e) {
        ApplicationExceptions appExps = new ApplicationExceptions();
        appExps.add(new ConfigException(ConfigException.PROP_FILEREAD_ERROR, StringHelper.convertToHTML(e.getMessage())));
        throw appExps;
    } catch (IOException e) {
        ApplicationExceptions appExps = new ApplicationExceptions();
        appExps.add(new ConfigException(ConfigException.PROP_FILEREAD_ERROR, StringHelper.convertToHTML(e.getMessage())));
        throw appExps;
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                String str = "Exception thrown while closing the Reader Stream";
                log.error(str, e);
                ApplicationExceptions appExps = new ApplicationExceptions();
                appExps.add(new ConfigException(ConfigException.PROP_FILEREAD_ERROR, StringHelper.convertToHTML(e.getMessage())));
                throw appExps;
            }
        }
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) ApplicationExceptions(org.jaffa.exceptions.ApplicationExceptions) ConfigException(org.jaffa.applications.jaffa.modules.admin.exceptions.ConfigException) URL(java.net.URL)

Example 3 with ConfigException

use of org.jaffa.applications.jaffa.modules.admin.exceptions.ConfigException in project jaffa-framework by jaffa-projects.

the class ValidationRulesEditorForm method doValidate1.

/**
 * This method should be invoked to ensure a valid state of the FormBean. It will validate the data in the models and set the corresponding properties.
 * Errors will be raised in the FormBean, if any validation fails.
 * @param request The request stream
 * @return A true indicates validations went through successfully.
 */
public boolean doValidate1(HttpServletRequest request) throws FrameworkException, ApplicationExceptions {
    String value = null;
    ApplicationExceptions appExps = new ApplicationExceptions();
    value = getFileContentsWM().getValue();
    if (value != null && value.trim().length() == 0)
        value = null;
    try {
        // Create a factory object for creating DOM parsers
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        // Specifies that the parser produced by this factory will validate documents as they are parsed.
        factory.setValidating(true);
        // Now use the factory to create a DOM parser
        DocumentBuilder parser = factory.newDocumentBuilder();
        // Specifies the EntityResolver onceo resolve DTD used in XML documents
        parser.setEntityResolver(new DefaultEntityResolver());
        // Specifies the ErrorHandler to handle warning/error/fatalError conditions
        parser.setErrorHandler(new DefaultErrorHandler());
        Document document = parser.parse(new InputSource(new StringReader(value)));
    } catch (ParserConfigurationException e) {
        // Cannot pass e.toString() and pass as parameter as the the meesage contains quotes
        // which dows not work properly with displaying the messages
        appExps.add(new ConfigException(ConfigException.PROP_XML_FILE_PARSE_ERROR, StringHelper.convertToHTML(e.getMessage())));
        throw appExps;
    } catch (SAXException e) {
        appExps.add(new ConfigException(ConfigException.PROP_XML_FILE_PARSE_ERROR, StringHelper.convertToHTML(e.getMessage())));
        throw appExps;
    } catch (IOException e) {
        appExps.add(new ConfigException(ConfigException.PROP_XML_FILE_PARSE_ERROR, StringHelper.convertToHTML(e.getMessage())));
        throw appExps;
    }
    setFileContents(value);
    return true;
}
Also used : InputSource(org.xml.sax.InputSource) ApplicationExceptions(org.jaffa.exceptions.ApplicationExceptions) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) ConfigException(org.jaffa.applications.jaffa.modules.admin.exceptions.ConfigException) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) DefaultEntityResolver(org.jaffa.util.DefaultEntityResolver) DocumentBuilder(javax.xml.parsers.DocumentBuilder) StringReader(java.io.StringReader) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) DefaultErrorHandler(org.jaffa.util.DefaultErrorHandler)

Aggregations

ConfigException (org.jaffa.applications.jaffa.modules.admin.exceptions.ConfigException)3 ApplicationExceptions (org.jaffa.exceptions.ApplicationExceptions)3 MalformedURLException (java.net.MalformedURLException)2 URL (java.net.URL)2 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 DefaultEntityResolver (org.jaffa.util.DefaultEntityResolver)1 DefaultErrorHandler (org.jaffa.util.DefaultErrorHandler)1 Document (org.w3c.dom.Document)1 InputSource (org.xml.sax.InputSource)1 SAXException (org.xml.sax.SAXException)1