use of javax.faces.context.PartialResponseWriter in project primefaces by primefaces.
the class PrimeExceptionHandler method handleRedirect.
protected void handleRedirect(FacesContext context, Throwable rootCause, ExceptionInfo info, boolean responseResetted) throws IOException {
ExternalContext externalContext = context.getExternalContext();
externalContext.getSessionMap().put(ExceptionInfo.ATTRIBUTE_NAME, info);
Map<String, String> errorPages = PrimeApplicationContext.getCurrentInstance(context).getConfig().getErrorPages();
String errorPage = evaluateErrorPage(errorPages, rootCause);
String url = constructRedirectUrl(context, errorPage);
// workaround for mojarra -> mojarra doesn't reset PartialResponseWriter#inChanges if we call externalContext#resetResponse
if (responseResetted && context.getPartialViewContext().isAjaxRequest()) {
PartialResponseWriter writer = context.getPartialViewContext().getPartialResponseWriter();
externalContext.addResponseHeader("Content-Type", "text/xml; charset=" + externalContext.getResponseCharacterEncoding());
externalContext.addResponseHeader("Cache-Control", "no-cache");
externalContext.setResponseContentType("text/xml");
writer.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
writer.startElement("partial-response", null);
writer.startElement("redirect", null);
writer.writeAttribute("url", url, null);
writer.endElement("redirect");
writer.endElement("partial-response");
} else {
// workaround for IllegalStateException from redirect of committed response
if (externalContext.isResponseCommitted() && !context.getPartialViewContext().isAjaxRequest()) {
PartialResponseWriter writer = context.getPartialViewContext().getPartialResponseWriter();
writer.startElement("script", null);
writer.write("window.location.href = '" + url + "';");
writer.endElement("script");
writer.getWrapped().endDocument();
} else {
externalContext.redirect(url);
}
}
context.responseComplete();
}
use of javax.faces.context.PartialResponseWriter in project primefaces by primefaces.
the class PrimePartialResponseWriter method startMetadataIfNecessary.
protected void startMetadataIfNecessary() throws IOException {
if (metadataRendered) {
return;
}
metadataRendered = true;
FacesContext context = FacesContext.getCurrentInstance();
PrimeApplicationContext applicationContext = PrimeApplicationContext.getCurrentInstance(context);
if (applicationContext != null) {
try {
// catch possible ViewExpired
UIViewRoot viewRoot = context.getViewRoot();
if (viewRoot != null) {
// portlet parameter namespacing
if (viewRoot instanceof NamingContainer) {
String parameterNamespace = viewRoot.getContainerClientId(context);
if (LangUtils.isNotEmpty(parameterNamespace)) {
String parameterPrefix = parameterNamespace;
if (applicationContext.getEnvironment().isAtLeastJsf23()) {
// https://java.net/jira/browse/JAVASERVERFACES_SPEC_PUBLIC-790
parameterPrefix += UINamingContainer.getSeparatorChar(context);
}
Map<String, Object> params = new HashMap<>();
params.put("parameterPrefix", parameterPrefix);
encodeCallbackParams(params);
}
}
// we also skip update=@all, as the head with all resources, will already be rendered
if (context.isPostback() && !context.getPartialViewContext().isRenderAll() && !applicationContext.getEnvironment().isAtLeastJsf23()) {
List<ResourceUtils.ResourceInfo> initialResources = DynamicResourcesPhaseListener.getInitialResources(context);
List<ResourceUtils.ResourceInfo> currentResources = ResourceUtils.getComponentResources(context);
if (initialResources != null && currentResources != null && currentResources.size() > initialResources.size()) {
List<ResourceUtils.ResourceInfo> newResources = new ArrayList<>(currentResources);
newResources.removeAll(initialResources);
boolean updateStarted = false;
for (int i = 0; i < newResources.size(); i++) {
ResourceUtils.ResourceInfo resourceInfo = newResources.get(i);
if (!updateStarted) {
((PartialResponseWriter) getWrapped()).startUpdate("javax.faces.Resource");
updateStarted = true;
}
resourceInfo.getResource().encodeAll(context);
}
if (updateStarted) {
((PartialResponseWriter) getWrapped()).endUpdate();
}
}
}
}
} catch (Exception e) {
throw new AbortProcessingException(e);
}
}
}
use of javax.faces.context.PartialResponseWriter in project primefaces by primefaces.
the class PrimePartialViewContext method getPartialResponseWriter.
@Override
public PartialResponseWriter getPartialResponseWriter() {
if (writer == null) {
PartialResponseWriter parentWriter = getWrapped().getPartialResponseWriter();
writer = new PrimePartialResponseWriter(parentWriter);
FacesContext context = FacesContext.getCurrentInstance();
PrimeConfiguration config = PrimeApplicationContext.getCurrentInstance(context).getConfig();
if (config.isCsp()) {
writer = new CspPartialResponseWriter(writer, context, PrimeFacesContext.getCspState(context));
}
}
return writer;
}
use of javax.faces.context.PartialResponseWriter in project primefaces by primefaces.
the class PrimeExceptionHandler method handleAjaxException.
protected void handleAjaxException(FacesContext context, Throwable rootCause, ExceptionInfo info) throws IOException {
ExternalContext externalContext = context.getExternalContext();
PartialResponseWriter writer = context.getPartialViewContext().getPartialResponseWriter();
// should not happen actually
if (writer == null) {
return;
}
boolean responseResetted = false;
// mojarra workaround to avoid invalid partial output due to open tags
if (context.getCurrentPhaseId().equals(PhaseId.RENDER_RESPONSE)) {
if (!externalContext.isResponseCommitted()) {
// this doesn't flush, just clears the internal state in mojarra
writer.flush();
writer.endCDATA();
writer.endInsert();
writer.endUpdate();
writer.startError("");
writer.endError();
writer.getWrapped().endElement("changes");
writer.getWrapped().endElement("partial-response");
String characterEncoding = externalContext.getResponseCharacterEncoding();
externalContext.responseReset();
externalContext.setResponseCharacterEncoding(characterEncoding);
responseResetted = true;
}
}
AjaxExceptionHandler handlerComponent = null;
try {
rootCause = buildView(context, rootCause, rootCause);
handlerComponent = findHandlerComponent(context, rootCause);
} catch (Exception ex) {
LOGGER.log(Level.WARNING, "Could not build view or lookup a AjaxExceptionHandler component!", ex);
}
context.getAttributes().put(ExceptionInfo.ATTRIBUTE_NAME, info);
// redirect if no AjaxExceptionHandler available
if (handlerComponent == null) {
handleRedirect(context, rootCause, info, responseResetted);
} else // handle custom update / onexception callback
{
externalContext.addResponseHeader("Content-Type", "text/xml; charset=" + externalContext.getResponseCharacterEncoding());
externalContext.addResponseHeader("Cache-Control", "no-cache");
externalContext.setResponseContentType("text/xml");
writer.startDocument();
writer.startElement("changes", null);
if (LangUtils.isNotBlank(handlerComponent.getUpdate())) {
List<UIComponent> updates = SearchExpressionFacade.resolveComponents(context, handlerComponent, handlerComponent.getUpdate());
if (updates != null && !updates.isEmpty()) {
context.setResponseWriter(writer);
for (int i = 0; i < updates.size(); i++) {
UIComponent component = updates.get(i);
writer.startElement("update", null);
writer.writeAttribute("id", component.getClientId(context), null);
writer.startCDATA();
component.encodeAll(context);
writer.endCDATA();
writer.endElement("update");
}
}
}
if (LangUtils.isNotBlank(handlerComponent.getOnexception())) {
writer.startElement("eval", null);
writer.startCDATA();
writer.write("var hf=function(type,message,timestampp){");
writer.write(handlerComponent.getOnexception());
writer.write("};hf.call(this,\"" + info.getType() + "\",\"" + EscapeUtils.forJavaScript(info.getMessage()) + "\",\"" + info.getFormattedTimestamp() + "\");");
writer.endCDATA();
writer.endElement("eval");
}
writer.endElement("changes");
writer.endDocument();
context.responseComplete();
}
}
use of javax.faces.context.PartialResponseWriter in project liferay-faces-bridge-impl by liferay.
the class ExternalContextCompat_2_0_Impl method redirectJSF2PartialResponse.
protected void redirectJSF2PartialResponse(FacesContext facesContext, ResourceResponse resourceResponse, String url) throws IOException {
resourceResponse.setContentType("text/xml");
resourceResponse.setCharacterEncoding("UTF-8");
PartialResponseWriter partialResponseWriter;
ResponseWriter responseWriter = facesContext.getResponseWriter();
if (responseWriter instanceof PartialResponseWriter) {
partialResponseWriter = (PartialResponseWriter) responseWriter;
} else {
partialResponseWriter = facesContext.getPartialViewContext().getPartialResponseWriter();
}
partialResponseWriter.startDocument();
partialResponseWriter.redirect(url);
partialResponseWriter.endDocument();
facesContext.responseComplete();
}
Aggregations