Search in sources :

Example 86 with SystemException

use of com.github.bordertech.wcomponents.util.SystemException in project wcomponents by BorderTech.

the class VelocityEngineFactory method getVelocityEngine.

/**
 * <p>
 * Returns the VelocityEngine associated with this factory. If this is the first time we are using the engine,
 * create it and initialise it.</p>
 *
 * <p>
 * Note that velocity engines are hugely resource intensive, so we don't want too many of them. For the time being
 * we have a single instance stored as a static variable. This would only be a problem if the VelocityLayout class
 * ever wanted to use different engine configurations (unlikely).</p>
 *
 * @return the VelocityEngine associated with this factory.
 */
public static synchronized VelocityEngine getVelocityEngine() {
    if (engine == null) {
        String fileTemplates = ConfigurationProperties.getVelocityFileTemplates();
        boolean cacheTemplates = ConfigurationProperties.getVelocityCacheTemplates();
        VelocityEngine newEngine = new VelocityEngine();
        Properties props = new Properties();
        // "source mode" or not
        if (fileTemplates != null && !"".equals(fileTemplates)) {
            // Source mode
            LOG.info("Velocity engine running in source mode from " + fileTemplates);
            props.setProperty("resource.loader", "file,class");
            props.setProperty("file.resource.loader.path", fileTemplates);
            props.setProperty("file.resource.loader.cache", "false");
            props.setProperty("file.resource.loader.modificationCheckInterval", "2");
            props.setProperty("class.resource.loader.cache", "false");
            props.setProperty("class.resource.loader.modificationCheckInterval", "2");
            props.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
        } else {
            String cache = String.valueOf(cacheTemplates);
            props.setProperty("class.resource.loader.cache", cache);
            props.setProperty("resource.loader", "class");
            props.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
        }
        // Setup commons logging for velocity
        props.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, "com.github.bordertech.wcomponents.velocity.VelocityLogger");
        // Set up access to the common velocity macros.
        props.setProperty(RuntimeConstants.VM_LIBRARY, ConfigurationProperties.getVelocityMacroLibrary());
        try {
            if (LOG.isInfoEnabled()) {
                // Dump properties
                StringWriter writer = new StringWriter();
                props.list(new PrintWriter(writer));
                LOG.info("Configuring velocity with the following properties...\n" + writer);
            }
            newEngine.init(props);
        } catch (Exception ex) {
            throw new SystemException("Failed to configure VelocityEngine", ex);
        }
        engine = newEngine;
    }
    return engine;
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) StringWriter(java.io.StringWriter) SystemException(com.github.bordertech.wcomponents.util.SystemException) Properties(java.util.Properties) ConfigurationProperties(com.github.bordertech.wcomponents.util.ConfigurationProperties) SystemException(com.github.bordertech.wcomponents.util.SystemException) PrintWriter(java.io.PrintWriter)

Example 87 with SystemException

use of com.github.bordertech.wcomponents.util.SystemException in project wcomponents by BorderTech.

the class WPartialDateFieldRenderer_Test method testXssEscaping.

@Test
public void testXssEscaping() throws IOException, SAXException, XpathException {
    WPartialDateField dateField = new WPartialDateField();
    dateField.setData(getMaliciousContent());
    try {
        assertSafeContent(dateField);
        Assert.fail("Invalid date should not have been parsed.");
    } catch (SystemException e) {
        Assert.assertNotNull("Exception has no message", e.getMessage());
    }
    dateField.setData(null);
    dateField.setToolTip(getMaliciousAttribute("ui:datefield"));
    assertSafeContent(dateField);
    dateField.setAccessibleText(getMaliciousAttribute("ui:datefield"));
    assertSafeContent(dateField);
}
Also used : SystemException(com.github.bordertech.wcomponents.util.SystemException) WPartialDateField(com.github.bordertech.wcomponents.WPartialDateField) Test(org.junit.Test)

Example 88 with SystemException

use of com.github.bordertech.wcomponents.util.SystemException in project wcomponents by BorderTech.

the class AbstractWComponentTestCase method invokeSetMethod.

/**
 * @param component the component to invoke the setter method on
 * @param methodName the name of the method
 * @param value the value to pass into the setter method
 * @param args if required the variable args
 */
private void invokeSetMethod(final WComponent component, final String methodName, final Object value, final Object[] args) {
    try {
        if (args == null) {
            PropertyUtils.setProperty(component, methodName, value);
        } else {
            // Invoke specifying the variable arg type as propertyUtils cannot handle this
            String setter = "set" + methodName.substring(0, 1).toUpperCase() + methodName.substring(1);
            Class[] argTypes = new Class[] { value.getClass(), args.getClass() };
            Method method = component.getClass().getMethod(setter, argTypes);
            method.invoke(component, value, args);
        }
    } catch (Exception e) {
        throw new SystemException("Failed to set value on component for method " + methodName + " on " + component.getClass(), e);
    }
}
Also used : SystemException(com.github.bordertech.wcomponents.util.SystemException) Method(java.lang.reflect.Method) SystemException(com.github.bordertech.wcomponents.util.SystemException)

Example 89 with SystemException

use of com.github.bordertech.wcomponents.util.SystemException in project wcomponents by BorderTech.

the class Serialization_Test method pipe.

/**
 * Take a copy of an input object via serialization.
 *
 * @param obj the object to copy
 *
 * @return the copy of the object
 */
private static Object pipe(final Object obj) {
    try {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(obj);
        oos.close();
        byte[] bytes = bos.toByteArray();
        FileOutputStream fos = new FileOutputStream("SerializeText.txt");
        fos.write(bytes);
        fos.flush();
        fos.close();
        ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
        ObjectInputStream ois = new ObjectInputStream(bis);
        Object out = ois.readObject();
        return out;
    } catch (Exception ex) {
        throw new SystemException("Failed to pipe " + obj, ex);
    }
}
Also used : SystemException(com.github.bordertech.wcomponents.util.SystemException) ByteArrayInputStream(java.io.ByteArrayInputStream) FileOutputStream(java.io.FileOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) SystemException(com.github.bordertech.wcomponents.util.SystemException) ObjectInputStream(java.io.ObjectInputStream)

Example 90 with SystemException

use of com.github.bordertech.wcomponents.util.SystemException in project wcomponents by BorderTech.

the class AbstractContainerHelper_Test method testHandleErrorDefault.

@Test
public void testHandleErrorDefault() throws IOException {
    MyContainerHelper helper = new MyContainerHelper();
    SystemException error = new SystemException("test");
    helper.handleError(error);
    String output = helper.stringWriter.toString();
    Assert.assertTrue("Should contain default error message", output.contains("currently unavailable"));
}
Also used : SystemException(com.github.bordertech.wcomponents.util.SystemException) Test(org.junit.Test)

Aggregations

SystemException (com.github.bordertech.wcomponents.util.SystemException)91 XmlStringBuilder (com.github.bordertech.wcomponents.XmlStringBuilder)17 WComponent (com.github.bordertech.wcomponents.WComponent)15 UIContext (com.github.bordertech.wcomponents.UIContext)14 ComponentWithContext (com.github.bordertech.wcomponents.ComponentWithContext)10 Test (org.junit.Test)9 WebElement (org.openqa.selenium.WebElement)8 WebXmlRenderContext (com.github.bordertech.wcomponents.servlet.WebXmlRenderContext)6 IOException (java.io.IOException)5 Select (org.openqa.selenium.support.ui.Select)5 Environment (com.github.bordertech.wcomponents.Environment)4 PrintWriter (java.io.PrintWriter)4 ArrayList (java.util.ArrayList)4 ActionEscape (com.github.bordertech.wcomponents.ActionEscape)3 Date (java.util.Date)3 List (java.util.List)3 ResourceNotFoundException (org.apache.velocity.exception.ResourceNotFoundException)3 AjaxOperation (com.github.bordertech.wcomponents.AjaxOperation)2 DefaultWComponent (com.github.bordertech.wcomponents.DefaultWComponent)2 OptionGroup (com.github.bordertech.wcomponents.OptionGroup)2