public final class Faces
extends java.lang.Object
FacesContext
. In effect, it 'flattens' the hierarchy of nested objects.
Do note that using the hierarchy is actually a better software design practice, but can lead to verbose code.
In addition, note that there's normally a minor overhead in obtaining the thread local FacesContext
. In case
client code needs to call methods in this class multiple times it's expected that performance will be slightly better
if instead the FacesContext
is obtained once and the required methods are called on that, although the
difference is practically negligible when used in modern server hardware.
In such case, consider using FacesLocal
instead. The difference with Faces
is that no one method of
FacesLocal
obtains the FacesContext
from the current thread by
FacesContext.getCurrentInstance()
. This job is up to the caller.
Modifier and Type | Method and Description |
---|---|
static void |
addResponseCookie(java.lang.String name,
java.lang.String value,
java.lang.String path,
int maxAge)
Add a cookie with given name, value, path and maxage to the HTTP response.
|
static void |
addResponseHeader(java.lang.String name,
java.lang.String value)
Add a header with given name and value to the HTTP response.
|
static boolean |
authenticate()
Trigger the default container managed authentication mechanism on the current request.
|
static <T> T |
evaluateExpressionGet(java.lang.String expression)
Programmatically evaluate the given EL expression and return the evaluated value.
|
static void |
evaluateExpressionSet(java.lang.String expression,
java.lang.Object value)
Programmatically evaluate the given EL expression and set the given value.
|
static javax.faces.application.Application |
getApplication()
Returns the application singleton.
|
static <T> T |
getApplicationAttribute(javax.faces.context.FacesContext context,
java.lang.String name)
Deprecated.
|
static <T> T |
getApplicationAttribute(java.lang.String name)
Returns the application scope attribute value associated with the given name.
|
static javax.faces.application.Application |
getApplicationFromFactory()
Gets the JSF Application singleton from the FactoryFinder.
|
static java.util.Map<java.lang.String,java.lang.Object> |
getApplicationMap()
Returns the application scope map.
|
static java.lang.String |
getBookmarkableURL(java.util.Collection<ParamHolder> params,
boolean includeViewParams)
Returns the concrete domain-relative URL to the current view with the given params URL-encoded in the query
string and optionally include view parameters as well.
|
static java.lang.String |
getBookmarkableURL(java.util.Map<java.lang.String,java.util.List<java.lang.String>> params,
boolean includeViewParams)
Returns the concrete domain-relative URL to the current view with the given params URL-encoded in the query
string and optionally include view parameters as well.
|
static java.lang.String |
getBookmarkableURL(java.lang.String viewId,
java.util.Collection<ParamHolder> params,
boolean includeViewParams)
Returns the concrete domain-relative URL to the given view with the given params URL-encoded in the query
string and optionally include view parameters as well.
|
static java.lang.String |
getBookmarkableURL(java.lang.String viewId,
java.util.Map<java.lang.String,java.util.List<java.lang.String>> params,
boolean includeViewParams)
Returns the concrete domain-relative URL to the given view with the given params URL-encoded in the query
string and optionally include view parameters as well.
|
static javax.faces.context.FacesContext |
getContext()
Returns the current faces context.
|
static javax.faces.context.FacesContext |
getContext(javax.el.ELContext elContext)
Returns the faces context that's stored in an ELContext.
|
static <T> T |
getContextAttribute(java.lang.String name)
Returns the Faces context attribute value associated with the given name.
|
static javax.faces.event.PhaseId |
getCurrentPhaseId()
Returns the current phase ID.
|
static java.util.Locale |
getDefaultLocale()
Returns the default locale, or
null if there is none. |
static javax.faces.context.ExternalContext |
getExternalContext()
Returns the current external context.
|
static <T> T |
getFaceletAttribute(java.lang.String name)
Returns the Facelet attribute value associated with the given name.
|
static javax.faces.view.facelets.FaceletContext |
getFaceletContext()
Returns the Facelet context.
|
static javax.servlet.ServletRegistration |
getFacesServletRegistration(javax.servlet.ServletContext servletContext)
Returns the
ServletRegistration associated with the FacesServlet . |
static javax.faces.context.Flash |
getFlash()
Returns the flash scope.
|
static <T> T |
getFlashAttribute(java.lang.String name)
Returns the flash scope attribute value associated with the given name.
|
static java.lang.String |
getImplInfo()
Returns the implementation information of currently loaded JSF implementation.
|
static java.lang.String |
getInitParameter(java.lang.String name)
Returns the application initialization parameter.
|
static java.util.Map<java.lang.String,java.lang.String> |
getInitParameterMap()
Returns the application initialization parameter map.
|
static java.util.Locale |
getLocale()
Returns the current locale.
|
static java.lang.String |
getMapping()
Determines and returns the faces servlet mapping used in the current request.
|
static <T> T |
getMetadataAttribute(java.lang.String name)
Returns the metadata attribute of the current view associated with the given name.
|
static <T> T |
getMetadataAttribute(java.lang.String viewId,
java.lang.String name)
Returns the metadata attribute of the given view ID associated with the given name.
|
static java.util.Map<java.lang.String,java.lang.Object> |
getMetadataAttributes(java.lang.String viewId)
Returns the metadata attribute map of the given view ID, or an empty map if there is no view metadata.
|
static java.lang.String |
getMimeType(java.lang.String name)
Returns the mime type for the given file name.
|
static java.lang.String |
getRealPath(java.lang.String webContentPath)
Returns the absolute disk file system path representation of the given web content path.
|
static java.lang.String |
getRemoteAddr()
Returns the Internet Protocol (IP) address of the client that sent the request.
|
static java.lang.String |
getRemoteUser()
Returns the name of the logged-in user for container managed FORM based authentication, if any.
|
static javax.servlet.http.HttpServletRequest |
getRequest()
Returns the HTTP servlet request.
|
static <T> T |
getRequestAttribute(javax.faces.context.FacesContext context,
java.lang.String name)
Deprecated.
Use
FacesLocal.getRequestAttribute(FacesContext, String) instead. |
static <T> T |
getRequestAttribute(java.lang.String name)
Returns the request scope attribute value associated with the given name.
|
static java.lang.String |
getRequestBaseURL()
Returns the HTTP request base URL.
|
static java.lang.String |
getRequestContextPath()
Returns the HTTP request context path.
|
static java.lang.String |
getRequestCookie(java.lang.String name)
Returns the value of the HTTP request cookie associated with the given name.
|
static java.lang.String |
getRequestDomainURL()
Returns the HTTP request domain URL.
|
static java.lang.String |
getRequestHeader(java.lang.String name)
Returns the HTTP request header value associated with the given name.
|
static java.util.Map<java.lang.String,java.lang.String> |
getRequestHeaderMap()
Returns the HTTP request header map.
|
static java.lang.String[] |
getRequestHeaderValues(java.lang.String name)
Returns the HTTP request header values associated with the given name.
|
static java.util.Map<java.lang.String,java.lang.String[]> |
getRequestHeaderValuesMap()
Returns the HTTP request header values map.
|
static java.lang.String |
getRequestHostname()
Returns the HTTP request hostname.
|
static java.util.Map<java.lang.String,java.lang.Object> |
getRequestMap()
Returns the request scope map.
|
static java.lang.String |
getRequestParameter(java.lang.String name)
Returns the HTTP request parameter value associated with the given name.
|
static java.util.Map<java.lang.String,java.lang.String> |
getRequestParameterMap()
Returns the HTTP request parameter map.
|
static java.lang.String[] |
getRequestParameterValues(java.lang.String name)
Returns the HTTP request parameter values associated with the given name.
|
static java.util.Map<java.lang.String,java.lang.String[]> |
getRequestParameterValuesMap()
Returns the HTTP request parameter values map.
|
static java.lang.String |
getRequestPathInfo()
Returns the HTTP request path info.
|
static java.lang.String |
getRequestQueryString()
Returns the HTTP request query string.
|
static java.util.Map<java.lang.String,java.util.List<java.lang.String>> |
getRequestQueryStringMap()
Returns the HTTP request query string as parameter values map.
|
static java.lang.String |
getRequestServletPath()
Returns the HTTP request servlet path.
|
static java.lang.String |
getRequestURI()
Returns the HTTP request URI.
|
static java.lang.String |
getRequestURIWithQueryString()
Returns the HTTP request URI with query string.
|
static java.lang.String |
getRequestURL()
Returns the HTTP request URL.
|
static java.lang.String |
getRequestURLWithQueryString()
Returns the HTTP request URL with query string.
|
static java.net.URL |
getResource(java.lang.String path)
Returns a URL for an application resource mapped to the specified path, if it exists; otherwise, return
null . |
static java.io.InputStream |
getResourceAsStream(java.lang.String path)
Returns an input stream for an application resource mapped to the specified path, if it exists; otherwise,
return
null . |
static java.util.Set<java.lang.String> |
getResourcePaths(java.lang.String path)
Returns a set of available application resource paths matching the specified path.
|
static javax.servlet.http.HttpServletResponse |
getResponse()
Returns the HTTP servlet response.
|
static int |
getResponseBufferSize()
Returns the HTTP response buffer size.
|
static java.lang.String |
getResponseCharacterEncoding()
Returns the HTTP response character encoding.
|
static java.lang.String |
getServerInfo()
Returns the server information of currently running application server implementation.
|
static javax.servlet.ServletContext |
getServletContext()
Returns the servlet context.
|
static javax.servlet.http.HttpSession |
getSession()
Returns the HTTP session and creates one if one doesn't exist.
|
static javax.servlet.http.HttpSession |
getSession(boolean create)
Returns the HTTP session and creates one if one doesn't exist and
create argument is
true , otherwise don't create one and return null . |
static <T> T |
getSessionAttribute(java.lang.String name)
Returns the session scope attribute value associated with the given name.
|
static long |
getSessionCreationTime()
Returns the time when the HTTP session was created, measured in epoch time.
|
static java.lang.String |
getSessionId()
Returns a string containing the unique identifier assigned to this session.
|
static long |
getSessionLastAccessedTime()
Returns the time of the previous request associated with the current HTTP session, measured in epoch time.
|
static java.util.Map<java.lang.String,java.lang.Object> |
getSessionMap()
Returns the session scope map.
|
static int |
getSessionMaxInactiveInterval()
Returns the HTTP session timeout in seconds.
|
static java.util.List<java.util.Locale> |
getSupportedLocales()
Returns a list of all supported locales on this application, with the default locale as the first item, if any.
|
static <T> T |
getViewAttribute(java.lang.String name)
Returns the view scope attribute value associated with the given name.
|
static java.lang.String |
getViewId()
Returns the ID of the current view root, or
null if there is no view. |
static java.util.Map<java.lang.String,java.lang.Object> |
getViewMap()
Returns the view scope map.
|
static java.util.Map<java.lang.String,java.util.List<java.lang.String>> |
getViewParameterMap()
Returns the view parameters of the current view as a parameter map, or an empty map if there is no view.
|
static java.util.Collection<javax.faces.component.UIViewParameter> |
getViewParameters()
Returns the view parameters of the current view, or an empty collection if there is no view.
|
static javax.faces.component.UIViewRoot |
getViewRoot()
Returns the current view root.
|
static boolean |
hasSession()
Returns whether the HTTP session has already been created.
|
static boolean |
hasSessionTimedOut()
Returns whether the HTTP session has been timed out for the current request.
|
static void |
invalidateSession()
Invalidates the current HTTP session.
|
static boolean |
isAjaxRequest()
Returns whether the current request is an ajax request.
|
static boolean |
isDevelopment()
Returns whether we're in development stage.
|
static boolean |
isPostback()
Returns whether the current request is a postback.
|
static boolean |
isPrefixMapping()
Returns whether the faces servlet mapping used in the current request is a prefix mapping.
|
static boolean |
isPrefixMapping(java.lang.String mapping)
Returns whether the given faces servlet mapping is a prefix mapping.
|
static boolean |
isRenderResponse()
Returns
true if we're currently in the render response phase. |
static boolean |
isResponseCommitted()
Returns whether the response is already committed.
|
static boolean |
isResponseComplete()
Returns
true if the FacesContext.responseComplete() has been called. |
static boolean |
isSessionNew()
Returns whether the HTTP session has been created for the first time in the current request.
|
static boolean |
isUserInRole(java.lang.String role)
Returns whether the currently logged-in user has the given role.
|
static boolean |
isValidationFailed()
Returns whether the validations phase of the current request has failed.
|
static void |
login(java.lang.String username,
java.lang.String password)
Perform programmatic login for container managed FORM based authentication.
|
static void |
logout()
Perform programmatic logout for container managed FORM based authentication.
|
static void |
navigate(java.lang.String outcome)
Perform the JSF navigation to the given outcome.
|
static java.lang.String |
normalizeViewId(java.lang.String path)
Normalize the given path as a valid view ID based on the current mapping, if necessary.
|
static void |
redirect(java.lang.String url,
java.lang.String... paramValues)
Sends a temporary (302) redirect to the given URL.
|
static void |
redirectPermanent(java.lang.String url,
java.lang.String... paramValues)
Sends a permanent (301) redirect to the given URL.
|
static <T> T |
removeApplicationAttribute(java.lang.String name)
Removes the application scope attribute value associated with the given name.
|
static <T> T |
removeFlashAttribute(java.lang.String name)
Removes the flash scope attribute value associated with the given name.
|
static <T> T |
removeRequestAttribute(java.lang.String name)
Removes the request scope attribute value associated with the given name.
|
static void |
removeResponseCookie(java.lang.String name,
java.lang.String path)
Remove the cookie with given name and path from the HTTP response.
|
static <T> T |
removeSessionAttribute(java.lang.String name)
Removes the session scope attribute value associated with the given name.
|
static <T> T |
removeViewAttribute(java.lang.String name)
Removes the view scope attribute value associated with the given name.
|
static void |
renderResponse()
Signals JSF that, as soon as the current phase of the lifecycle has been completed, control should be passed to
the Render Response phase, bypassing any phases that have not been executed yet.
|
static void |
responseComplete()
Signals JSF that the response for this request has already been generated (such as providing a file download),
and that the lifecycle should be terminated as soon as the current phase is completed.
|
static void |
responseReset()
Resets the current response.
|
static void |
responseSendError(int status,
java.lang.String message)
Sends a HTTP response error with the given status and message.
|
static void |
sendFile(byte[] content,
java.lang.String filename,
boolean attachment)
Send the given byte array as a file to the response.
|
static void |
sendFile(java.io.File file,
boolean attachment)
Send the given file to the response.
|
static void |
sendFile(java.io.InputStream content,
java.lang.String filename,
boolean attachment)
Send the given input stream as a file to the response.
|
static void |
setApplicationAttribute(java.lang.String name,
java.lang.Object value)
Sets the application scope attribute value associated with the given name.
|
static void |
setContext(javax.faces.context.FacesContext context)
Sets the given faces context as current instance.
|
static void |
setContextAttribute(java.lang.String name,
java.lang.Object value)
Sets the Faces context attribute value associated with the given name.
|
static void |
setFaceletAttribute(java.lang.String name,
java.lang.Object value)
Sets the Facelet attribute value associated with the given name.
|
static void |
setFlashAttribute(java.lang.String name,
java.lang.Object value)
Sets the flash scope attribute value associated with the given name.
|
static void |
setLocale(java.util.Locale locale)
Set the locale of the current view, which is to be used in localizing of the response.
|
static void |
setRequestAttribute(java.lang.String name,
java.lang.Object value)
Sets the request scope attribute value associated with the given name.
|
static void |
setResponseStatus(int status)
Sets the HTTP response status code.
|
static void |
setSessionAttribute(java.lang.String name,
java.lang.Object value)
Sets the session scope attribute value associated with the given name.
|
static void |
setSessionMaxInactiveInterval(int seconds)
Sets the HTTP session timeout in seconds.
|
static void |
setViewAttribute(java.lang.String name,
java.lang.Object value)
Sets the view scope attribute value associated with the given name.
|
static void |
setViewRoot(java.lang.String viewId)
Sets the current view root to the given view ID.
|
static void |
validationFailed()
Signals JSF that the validations phase of the current request has failed.
|
public static javax.faces.context.FacesContext getContext()
Note that whenever you absolutely need this method to perform a general task, you might want to consider to submit a feature request to OmniFaces in order to add a new utility method which performs exactly this general task.
FacesContext.getCurrentInstance()
public static javax.faces.context.FacesContext getContext(javax.el.ELContext elContext)
Note that this only works for an ELContext that is created in the context of JSF.
elContext
- the EL context to obtain the faces context from.public static void setContext(javax.faces.context.FacesContext context)
FacesContextWrapper
which you'd like to (temporarily) use as the current instance of the faces context.context
- The faces context to be set as the current instance.public static javax.faces.context.ExternalContext getExternalContext()
Note that whenever you absolutely need this method to perform a general task, you might want to consider to submit a feature request to OmniFaces in order to add a new utility method which performs exactly this general task.
FacesContext.getExternalContext()
public static javax.faces.application.Application getApplication()
Note that whenever you absolutely need this method to perform a general task, you might want to consider to submit a feature request to OmniFaces in order to add a new utility method which performs exactly this general task.
FacesContext.getApplication()
public static javax.faces.application.Application getApplicationFromFactory()
This method is an alternative for getApplication()
for those situations where the
FacesContext
isn't available.
public static java.lang.String getImplInfo()
Package.getImplementationTitle()
,
Package.getImplementationVersion()
public static java.lang.String getServerInfo()
ServletContext.getServerInfo()
public static boolean isDevelopment()
javax.faces.PROJECT_STAGE
context parameter in web.xml
is set to Development
.true
if we're in development stage, otherwise false
.Application.getProjectStage()
public static java.lang.String getMapping()
/faces/*
), then this returns the whole path, with a leading slash (e.g. /faces
). If JSF
is suffix mapped (e.g. *.xhtml
), then this returns the whole extension (e.g. .xhtml
).getRequestPathInfo()
,
getRequestServletPath()
public static boolean isPrefixMapping()
true
if the faces servlet mapping used in the current request is a prefix mapping, otherwise
false
.getMapping()
,
isPrefixMapping(String)
public static boolean isPrefixMapping(java.lang.String mapping)
isPrefixMapping()
when you already have obtained the mapping from getMapping()
so that the
mapping won't be calculated twice.mapping
- The mapping to be tested.true
if the faces servlet mapping used in the current request is a prefix mapping, otherwise
false
.java.lang.NullPointerException
- When mapping is null
.public static javax.faces.event.PhaseId getCurrentPhaseId()
FacesContext.getCurrentPhaseId()
public static void validationFailed()
isValidationFailed()
in Java and by
#{facesContext.validationFailed}
in EL.FacesContext.validationFailed()
public static boolean isValidationFailed()
true
if the validations phase of the current request has failed, otherwise
false
.FacesContext.isValidationFailed()
public static <T> T evaluateExpressionGet(java.lang.String expression)
T
- The expected return type.expression
- The EL expression to be evaluated.java.lang.ClassCastException
- When T
is of wrong type.Application.evaluateExpressionGet(FacesContext, String, Class)
public static void evaluateExpressionSet(java.lang.String expression, java.lang.Object value)
expression
- The EL expression to be evaluated.value
- The value to be set in the property behind the EL expression.Application.getExpressionFactory()
,
ExpressionFactory.createValueExpression(ELContext, String, Class)
,
ValueExpression.setValue(ELContext, Object)
public static <T> T getContextAttribute(java.lang.String name)
name
- The Faces context attribute name.java.lang.ClassCastException
- When T
is of wrong type.FacesContext.getAttributes()
public static void setContextAttribute(java.lang.String name, java.lang.Object value)
name
- The Faces context attribute name.value
- The Faces context attribute value.FacesContext.getAttributes()
public static javax.faces.component.UIViewRoot getViewRoot()
FacesContext.getViewRoot()
public static void setViewRoot(java.lang.String viewId)
viewId
- The ID of the view which needs to be set as the current view root.ViewHandler.createView(FacesContext, String)
,
FacesContext.setViewRoot(UIViewRoot)
public static java.lang.String getViewId()
null
if there is no view.null
if there is no view.UIViewRoot.getViewId()
public static java.lang.String normalizeViewId(java.lang.String path)
path
- The path to be normalized as a valid view ID based on the current mapping.getMapping()
,
isPrefixMapping(String)
public static java.util.Collection<javax.faces.component.UIViewParameter> getViewParameters()
ViewMetadata.getViewParameters(UIViewRoot)
public static java.util.Map<java.lang.String,java.util.List<java.lang.String>> getViewParameterMap()
ViewHandler.getBookmarkableURL(FacesContext, String, Map, boolean)
.ViewMetadata.getViewParameters(UIViewRoot)
public static java.util.Map<java.lang.String,java.lang.Object> getMetadataAttributes(java.lang.String viewId)
viewId
- The view ID to return the metadata attribute map for.ViewDeclarationLanguage.getViewMetadata(FacesContext, String)
public static <T> T getMetadataAttribute(java.lang.String viewId, java.lang.String name)
getViewAttribute(String)
.viewId
- The view ID to return the metadata attribute for.name
- The metadata attribute name.java.lang.ClassCastException
- When T
is of wrong type.ViewDeclarationLanguage.getViewMetadata(FacesContext, String)
public static <T> T getMetadataAttribute(java.lang.String name)
getViewAttribute(String)
.name
- The metadata attribute name.java.lang.ClassCastException
- When T
is of wrong type.UIComponentBase.getAttributes()
public static java.util.Locale getLocale()
UIViewRoot.getLocale()
,
ExternalContext.getRequestLocale()
,
Application.getDefaultLocale()
,
Locale.getDefault()
public static java.util.Locale getDefaultLocale()
null
if there is none.null
if there is none.Application.getDefaultLocale()
public static java.util.List<java.util.Locale> getSupportedLocales()
faces-config.xml
.Application.getDefaultLocale()
,
Application.getSupportedLocales()
public static void setLocale(java.util.Locale locale)
locale
- The locale of the current view.java.lang.IllegalStateException
- When there is no view (i.e. when it is null
). This can happen if the
method is called at the wrong moment in the JSF lifecycle, e.g. before the view has been restored/created.UIViewRoot.setLocale(Locale)
public static void navigate(java.lang.String outcome)
outcome
- The navigation outcome.Application.getNavigationHandler()
,
NavigationHandler.handleNavigation(FacesContext, String, String)
public static java.lang.String getBookmarkableURL(java.util.Map<java.lang.String,java.util.List<java.lang.String>> params, boolean includeViewParams)
<form action>
, or in <a href>
.params
- The parameters to be URL-encoded in the query string. Can be null
.includeViewParams
- Whether the view parameters of the current view should be included as well.java.lang.IllegalStateException
- When there is no view (i.e. when it is null
). This can happen if the
method is called at the wrong moment in the JSF lifecycle, e.g. before the view has been restored/created.ViewHandler.getBookmarkableURL(FacesContext, String, Map, boolean)
public static java.lang.String getBookmarkableURL(java.lang.String viewId, java.util.Map<java.lang.String,java.util.List<java.lang.String>> params, boolean includeViewParams)
<form action>
, or in <a href>
.viewId
- The view ID to create the bookmarkable URL for.params
- The parameters to be URL-encoded in the query string. Can be null
.includeViewParams
- Whether the view parameters of the current view which are also declared in the target
view should be included as well. Note thus that this does not include the view parameters which are not declared
in the target view!ViewHandler.getBookmarkableURL(FacesContext, String, Map, boolean)
public static java.lang.String getBookmarkableURL(java.util.Collection<ParamHolder> params, boolean includeViewParams)
<form action>
, or in <a href>
.params
- The parameters to be URL-encoded in the query string. Can be null
.includeViewParams
- Whether the view parameters of the current view should be included as well.java.lang.IllegalStateException
- When there is no view (i.e. when it is null
). This can happen if the
method is called at the wrong moment in the JSF lifecycle, e.g. before the view has been restored/created.ViewHandler.getBookmarkableURL(FacesContext, String, Map, boolean)
public static java.lang.String getBookmarkableURL(java.lang.String viewId, java.util.Collection<ParamHolder> params, boolean includeViewParams)
<form action>
, or in <a href>
.viewId
- The view ID to create the bookmarkable URL for.params
- The parameters to be URL-encoded in the query string. Can be null
.includeViewParams
- Whether the view parameters of the current view which are also declared in the target
view should be included as well. Note thus that this does not include the view parameters which are not declared
in the target view!ViewHandler.getBookmarkableURL(FacesContext, String, Map, boolean)
public static javax.faces.view.facelets.FaceletContext getFaceletContext()
Note that whenever you absolutely need this method to perform a general task, you might want to consider to submit a feature request to OmniFaces in order to add a new utility method which performs exactly this general task.
FaceletContext
public static <T> T getFaceletAttribute(java.lang.String name)
<ui:param>
which is been declared inside the Facelet file, or is been passed into the Facelet
file by e.g. an <ui:include>
.name
- The Facelet attribute name.java.lang.ClassCastException
- When T
is of wrong type.FaceletContext.getAttribute(String)
public static void setFaceletAttribute(java.lang.String name, java.lang.Object value)
<ui:param>
which is been declared inside the Facelet file, or is been passed into the Facelet
file by e.g. an <ui:include>
.name
- The Facelet attribute name.value
- The Facelet attribute value.FaceletContext.setAttribute(String, Object)
public static javax.servlet.http.HttpServletRequest getRequest()
Note that whenever you absolutely need this method to perform a general task, you might want to consider to submit a feature request to OmniFaces in order to add a new utility method which performs exactly this general task.
ExternalContext.getRequest()
public static boolean isAjaxRequest()
true
for an ajax request, false
for a non-ajax (synchronous) request.PartialViewContext.isAjaxRequest()
public static boolean isPostback()
true
for a postback, false
for a non-postback (GET) request.FacesContext.isPostback()
public static java.util.Map<java.lang.String,java.lang.String> getRequestParameterMap()
ExternalContext.getRequestParameterMap()
public static java.lang.String getRequestParameter(java.lang.String name)
name
- The HTTP request parameter name.ExternalContext.getRequestParameterMap()
public static java.util.Map<java.lang.String,java.lang.String[]> getRequestParameterValuesMap()
ExternalContext.getRequestParameterValuesMap()
public static java.lang.String[] getRequestParameterValues(java.lang.String name)
name
- The HTTP request parameter name.ExternalContext.getRequestParameterValuesMap()
public static java.util.Map<java.lang.String,java.lang.String> getRequestHeaderMap()
ExternalContext.getRequestHeaderMap()
public static java.lang.String getRequestHeader(java.lang.String name)
name
- The HTTP request header name.ExternalContext.getRequestHeaderMap()
public static java.util.Map<java.lang.String,java.lang.String[]> getRequestHeaderValuesMap()
ExternalContext.getRequestHeaderValuesMap()
public static java.lang.String[] getRequestHeaderValues(java.lang.String name)
name
- The HTTP request header name.ExternalContext.getRequestHeaderValuesMap()
public static java.lang.String getRequestContextPath()
ExternalContext.getRequestContextPath()
public static java.lang.String getRequestServletPath()
/faces/*
), then this returns
the whole prefix mapping (e.g. /faces
). If JSF is suffix mapped (e.g. *.xhtml
), then
this returns the whole part after the context path, with a leading slash.ExternalContext.getRequestServletPath()
public static java.lang.String getRequestPathInfo()
/faces/*
), then this returns the
whole part after the prefix mapping, with a leading slash. If JSF is suffix mapped (e.g. *.xhtml
),
then this returns null
.ExternalContext.getRequestPathInfo()
public static java.lang.String getRequestHostname()
ServletRequest.getServerName()
as its
outcome can be influenced by proxies.java.lang.IllegalArgumentException
- When the URL is malformed. This is however unexpected as the request would
otherwise not have hit the server at all.HttpServletRequest.getRequestURL()
public static java.lang.String getRequestBaseURL()
<base>
tag.HttpServletRequest.getRequestURL()
,
HttpServletRequest.getRequestURI()
,
HttpServletRequest.getContextPath()
public static java.lang.String getRequestDomainURL()
HttpServletRequest.getRequestURL()
,
HttpServletRequest.getRequestURI()
public static java.lang.String getRequestURL()
HttpServletRequest.getRequestURL()
public static java.lang.String getRequestURI()
HttpServletRequest.getRequestURI()
public static java.lang.String getRequestQueryString()
?
in the request URL as the
enduser sees in browser address bar.HttpServletRequest.getQueryString()
public static java.util.Map<java.lang.String,java.util.List<java.lang.String>> getRequestQueryStringMap()
getRequestParameterValuesMap()
, which contains both
the request URL (GET) parameters and and the request body (POST) parameters. This is ready for usage in among
others ViewHandler.getBookmarkableURL(FacesContext, String, Map, boolean)
.HttpServletRequest.getQueryString()
public static java.lang.String getRequestURLWithQueryString()
HttpServletRequest.getRequestURL()
,
HttpServletRequest.getQueryString()
public static java.lang.String getRequestURIWithQueryString()
HttpServletRequest.getRequestURI()
,
HttpServletRequest.getQueryString()
public static java.lang.String getRemoteAddr()
X-Forwarded-For
request header and if it's present, then return its first IP address, else just
return ServletRequest.getRemoteAddr()
unmodified.ServletRequest.getRemoteAddr()
public static javax.servlet.http.HttpServletResponse getResponse()
Note that whenever you absolutely need this method to perform a general task, you might want to consider to submit a feature request to OmniFaces in order to add a new utility method which performs exactly this general task.
ExternalContext.getResponse()
public static int getResponseBufferSize()
javax.faces.FACELETS_BUFFER_SIZE
context parameter is been set, then it's the context parameter value which will be returned. Otherwise it
returns the implementation independent default value, which is 1024 in Mojarra.ExternalContext.getResponseBufferSize()
public static java.lang.String getResponseCharacterEncoding()
ExternalContext.getResponseCharacterEncoding()
public static void setResponseStatus(int status)
HttpServletResponse
for
this. For example, Faces.setResponseStatus(HttpServletResponse.SC_BAD_REQUEST)
.status
- The HTTP status code to be set on the current response.public static void redirect(java.lang.String url, java.lang.String... paramValues) throws java.io.IOException
http://
,
https://
or /
, then the request context path will be prepended, otherwise it will be
the unmodified redirect URL. So, when redirecting to another page in the same web application, always specify the
full path from the context root on (which in turn does not need to start with /
).
You can use String.format(String, Object...)
placeholder %s
in the redirect URL to represent
placeholders for any request parameter values which needs to be URL-encoded. Here's a concrete example:
Faces.redirect("other.xhtml?foo=%s&bar=%s", foo, bar);
This method implicitly also calls Flash.setRedirect(boolean)
with true
so that any flash
scoped attributes will survive the redirect.
url
- The URL to redirect the current response to.paramValues
- The request parameter values which you'd like to put URL-encoded in the given URL.java.io.IOException
- Whenever something fails at I/O level. The caller should preferably not catch it, but just
redeclare it in the action method. The servletcontainer will handle it.java.lang.NullPointerException
- When url is null
.ExternalContext.redirect(String)
public static void redirectPermanent(java.lang.String url, java.lang.String... paramValues)
http://
,
https://
or /
, then the request context path will be prepended, otherwise it will be
the unmodified redirect URL. So, when redirecting to another page in the same web application, always specify the
full path from the context root on (which in turn does not need to start with /
).
You can use String.format(String, Object...)
placeholder %s
in the redirect URL to represent
placeholders for any request parameter values which needs to be URL-encoded. Here's a concrete example:
Faces.redirectPermanent("other.xhtml?foo=%s&bar=%s", foo, bar);
This method implicitly also calls Flash.setRedirect(boolean)
with true
so that any flash
scoped attributes will survive the redirect.
This method does by design not work on ajax requests. It is not possible to return a "permanent redirect" via JSF ajax XML response.
url
- The URL to redirect the current response to.paramValues
- The request parameter values which you'd like to put URL-encoded in the given URL.java.lang.NullPointerException
- When url is null
.ExternalContext.setResponseStatus(int)
,
ExternalContext.setResponseHeader(String, String)
public static void responseSendError(int status, java.lang.String message) throws java.io.IOException
<error-page>
whose <error-code>
matches the given status, or in a servlet
container specific default error page if there is none. The message will be available in the error page as a
request attribute with name javax.servlet.error.message
. The FacesContext.responseComplete()
will implicitly be called after sending the error.status
- The HTTP response status which is supposed to be in the range 4nn-5nn. You can use the constant
field values of HttpServletResponse
for this.message
- The message which is supposed to be available in the error page.java.io.IOException
- Whenever something fails at I/O level. The caller should preferably not catch it, but just
redeclare it in the action method. The servletcontainer will handle it.ExternalContext.responseSendError(int, String)
public static void addResponseHeader(java.lang.String name, java.lang.String value)
name
- The header name.value
- The header value.ExternalContext.addResponseHeader(String, String)
public static boolean isResponseCommitted()
true
if the response is already committed, otherwise false
.ExternalContext.isResponseCommitted()
public static void responseReset()
java.lang.IllegalStateException
- When the response is already committed.ExternalContext.responseReset()
public static void renderResponse()
FacesContext.renderResponse()
public static boolean isRenderResponse()
true
if we're currently in the render response phase. This explicitly checks the current
phase ID instead of FacesContext.getRenderResponse()
as the latter may unexpectedly return false during
a GET request when <f:viewParam>
is been used.true
if we're currently in the render response phase.FacesContext.getCurrentPhaseId()
public static void responseComplete()
FacesContext.responseComplete()
public static boolean isResponseComplete()
true
if the FacesContext.responseComplete()
has been called.true
if the FacesContext.responseComplete()
has been called.FacesContext.responseComplete()
public static void login(java.lang.String username, java.lang.String password) throws javax.servlet.ServletException
username
- The login username.password
- The login password.javax.servlet.ServletException
- When the login is invalid, or when container managed FORM based authentication is not
enabled.HttpServletRequest.login(String, String)
public static boolean authenticate() throws javax.servlet.ServletException, java.io.IOException
true
if the authentication was successful, otherwise false
.javax.servlet.ServletException
- When the authentication has failed. The caller is responsible for handling it.java.io.IOException
- Whenever something fails at I/O level. The caller should preferably not catch it, but just
redeclare it in the action method. The servletcontainer will handle it.HttpServletRequest.authenticate(HttpServletResponse)
public static void logout() throws javax.servlet.ServletException
invalidateSession()
instead. Note
that the user principal is still present in the response of the current request, it's therefore recommend to
send a redirect after logout()
or invalidateSession()
. You can use
redirect(String, String...)
for this.javax.servlet.ServletException
- When the logout has failed.HttpServletRequest.logout()
public static java.lang.String getRemoteUser()
ExternalContext.getRemoteUser()
public static boolean isUserInRole(java.lang.String role)
role
- The role to be checked on the currently logged-in user.true
if the currently logged-in user has the given role, otherwise false
.ExternalContext.isUserInRole(String)
public static java.lang.String getRequestCookie(java.lang.String name)
name
- The HTTP request cookie name.java.lang.UnsupportedOperationException
- If UTF-8 is not supported on this machine.ExternalContext.getRequestCookieMap()
public static void addResponseCookie(java.lang.String name, java.lang.String value, java.lang.String path, int maxAge)
name
- The cookie name.value
- The cookie value.path
- The cookie path. If this is /
, then the cookie is available in all pages of the webapp.
If this is /somespecificpath
, then the cookie is only available in pages under the specified path.maxAge
- The maximum age of the cookie, in seconds. If this is 0
, then the cookie will be
removed. Note that the name and path must be exactly the same as it was when the cookie was created. If this is
-1
then the cookie will become a session cookie and thus live as long as the established HTTP
session.java.lang.UnsupportedOperationException
- If UTF-8 is not supported on this machine.ExternalContext.addResponseCookie(String, String, Map)
public static void removeResponseCookie(java.lang.String name, java.lang.String path)
name
- The cookie name.path
- The cookie path.ExternalContext.addResponseCookie(String, String, Map)
public static javax.servlet.http.HttpSession getSession()
Note that whenever you absolutely need this method to perform a general task, you might want to consider to submit a feature request to OmniFaces in order to add a new utility method which performs exactly this general task.
ExternalContext.getSession(boolean)
public static javax.servlet.http.HttpSession getSession(boolean create)
create
argument is
true
, otherwise don't create one and return null
.
Note that whenever you absolutely need this method to perform a general task, you might want to consider to submit a feature request to OmniFaces in order to add a new utility method which performs exactly this general task.
ExternalContext.getSession(boolean)
public static java.lang.String getSessionId()
HttpSession.getId()
public static void invalidateSession()
ExternalContext.invalidateSession()
public static boolean hasSession()
true
if the HTTP session has already been created, otherwise false
.ExternalContext.getSession(boolean)
public static boolean isSessionNew()
false
when there is no means of a HTTP session.true
if the HTTP session has been created for the first time in the current request,
otherwise false
.ExternalContext.getSession(boolean)
,
HttpSession.isNew()
public static long getSessionCreationTime()
HttpSession.getCreationTime()
public static long getSessionLastAccessedTime()
HttpSession.getLastAccessedTime()
public static int getSessionMaxInactiveInterval()
HttpSession.getMaxInactiveInterval()
public static void setSessionMaxInactiveInterval(int seconds)
seconds
- The HTTP session timeout in seconds.HttpSession.setMaxInactiveInterval(int)
public static boolean hasSessionTimedOut()
true
if the HTTP session has been timed out for the current request, otherwise
false
.HttpServletRequest.getRequestedSessionId()
,
HttpServletRequest.isRequestedSessionIdValid()
public static javax.servlet.ServletContext getServletContext()
Note that whenever you absolutely need this method to perform a general task, you might want to consider to submit a feature request to OmniFaces in order to add a new utility method which performs exactly this general task.
ExternalContext.getContext()
public static java.util.Map<java.lang.String,java.lang.String> getInitParameterMap()
<context-param>
entries in in web.xml
.ExternalContext.getInitParameterMap()
public static java.lang.String getInitParameter(java.lang.String name)
<param-value>
of a
<context-param>
in web.xml
associated with the given
<param-name>
.name
- The application initialization parameter name.null
if
there is none.ExternalContext.getInitParameter(String)
public static java.lang.String getMimeType(java.lang.String name)
<mime-mapping>
entries in web.xml
. When the mime type is
unknown, then a default of application/octet-stream
will be returned.name
- The file name to return the mime type for.ExternalContext.getMimeType(String)
public static java.net.URL getResource(java.lang.String path) throws java.net.MalformedURLException
null
.path
- The application resource path to return an input stream for.java.net.MalformedURLException
ExternalContext.getResource(String)
public static java.io.InputStream getResourceAsStream(java.lang.String path)
null
.path
- The application resource path to return an input stream for.ExternalContext.getResourceAsStream(String)
public static java.util.Set<java.lang.String> getResourcePaths(java.lang.String path)
path
- The partial application resource path used to return matching resource paths.ExternalContext.getResourcePaths(String)
public static java.lang.String getRealPath(java.lang.String webContentPath)
/index.xhtml
) to an absolute disk file system path (e.g.
/path/to/server/work/folder/some.war/index.xhtml
) which can then be used in File
,
FileInputStream
, etc.
Note that this will return null
when the WAR is not expanded into the disk file system, but instead
into memory. If all you want is just an InputStream
of the web content resource, then better use
getResourceAsStream(String)
instead.
Also note that it wouldn't make sense to modify or create files in this location, as those changes would get lost anyway when the WAR is redeployed or even when the server is restarted. This is thus absolutely not a good location to store for example uploaded files.
webContentPath
- The web content path to be converted to an absolute disk file system path.public static javax.servlet.ServletRegistration getFacesServletRegistration(javax.servlet.ServletContext servletContext)
ServletRegistration
associated with the FacesServlet
.servletContext
- The context to get the ServletRegistration from.null
if the FacesServlet is not installed.public static java.util.Map<java.lang.String,java.lang.Object> getRequestMap()
ExternalContext.getRequestMap()
public static <T> T getRequestAttribute(java.lang.String name)
name
- The request scope attribute name.java.lang.ClassCastException
- When T
is of wrong type.ExternalContext.getRequestMap()
public static void setRequestAttribute(java.lang.String name, java.lang.Object value)
name
- The request scope attribute name.value
- The request scope attribute value.ExternalContext.getRequestMap()
public static <T> T removeRequestAttribute(java.lang.String name)
name
- The request scope attribute name.null
if
there is no such attribute.java.lang.ClassCastException
- When T
is of wrong type.ExternalContext.getRequestMap()
@Deprecated public static <T> T getRequestAttribute(javax.faces.context.FacesContext context, java.lang.String name)
FacesLocal.getRequestAttribute(FacesContext, String)
instead.public static javax.faces.context.Flash getFlash()
Flash
implements Map<String, Object>
, so you
can just treat it like a Map<String, Object>
.ExternalContext.getFlash()
public static <T> T getFlashAttribute(java.lang.String name)
name
- The flash scope attribute name.java.lang.ClassCastException
- When T
is of wrong type.ExternalContext.getFlash()
public static void setFlashAttribute(java.lang.String name, java.lang.Object value)
name
- The flash scope attribute name.value
- The flash scope attribute value.ExternalContext.getFlash()
public static <T> T removeFlashAttribute(java.lang.String name)
name
- The flash scope attribute name.null
if
there is no such attribute.java.lang.ClassCastException
- When T
is of wrong type.ExternalContext.getFlash()
public static java.util.Map<java.lang.String,java.lang.Object> getViewMap()
UIViewRoot.getViewMap()
public static <T> T getViewAttribute(java.lang.String name)
name
- The view scope attribute name.java.lang.ClassCastException
- When T
is of wrong type.UIViewRoot.getViewMap()
public static void setViewAttribute(java.lang.String name, java.lang.Object value)
name
- The view scope attribute name.value
- The view scope attribute value.UIViewRoot.getViewMap()
public static <T> T removeViewAttribute(java.lang.String name)
name
- The view scope attribute name.null
if
there is no such attribute.java.lang.ClassCastException
- When T
is of wrong type.UIViewRoot.getViewMap()
public static java.util.Map<java.lang.String,java.lang.Object> getSessionMap()
ExternalContext.getSessionMap()
public static <T> T getSessionAttribute(java.lang.String name)
name
- The session scope attribute name.java.lang.ClassCastException
- When T
is of wrong type.ExternalContext.getSessionMap()
public static void setSessionAttribute(java.lang.String name, java.lang.Object value)
name
- The session scope attribute name.value
- The session scope attribute value.ExternalContext.getSessionMap()
public static <T> T removeSessionAttribute(java.lang.String name)
name
- The session scope attribute name.null
if
there is no such attribute.java.lang.ClassCastException
- When T
is of wrong type.ExternalContext.getSessionMap()
public static java.util.Map<java.lang.String,java.lang.Object> getApplicationMap()
ExternalContext.getApplicationMap()
public static <T> T getApplicationAttribute(java.lang.String name)
name
- The application scope attribute name.java.lang.ClassCastException
- When T
is of wrong type.ExternalContext.getApplicationMap()
public static void setApplicationAttribute(java.lang.String name, java.lang.Object value)
name
- The application scope attribute name.value
- The application scope attribute value.ExternalContext.getApplicationMap()
public static <T> T removeApplicationAttribute(java.lang.String name)
name
- The application scope attribute name.null
if
there is no such attribute.java.lang.ClassCastException
- When T
is of wrong type.ExternalContext.getApplicationMap()
@Deprecated public static <T> T getApplicationAttribute(javax.faces.context.FacesContext context, java.lang.String name)
FacesLocal.getApplicationAttribute(FacesContext, String)
instead.public static void sendFile(java.io.File file, boolean attachment) throws java.io.IOException
FacesContext.responseComplete()
will implicitly be called
after successful streaming.file
- The file to be sent to the response.attachment
- Whether the file should be provided as attachment, or just inline.java.io.IOException
- Whenever something fails at I/O level. The caller should preferably not catch it, but just
redeclare it in the action method. The servletcontainer will handle it.public static void sendFile(byte[] content, java.lang.String filename, boolean attachment) throws java.io.IOException
FacesContext.responseComplete()
will
implicitly be called after successful streaming.content
- The file content as byte array.filename
- The file name which should appear in content disposition header.attachment
- Whether the file should be provided as attachment, or just inline.java.io.IOException
- Whenever something fails at I/O level. The caller should preferably not catch it, but just
redeclare it in the action method. The servletcontainer will handle it.public static void sendFile(java.io.InputStream content, java.lang.String filename, boolean attachment) throws java.io.IOException
InputStream.close()
will implicitly be called after streaming, regardless of whether an exception is
been thrown or not. The FacesContext.responseComplete()
will implicitly be called after successful
streaming.content
- The file content as input stream.filename
- The file name which should appear in content disposition header.attachment
- Whether the file should be provided as attachment, or just inline.java.io.IOException
- Whenever something fails at I/O level. The caller should preferably not catch it, but just
redeclare it in the action method. The servletcontainer will handle it.