@Override
public void encodeBegin(final FacesContext facesContext, final UIComponent component) throws IOException {
final UIPage page = (UIPage) component;
final TobagoConfig tobagoConfig = TobagoConfig.getInstance(facesContext);
// invoke prepareRender
EncodeUtils.prepareRendererAll(facesContext, page);
final LayoutContext layoutContext = new LayoutContext(page);
layoutContext.layout();
if (FacesContextUtils.getFocusId(facesContext) == null && !StringUtils.isBlank(page.getFocusId())) {
FacesContextUtils.setFocusId(facesContext, page.getFocusId());
}
final TobagoResponseWriter writer = HtmlRendererUtils.getTobagoResponseWriter(facesContext);
// reset responseWriter and render page
facesContext.setResponseWriter(writer);
ResponseUtils.ensureNoCacheHeader(facesContext);
ResponseUtils.ensureContentSecurityPolicyHeader(facesContext, tobagoConfig.getContentSecurityPolicy());
if (LOG.isDebugEnabled()) {
for (final Object o : page.getAttributes().entrySet()) {
final Map.Entry entry = (Map.Entry) o;
LOG.debug("*** '" + entry.getKey() + "' -> '" + entry.getValue() + "'");
}
}
final ExternalContext externalContext = facesContext.getExternalContext();
final Object response = externalContext.getResponse();
final Application application = facesContext.getApplication();
final ViewHandler viewHandler = application.getViewHandler();
final String viewId = facesContext.getViewRoot().getViewId();
final String formAction = externalContext.encodeActionURL(viewHandler.getActionURL(facesContext, viewId));
final String partialAction;
final boolean portlet = PortletUtils.isPortletApiAvailable() && response instanceof MimeResponse;
if (portlet) {
final MimeResponse mimeResponse = (MimeResponse) response;
final ResourceURL resourceURL = mimeResponse.createResourceURL();
partialAction = externalContext.encodeResourceURL(resourceURL.toString());
} else {
partialAction = null;
}
final String contentType = writer.getContentTypeWithCharSet();
ResponseUtils.ensureContentTypeHeader(facesContext, contentType);
final String clientId = page.getClientId(facesContext);
final ClientProperties client = ClientProperties.getInstance(facesContext);
final ProjectStage projectStage = tobagoConfig.getProjectStage();
final boolean developmentMode = projectStage == ProjectStage.Development;
final boolean productionMode = projectStage == ProjectStage.Production;
int clientLogSeverity = 2;
if (developmentMode) {
final String severity = (String) externalContext.getRequestMap().get(CLIENT_DEBUG_SEVERITY);
if (LOG.isDebugEnabled()) {
LOG.debug("get " + CLIENT_DEBUG_SEVERITY + " = " + severity);
}
if (severity != null) {
try {
int index = severity.indexOf(';');
if (index == -1) {
index = severity.length();
}
clientLogSeverity = Integer.parseInt(severity.substring(0, index));
} catch (final NumberFormatException e) {
// ignore; use default
}
}
}
final boolean preventFrameAttacks = tobagoConfig.isPreventFrameAttacks();
if (!FacesContextUtils.isAjax(facesContext)) {
final String title = (String) page.getAttributes().get(Attributes.LABEL);
writer.startElement(HtmlElements.HEAD, null);