logo       

Re: JSF PhaseListener Vs Servlet Filter: msg#00149

java.facelets.user

Subject: Re: JSF PhaseListener Vs Servlet Filter

Bansilal Haudakari wrote:

We use a Homegrown security application
which on successful authentication returns the RequestHeader Variables i.e. employee Id etc.
We use JSF Spring Hibernate
Wondering whether i should JSF PhaseListener OR Servlet Filter to retrieve RequestHeader Variables. This is what i am doing currently not sure its best practice though. Any suggestions/pointers highly appreciated

Well, the FacesContext isn't available to a Servlet Filter, so unless you want to do some refactoring of your code I'd stick with what you've got.

If you are listening on the RESTORE_VIEW phase, I don't suppose a phase listener is much different from a Servlet Filter which has been mapped to the Faces Servlet (except that the FacesContext is available).

A Servlet Filter can be useful for intercepting a request before the Faces Servlet is invoked, for example doing URL mapping, sending HTTP redirects, customizing ServletRequest/Response objects etc etc.

* *
*public* *class* LoginPhaseListener *implements* PhaseListener
{
**
* *
*public* *void* afterPhase(PhaseEvent pe)
{
FacesContext facesContext = pe.getFacesContext();
String viewId = pe.getFacesContext().getViewRoot().getViewId();
*if* (viewId.endsWith(".xhtml")) {
String managedBeanName = getManagedBeanNameFromView(viewId);
Object object = facesContext.getApplication().createValueBinding("#{" + managedBeanName + "}").getValue(facesContext);
*if* (object == *null*)
/logger/.error("OnPageLoad cannot be executed, no such managed bean:"+ managedBeanName);
*else* {
Login loginBean = (Login) object;
loginBean.onPageLoad();
}
}
}
*public* String getManagedBeanNameFromView(String viewId) {
String pageName = viewId.substring(1, viewId.length() - 6);
System./out/.println("Name="+StringUtils./capitalize/(pageName)+"Bean");
*return* pageName+"Bean";
}
*_JSF Backing Bean:_*
* *
*public* *class* LoginBean *implements* Login {
**
* *
*public* *void* onPageLoad() {
//System.out.println("***Inside onPageLoad******");
Map requestHeaderMap =
FacesContext./getCurrentInstan ce/().getExternalContext().getRequestHeaderMap();
String empID = (String) requestHeaderMap.get("EmployeeID");
//Retrieve UserId and User roles from database and populate it with POJO
UserInfo userInfo //pojo
userInfo.setUserId(userId);
userInfo.setUserRoles(userRoles);
/* Store in Session so as to make it available to Spring Layer */
HttpSession userSession =
(HttpSession) FacesContext./getCurrentInstance/().getExternalContext()
.getSession(*true*);
userSession.setAttribute("userInfo", userInfo);
}
}
I am also using Servlet Filter to retrieve Session object
* *
*public* *void* doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) *throws* IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
// grab the current value from the session
UserInfo userInfo = (UserInfo) request.getSession().getAttribute("userInfo");
// Update the user info bean holder for the current thread.
UserInfoHolder./setUserInfo/(userInfo); // ThreadLocal object
// call the chain
//System.out.println("In Security Filter");
chain.doFilter(req,resp);
// threadLocal no longer needed (will be recreated next time through the filter)
//UserInfoHolder.setUserInfo(null);
}
&n bsp;
Now i am thinking instead of using PhaseListener cant i just use the Servelt Filter to retrieve the RequestHeader variables, make connection to database instead of using HttpSession
Not sure if its best practice though & also how to retrieve RequestHeader variables in Servlet Filter
Regards
Bansi

------------------------------------------------------------------------
Be a better Globetrotter. Get better travel answers <http://us.rd.yahoo.com/evt=48254/*http://answers.yahoo.com/dir/_ylc=X3oDMTI5MGx2aThyBF9TAzIxMTU1MDAzNTIEX3MDMzk2NTQ1MTAzBHNlYwNCQUJwaWxsYXJfTklfMzYwBHNsawNQcm9kdWN0X3F1ZXN0aW9uX3BhZ2U-?link=list&sid=396545469>from someone who knows.
Yahoo! Answers - Check it out.


--
----------------------------------------
Ninth Avenue Software
p: +61 7 3137 1351 (UTC +10)
f: +61 7 3102 9141
w: http://www.ninthavenue.com.au
e: info@xxxxxxxxxxxxxxxxxx
----------------------------------------


<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise