Search in sources :

Example 1 with ExternalContext

use of jakarta.faces.context.ExternalContext in project spring-framework by spring-projects.

the class FacesContextUtils method getSessionMutex.

/**
 * Return the best available mutex for the given session:
 * that is, an object to synchronize on for the given session.
 * <p>Returns the session mutex attribute if available; usually,
 * this means that the HttpSessionMutexListener needs to be defined
 * in {@code web.xml}. Falls back to the Session reference itself
 * if no mutex attribute found.
 * <p>The session mutex is guaranteed to be the same object during
 * the entire lifetime of the session, available under the key defined
 * by the {@code SESSION_MUTEX_ATTRIBUTE} constant. It serves as a
 * safe reference to synchronize on for locking on the current session.
 * <p>In many cases, the Session reference itself is a safe mutex
 * as well, since it will always be the same object reference for the
 * same active logical session. However, this is not guaranteed across
 * different servlet containers; the only 100% safe way is a session mutex.
 * @param fc the FacesContext to find the session mutex for
 * @return the mutex object (never {@code null})
 * @see org.springframework.web.util.WebUtils#SESSION_MUTEX_ATTRIBUTE
 * @see org.springframework.web.util.HttpSessionMutexListener
 */
@Nullable
public static Object getSessionMutex(FacesContext fc) {
    Assert.notNull(fc, "FacesContext must not be null");
    ExternalContext ec = fc.getExternalContext();
    Object mutex = ec.getSessionMap().get(WebUtils.SESSION_MUTEX_ATTRIBUTE);
    if (mutex == null) {
        mutex = ec.getSession(true);
    }
    return mutex;
}
Also used : ExternalContext(jakarta.faces.context.ExternalContext) Nullable(org.springframework.lang.Nullable)

Example 2 with ExternalContext

use of jakarta.faces.context.ExternalContext in project rubia-forums by flashboss.

the class JSFUtil method createFeedLink.

/**
 * Creates feed link.
 *
 * @param type RSS/Atom. See FeedConstants
 * @param what Kind of the link. See available kinds in FeedConstants
 * @param id   Id - for kind FeedCostants.GLOBAL is ignored
 * @return String with proper address
 */
public static String createFeedLink(String type, String what, Integer id) {
    ExternalContext ctx = FacesContext.getCurrentInstance().getExternalContext();
    String url = ctx.getRequestContextPath() + "/feeds/" + type + "/" + what + (GLOBAL.equals(what) ? "" : "/" + id.toString());
    String urlParam = getContextPath();
    String urlType = "s";
    url += "?url=" + urlParam + "&urlType=" + urlType;
    return url;
}
Also used : ExternalContext(jakarta.faces.context.ExternalContext)

Example 3 with ExternalContext

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

the class FlashImpl method _isRedirectTrueOnPreviousRequest.

/**
 * Return true if setRedirect(true) was called on the previous request.
 * Precondition: doPrePhaseActions() must have been called on restore view phase.
 * @param facesContext
 * @return
 */
private boolean _isRedirectTrueOnPreviousRequest(FacesContext facesContext) {
    ExternalContext externalContext = facesContext.getExternalContext();
    Map<String, Object> requestMap = externalContext.getRequestMap();
    Boolean redirect = (Boolean) requestMap.get(FLASH_PREVIOUS_REQUEST_REDIRECT);
    return Boolean.TRUE.equals(redirect);
}
Also used : ExternalContext(jakarta.faces.context.ExternalContext)

Example 4 with ExternalContext

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

the class FlashImpl method setRedirect.

@Override
public void setRedirect(boolean redirect) {
    if (_flashScopeDisabled) {
        return;
    }
    // FIXME this method has a design flaw, because the only valid value
    // is true and it should only be called by the NavigationHandler
    // in a redirect case RIGHT BEFORE ExternalContext.redirect().
    // Maybe a PreRedirectEvent issued by the ExternalContext would be a good
    // choice for JSF 2.1.
    FacesContext facesContext = FacesContext.getCurrentInstance();
    ExternalContext externalContext = facesContext.getExternalContext();
    Map<String, Object> requestMap = externalContext.getRequestMap();
    // save the value on the requestMap for this request
    Boolean alreadySet = (Boolean) requestMap.get(FLASH_REDIRECT);
    alreadySet = (alreadySet == null ? Boolean.FALSE : Boolean.TRUE);
    // if true and not already set, store it for the following request
    if (!alreadySet && redirect) {
        requestMap.put(FLASH_REDIRECT, Boolean.TRUE);
        // save redirect=true for the next request
        _saveRedirectValue(facesContext);
    } else {
        if (alreadySet) {
            log.warning("Multiple call to setRedirect() ignored.");
        } else // redirect = false
        {
            log.warning("Ignored call to setRedirect(false), because this " + "should only be set to true by the NavigationHandler. " + "No one else should change it.");
        }
    }
}
Also used : FacesContext(jakarta.faces.context.FacesContext) ExternalContext(jakarta.faces.context.ExternalContext)

Example 5 with ExternalContext

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

the class FlashImpl method isKeepMessages.

/**
 * Returns the value of a previous call to setKeepMessages() from this
 * request. If there was no call yet, false is returned.
 */
@Override
public boolean isKeepMessages() {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    Boolean keepMessages = null;
    if (facesContext != null) {
        ExternalContext externalContext = facesContext.getExternalContext();
        Map<String, Object> requestMap = externalContext.getRequestMap();
        keepMessages = (Boolean) requestMap.get(FLASH_KEEP_MESSAGES);
    }
    return (keepMessages == null ? Boolean.FALSE : keepMessages);
}
Also used : FacesContext(jakarta.faces.context.FacesContext) ExternalContext(jakarta.faces.context.ExternalContext)

Aggregations

ExternalContext (jakarta.faces.context.ExternalContext)214 FacesContext (jakarta.faces.context.FacesContext)70 FacesException (jakarta.faces.FacesException)43 Map (java.util.Map)27 IOException (java.io.IOException)25 PrintWriter (java.io.PrintWriter)19 PrivilegedActionException (java.security.PrivilegedActionException)18 ArrayList (java.util.ArrayList)16 HashMap (java.util.HashMap)16 UIViewRoot (jakarta.faces.component.UIViewRoot)15 PartialResponseWriter (jakarta.faces.context.PartialResponseWriter)14 ResponseWriter (jakarta.faces.context.ResponseWriter)14 List (java.util.List)11 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)10 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)10 HttpSession (jakarta.servlet.http.HttpSession)10 Application (jakarta.faces.application.Application)9 UIComponent (jakarta.faces.component.UIComponent)9 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)9 ViewHandler (jakarta.faces.application.ViewHandler)8