The CDI
annotation Cookie is available starting with OmniFaces 2.1 and
it allows you to inject a HTTP request cookie value from the current JSF
context in a CDI managed bean. It's basically like:
@ManagedProperty("#{cookie.cookieName.value}")
private
String cookieName;
in a
"plain old" JSF managed bean. You can use this as:
// By default
the name of the cookie is taken from the name of the variable
// into which injection takes place. The example below injects the cookie with name foo.
// into which injection takes place. The example below injects the cookie with name foo.
@Inject
@Cookie
private String
foo;
// The name
can be optionally specified via the name attribute.
// The example below injects the cookie with name foo into a variable named bar.
// The example below injects the cookie with name foo into a variable named bar.
@Inject
@Cookie(name="foo")
private
String bar;
The
implementation of this annotation can be a source of inspiration for creating
new such annotations. The Cookie annotation is declared in org.omnifaces.cdi.Cookie as:
@Qualifier
@Retention(RUNTIME)
@Target({
TYPE, METHOD, FIELD, PARAMETER })
public
@interface Cookie {
@Nonbinding String
name() default "";
}
Further,
the producer for injecting a JSF request cookie as defined by the above Cookie
annotation is defined in org.omnifaces.cdi.cookie.RequestCookieProducer:
public class
RequestCookieProducer {
@SuppressWarnings("unused") //
Workaround for OpenWebBeans not properly passing it as produce() method
argument.
@Inject
private InjectionPoint injectionPoint;
@Produces
@Cookie
public String produce(InjectionPoint
injectionPoint) {
Cookie cookie = getQualifier(injectionPoint,
Cookie.class);
String name = cookie.name().isEmpty() ?
injectionPoint.getMember().getName() : cookie.name();
return getRequestCookie(name);
}
}
The getQualifier() method
returns the qualifier annotation of the given qualifier class from the given
injection point and you can read further details here, where you can see an
annotation for injecting the value of an application initialization parameter
(declared in web.xml via <context-param>).
Niciun comentariu :
Trimiteți un comentariu