Search in sources :

Example 1 with SystemException

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

the class AbstractContainerHelper method render.

/**
 * Support standard processing of the render phase of a request.
 *
 * @throws IOException IO Exception
 */
public void render() throws IOException {
    if (isDisposed()) {
        LOG.debug("Skipping render phase.");
        return;
    }
    try {
        // Check user context has been prepared
        if (getNewConversation() == null) {
            throw new IllegalStateException("User context has not been prepared before the render phase");
        }
        prepareRender();
        UIContext uic = getUIContext();
        if (uic == null) {
            throw new IllegalStateException("No user context set for the render phase.");
        }
        UIContextHolder.pushContext(uic);
        prepareRequest();
        // Handle errors from the action phase now.
        if (havePropogatedError()) {
            handleError(getPropogatedError());
            return;
        }
        WComponent uiComponent = getUI();
        if (uiComponent == null) {
            throw new SystemException("No UI Component exists.");
        }
        Environment environment = uiComponent.getEnvironment();
        if (environment == null) {
            throw new SystemException("No WEnvironment exists.");
        }
        getInterceptor().attachResponse(getResponse());
        getInterceptor().preparePaint(getRequest());
        String contentType = getUI().getHeaders().getContentType();
        Response response = getResponse();
        response.setContentType(contentType);
        addGenericHeaders(uic, getUI());
        PrintWriter writer = getPrintWriter();
        getInterceptor().paint(new WebXmlRenderContext(writer, uic.getLocale()));
        // The following only matters for a Portal context
        String title = uiComponent instanceof WApplication ? ((WApplication) uiComponent).getTitle() : null;
        if (title != null) {
            setTitle(title);
        }
    } catch (Escape esc) {
        LOG.debug("Escape performed during render phase.");
        handleEscape(esc);
    } catch (Throwable t) {
        // We try not to let any exception propagate to container.
        String message = "Caught exception during render phase.";
        LOG.error(message, t);
        handleError(t);
    } finally {
        UIContextHolder.reset();
        dispose();
    }
}
Also used : WComponent(com.github.bordertech.wcomponents.WComponent) Response(com.github.bordertech.wcomponents.Response) WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) Escape(com.github.bordertech.wcomponents.Escape) ActionEscape(com.github.bordertech.wcomponents.ActionEscape) SystemException(com.github.bordertech.wcomponents.util.SystemException) UIContext(com.github.bordertech.wcomponents.UIContext) WApplication(com.github.bordertech.wcomponents.WApplication) Environment(com.github.bordertech.wcomponents.Environment) PrintWriter(java.io.PrintWriter)

Example 2 with SystemException

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

the class InternalResourceMap method computeHash.

/**
 * Computes a simple hash of the resource contents.
 *
 * @param resource the resource to hash.
 * @return a hash of the resource contents.
 */
public static String computeHash(final InternalResource resource) {
    final int bufferSize = 1024;
    try (InputStream stream = resource.getStream()) {
        if (stream == null) {
            return null;
        }
        // Compute CRC-32 checksum
        // TODO: Is a 1 in 2^32 chance of a cache bust fail good enough?
        // Checksum checksumEngine = new Adler32();
        Checksum checksumEngine = new CRC32();
        byte[] buf = new byte[bufferSize];
        for (int read = stream.read(buf); read != -1; read = stream.read(buf)) {
            checksumEngine.update(buf, 0, read);
        }
        return Long.toHexString(checksumEngine.getValue());
    } catch (Exception e) {
        throw new SystemException("Error calculating resource hash", e);
    }
}
Also used : SystemException(com.github.bordertech.wcomponents.util.SystemException) CRC32(java.util.zip.CRC32) InputStream(java.io.InputStream) Checksum(java.util.zip.Checksum) SystemException(com.github.bordertech.wcomponents.util.SystemException)

Example 3 with SystemException

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

the class TargetableInterceptor method paint.

@Override
public void paint(final RenderContext renderContext) {
    // Should still be visible
    ComponentWithContext target = WebUtilities.getComponentById(targetId, true);
    if (target == null) {
        throw new SystemException("No target component found for id " + targetId);
    }
    UIContextHolder.pushContext(target.getContext());
    try {
        super.paint(renderContext);
    } finally {
        UIContextHolder.popContext();
    }
}
Also used : SystemException(com.github.bordertech.wcomponents.util.SystemException) ComponentWithContext(com.github.bordertech.wcomponents.ComponentWithContext)

Example 4 with SystemException

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

the class VelocityInterceptor method paint.

/**
 * Renders the component using the velocity template which has been provided.
 *
 * @param renderContext the context for rendering.
 */
@Override
public void paint(final RenderContext renderContext) {
    if (!(renderContext instanceof WebXmlRenderContext)) {
        throw new SystemException("Unable to render to " + renderContext);
    }
    PrintWriter writer = ((WebXmlRenderContext) renderContext).getWriter();
    Template template = null;
    try {
        template = VelocityEngineFactory.getVelocityEngine().getTemplate(templateUrl);
    } catch (Exception ex) {
        String message = "Could not open velocity template \"" + templateUrl + "\" for \"" + this.getClass().getName() + "\"";
        LOG.error(message, ex);
        writer.println(message);
        return;
    }
    try {
        VelocityContext context = new VelocityContext();
        fillContext(context);
        template.merge(context, writer);
    } catch (ResourceNotFoundException rnfe) {
        LOG.error("Could not find template " + templateUrl, rnfe);
    } catch (ParseErrorException pee) {
        // syntax error : problem parsing the template
        LOG.error("Parse problems", pee);
    } catch (MethodInvocationException mie) {
        // something invoked in the template
        // threw an exception
        Throwable wrapped = mie.getWrappedThrowable();
        LOG.error("Problems with velocity", mie);
        if (wrapped != null) {
            LOG.error("Wrapped exception...", wrapped);
        }
    } catch (Exception e) {
        LOG.error("Problems with velocity", e);
    }
}
Also used : WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) SystemException(com.github.bordertech.wcomponents.util.SystemException) VelocityContext(org.apache.velocity.VelocityContext) ParseErrorException(org.apache.velocity.exception.ParseErrorException) MethodInvocationException(org.apache.velocity.exception.MethodInvocationException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) ParseErrorException(org.apache.velocity.exception.ParseErrorException) SystemException(com.github.bordertech.wcomponents.util.SystemException) MethodInvocationException(org.apache.velocity.exception.MethodInvocationException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) PrintWriter(java.io.PrintWriter) Template(org.apache.velocity.Template)

Example 5 with SystemException

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

the class WWindowInterceptor method preparePaint.

/**
 * Temporarily replaces the environment while the UI prepares to render.
 *
 * @param request the request being responded to.
 */
@Override
public void preparePaint(final Request request) {
    if (windowId == null) {
        super.preparePaint(request);
    } else {
        // Get the window component
        ComponentWithContext target = WebUtilities.getComponentById(windowId, true);
        if (target == null) {
            throw new SystemException("No window component for id " + windowId);
        }
        UIContext uic = UIContextHolder.getCurrentPrimaryUIContext();
        Environment originalEnvironment = uic.getEnvironment();
        uic.setEnvironment(new EnvironmentDelegate(originalEnvironment, windowId, target));
        UIContextHolder.pushContext(target.getContext());
        try {
            super.preparePaint(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)

Aggregations

SystemException (com.github.bordertech.wcomponents.util.SystemException)94 XmlStringBuilder (com.github.bordertech.wcomponents.XmlStringBuilder)17 WComponent (com.github.bordertech.wcomponents.WComponent)15 UIContext (com.github.bordertech.wcomponents.UIContext)13 Test (org.junit.Test)12 ComponentWithContext (com.github.bordertech.wcomponents.ComponentWithContext)10 WebElement (org.openqa.selenium.WebElement)9 WebXmlRenderContext (com.github.bordertech.wcomponents.servlet.WebXmlRenderContext)6 IOException (java.io.IOException)5 Select (org.openqa.selenium.support.ui.Select)5 ErrorCodeEscape (com.github.bordertech.wcomponents.ErrorCodeEscape)4 Request (com.github.bordertech.wcomponents.Request)4 InputStream (java.io.InputStream)4 PrintWriter (java.io.PrintWriter)4 ArrayList (java.util.ArrayList)4 Environment (com.github.bordertech.wcomponents.Environment)3 MockRequest (com.github.bordertech.wcomponents.util.mock.MockRequest)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 Date (java.util.Date)3 List (java.util.List)3