Search in sources :

Example 1 with PartialResponseWriter

use of jakarta.faces.context.PartialResponseWriter in project myfaces by apache.

the class PartialViewContextImpl method processPartialRendering.

private void processPartialRendering(UIViewRoot viewRoot, PhaseId phaseId) {
    // note that we cannot use this.getPartialResponseWriter(), because
    // this could cause problems if PartialResponseWriter is wrapped
    PartialResponseWriter writer = context.getPartialViewContext().getPartialResponseWriter();
    PartialViewContext pvc = context.getPartialViewContext();
    ResponseWriter oldWriter = context.getResponseWriter();
    boolean inDocument = false;
    // response type = text/xml
    // no caching and no timeout if possible!
    ExternalContext externalContext = context.getExternalContext();
    externalContext.setResponseContentType("text/xml");
    externalContext.addResponseHeader("Pragma", "no-cache");
    externalContext.addResponseHeader("Cache-control", "no-cache");
    // under normal circumstances pragma should be enough, IE needs
    // a special treatment!
    // http://support.microsoft.com/kb/234067
    externalContext.addResponseHeader("Expires", "-1");
    try {
        String currentEncoding = writer.getCharacterEncoding();
        writer.writePreamble("<?xml version=\"1.0\" encoding=\"" + (currentEncoding == null ? "UTF-8" : currentEncoding) + "\"?>");
        writer.startDocument();
        writer.writeAttribute("id", viewRoot.getContainerClientId(context), "id");
        inDocument = true;
        context.setResponseWriter(writer);
        if (isResetValues()) {
            viewRoot.resetValues(context, getRenderIds());
        }
        if (pvc.isRenderAll()) {
            processRenderAll(viewRoot, writer);
        } else {
            Collection<String> renderIds = pvc.getRenderIds();
            // Only apply partial visit if we have ids to traverse
            if (renderIds != null && !renderIds.isEmpty()) {
                // render=@all, so output the body.
                if (renderIds.contains(PartialResponseWriter.RENDER_ALL_MARKER)) {
                    processRenderAll(viewRoot, writer);
                } else {
                    // In JSF 2.3 it was added jakarta.faces.Resource as an update target to add scripts or
                    // stylesheets inside <head> tag
                    List<UIComponent> updatedComponents = new ArrayList<>();
                    RequestViewContext rvc = RequestViewContext.getCurrentInstance(context);
                    processRenderResource(context, writer, rvc, updatedComponents, "head");
                    processRenderResource(context, writer, rvc, updatedComponents, "body");
                    processRenderResource(context, writer, rvc, updatedComponents, "form");
                    VisitContext visitCtx = getVisitContextFactory().getVisitContext(context, renderIds, MyFacesVisitHints.SET_SKIP_UNRENDERED);
                    viewRoot.visitTree(visitCtx, new PhaseAwareVisitCallback(context, phaseId, updatedComponents));
                }
            } else {
                List<UIComponent> updatedComponents = new ArrayList<>();
                RequestViewContext rvc = RequestViewContext.getCurrentInstance(context);
                processRenderResource(context, writer, rvc, updatedComponents, "head");
                processRenderResource(context, writer, rvc, updatedComponents, "body");
                processRenderResource(context, writer, rvc, updatedComponents, "form");
            }
            List<String> evalScripts = pvc.getEvalScripts();
            if (evalScripts != null && evalScripts.size() > 0) {
                for (String script : evalScripts) {
                    writer.startEval();
                    writer.write(script);
                    writer.endEval();
                }
            }
        }
        // invoke encodeAll() on every UIViewParameter in the view to
        // enable every UIViewParameter to save its value in the state
        // just like UIViewRoot.encodeEnd() does on a normal request
        // (see MYFACES-2645 for details)
        Collection<UIViewParameter> viewParams = ViewMetadata.getViewParameters(viewRoot);
        if (!viewParams.isEmpty()) {
            for (UIViewParameter param : viewParams) {
                param.encodeAll(context);
            }
        }
        // Retrieve the state and apply it if it is not null.
        String viewState = context.getApplication().getStateManager().getViewState(context);
        if (viewState != null) {
            writer.startUpdate(HtmlResponseStateManager.generateUpdateViewStateId(context));
            writer.write(viewState);
            writer.endUpdate();
        } else if (viewRoot.isTransient()) {
            writer.startUpdate(HtmlResponseStateManager.generateUpdateViewStateId(context));
            writer.write(StateTokenProcessor.STATELESS_TOKEN);
            writer.endUpdate();
        }
        ClientWindow cw = context.getExternalContext().getClientWindow();
        if (cw != null) {
            writer.startUpdate(HtmlResponseStateManager.generateUpdateClientWindowId(context));
            writer.writeText(cw.getId(), null);
            writer.endUpdate();
        }
    } catch (IOException ex) {
        if (log.isLoggable(Level.SEVERE)) {
            log.log(Level.SEVERE, "", ex);
        }
    } finally {
        try {
            if (inDocument) {
                writer.endDocument();
            }
            writer.flush();
        } catch (IOException ex) {
            if (log.isLoggable(Level.SEVERE)) {
                log.log(Level.SEVERE, "", ex);
            }
        }
        context.setResponseWriter(oldWriter);
    }
}
Also used : VisitContext(jakarta.faces.component.visit.VisitContext) ClientWindow(jakarta.faces.lifecycle.ClientWindow) UIComponent(jakarta.faces.component.UIComponent) ArrayList(java.util.ArrayList) RequestViewContext(org.apache.myfaces.context.RequestViewContext) PartialResponseWriter(jakarta.faces.context.PartialResponseWriter) IOException(java.io.IOException) ResponseWriter(jakarta.faces.context.ResponseWriter) PartialResponseWriter(jakarta.faces.context.PartialResponseWriter) ExternalContext(jakarta.faces.context.ExternalContext) UIViewParameter(jakarta.faces.component.UIViewParameter) PartialViewContext(jakarta.faces.context.PartialViewContext)

Example 2 with PartialResponseWriter

use of jakarta.faces.context.PartialResponseWriter in project myfaces by apache.

the class PartialViewContextImpl method getPartialResponseWriter.

@Override
public PartialResponseWriter getPartialResponseWriter() {
    assertNotReleased();
    if (_partialResponseWriter == null) {
        ResponseWriter responseWriter = context.getResponseWriter();
        if (responseWriter == null) {
            // ResponseWriter from the RenderKit and then wrap if necessary.
            try {
                RenderKit renderKit = context.getRenderKit();
                if (renderKit == null) {
                    // If the viewRoot was set to null by some reason, or there is no
                    // renderKitId on that view, this could be still an ajax redirect,
                    // so we have to try to calculate the renderKitId and return a
                    // RenderKit instance, to send the response.
                    String renderKitId = context.getApplication().getViewHandler().calculateRenderKitId(context);
                    RenderKitFactory rkf = (RenderKitFactory) FactoryFinder.getFactory(FactoryFinder.RENDER_KIT_FACTORY);
                    renderKit = rkf.getRenderKit(context, renderKitId);
                }
                responseWriter = renderKit.createResponseWriter(context.getExternalContext().getResponseOutputWriter(), "text/xml", context.getExternalContext().getRequestCharacterEncoding());
            } catch (IOException e) {
                throw new IllegalStateException("Cannot create Partial Response Writer", e);
            }
        }
        // createResponseWriter,  so we should cast here for it and prevent double wrapping.
        if (responseWriter instanceof PartialResponseWriter) {
            _partialResponseWriter = (PartialResponseWriter) responseWriter;
        } else {
            _partialResponseWriter = new PartialResponseWriterImpl(responseWriter);
        }
    }
    return _partialResponseWriter;
}
Also used : PartialResponseWriterImpl(org.apache.myfaces.context.PartialResponseWriterImpl) ResponseWriter(jakarta.faces.context.ResponseWriter) PartialResponseWriter(jakarta.faces.context.PartialResponseWriter) RenderKit(jakarta.faces.render.RenderKit) PartialResponseWriter(jakarta.faces.context.PartialResponseWriter) IOException(java.io.IOException) RenderKitFactory(jakarta.faces.render.RenderKitFactory)

Example 3 with PartialResponseWriter

use of jakarta.faces.context.PartialResponseWriter in project myfaces by apache.

the class ServletExternalContextImpl method redirect.

@Override
public void redirect(final String url) throws IOException {
    FacesContext facesContext = getCurrentFacesContext();
    PartialViewContext partialViewContext = facesContext.getPartialViewContext();
    if (partialViewContext.isPartialRequest()) {
        PartialResponseWriter writer = partialViewContext.getPartialResponseWriter();
        this.setResponseContentType("text/xml");
        this.setResponseCharacterEncoding("UTF-8");
        this.addResponseHeader("Cache-control", "no-cache");
        writer.startDocument();
        writer.redirect(url);
        writer.endDocument();
        facesContext.responseComplete();
    } else if (_servletResponse instanceof HttpServletResponse) {
        ((HttpServletResponse) _servletResponse).sendRedirect(url);
        facesContext.responseComplete();
    } else {
        throw new IllegalArgumentException("Only HttpServletResponse supported");
    }
}
Also used : FacesContext(jakarta.faces.context.FacesContext) PartialResponseWriter(jakarta.faces.context.PartialResponseWriter) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) PartialViewContext(jakarta.faces.context.PartialViewContext)

Example 4 with PartialResponseWriter

use of jakarta.faces.context.PartialResponseWriter in project mojarra by eclipse-ee4j.

the class ExternalContextImpl method redirect.

/**
 * @see ExternalContext#redirect(String)
 */
@Override
public void redirect(String requestURI) throws IOException {
    FacesContext ctx = FacesContext.getCurrentInstance();
    doLastPhaseActions(ctx, true);
    if (ctx.getPartialViewContext().isPartialRequest()) {
        if (response instanceof HttpServletResponse && ctx.getResponseComplete()) {
            throw new IllegalStateException();
        }
        PartialResponseWriter pwriter;
        ResponseWriter writer = ctx.getResponseWriter();
        if (writer instanceof PartialResponseWriter) {
            pwriter = (PartialResponseWriter) writer;
        } else {
            pwriter = ctx.getPartialViewContext().getPartialResponseWriter();
        }
        setResponseContentType(RIConstants.TEXT_XML_CONTENT_TYPE);
        setResponseCharacterEncoding("UTF-8");
        addResponseHeader("Cache-Control", "no-cache");
        // pwriter.writePreamble("<?xml version='1.0' encoding='UTF-8'?>\n");
        pwriter.startDocument();
        pwriter.redirect(requestURI);
        pwriter.endDocument();
    } else if (response instanceof HttpServletResponse) {
        ((HttpServletResponse) response).sendRedirect(requestURI);
    } else {
        throw new IllegalStateException();
    }
    ctx.responseComplete();
}
Also used : FacesContext(jakarta.faces.context.FacesContext) ResponseWriter(jakarta.faces.context.ResponseWriter) PartialResponseWriter(jakarta.faces.context.PartialResponseWriter) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) PartialResponseWriter(jakarta.faces.context.PartialResponseWriter)

Example 5 with PartialResponseWriter

use of jakarta.faces.context.PartialResponseWriter in project mojarra by eclipse-ee4j.

the class PartialViewContextImpl method createPartialResponseWriter.

private PartialResponseWriter createPartialResponseWriter() {
    ExternalContext extContext = ctx.getExternalContext();
    String encoding = extContext.getRequestCharacterEncoding();
    extContext.setResponseCharacterEncoding(encoding);
    ResponseWriter responseWriter = null;
    Writer out = null;
    try {
        out = extContext.getResponseOutputWriter();
    } catch (IOException ioe) {
        if (LOGGER.isLoggable(Level.SEVERE)) {
            LOGGER.log(Level.SEVERE, ioe.toString(), ioe);
        }
    }
    if (out != null) {
        UIViewRoot viewRoot = ctx.getViewRoot();
        if (viewRoot != null) {
            responseWriter = ctx.getRenderKit().createResponseWriter(out, RIConstants.TEXT_XML_CONTENT_TYPE, encoding);
        } else {
            RenderKitFactory factory = (RenderKitFactory) FactoryFinder.getFactory(FactoryFinder.RENDER_KIT_FACTORY);
            RenderKit renderKit = factory.getRenderKit(ctx, RenderKitFactory.HTML_BASIC_RENDER_KIT);
            responseWriter = renderKit.createResponseWriter(out, RIConstants.TEXT_XML_CONTENT_TYPE, encoding);
        }
    }
    if (responseWriter instanceof PartialResponseWriter) {
        return (PartialResponseWriter) responseWriter;
    } else {
        return new PartialResponseWriter(responseWriter);
    }
}
Also used : ResponseWriter(jakarta.faces.context.ResponseWriter) PartialResponseWriter(jakarta.faces.context.PartialResponseWriter) ExternalContext(jakarta.faces.context.ExternalContext) RenderKit(jakarta.faces.render.RenderKit) PartialResponseWriter(jakarta.faces.context.PartialResponseWriter) IOException(java.io.IOException) UIViewRoot(jakarta.faces.component.UIViewRoot) ResponseWriter(jakarta.faces.context.ResponseWriter) PartialResponseWriter(jakarta.faces.context.PartialResponseWriter) Writer(java.io.Writer) RenderKitFactory(jakarta.faces.render.RenderKitFactory)

Aggregations

PartialResponseWriter (jakarta.faces.context.PartialResponseWriter)23 ExternalContext (jakarta.faces.context.ExternalContext)14 FacesContext (jakarta.faces.context.FacesContext)12 FacesException (jakarta.faces.FacesException)10 PartialViewContext (jakarta.faces.context.PartialViewContext)7 ResponseWriter (jakarta.faces.context.ResponseWriter)7 IOException (java.io.IOException)7 HashMap (java.util.HashMap)6 Map (java.util.Map)6 UIComponent (jakarta.faces.component.UIComponent)3 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)3 UIViewRoot (jakarta.faces.component.UIViewRoot)2 ClientWindow (jakarta.faces.lifecycle.ClientWindow)2 RenderKit (jakarta.faces.render.RenderKit)2 RenderKitFactory (jakarta.faces.render.RenderKitFactory)2 ArrayList (java.util.ArrayList)2 ResourceHandler (jakarta.faces.application.ResourceHandler)1 NamingContainer (jakarta.faces.component.NamingContainer)1 UIViewParameter (jakarta.faces.component.UIViewParameter)1 VisitContext (jakarta.faces.component.visit.VisitContext)1