Thursday, April 25, 2013

JSF Tip of the Day: Reading Authorization Header in JSF

After I did the JAX-RS Tip of the Day today, I wondered about reading the authorization header from JSF. The technique is the same as the JAX-RS version, but the methods are different depending on what is available to the JSF application. The JAX-RS Base64 class is not part of the web profile in Java EE 6. It will be part of Java EE 7 so you could use it. I chose to add the comments in the code below, but decided that I would use the com.sun.misc.Base64Decoder which is currently available in Java SE 6 and 7.
UPDATE: I got a suggestion on Google+ from +Thomas Darimont who mentioned using DatatypeConverter.parseBase64Binary() from the Java API for XML Binding (JAXB). I confirmed that it is available in Java EE 5 and 6. It is also in the Web Profile in Java EE 6.

JAX-RS Tip of the Day: How Do I Read the Authorization Header?

I was looking for a simple method to read the HttpHeaders to determine the username and password for a request. I didn't find a quick answer so I wrote a simple utility method that will return the Base 64 decoded values from the authorization header.

Thursday, April 11, 2013

JSF 2.x Tip of the Day: RichFaces 4.3.x Charts in JSF using JSFlot, JFreeCharts, and PrimeFaces

Sexy PrimeFaces Charts in RichFaces

Introduction

I was asked to look at various charting solutions for JSF. Specifically, I was asked for some choices for use with RichFaces 4.3.1. I knew that PrimeFaces has really good chart components already so I knew it might be kind of fun to integrate them if possible. I was also aware of JFreeChart which will work, but produces some... rather ugly charts. Finally, someone had suggested that I look at jsflot. This was a very promising solution. This project is a proof of concept which demonstrates integration between projects, and technologies.

Requirements

  • jsflot
  • NetBeans This is required for the sample database, although you could create your own data.
The jsflot framework is currently not Mavenized, so if you are going to run my example code, you will need to download it from the link above, and install it in Maven using the command below.

Code

The code for this NetBeans Maven project can be found on BitBucket here: chart-demo

This is the bean that provides the data models for the charting solutions. There are a number of chart demos in the project. The two charts above are just examples.

ChartBean.java

Wednesday, April 10, 2013

JSF 2.x Tip of the Day: Post Exception Back Button

Introduction

This is hopefully one simple example of how to make a "back" button when an exception occurs, and sends you to an exception page. The question becomes "How do I get back to the page where the exception occurred?" The navigation back to the offending page is possible, but you want to make sure that you handle the exception, or you may get into a cycle.

A solution was suggested to me by my friend Markus Eisele using an ExceptionHandler in JSF. I had used exception handlers in the past, and thought that it was a simple and elegant idea. The code I am providing below DOES NOT handle the exception. This is specific to your implementation. This is just a handler that sits on top of your exception hierarchy, and provides a convenient mechanism to navigate back.

Code

The example code for the NetBeans Maven project can be downloaded from BitBucket here: exception-handler-navigation-button

You will need to register the factory below in the faces-config.xml file.

GeneralExceptionHandlerFactory.java


GeneralExceptionHandler.java


Usage

Here is an example of how to use it from an exception page.

Tuesday, April 09, 2013

JSF 2.x Tip of the Day: PrimeFaces Chart Coding Exam

PrimeFaces Pie Chart

Background

I have been asked to interview a number of engineers over the last couple of years. We were interested in hiring an engineer with a background in JSF specifically PrimeFaces, Java EE 6, and NetBeans.

Basically I wanted someone...like me.

I came into work one day, and I was told that I needed to interview someone in about an hour about a position. The person claimed experience in all of the aforementioned technologies. I needed to come up with a coding exam idea for them. Tick tock tick tock.

I decided to create a NetBeans project using the sample Java DB database, JPA, EJB facades, and a JSF front end using PrimeFaces with a pie chart. The whole process took about 7 minutes to create. When the engineer arrived, I showed him the result and said "I want to see the same results", and explained that I used the sample database from NetBeans.

The engineer was successful and created something similar in about 30 minutes, and additionally showed me up by adding percentage tags to the chart. We hired him after the interview. I am glad to say he is an integral part of our team today.

Coding Exam

What do I want?

Please create a JSF application using PrimeFaces that displays a pie chart using data provided. The application must be developed using NetBeans and Java EE 6 technologies. The example data is provided using the sample Java DB database in NetBeans  We would like to have a pie chart that shows sales totals by customer as a percentage of all sales. You have 1 hour to complete the code, and will be required to explain your design decisions, and results.

Note: I just showed them the pie chart that was generated in the browser and not the actual code. I don't want to sway the candidates decision making, or give them hints on how to solve the problem.

Here is the code for the project: PrimeFacesCodingExam

Conclusion

Even if you don't use this in a coding exam, it is a good example of combining Java EE technologies using NetBeans  and PrimeFaces.

Saturday, April 06, 2013

Java EE @WebFilter Filters

I got an email from a developer a couple of days ago that was complaining that the filters he implemented were not executing on the url patterns he had set. The email was not explicit enough for me to determine if the ordering was an issue too. I created a simple project that shows how to use @WebFilter (Servlet Filters). I was going to dispose of the code, but I thought it might have some redeeming qualities for those who may need a working example.

I have commented out the @WebFilter annotations so that the web.xml controls the ordering. If you uncomment the @WebFilter annotations, you will need to comment the lines in the web.xml. Doing so will produce the same issues reported to me about execution and ordering.

Here is the NetBeans project using Apache Maven: zsolt-filter-example. I didn't want to put in a repo since it was not really a planned project.

JSF 2.x Tip of the Day: JSF Expression Language (EL) Keywords and Implicit Objects

A friend of mine asked me if there was a list of reserved words in EL and JSF. He had previously looked for it, and after some Google searching I didn't find a comprehensive list anywhere. I decided to create one for him and anyone else who may need it. If you are aware of any other keywords, please post a comment and I will add them to the listings.

Expression Language (EL) Reserved Keywords
Value Description
and Logical operator alternative to (&&)
false Boolean literal
le Relation operator less than or equal to alternative to (<=)
not Logical operator reverse alternative to (!)
div Arithmetic operator division alternative to (/)
ge Relational operator greater or equal to alternative to (>=)
lt Relational operator less than alternative to (<)
null Null literal
empty The empty operator is a prefix operation that can be used to determine whether a value is null or empty.
gt Relational operator greater than alternative to (>)
mod Arithmetic operator modulo alternative to (%)
or Logical operator alternative to (||)
eq Logical operator alternative to (==)
instanceof Java Keyword to do a Class comparison between Objects
ne Relational operator not equal alternative to (!=)
true Boolean literal


JSF 2.x Expression Language (EL) Implicit Objects
Value Description
application This provides user access to the ApplicationContext implementation of ServletContext that represents a web application's execution environment.
Note: This is not related to the JSF Application object.
applicationScope Maps application-scoped variable names to their values.
cc Implicit EL object that provides access to the JSF Composite Component. cc refers to the top level composite component processed at the time of evaluation
component Implicit EL object representing javax.faces.component.UIComponentfor the current component.
cookie Maps a cookie name to a single cookie.
facesContext The FacesContext instance for the current request.
flash Provides user access to the EL implicit javax.faces.context.Flash object. It may additional obtained via #{facesContext.externalContext.flash}. The implementation must ensure that the flash is usable from both JSP and from Facelets for JSF 2.
header Maps a request header name to a single value.
headerValues Maps a request header name to an array of values.
initParam Maps a context initialization parameter name to a single value.
param Maps a request parameter name to a single value.
paramValues Maps a request parameter name to an array of values.
request EL implicit object for current request.
requestScope Maps request-scoped variable names to their values.
resource EL implicit object for javax.faces.application.ResourceHandler.
session Provides EL access to the current HttpSession object.
sessionScope Maps session-scoped variable names to their values.
view Provides access to the javax.faces.component.UIViewRoot for the current instance.
viewScope Maps view-scoped variable names to their values.

Tuesday, April 02, 2013

JSF 2.x Tip of the Day: RichFaces <rich:tree /> Component Examples

Custom TreeModel and TreeNode
I have some code that I originally wrote for a proof of concept for doing some RichFaces <rich:tree /> examples. The modified code in these examples demonstrate how to use custom TreeNode, and TreeDataModel implementations to make really nice and functional trees. The code for my generic TypedTreeNode and TreeNodesSequenceKeyModel are fully functional and can be used out of the box as the basis of your own tree implementations.

The examples include the model provided by RichFaces, my custom model (TreeNodesSequenceKeyModel), and a custom implementation of a node. The custom node is generic so you can pass in any object you like. I chose to use text, but you could use a more complex object.

The custom model, custom node, and tree are shown in the image on the right. This also has events being shown when a node is selected, or toggled.

The project was developed using NetBeans along with Apache Maven.

The project can be found on GitHub here: richfaces-tree

Note: The project has been moved to GitHub and has a couple of release versions.
  • 1.0 - Initial Release using Java EE 6
  • 1.2 - Updated functionality using Java EE 6
  • 2.0 - Updated to RichFaces 4.5.14.Final and Java EE 7.


Code

TypedTreeNode.java



TreeNodesSequenceKeyModel.java


Monday, April 01, 2013

Determining What Classes are Loaded by ClassLoaders

I saw a question posted a couple of months ago on stackoverflow, or a forum. I can't remember where I saw it actually. The question was how do I determine what classes are currently loaded by the ClassLoader when an application is loaded and running. A number of folks posted various solutions like using java -verbose which are of limited help. Another solution was to get using something like ClassLoader.getSystemClassLoader();. The latter looks very promising, but is wrong. I knew that there are a number of classes that are loaded that this would not display.

Ssssh... I will show you how I know.

The problem and solution is surprisingly non-trivial. I thought I would come up with a solution like the one above in 5 minutes. I did come up with one above in about that much time. It turns out to be incorrect.

The solution is to use a Java agent to instrument the JVM and see what it is loading. I am sure a number of you have seen the -javaagent:[=] flag for the VM and wondered what is that for. I am going to show you.

First some results:

all length -> 815
system length -> 163
appLoader length -> 163
classes size -> 61

The first value all indicates all of the classes loaded by the JVM. That is a lot of classes. This is via an Instrumentation agent.

The second value system indicates all of the classes loaded by the System ClassLoader. This is significantly less than loaded by the JVM. This is via an Instrumentation agent.

The third value is the appLoader which is the application classloader. It matches the System, but this may not always be the case. This is via an Instrumentation agent.

Finally, the last value classes is what you get from the ClassLoader without instrumentation. It is a paltry amount of the total classes loaded.

So which one is right? Good question... Here is an answer only a parent, or teacher can give. "It depends."

If I am looking at everything being loaded to check for something forensically I would use the 815 and look at what these classes are and where they came from. If I am checking which classes are loaded to help with reflection, I would look at the 61.

If you have read this far, then you want the code to look at. I have split it into a couple of NetBeans Maven projects hosted on BitBucket using Mercurial.


Code

InstrumentationAgent.java


AgentLoader.java


App.java


Note: I had to put the tools.jar in my Maven repository to make it easy to add as a library.

References

We can not achieve success alone. It is on the shoulders of giants that we see further. I used an example from Dhruba Bandopadhyay to figure out some of the instrumentation process.

Popular Posts