Search in sources :

Example 1 with ClientWindow

use of jakarta.faces.lifecycle.ClientWindow in project myfaces by apache.

the class FlashImpl method _clearRenderFlashTokenIfMapEmpty.

private void _clearRenderFlashTokenIfMapEmpty(FacesContext facesContext) {
    // Keep in mind that we cannot remove a cookie once the response has been sent,
    // but we can remove an attribute from session anytime, so the idea is check
    // if the map is empty or not and if that so, do not save the token. The effect
    // is we can reduce the session size a bit.
    ExternalContext externalContext = facesContext.getExternalContext();
    Object session = facesContext.getExternalContext().getSession(false);
    ClientWindow clientWindow = externalContext.getClientWindow();
    if (session != null && clientWindow != null) {
        Map<String, Object> map = _getRenderFlashMap(facesContext);
        if (map.isEmpty()) {
            if (facesContext.getApplication().getStateManager().isSavingStateInClient(facesContext)) {
                Map<String, Object> sessionMap = externalContext.getSessionMap();
                sessionMap.remove(FLASH_RENDER_MAP_TOKEN + SEPARATOR_CHAR + clientWindow.getId());
            } else {
                // Remove token, because it is not necessary
                FlashClientWindowTokenCollection lruMap = getFlashClientWindowTokenCollection(externalContext, false);
                if (lruMap != null) {
                    lruMap.remove(clientWindow.getId());
                    Map<String, Object> sessionMap = externalContext.getSessionMap();
                    if (lruMap.isEmpty()) {
                        sessionMap.remove(FLASH_CW_LRU_MAP);
                    } else {
                        // refresh remove
                        sessionMap.put(FLASH_CW_LRU_MAP, lruMap);
                    }
                }
            }
        }
    }
}
Also used : ExternalContext(jakarta.faces.context.ExternalContext) ClientWindow(jakarta.faces.lifecycle.ClientWindow)

Example 2 with ClientWindow

use of jakarta.faces.lifecycle.ClientWindow in project myfaces by apache.

the class FlashImpl method _getRenderFlashMapTokenFromPreviousRequest.

/**
 * Retrieve the map token of the render map from the previous request.
 *
 * Returns the value of _saveRenderFlashMapTokenForNextRequest() from
 * the previous request.
 * @param facesContext
 * @return
 */
private String _getRenderFlashMapTokenFromPreviousRequest(FacesContext facesContext) {
    ExternalContext externalContext = facesContext.getExternalContext();
    String tokenValue = null;
    ClientWindow clientWindow = externalContext.getClientWindow();
    if (clientWindow != null) {
        if (facesContext.getApplication().getStateManager().isSavingStateInClient(facesContext)) {
            Map<String, Object> sessionMap = externalContext.getSessionMap();
            tokenValue = (String) sessionMap.get(FLASH_RENDER_MAP_TOKEN + SEPARATOR_CHAR + clientWindow.getId());
        } else {
            FlashClientWindowTokenCollection lruMap = getFlashClientWindowTokenCollection(externalContext, false);
            if (lruMap != null) {
                tokenValue = (String) lruMap.get(clientWindow.getId());
            }
        }
    } else {
        HttpServletResponse httpResponse = ExternalContextUtils.getHttpServletResponse(externalContext);
        if (httpResponse != null) {
            // Use a cookie
            Cookie cookie = (Cookie) externalContext.getRequestCookieMap().get(FLASH_RENDER_MAP_TOKEN);
            if (cookie != null) {
                tokenValue = cookie.getValue();
            }
        } else {
            // Use HttpSession or PortletSession object
            Map<String, Object> sessionMap = externalContext.getSessionMap();
            tokenValue = (String) sessionMap.get(FLASH_RENDER_MAP_TOKEN);
        }
    }
    return tokenValue;
}
Also used : Cookie(jakarta.servlet.http.Cookie) ExternalContext(jakarta.faces.context.ExternalContext) ClientWindow(jakarta.faces.lifecycle.ClientWindow) HttpServletResponse(jakarta.servlet.http.HttpServletResponse)

Example 3 with ClientWindow

use of jakarta.faces.lifecycle.ClientWindow in project myfaces by apache.

the class FlashImpl method clearFlashMap.

@Override
public void clearFlashMap(FacesContext facesContext, String clientWindowId, String token) {
    if ((!_flashScopeDisabled) && (!facesContext.getApplication().getStateManager().isSavingStateInClient(facesContext))) {
        ExternalContext externalContext = facesContext.getExternalContext();
        ClientWindow clientWindow = externalContext.getClientWindow();
        if (clientWindow != null) {
            if (token != null) {
                String fullToken = FLASH_SESSION_MAP_SUBKEY_PREFIX + SEPARATOR_CHAR + token + SEPARATOR_CHAR;
                Map<String, Object> map = _createSubKeyMap(facesContext, fullToken);
                map.clear();
            }
        }
    }
}
Also used : ExternalContext(jakarta.faces.context.ExternalContext) ClientWindow(jakarta.faces.lifecycle.ClientWindow)

Example 4 with ClientWindow

use of jakarta.faces.lifecycle.ClientWindow 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 5 with ClientWindow

use of jakarta.faces.lifecycle.ClientWindow in project myfaces by apache.

the class LifecycleImpl method attachWindow.

@Override
public void attachWindow(FacesContext facesContext) {
    ClientWindow clientWindow = facesContext.getExternalContext().getClientWindow();
    if (clientWindow == null) {
        clientWindow = getClientWindowFactory().getClientWindow(facesContext);
    }
    if (clientWindow != null) {
        try {
            clientWindow.decode(facesContext);
            facesContext.getExternalContext().setClientWindow(clientWindow);
        } catch (RuntimeException e) {
            facesContext.getExternalContext().setClientWindow(null);
            throw e;
        }
    }
}
Also used : ClientWindow(jakarta.faces.lifecycle.ClientWindow)

Aggregations

ClientWindow (jakarta.faces.lifecycle.ClientWindow)28 ArrayList (java.util.ArrayList)10 List (java.util.List)8 ExternalContext (jakarta.faces.context.ExternalContext)6 Map (java.util.Map)5 FlowHandler (jakarta.faces.flow.FlowHandler)4 HashMap (java.util.HashMap)4 FacesContext (jakarta.faces.context.FacesContext)3 Flow (jakarta.faces.flow.Flow)3 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)3 FacesException (jakarta.faces.FacesException)2 ViewHandler (jakarta.faces.application.ViewHandler)2 PartialResponseWriter (jakarta.faces.context.PartialResponseWriter)2 PartialViewContext (jakarta.faces.context.PartialViewContext)2 Cookie (jakarta.servlet.http.Cookie)2 MessageUtils.getExceptionMessageString (com.sun.faces.util.MessageUtils.getExceptionMessageString)1 FacesWrapper (jakarta.faces.FacesWrapper)1 Application (jakarta.faces.application.Application)1 ConfigurableNavigationHandler (jakarta.faces.application.ConfigurableNavigationHandler)1 NavigationCase (jakarta.faces.application.NavigationCase)1