Package javax.faces.context

Examples of javax.faces.context.PartialResponseWriter


    {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        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);
View Full Code Here


        //    return;
        //}

        // note that we cannot use this.getPartialResponseWriter(), because
        // this could cause problems if PartialResponseWriter is wrapped
        PartialResponseWriter writer = _facesContext.getPartialViewContext().getPartialResponseWriter();
        PartialViewContext pvc = _facesContext.getPartialViewContext();
       
        ResponseWriter oldWriter = _facesContext.getResponseWriter();
        boolean inDocument = false;

        //response type = text/xml
        //no caching and no timeout if possible!
        ExternalContext externalContext = _facesContext.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
        {
            writer.startDocument();
            inDocument = true;
            _facesContext.setResponseWriter(writer);

            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())
                {
                    Set<VisitHint> hints = new HashSet<VisitHint>();
                    // unrendered have to be skipped, transient definitely must be added to our list!
                    hints.add(VisitHint.SKIP_UNRENDERED);
                   
                    // render=@all, so output the body.
                    if (renderIds.contains (PartialResponseWriter.RENDER_ALL_MARKER))
                    {
                        processRenderAll(viewRoot, writer);
                    }
                    else
                    {
                        List<UIComponent> updatedComponents = null;
                        if (!ExternalContextUtils.isPortlet(_facesContext.getExternalContext()) &&
                             MyfacesConfig.getCurrentInstance(externalContext).isStrictJsf2RefreshTargetAjax())
                        {
                            RequestViewContext rvc = RequestViewContext.getCurrentInstance(_facesContext);
                            if (rvc.isRenderTarget("head"))
                            {
                                UIComponent head = findHeadComponent(viewRoot);
                                if (head != null)
                                {
                                    writer.startUpdate("javax.faces.ViewHead");
                                    head.encodeAll(_facesContext);
                                    writer.endUpdate();
                                    if (updatedComponents == null)
                                    {
                                        updatedComponents = new ArrayList<UIComponent>();
                                    }
                                    updatedComponents.add(head);
                                }
                            }
                            if (rvc.isRenderTarget("body") || rvc.isRenderTarget("form"))
                            {
                                UIComponent body = findBodyComponent(viewRoot);
                                if (body != null)
                                {
                                    writer.startUpdate("javax.faces.ViewBody");
                                    body.encodeAll(_facesContext);
                                    writer.endUpdate();
                                    if (updatedComponents == null)
                                    {
                                        updatedComponents = new ArrayList<UIComponent>();
                                    }
                                    updatedComponents.add(body);
                                }
                            }
                        }

                        VisitContext visitCtx = VisitContext.createVisitContext(_facesContext, renderIds, hints);
                        viewRoot.visitTree(visitCtx, new PhaseAwareVisitCallback(_facesContext, phaseId, updatedComponents));
                    }
                }
                else if (!ExternalContextUtils.isPortlet(_facesContext.getExternalContext()) &&
                        MyfacesConfig.getCurrentInstance(externalContext).isStrictJsf2RefreshTargetAjax())
                {
                    RequestViewContext rvc = RequestViewContext.getCurrentInstance(_facesContext);
                    if (rvc.isRenderTarget("head"))
                    {
                        UIComponent head = findHeadComponent(viewRoot);
                        if (head != null)
                        {
                            writer.startUpdate("javax.faces.ViewHead");
                            head.encodeAll(_facesContext);
                            writer.endUpdate();
                        }
                    }
                    if (rvc.isRenderTarget("body") || rvc.isRenderTarget("form"))
                    {
                        UIComponent body = findBodyComponent(viewRoot);
                        if (body != null)
                        {
                            writer.startUpdate("javax.faces.ViewBody");
                            body.encodeAll(_facesContext);
                            writer.endUpdate();
                        }
                    }
                }
            }
           
            // 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(_facesContext);
                }
            }
           
            //Retrieve the state and apply it if it is not null.
            String viewState = _facesContext.getApplication().getStateManager().getViewState(_facesContext);
            if (viewState != null)
            {
                writer.startUpdate(PartialResponseWriter.VIEW_STATE_MARKER);
                writer.write(viewState);
                writer.endUpdate();
            }
        } catch (IOException ex) {
            Logger log = Logger.getLogger(PartialViewContextImpl.class.getName());
            if (log.isLoggable(Level.SEVERE)) {
                log.log(Level.SEVERE, "" , ex);
            }

        } finally {
            try {
                if (inDocument) {
                    writer.endDocument();
                }
                writer.flush();
            } catch (IOException ex) {
                Logger log = Logger.getLogger(PartialViewContextImpl.class.getName());
                if (log.isLoggable(Level.SEVERE)) {
                    log.log(Level.SEVERE, "" , ex);
                }
View Full Code Here

                            try
                            {
                                Writer writer = httpResp.getWriter();
                                // can't use facesContext.getResponseWriter(), because it might not have been set
                                ResponseWriter responseWriter = new HtmlResponseWriterImpl(writer, "text/xml", "utf-8");
                                PartialResponseWriter partialWriter = new PartialResponseWriter(responseWriter);
                                partialWriter.startDocument();
                                partialWriter.startError(ex.getClass().getName());
                                if (ex.getCause() != null)
                                {
                                    partialWriter.write(ex.getCause().toString());
                                }
                                else if (ex.getMessage() != null)
                                {
                                    partialWriter.write(ex.getMessage());
                                }
                                partialWriter.endError();
                                partialWriter.endDocument();
                            }
                            catch(IOException ioe)
                            {
                                throw new FacesException("Could not write the error page", ioe);
                            }
View Full Code Here

        //    return;
        //}

        // note that we cannot use this.getPartialResponseWriter(), because
        // this could cause problems if PartialResponseWriter is wrapped
        PartialResponseWriter writer = _facesContext.getPartialViewContext().getPartialResponseWriter();
        PartialViewContext pvc = _facesContext.getPartialViewContext();

        ResponseWriter oldWriter = _facesContext.getResponseWriter();
        boolean inDocument = false;

        //response type = text/xml
        //no caching and no timeout if possible!
        ExternalContext externalContext = _facesContext.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
        {
            writer.startDocument();
            inDocument = true;
            _facesContext.setResponseWriter(writer);

            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
                    {
                        List<UIComponent> updatedComponents = null;
                        if (!ExternalContextUtils.isPortlet(_facesContext.getExternalContext()) &&
                                MyfacesConfig.getCurrentInstance(externalContext).isStrictJsf2RefreshTargetAjax())
                        {
                            RequestViewContext rvc = RequestViewContext.getCurrentInstance(_facesContext);
                            if (rvc.isRenderTarget("head"))
                            {
                                UIComponent head = findHeadComponent(viewRoot);
                                if (head != null)
                                {
                                    writer.startUpdate("javax.faces.ViewHead");
                                    head.encodeAll(_facesContext);
                                    writer.endUpdate();
                                    if (updatedComponents == null)
                                    {
                                        updatedComponents = new ArrayList<UIComponent>();
                                    }
                                    updatedComponents.add(head);
                                }
                            }
                            if (rvc.isRenderTarget("body") || rvc.isRenderTarget("form"))
                            {
                                UIComponent body = findBodyComponent(viewRoot);
                                if (body != null)
                                {
                                    writer.startUpdate("javax.faces.ViewBody");
                                    body.encodeAll(_facesContext);
                                    writer.endUpdate();
                                    if (updatedComponents == null)
                                    {
                                        updatedComponents = new ArrayList<UIComponent>();
                                    }
                                    updatedComponents.add(body);
                                }
                            }
                        }

                        VisitContext visitCtx = getVisitContextFactory().getVisitContext(
                                _facesContext, renderIds, PARTIAL_RENDER_HINTS);
                        viewRoot.visitTree(visitCtx,
                                           new PhaseAwareVisitCallback(_facesContext, phaseId, updatedComponents));
                    }
                }
                else if (!ExternalContextUtils.isPortlet(_facesContext.getExternalContext()) &&
                        MyfacesConfig.getCurrentInstance(externalContext).isStrictJsf2RefreshTargetAjax())
                {
                    RequestViewContext rvc = RequestViewContext.getCurrentInstance(_facesContext);
                    if (rvc.isRenderTarget("head"))
                    {
                        UIComponent head = findHeadComponent(viewRoot);
                        if (head != null)
                        {
                            writer.startUpdate("javax.faces.ViewHead");
                            head.encodeAll(_facesContext);
                            writer.endUpdate();
                        }
                    }
                    if (rvc.isRenderTarget("body") || rvc.isRenderTarget("form"))
                    {
                        UIComponent body = findBodyComponent(viewRoot);
                        if (body != null)
                        {
                            writer.startUpdate("javax.faces.ViewBody");
                            body.encodeAll(_facesContext);
                            writer.endUpdate();
                        }
                    }
                }
            }

            // 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(_facesContext);
                }
            }

            //Retrieve the state and apply it if it is not null.
            String viewState = _facesContext.getApplication().getStateManager().getViewState(_facesContext);
            if (viewState != null)
            {
                writer.startUpdate(PartialResponseWriter.VIEW_STATE_MARKER);
                writer.write(viewState);
                writer.endUpdate();
            }
        }
        catch (IOException ex)
        {
            Logger log = Logger.getLogger(PartialViewContextImpl.class.getName());
            if (log.isLoggable(Level.SEVERE))
            {
                log.log(Level.SEVERE, "", ex);
            }

        }
        finally
        {
            try
            {
                if (inDocument)
                {
                    writer.endDocument();
                }
                writer.flush();
            }
            catch (IOException ex)
            {
                Logger log = Logger.getLogger(PartialViewContextImpl.class.getName());
                if (log.isLoggable(Level.SEVERE))
View Full Code Here

         * @param target the target component to be handled!
         */
        private void processRenderComponent(UIComponent target)
        {
            boolean inUpdate = false;
            PartialResponseWriter writer = (PartialResponseWriter) _facesContext.getResponseWriter();
            if (this._alreadyUpdatedComponents != null)
            {
                //Check if the parent was already updated.
                UIComponent parent = target;
                while (parent != null)
                {
                    if (this._alreadyUpdatedComponents.contains(parent))
                    {
                        return;
                    }
                    parent = parent.getParent();
                }
            }
            try
            {
                writer.startUpdate(target.getClientId(_facesContext));
                inUpdate = true;
                target.encodeAll(_facesContext);
            }
            catch (IOException ex)
            {
                Logger log = Logger.getLogger(PartialViewContextImpl.class.getName());
                if (log.isLoggable(Level.SEVERE))
                {
                    log.log(Level.SEVERE, "IOException for rendering component", ex);
                }
            }
            finally
            {
                if (inUpdate)
                {
                    try
                    {
                        writer.endUpdate();
                    }
                    catch (IOException ex)
                    {
                        Logger log = Logger.getLogger(PartialViewContextImpl.class.getName());
                        if (log.isLoggable(Level.SEVERE))
View Full Code Here

    {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        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);
View Full Code Here

    {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        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);
View Full Code Here

                }
            } while (!unhandled.isEmpty());

            if (!throwableList.isEmpty())
            {
                PartialResponseWriter partialWriter = null;
                boolean responseComplete = false;

                //Retrieve the partial writer used to render the exception
                if (facesContext == null)
                {
                    facesContext = FacesContext.getCurrentInstance();
                }
               
                facesContext = (facesContext == null) ? FacesContext.getCurrentInstance() : facesContext;
                ExternalContext externalContext = facesContext.getExternalContext();
               
                //Clean up the response if by some reason something has been written before.
                if (!facesContext.getExternalContext().isResponseCommitted())
                {
                    facesContext.getExternalContext().responseReset();
                }
               
                PartialViewContext partialViewContext = facesContext.getPartialViewContext();
                partialWriter = partialViewContext.getPartialResponseWriter();

                // ajax request --> xml error page
                externalContext.setResponseContentType("text/xml");
                externalContext.setResponseCharacterEncoding("UTF-8");
                externalContext.addResponseHeader("Cache-control", "no-cache");
               
                try
                {
                    partialWriter.startDocument();
   
                    for (Throwable t : throwableList)
                    {
                        renderAjaxError(partialWriter, t);
                    }
                   
                    responseComplete = true;
                }
                catch (IOException e)
                {
                    if (log.isLoggable(Level.SEVERE))
                    {
                        log.log(Level.SEVERE, "Cannot render exception on ajax request", e);
                    }
                }
                finally
                {
                    if (responseComplete)
                    {
                        try
                        {
                            partialWriter.endDocument();
                            facesContext.responseComplete();
                        }
                        catch (IOException e1)
                        {
                            if (log.isLoggable(Level.SEVERE))
View Full Code Here

        //    return;
        //}

        // note that we cannot use this.getPartialResponseWriter(), because
        // this could cause problems if PartialResponseWriter is wrapped
        PartialResponseWriter writer = _facesContext.getPartialViewContext().getPartialResponseWriter();
        PartialViewContext pvc = _facesContext.getPartialViewContext();

        ResponseWriter oldWriter = _facesContext.getResponseWriter();
        boolean inDocument = false;

        //response type = text/xml
        //no caching and no timeout if possible!
        ExternalContext externalContext = _facesContext.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
        {
            writer.startDocument();
            inDocument = true;
            _facesContext.setResponseWriter(writer);

            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
                    {
                        List<UIComponent> updatedComponents = null;
                        if (!ExternalContextUtils.isPortlet(_facesContext.getExternalContext()) &&
                                MyfacesConfig.getCurrentInstance(externalContext).isStrictJsf2RefreshTargetAjax())
                        {
                            RequestViewContext rvc = RequestViewContext.getCurrentInstance(_facesContext);
                            if (rvc.isRenderTarget("head"))
                            {
                                UIComponent head = findHeadComponent(viewRoot);
                                if (head != null)
                                {
                                    writer.startUpdate("javax.faces.ViewHead");
                                    head.encodeAll(_facesContext);
                                    writer.endUpdate();
                                    if (updatedComponents == null)
                                    {
                                        updatedComponents = new ArrayList<UIComponent>();
                                    }
                                    updatedComponents.add(head);
                                }
                            }
                            if (rvc.isRenderTarget("body") || rvc.isRenderTarget("form"))
                            {
                                UIComponent body = findBodyComponent(viewRoot);
                                if (body != null)
                                {
                                    writer.startUpdate("javax.faces.ViewBody");
                                    body.encodeAll(_facesContext);
                                    writer.endUpdate();
                                    if (updatedComponents == null)
                                    {
                                        updatedComponents = new ArrayList<UIComponent>();
                                    }
                                    updatedComponents.add(body);
                                }
                            }
                        }

                        VisitContext visitCtx = getVisitContextFactory().getVisitContext(
                                _facesContext, renderIds, PARTIAL_RENDER_HINTS);
                        viewRoot.visitTree(visitCtx,
                                           new PhaseAwareVisitCallback(_facesContext, phaseId, updatedComponents));
                    }
                }
                else if (!ExternalContextUtils.isPortlet(_facesContext.getExternalContext()) &&
                        MyfacesConfig.getCurrentInstance(externalContext).isStrictJsf2RefreshTargetAjax())
                {
                    RequestViewContext rvc = RequestViewContext.getCurrentInstance(_facesContext);
                    if (rvc.isRenderTarget("head"))
                    {
                        UIComponent head = findHeadComponent(viewRoot);
                        if (head != null)
                        {
                            writer.startUpdate("javax.faces.ViewHead");
                            head.encodeAll(_facesContext);
                            writer.endUpdate();
                        }
                    }
                    if (rvc.isRenderTarget("body") || rvc.isRenderTarget("form"))
                    {
                        UIComponent body = findBodyComponent(viewRoot);
                        if (body != null)
                        {
                            writer.startUpdate("javax.faces.ViewBody");
                            body.encodeAll(_facesContext);
                            writer.endUpdate();
                        }
                    }
                }
            }

            // 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(_facesContext);
                }
            }

            //Retrieve the state and apply it if it is not null.
            String viewState = _facesContext.getApplication().getStateManager().getViewState(_facesContext);
            if (viewState != null)
            {
                writer.startUpdate(PartialResponseWriter.VIEW_STATE_MARKER);
                writer.write(viewState);
                writer.endUpdate();
            }
        }
        catch (IOException ex)
        {
            Logger log = Logger.getLogger(PartialViewContextImpl.class.getName());
            if (log.isLoggable(Level.SEVERE))
            {
                log.log(Level.SEVERE, "", ex);
            }

        }
        finally
        {
            try
            {
                if (inDocument)
                {
                    writer.endDocument();
                }
                writer.flush();
            }
            catch (IOException ex)
            {
                Logger log = Logger.getLogger(PartialViewContextImpl.class.getName());
                if (log.isLoggable(Level.SEVERE))
View Full Code Here

         * @param target the target component to be handled!
         */
        private void processRenderComponent(UIComponent target)
        {
            boolean inUpdate = false;
            PartialResponseWriter writer = (PartialResponseWriter) _facesContext.getResponseWriter();
            if (this._alreadyUpdatedComponents != null)
            {
                //Check if the parent was already updated.
                UIComponent parent = target;
                while (parent != null)
                {
                    if (this._alreadyUpdatedComponents.contains(parent))
                    {
                        return;
                    }
                    parent = parent.getParent();
                }
            }
            try
            {
                writer.startUpdate(target.getClientId(_facesContext));
                inUpdate = true;
                target.encodeAll(_facesContext);
            }
            catch (IOException ex)
            {
                Logger log = Logger.getLogger(PartialViewContextImpl.class.getName());
                if (log.isLoggable(Level.SEVERE))
                {
                    log.log(Level.SEVERE, "IOException for rendering component", ex);
                }
            }
            finally
            {
                if (inUpdate)
                {
                    try
                    {
                        writer.endUpdate();
                    }
                    catch (IOException ex)
                    {
                        Logger log = Logger.getLogger(PartialViewContextImpl.class.getName());
                        if (log.isLoggable(Level.SEVERE))
View Full Code Here

TOP

Related Classes of javax.faces.context.PartialResponseWriter

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.