Search in sources :

Example 66 with SystemException

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

the class TransformXMLInterceptor method initTemplates.

/**
 * Statically initialize the XSLT templates that are cached for all future transforms.
 *
 * @return the XSLT Templates.
 */
private static Templates initTemplates() {
    try {
        URL xsltURL = ThemeUtil.class.getResource(RESOURCE_NAME);
        if (xsltURL != null) {
            Source xsltSource = new StreamSource(xsltURL.openStream(), xsltURL.toExternalForm());
            TransformerFactory factory = new net.sf.saxon.TransformerFactoryImpl();
            Templates templates = factory.newTemplates(xsltSource);
            LOG.debug("Generated XSLT templates for: " + RESOURCE_NAME);
            return templates;
        } else {
            // Server-side XSLT enabled but theme resource not on classpath.
            throw new IllegalStateException(RESOURCE_NAME + " not on classpath");
        }
    } catch (IOException | TransformerConfigurationException ex) {
        throw new SystemException("Could not create transformer for " + RESOURCE_NAME, ex);
    }
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) SystemException(com.github.bordertech.wcomponents.util.SystemException) StreamSource(javax.xml.transform.stream.StreamSource) Templates(javax.xml.transform.Templates) IOException(java.io.IOException) URL(java.net.URL) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source)

Example 67 with SystemException

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

the class WWindowInterceptor method serviceRequest.

/**
 * Temporarily replaces the environment while the request is being handled.
 *
 * @param request the request being responded to.
 */
@Override
public void serviceRequest(final Request request) {
    // Get window id off the request
    windowId = request.getParameter(WWindow.WWINDOW_REQUEST_PARAM_KEY);
    if (windowId == null) {
        super.serviceRequest(request);
    } else {
        // Get the window component
        ComponentWithContext target = WebUtilities.getComponentById(windowId, true);
        if (target == null) {
            throw new SystemException("No window component for id " + windowId);
        }
        // Setup the Environment on the context
        UIContext uic = UIContextHolder.getCurrentPrimaryUIContext();
        Environment originalEnvironment = uic.getEnvironment();
        uic.setEnvironment(new EnvironmentDelegate(originalEnvironment, windowId, target));
        if (attachWindow) {
            attachUI(target.getComponent());
        }
        UIContextHolder.pushContext(target.getContext());
        try {
            super.serviceRequest(request);
        } finally {
            uic.setEnvironment(originalEnvironment);
            UIContextHolder.popContext();
        }
    }
}
Also used : SystemException(com.github.bordertech.wcomponents.util.SystemException) UIContext(com.github.bordertech.wcomponents.UIContext) Environment(com.github.bordertech.wcomponents.Environment) ComponentWithContext(com.github.bordertech.wcomponents.ComponentWithContext)

Example 68 with SystemException

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

the class WrongStepAjaxInterceptor method serviceRequest.

/**
 * Override to check whether the step variable in the incoming request matches what we expect.
 *
 * @param request the request being serviced.
 */
@Override
public void serviceRequest(final Request request) {
    // Get trigger id
    triggerId = request.getParameter(WServlet.AJAX_TRIGGER_PARAM_NAME);
    if (triggerId == null) {
        throw new SystemException("No AJAX trigger id to check step count");
    }
    // Get trigger and its context
    ComponentWithContext trigger = AjaxHelper.getCurrentTriggerAndContext();
    if (trigger == null) {
        throw new IllegalStateException("No component/context available for AJAX trigger " + triggerId + ".");
    }
    // Get expected step count
    UIContext uic = UIContextHolder.getCurrent();
    int expected = uic.getEnvironment().getStep();
    // Step should already be set on the session
    if (expected == 0) {
        throw new SystemException("Step count should already be set on the session before AJAX request.");
    }
    // Get step count on the request
    int got = StepCountUtil.getRequestStep(request);
    // Check we are on the current step
    if (expected == got) {
        // Process Service Request
        getBackingComponent().serviceRequest(request);
    } else {
        // Invalid step
        LOG.warn("AJAX: Wrong step detected. Expected step " + expected + " but got step " + got);
        // "GET" Ajax requests are just ignored and return an error code
        if ("GET".equals(request.getMethod())) {
            LOG.warn("Error code will be sent in the response for AJAX GET Request.");
            handleErrorCode();
            // Make sure the render phase is not processed
            throw new ActionEscape();
        } else if (StepCountUtil.isErrorRedirect()) {
            // Redirect to error page
            LOG.warn("User will be redirected to an error page.");
            redirectUrl = StepCountUtil.getErrorUrl();
        } else {
            // Warp to the future by refreshing the page
            LOG.warn("Warp the user back to the future by refreshing the page.");
            handleWarpToTheFuture(uic);
            redirectUrl = buildApplicationUrl(uic);
        }
    }
}
Also used : ActionEscape(com.github.bordertech.wcomponents.ActionEscape) SystemException(com.github.bordertech.wcomponents.util.SystemException) UIContext(com.github.bordertech.wcomponents.UIContext) ComponentWithContext(com.github.bordertech.wcomponents.ComponentWithContext)

Example 69 with SystemException

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

the class WrongStepContentInterceptor method serviceRequest.

/**
 * Override to check whether the step variable in the incoming request matches what we expect.
 *
 * @param request the request being serviced.
 */
@Override
public void serviceRequest(final Request request) {
    // Get expected step count
    UIContext uic = UIContextHolder.getCurrent();
    int expected = uic.getEnvironment().getStep();
    // Step should already be set on the session
    if (expected == 0) {
        throw new SystemException("Step count should already be set on the session before content request.");
    }
    // Get step count on the request
    int got = StepCountUtil.getRequestStep(request);
    // Check tokens match (both must be provided)
    if (expected == got) {
        // Process Service Request
        getBackingComponent().serviceRequest(request);
    } else if (!StepCountUtil.isStepOnRequest(request) && StepCountUtil.isCachedContentRequest(request)) {
        // cached content (no step on request)
        // Process Service Request
        getBackingComponent().serviceRequest(request);
    } else {
        // Invalid token
        // Set an error code
        LOG.warn("Wrong step detected for content request. Expected step [" + expected + "] but got step [" + got + "].");
        handleError();
    }
}
Also used : SystemException(com.github.bordertech.wcomponents.util.SystemException) UIContext(com.github.bordertech.wcomponents.UIContext)

Example 70 with SystemException

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

the class UIRegistryAmicableImpl method loadUI.

/**
 * Attempts to load a UI using the key as a class name.
 *
 * @param key The registration key.
 * @return A WComponent if one could be loaded from the classpath, else an ErrorPage WComponent containing the
 * problem.
 */
private static WComponent loadUI(final String key) {
    String classname = key.trim();
    try {
        Class<?> clas = Class.forName(classname);
        if (WComponent.class.isAssignableFrom(clas)) {
            WComponent instance = (WComponent) clas.newInstance();
            LOG.debug("WComponent successfully loaded with class name \"" + classname + "\".");
            return instance;
        } else {
            throw new SystemException("The resource with the name \"" + classname + "\" is not a WComponent.");
        }
    } catch (Exception ex) {
        LOG.error("Unable to load a WComponent using the resource name \"" + classname + "\"", ex);
        // Are we in developer friendly error mode?
        boolean friendly = ConfigurationProperties.getDeveloperErrorHandling();
        FatalErrorPageFactory factory = Factory.newInstance(FatalErrorPageFactory.class);
        WComponent errorPage = factory.createErrorPage(friendly, ex);
        return errorPage;
    }
}
Also used : WComponent(com.github.bordertech.wcomponents.WComponent) SystemException(com.github.bordertech.wcomponents.util.SystemException) FatalErrorPageFactory(com.github.bordertech.wcomponents.FatalErrorPageFactory) SystemException(com.github.bordertech.wcomponents.util.SystemException)

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