Search in sources :

Example 96 with UIContext

use of com.github.bordertech.wcomponents.UIContext in project wcomponents by BorderTech.

the class ServletUtil method handleError.

/**
 * Called if a Throwable is caught by the top-level service method. By default we display an error and terminate the
 * session.
 *
 * @param helper the current servlet helper
 * @param throwable the throwable
 * @throws ServletException a servlet exception
 * @throws IOException an IO Exception
 */
public static void handleError(final HttpServletHelper helper, final Throwable throwable) throws ServletException, IOException {
    HttpServletRequest httpServletRequest = helper.getBackingRequest();
    HttpServletResponse httpServletResponse = helper.getBackingResponse();
    // Allow for multi part requests
    Map<String, String[]> parameters = getRequestParameters(httpServletRequest);
    // Set error code for AJAX, Content or data requests
    boolean dataRequest = parameters.get(WServlet.DATA_LIST_PARAM_NAME) != null;
    Object target = parameters.get(WServlet.AJAX_TRIGGER_PARAM_NAME);
    if (target == null) {
        target = parameters.get(WServlet.TARGET_ID_PARAM_NAME);
    }
    if (target != null || dataRequest) {
        httpServletResponse.sendError(500, "Internal Error");
        return;
    }
    // Decide whether we should use the ErrorPageFactory.
    boolean handleErrorWithFatalErrorPageFactory = ConfigurationProperties.getHandleErrorWithFatalErrorPageFactory();
    // use the new technique and delegate to the ErrorPageFactory.
    if (handleErrorWithFatalErrorPageFactory) {
        helper.handleError(throwable);
        helper.dispose();
    } else {
        // use the old technique and just display a raw message.
        // First, decide whether we are in friendly mode or not.
        boolean friendly = ConfigurationProperties.getDeveloperErrorHandling();
        String message = InternalMessages.DEFAULT_SYSTEM_ERROR;
        // If we are unfriendly, terminate the session
        if (!friendly) {
            HttpSession session = httpServletRequest.getSession(true);
            session.invalidate();
            message = InternalMessages.DEFAULT_SYSTEM_ERROR_SEVERE;
        }
        // Display an error to the user.
        UIContext uic = helper.getUIContext();
        Locale locale = uic == null ? null : uic.getLocale();
        message = I18nUtilities.format(locale, message);
        httpServletResponse.getWriter().println(message);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Locale(java.util.Locale) UIContext(com.github.bordertech.wcomponents.UIContext) HttpSession(javax.servlet.http.HttpSession) HttpServletResponse(javax.servlet.http.HttpServletResponse)

Example 97 with UIContext

use of com.github.bordertech.wcomponents.UIContext in project wcomponents by BorderTech.

the class VelocityRendererImpl method renderInline.

/**
 * {@inheritDoc}
 */
@Override
public void renderInline(final String templateInline, final Map<String, Object> context, final Map<String, WComponent> taggedComponents, final Writer writer, final Map<String, Object> options) {
    LOG.debug("Rendering inline velocity template.");
    try {
        // Map the tagged components to be used in the replace writer
        Map<String, WComponent> componentsByKey = TemplateUtil.mapTaggedComponents(context, taggedComponents);
        // Setup context
        VelocityContext velocityContext = new VelocityContext();
        for (Map.Entry<String, Object> entry : context.entrySet()) {
            velocityContext.put(entry.getKey(), entry.getValue());
        }
        // Write inline template
        UIContext uic = UIContextHolder.getCurrent();
        try (TemplateWriter velocityWriter = new TemplateWriter(writer, componentsByKey, uic)) {
            getVelocityEngine().evaluate(velocityContext, velocityWriter, templateInline, templateInline);
        }
    } catch (Exception e) {
        throw new SystemException("Problems with inline velocity template." + e.getMessage(), e);
    }
}
Also used : WComponent(com.github.bordertech.wcomponents.WComponent) SystemException(com.github.bordertech.wcomponents.util.SystemException) UIContext(com.github.bordertech.wcomponents.UIContext) VelocityContext(org.apache.velocity.VelocityContext) Map(java.util.Map) SystemException(com.github.bordertech.wcomponents.util.SystemException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException)

Example 98 with UIContext

use of com.github.bordertech.wcomponents.UIContext in project wcomponents by BorderTech.

the class I18nUtilities method getEffectiveLocale.

/**
 * Get the locale currently in use.
 * @return The currently active locale.
 */
public static Locale getEffectiveLocale() {
    Locale effectiveLocale = null;
    UIContext uic = UIContextHolder.getCurrent();
    if (uic != null) {
        effectiveLocale = uic.getLocale();
    }
    if (effectiveLocale == null) {
        String defaultLocale = ConfigurationProperties.getDefaultLocale();
        effectiveLocale = new Locale(defaultLocale);
    }
    return effectiveLocale;
}
Also used : Locale(java.util.Locale) UIContext(com.github.bordertech.wcomponents.UIContext)

Example 99 with UIContext

use of com.github.bordertech.wcomponents.UIContext in project wcomponents by BorderTech.

the class TableUtil method getCurrentRowIndex.

/**
 * This can be used by column components on a {@link WTable} to determine the current row index.
 *
 * @param component the column component
 * @return the row index for the current row, or null if no row details
 */
public static List<Integer> getCurrentRowIndex(final WComponent component) {
    UIContext uic = UIContextHolder.getCurrent();
    // Check have correct context
    if (!(uic instanceof SubUIContext)) {
        return null;
    }
    // Find the table
    WTable table = WebUtilities.getAncestorOfClass(WTable.class, component);
    if (table == null) {
        return null;
    }
    int repeaterIdx = ((SubUIContext) uic).getRowIndex();
    RowIdWrapper wrapper = table.getRepeater().getBeanList().get(repeaterIdx);
    return wrapper.getRowIndex();
}
Also used : SubUIContext(com.github.bordertech.wcomponents.WRepeater.SubUIContext) RowIdWrapper(com.github.bordertech.wcomponents.WTable.RowIdWrapper) SubUIContext(com.github.bordertech.wcomponents.WRepeater.SubUIContext) UIContext(com.github.bordertech.wcomponents.UIContext) WTable(com.github.bordertech.wcomponents.WTable)

Example 100 with UIContext

use of com.github.bordertech.wcomponents.UIContext in project wcomponents by BorderTech.

the class TreeUtil method doTraverse.

/**
 * Internal implementation of tree traversal method.
 *
 * @param node the node to traverse.
 * @param visibleOnly if true, only visit visible components.
 * @param visitor the visitor to notify as the tree is traversed.
 * @return how the traversal should continue.
 */
private static VisitorResult doTraverse(final WComponent node, final boolean visibleOnly, final WComponentTreeVisitor visitor) {
    if (visibleOnly) {
        // Certain components have their visibility altered to implement custom processing.
        if (node instanceof WInvisibleContainer) {
            WComponent parent = node.getParent();
            // If inside a CardManager, skip the InvisibleContainer and process the visible card.
            if (parent instanceof WCardManager) {
                WComponent visible = ((WCardManager) node.getParent()).getVisible();
                if (visible == null) {
                    return VisitorResult.ABORT_BRANCH;
                }
                return doTraverse(visible, visibleOnly, visitor);
            } else if (parent instanceof WWindow) {
                // Abort branch if WWindow is not in ACTIVE state
                if (((WWindow) parent).getState() != WWindow.ACTIVE_STATE) {
                    return VisitorResult.ABORT_BRANCH;
                }
            }
        } else if (node instanceof WRepeatRoot) {
        // Let be processed.
        } else if (!node.isVisible()) {
            // For most components, we just need to see if they're marked as visible
            return VisitorResult.ABORT_BRANCH;
        }
    }
    VisitorResult result = visitor.visit(node);
    switch(result) {
        case ABORT_BRANCH:
            // Continue processing, but not down this branch
            return VisitorResult.CONTINUE;
        case CONTINUE:
            // Process repeater rows
            if (node instanceof WRepeater) {
                // Get parent repeater
                WRepeater repeater = (WRepeater) node;
                // Get row contexts
                List<UIContext> rowContextList = repeater.getRowContexts();
                WRepeatRoot repeatRoot = (WRepeatRoot) repeater.getRepeatedComponent().getParent();
                for (UIContext rowContext : rowContextList) {
                    UIContextHolder.pushContext(rowContext);
                    try {
                        result = doTraverse(repeatRoot, visibleOnly, visitor);
                    } finally {
                        UIContextHolder.popContext();
                    }
                    if (VisitorResult.ABORT.equals(result)) {
                        return VisitorResult.ABORT;
                    }
                }
            } else if (node instanceof Container) {
                Container container = (Container) node;
                for (int i = 0; i < container.getChildCount(); i++) {
                    result = doTraverse(container.getChildAt(i), visibleOnly, visitor);
                    if (VisitorResult.ABORT.equals(result)) {
                        return VisitorResult.ABORT;
                    }
                }
            }
            return VisitorResult.CONTINUE;
        default:
            // Abort entire traversal
            return VisitorResult.ABORT;
    }
}
Also used : WComponent(com.github.bordertech.wcomponents.WComponent) VisitorResult(com.github.bordertech.wcomponents.util.WComponentTreeVisitor.VisitorResult) Container(com.github.bordertech.wcomponents.Container) WInvisibleContainer(com.github.bordertech.wcomponents.WInvisibleContainer) WRepeatRoot(com.github.bordertech.wcomponents.WRepeater.WRepeatRoot) UIContext(com.github.bordertech.wcomponents.UIContext) WCardManager(com.github.bordertech.wcomponents.WCardManager) WWindow(com.github.bordertech.wcomponents.WWindow) WInvisibleContainer(com.github.bordertech.wcomponents.WInvisibleContainer) WRepeater(com.github.bordertech.wcomponents.WRepeater)

Aggregations

UIContext (com.github.bordertech.wcomponents.UIContext)114 Test (org.junit.Test)47 WComponent (com.github.bordertech.wcomponents.WComponent)18 WebXmlRenderContext (com.github.bordertech.wcomponents.servlet.WebXmlRenderContext)15 SystemException (com.github.bordertech.wcomponents.util.SystemException)14 WApplication (com.github.bordertech.wcomponents.WApplication)13 UIContextImpl (com.github.bordertech.wcomponents.UIContextImpl)11 WText (com.github.bordertech.wcomponents.WText)11 PrintWriter (java.io.PrintWriter)11 ComponentWithContext (com.github.bordertech.wcomponents.ComponentWithContext)10 SeleniumWComponentsWebDriver (com.github.bordertech.wcomponents.test.selenium.driver.SeleniumWComponentsWebDriver)9 ActionEscape (com.github.bordertech.wcomponents.ActionEscape)7 DefaultWComponent (com.github.bordertech.wcomponents.DefaultWComponent)7 WDropdown (com.github.bordertech.wcomponents.WDropdown)7 MockRequest (com.github.bordertech.wcomponents.util.mock.MockRequest)7 StringWriter (java.io.StringWriter)7 AjaxOperation (com.github.bordertech.wcomponents.AjaxOperation)6 Environment (com.github.bordertech.wcomponents.Environment)6 MockWEnvironment (com.github.bordertech.wcomponents.MockWEnvironment)6 WButton (com.github.bordertech.wcomponents.WButton)6