My JSF Books/Videos My JSF Tutorials OmniFaces/JSF PPTs
JSF 2.3 Tutorial
JSF Caching Tutorial
JSF Navigation Tutorial
JSF Scopes Tutorial
JSF Page Author Beginner's Guide
OmniFaces 2.3 Tutorial Examples
OmniFaces 2.2 Tutorial Examples
JSF Events Tutorial
OmniFaces Callbacks Usages
JSF State Tutorial
JSF and Design Patterns
JSF 2.3 New Features (2.3-m04)
Introduction to OmniFaces
25+ Reasons to use OmniFaces in JSF
OmniFaces Validators
OmniFaces Converters
JSF Design Patterns
Mastering OmniFaces
Reusable and less-verbose JSF code

My JSF Resources ...

Java EE Guardian
Member of JCG Program
Member MVB DZone
Blog curated on ZEEF
OmniFaces is an utility library for JSF, including PrimeFaces, RichFaces, ICEfaces ...

.

.

.

.

.

.

.

.


[OmniFaces Utilities] - Find the right JSF OmniFaces 2 utilities methods/functions

Search on blog

Petition by Java EE Guardians

Twitter

duminică, 22 martie 2015

[OmniFaces utilities (2.0)] Get the HTTP header parameter map / HTTP header parameter values map


[OmniFaces utilities] The getRequestHeaderMap() method returns the HTTP request header map.
[OmniFaces utilities] The getRequestHeader() method returns the HTTP request header value associated with the given name.
[OmniFaces utilities] The getRequestHeaderValuesMap() method returns the HTTP request header values map.
[OmniFaces utilities] The getRequestHeaderValues() method returns the HTTP request header values associated with the given name.

Method Faces#getRequestHeaderMap() - returns the HTTP request header map:

See also: Faces#getContext()

Method Faces#getRequestHeader() - returns the HTTP request header value associated with the given name:

See also: Faces#getContext()

Method Faces#getRequestHeaderValuesMap() - returns the HTTP request header values map:


See also: Faces#getContext()

Method Faces#getRequestHeaderValues() - returns the HTTP request header values associated with the given name:

See also: Faces#getContext()
Usage:

Let's suppose a simple form, as below (the typed e-mail is rafa@rg.com; the typed password is rafa2015):

<h:form id="loginFormId">                                        
 E-mail: <h:inputText id="emailId" required="true" value="#{loginBean.email}"/>
 Password: <h:inputSecret id="passwordId" required="true" value="#{loginBean.password}"/>                                         
 <h:commandButton id="submitId" value="Login" action="#{loginBean.loginAction()}">           
  <o:param name="name" value="Rafael"/>
  <o:param name="surname" value="Nadal"/>
 </h:commandButton>   
</h:form>

The HTTP header parameter map can be accessed via Faces#getRequestHeaderMap(), as below:

import org.omnifaces.util.Faces;
...
Map<String, String> requestHeaderMap = Faces.getRequestHeaderMap();
for (Map.Entry<String, String> entry : requestHeaderMap.entrySet()) {
     System.out.println(entry.getKey() + "#" + entry.getValue());
     // do something with header parameters
}

For example, a possible output can be:

host#localhost:8080
user-agent#Mozilla/5.0 (Windows NT 6.1; WOW64; rv:36.0) Gecko/20100101 Firefox/36.0
accept#text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
accept-language#ro,ro_ro;q=0.8,en;q=0.7,en_us;q=0.5,fr;q=0.3,fr_fr;q=0.2
accept-encoding#gzip, deflate
referer#http://localhost:8080/TestFaces/
cookie#JSESSIONID=081041b0fbc9018a0c4e26155a7d
connection#keep-alive
content-type#application/x-www-form-urlencoded
content-length#186

If you need only the value of a single header parameter then you can use Faces#getRequestHeader(). For example, let's suppose that we are interested only in the accept header parameter value. Then we write this:

import org.omnifaces.util.Faces;
...
// the 'acceptValue' will be 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'
String acceptValue = Faces.getRequestHeader("accept");

If you need the "an immutable Map whose keys are the set of request header names included in the current request, and whose values (of type String[]) are all of the value for each header name returned by the underlying request. The returned Map must implement the entire contract for an unmodifiable map as described in the JavaDocs for java.util.Map. - Mojarra documentation", then you can use Faces#getRequestHeaderValuesMap(), as below:

import org.omnifaces.util.Faces;
...
Map<String, String[]> requestHeaderValuesMap = Faces.getRequestHeaderValuesMap();
for (Map.Entry<String, String[]> entry : requestHeaderValuesMap.entrySet()) {
     System.out.println(entry.getKey() + "#" + Arrays.toString(entry.getValue()));
     // do something with header parameters
}

For example, a possible output can be:

host#[localhost:8080]
user-agent#[Mozilla/5.0 (Windows NT 6.1; WOW64; rv:36.0) Gecko/20100101 Firefox/36.0]
accept#[text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8]
accept-language#[ro,ro_ro;q=0.8,en;q=0.7,en_us;q=0.5,fr;q=0.3,fr_fr;q=0.2]
accept-encoding#[gzip, deflate]
referer#[http://localhost:8080/TestFaces/]
cookie#[JSESSIONID=082ed91c5c389e2bb6e0a317ec67]
connection#[keep-alive]
content-type#[application/x-www-form-urlencoded]
content-length#[186]

If you need to returns the HTTP header parameter values associated with the given name, then you can use Faces#getRequestHeaderValues(). Let's try the same  accept header parameter (now, the values are placed in an array, String[]):

import org.omnifaces.util.Faces;
...
// the 'acceptValues' array will contain '[text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8]'
String[] acceptValues = Faces.getRequestHeaderValues("accept");

Niciun comentariu :

Trimiteți un comentariu

JSF BOOKS COLLECTION

Postări populare

OmniFaces/JSF Fans

Visitors Starting 4 September 2015

Locations of Site Visitors