Re: Current http request in dynaview using pretty faces 3.2.0, seam 2.2.0, jboss 4.3

Splash Forums PrettyFaces Users Current http request in dynaview using pretty faces 3.2.0, seam 2.2.0, jboss 4.3 Re: Current http request in dynaview using pretty faces 3.2.0, seam 2.2.0, jboss 4.3

#21668

andyredhead
Participant

Hi,

Lincoln has been kind enough to post a response to my original email (thank you)

http://groups.google.com/group/prettyfaces-users/msg/2ca8ebd5820351a4

Unfortunately the aging version of Seam we are currently working with doesnt have @RequestScope.

I have a work-around which is to use a servlet filter to add the current request as a thread local variable which can be accessed by the dynaview resolving bean. I haven’t tested it much beyond clicking the url in a browser a couple of times but it seems to work :)

My hacky prototype code shown below.

Cheers, Andy

Filter:

public class RequestCaptureFilter implements Filter {

/** Standard logger. */
private static final Log LOG = LogFactory.getLog(RequestCaptureFilter.class);

@Override
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain filterChain) throws IOException, ServletException {
if (LOG.isDebugEnabled()) {
LOG.debug("doFilter - start");
}

String pathStr = ((HttpServletRequest) req).getServletPath();
String reqUri = ((HttpServletRequest) req).getRequestURI();
String contextPath = ((HttpServletRequest) req).getContextPath();

if (LOG.isDebugEnabled()) {
LOG.debug("servlet path is: " + pathStr + ", context path: " + contextPath + ", reqUri: " + reqUri);
}

try {

ThreadLocalRequest.set(req);

filterChain.doFilter(req, resp);

} catch (Exception e) {
LOG.info("doFilter - exception from further down filter chain", e);
} finally {
try {
ThreadLocalRequest.unset();
} catch (Exception e) {
LOG.warn("doFilter - problem unsetting thread local request", e);
}
}

if (LOG.isDebugEnabled()) {
LOG.debug("doFilter - finish");
}
}

}

Thread Local accessor class:

public class ThreadLocalRequest {

private static final ThreadLocal<ServletRequest> userThreadLocal = new ThreadLocal<ServletRequest>();

public static void set(ServletRequest req) {
userThreadLocal.set(req);
}

public static void unset() {
userThreadLocal.remove();
}

public static ServletRequest get() {
return userThreadLocal.get();
}

}

Dynaview resolver bean:

@Name(value = "AppUrlDynaview")
@Scope(ScopeType.APPLICATION)
@Startup(depends={"org.jboss.seam.navigation.pages"})
public class AppUrlDynaview {

@Logger
Log _log;

@Create
public void create() {
_log.debug("create - hello");
}

String _theUrl;

public String getViewPath() {

_log.info("getViewPath - start");

ServletRequest servletReq = (HttpServletRequest) ThreadLocalRequest.get();

if (servletReq != null) {

_log.info("getViewPath - non null thread local request", servletReq);

} else {
_log.warn("getViewPath - null request in ThreadLocalRequest");
}

return "/home.seam";
}

public String getTheUrl() {
return _theUrl;
}

public void setTheUrl(String urlStr) {
_theUrl = urlStr;
}

}