use of com.github.bordertech.wcomponents.UIContext in project wcomponents by BorderTech.
the class AbstractContainerHelper method renderErrorPageToHTML.
/**
* Render the error page component to HTML.
*
* @param errorPage the error page component
* @return the error page as HTML
*/
protected String renderErrorPageToHTML(final WComponent errorPage) {
// Check if using the default error page
boolean defaultErrorPage = errorPage instanceof FatalErrorPage;
String html = null;
// If not default implementation of error page, Transform error page to HTML
if (!defaultErrorPage) {
// Set UIC and Environment (Needed for Theme Paths)
UIContext uic = new UIContextImpl();
uic.setEnvironment(createEnvironment());
UIContextHolder.pushContext(uic);
try {
html = WebUtilities.renderWithTransformToHTML(errorPage);
} catch (Exception e) {
LOG.warn("Could not transform error page.", e);
} finally {
UIContextHolder.popContext();
}
}
// Not transformed. So just render.
if (html == null) {
UIContextHolder.pushContext(new UIContextImpl());
try {
html = WebUtilities.render(errorPage);
} catch (Exception e) {
LOG.warn("Could not render error page.", e);
html = "System error occurred but could not render error page.";
} finally {
UIContextHolder.popContext();
}
}
return html;
}
use of com.github.bordertech.wcomponents.UIContext in project wcomponents by BorderTech.
the class AbstractContainerHelper method processAction.
/**
* Support standard processing of the action phase of a request.
*
* @throws IOException if there is an IO error on writing a response.
*/
public void processAction() throws IOException {
if (isDisposed()) {
LOG.error("Skipping action phase. Attempt to reuse disposed ContainerHelper instance");
return;
}
try {
// Check user context has been prepared
if (getNewConversation() == null) {
throw new IllegalStateException("User context has not been prepared before the action phase");
}
prepareAction();
UIContext uic = getUIContext();
if (uic == null) {
throw new IllegalStateException("No user context set for the action phase.");
}
UIContextHolder.pushContext(uic);
// Make sure maps are cleared up
uic.clearScratchMap();
uic.clearRequestScratchMap();
prepareRequest();
Request req = getRequest();
getInterceptor().attachResponse(getResponse());
getInterceptor().serviceRequest(req);
if (req.isLogout()) {
handleLogout();
dispose();
}
} catch (ActionEscape esc) {
LOG.debug("ActionEscape performed.");
// Action escapes must be handled in the action phase and then
// do nothing if they reach the render phase (which they will in
// the servlet implementation)
handleEscape(esc);
dispose();
} catch (Escape esc) {
LOG.debug("Escape performed during action phase.");
// We can't handle the escape until the render phase.
} catch (Throwable t) {
// We try not to let any exception propagate to container.
String message = "Caught exception during action phase.";
LOG.error(message, t);
// We can't handle the error until the render phase.
propogateError(t);
} finally {
UIContextHolder.reset();
}
}
use of com.github.bordertech.wcomponents.UIContext 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();
}
}
use of com.github.bordertech.wcomponents.UIContext in project wcomponents by BorderTech.
the class AbstractContainerHelper method prepareRequest.
/**
* Prepare the session for the current request.
*/
protected void prepareRequest() {
LOG.debug("Preparing for request by adding headers and environment to top wcomponent");
// Configure the UIContext to handle this request.
UIContext uiContext = getUIContext();
// Add WEnvironment if not already done.
// If the component is new, then it will not have a WEnvironment yet.
Environment env;
if (uiContext.isDummyEnvironment()) {
env = createEnvironment();
uiContext.setEnvironment(env);
} else {
env = uiContext.getEnvironment();
}
// Update the environment for the current phase of the request
// processing.
updateEnvironment(env);
// container we are running in.
if (getRequest() == null) {
setRequest(createRequest());
}
// Update the wcomponent Request for the current phase of the request
// processing.
updateRequest(getRequest());
}
use of com.github.bordertech.wcomponents.UIContext in project wcomponents by BorderTech.
the class DevToolkit method paint.
/**
* Paints the DevToolkit content.
*
* @param templateName the resource name of the Velocity template to use.
* @param writer the writer to send the content to.
*/
private void paint(final String templateName, final PrintWriter writer) {
if (!isEnabled()) {
return;
}
try {
Template template = VelocityEngineFactory.getVelocityEngine().getTemplate(templateName);
VelocityContext context = new VelocityContext();
context.put("this", this);
UIContext uic = UIContextHolder.getCurrentPrimaryUIContext();
context.put("uic", uic);
context.put("ui", uic.getUI());
template.merge(context, writer);
} catch (Exception e) {
LOG.error("Unable to render dev toolkit", e);
}
}
Aggregations