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


joi, 11 decembrie 2014

Use OmniFaces to Buffer FacesServlet Output

Sometimes it can be useful to capture/buffer the response that comes from the javax.faces.webapp.FacesServlet. Per example, OmniFaces does this for implementing the caching mechanism. But, in this post we are not talking about OmniFaces Cache component, and about the org.omnifaces.filter.OnDemandResponseBufferFilter. This is an OmniFaces filter that can be configured to buffer a servlet output, in this case the FacesServlet output. First, you need to configure it in web.xml, as below (there is also a configuration using a boolean context parameter, named org.omnifaces.CACHE_INSTALL_BUFFER_FILTER):

Now, the OnDemandResponseBufferFilter  is ready for use! Below, you can see a simple phase listener that before the Render Response phase it starts buffering, and after Render Response phase, it just send to the log the buffer content:

package my.dissecting.omnifaces;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;
import org.omnifaces.component.output.cache.CacheInitializer;
import org.omnifaces.filter.OnDemandResponseBufferFilter;
import static org.omnifaces.filter.OnDemandResponseBufferFilter.BUFFERED_RESPONSE;
import org.omnifaces.servlet.BufferedHttpServletResponse;
import static org.omnifaces.util.Faces.getRequestAttribute;

public class BufferingPhaseListener implements PhaseListener {
    private static final Logger LOG = Logger.getLogger(BufferingPhaseListener.class.getName());

    private static final String ERROR_NO_BUFFERED_RESPONSE = String.format(
            "Check setting the '%s' context parameter or installing the '%s' filter manually.",
            CacheInitializer.CACHE_INSTALL_BUFFER_FILTER, OnDemandResponseBufferFilter.class
    private BufferedHttpServletResponse bufferedHttpServletResponse;

    public void afterPhase(PhaseEvent event) {
        try {
  "--------------------- Buffered code start ---------------------");
  "--------------------- Buffered code end -----------------------");
        } catch (IOException ex) {
                                            log(Level.SEVERE, null, ex);

    public void beforePhase(PhaseEvent event) {
        bufferedHttpServletResponse = getRequestAttribute(BUFFERED_RESPONSE);

        if (bufferedHttpServletResponse == null) {
            throw new IllegalStateException(ERROR_NO_BUFFERED_RESPONSE);

        // Start buffering the response from now on

    public PhaseId getPhaseId() {
        return PhaseId.RENDER_RESPONSE;


Once you pass the false value to the setPassThrough()  method the buffering starts. If you pass true, the buffering stops. The buffered content can be obtained as string via getBufferAsString() method, or as a byte array, via getBuffer() method.
Per example, if you have the below page:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns=""
        <title>My Page</title>       
        <h:outputStylesheet library="omnifaces" name="css/styles.css"/>
        <h:panelGrid columns="1" style="text-align: center;">
            <h:outputLink value="">
                <p>OmniFaces 2.0 rocks!</p>
            <o:graphicImage library="omnifaces" name="images/omnifaces20.png" dataURI="false"/>

Then the buffer content will be:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html>
<html xmlns="">
 <head id="j_idt2">
  <title>My Page</title>
  <link type="text/css" rel="stylesheet"    
         css/styles.css;jsessionid=97d1683b0cfc45944cd7a1cf7c4a?ln=omnifaces" />
  <table style="text-align: center;">
      <a href="">
       <p>OmniFaces 2.0 rocks!</p>
      <img src="/OmniFacesBufferedHttpServletResponse/faces/javax.faces.resource/
                alt="" />

So, if you are a fan of OmniFaces, probably you have the library already loaded in your project. Now, if this kind of task appears, you know what to do without losing time or install other libraries or filters.

Niciun comentariu :

Trimiteți un comentariu


Postări populare

Follow by Email

Visitors Starting 4 September 2015

Locations of Site Visitors