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);
}
}
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);
}
}
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;
}
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();
}
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;
}
}
Aggregations