Search in sources :

Example 1 with PartialResponseWriter

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();
}
Also used : ExternalContext(javax.faces.context.ExternalContext) PartialResponseWriter(javax.faces.context.PartialResponseWriter)

Example 2 with PartialResponseWriter

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);
        }
    }
}
Also used : FacesContext(javax.faces.context.FacesContext) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) PartialResponseWriter(javax.faces.context.PartialResponseWriter) AbortProcessingException(javax.faces.event.AbortProcessingException) IOException(java.io.IOException) JSONException(org.json.JSONException) UINamingContainer(javax.faces.component.UINamingContainer) NamingContainer(javax.faces.component.NamingContainer) ResourceUtils(org.primefaces.util.ResourceUtils) AbortProcessingException(javax.faces.event.AbortProcessingException) JSONObject(org.json.JSONObject) UIViewRoot(javax.faces.component.UIViewRoot)

Example 3 with PartialResponseWriter

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;
}
Also used : FacesContext(javax.faces.context.FacesContext) PartialResponseWriter(javax.faces.context.PartialResponseWriter) CspPartialResponseWriter(org.primefaces.csp.CspPartialResponseWriter) PrimeConfiguration(org.primefaces.config.PrimeConfiguration) CspPartialResponseWriter(org.primefaces.csp.CspPartialResponseWriter)

Example 4 with PartialResponseWriter

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();
    }
}
Also used : ExternalContext(javax.faces.context.ExternalContext) UIComponent(javax.faces.component.UIComponent) PartialResponseWriter(javax.faces.context.PartialResponseWriter) AjaxExceptionHandler(org.primefaces.component.ajaxexceptionhandler.AjaxExceptionHandler) ELException(javax.el.ELException) IOException(java.io.IOException) FacesException(javax.faces.FacesException) ViewExpiredException(javax.faces.application.ViewExpiredException)

Example 5 with PartialResponseWriter

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();
}
Also used : PartialResponseWriter(javax.faces.context.PartialResponseWriter) ResponseWriter(javax.faces.context.ResponseWriter) PartialResponseWriter(javax.faces.context.PartialResponseWriter)

Aggregations

PartialResponseWriter (javax.faces.context.PartialResponseWriter)5 IOException (java.io.IOException)2 ExternalContext (javax.faces.context.ExternalContext)2 FacesContext (javax.faces.context.FacesContext)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 ELException (javax.el.ELException)1 FacesException (javax.faces.FacesException)1 ViewExpiredException (javax.faces.application.ViewExpiredException)1 NamingContainer (javax.faces.component.NamingContainer)1 UIComponent (javax.faces.component.UIComponent)1 UINamingContainer (javax.faces.component.UINamingContainer)1 UIViewRoot (javax.faces.component.UIViewRoot)1 ResponseWriter (javax.faces.context.ResponseWriter)1 AbortProcessingException (javax.faces.event.AbortProcessingException)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1 AjaxExceptionHandler (org.primefaces.component.ajaxexceptionhandler.AjaxExceptionHandler)1 PrimeConfiguration (org.primefaces.config.PrimeConfiguration)1 CspPartialResponseWriter (org.primefaces.csp.CspPartialResponseWriter)1