Monday, December 31, 2012

JSF 2.x Tip of the Day: Dynamic <ui:include/> with <f:viewParam/>

Introduction

I encountered a situation the other day where I need to include a JSF page based on a parameter passed in via a <f:viewParam>. This seems like a very reasonable kind of idea. I expected to be able to make the <ui:include/> use a dynamically set src parameter using EL from the <f:viewParam>. This is where I went wrong (the idea was good...). The <ui:include/> is a tag handler. It is evaluated on Restore View Phase. The value processing does not take place until the Apply Request Values Phase. This presents a problem since the EL binding for my <ui:include/> is based on the <f:viewParam> which is a UIInput object. So we are out of phase. The page will display the results, but all the AJAX will not work.

A Solution

A solution to the problem involves using static <ui:include/> elements wrapped in <ui:fragment/> elements. This allows the tag handler to resolve the page to include, and then the fragment determines (via late binding) whether to display the page based on the <f:viewParam> passed. The page is supported by a @ViewScoped bean to keep the page in scope.

Code

Here is an example of how to do it.

Conclusion

I have put the complete NetBeans 7.2.1 project using Mojarra on Bitbucket. The project uses Apache Maven, and was tested using GlassFish 3.1.2. Here is the complete project: dynamic-include

Creating and Implementing An HttpSessionWrapper

Introduction

I am faced with a problem currently where I have an application that was written before the J2EE specification was written. The application has its own application server, session, and state management. I am in the process of modernizing this application and transforming it into a standard Java EE application. However, there is a lot of code over 1.5m LOC. That makes this process very intensive.

One of the issues I have been wrestling with is session management. As I previously noted, the application has its own session and state management mechanisms. The session object is not too vastly different from a standard HttpSession object. This has given rise to an idea that I could wrap a standard HttpSession object with a decorator similar to HttpServletRequestWrapper and perhaps a implementation of the old session mechanism.

Currently there is no HttpSessionWrapper like HttpServletRequestWrapper, or HttpServletResponseWrapper so I wrote my own. It was simple to do and I hope others who may need a HttpSessionWrapper will use it. Once I wrote the wrapper, implementing it was the harder part.

Implementation

Implementation requires a little knowledge of how HTTP requests are handled by servlets, and request and response handling in general. The most important thing to remember is: the HttpSession is created from the HttpServletRequest object. This important fact can help determine how to implement our HttpSessionWrapper. I think that the easiest mechanism is to use an HttpServletRequestWrapper in a @WebFilter. Filters are the first items that a request will pass through to reach the servlet, and the last thing it will pass through in the response.

If you use this mechanism, for implementing your HttpSessionWrapper, you must ensure that this is the first filter that the request will pass through. If it is the only filter, that should be very easy to implement. If you are using multiple filters with annotations, you will need to use a web.xml file to place them in the order you want them applied.

The example code I have below takes advantage of the fact that I can use a filter and that once I pass in my HttpServletRequestWrapper with its modified getSession() methods that return my HttpSessionWrapper. The HttpSessionWrapper can be downloaded here: session-wrapper

Code

The code below includes the HttpSessionWrapper and an example implementation of how to use it.

HttpSessionWrapper.java



HttpSessionWrapperImpl.java



HttpServletRequestWrapperImpl.java



WrappingFilter.java



Conclusion

Implementing a HttpSessionWrapper can be done very easily with a little thought, and perhaps a good blog article on it. The session-wrapper and session-wrapper-example code is available on BitBucket as Mercurial projects.. The projects were developed using NetBeans 7.2.1 and Apache Maven and was tested using Glassfish 3.1.2.

Thursday, December 27, 2012

Tomcat 7: Custom Valve

I was asked today by a colleague for some help on creating a valve for use with Apache Tomcat 7. I had never done one before, so I thought I would give it a quick try. +Craig McClanahan  had done a great job on creating a simple mechanism for creating a Valve with a lot of the heavy lifting done by ValveBase.

Creating your own valve is really simple using NetBeans 7.2.1 along with Apache Maven.

  1. Create a Maven Java Application.
  2. Add the following dependency:
  3. Create your Java class and extend it from ValveBase.
  4. Implement invoke(Request request, Response response)
  5. Build your library (.jar) file
  6. Install the library in the ${tomcat.home}/lib directory.
  7. Configure server.xml to use your new Valve. For example:
  8. Start the server to see your new valve in action
Here is my simple example.

ProcessingValve.java

Here is the output...
INFO: Server startup in 1442 ms
Dec 27, 2012 5:54:01 PM com.bluelotussoftware.tomcat.ProcessingValve invoke
INFO: Header --> host Value --> localhost:8080
Dec 27, 2012 5:54:01 PM com.bluelotussoftware.tomcat.ProcessingValve invoke
INFO: Header --> host Value --> localhost:8080
Dec 27, 2012 5:54:01 PM com.bluelotussoftware.tomcat.ProcessingValve invoke
INFO: Header --> host Value --> localhost:8080
Dec 27, 2012 5:54:01 PM com.bluelotussoftware.tomcat.ProcessingValve invoke
INFO: Header --> host Value --> localhost:8080

As you can see it was very easy to implement. Here is the code for my example: valve-example.zip

The source can also be found on Bitbucket: valve-example

Popular Posts