Re: Jar for download from the page (same as prettyfaces).

Splash Forums Rewrite Users Jar for download from the page (same as prettyfaces). Re: Jar for download from the page (same as prettyfaces).

#22351

Tony Herstell
Participant

So as not to make it too boring; can you also post an example of how to get a “message” out from the ‘reWrite’ to show on the screen.

I also have my login on the landing page; so will have to think that one out when I implement your re-write.

I used the MultiPageMessageSupport that I believe I found on your site to solve the problem of messages not passing over pages, but can’t seem to get a “you are not logged in muppet” message to show on the screen.

Not working:

if (!passedLoginTrap && !bypassCheck) {
this.logger.info("Redirecting...");
FacesContext fc = this.facesContextBuilder.getFacesContext(httpServletRequest, httpServletResponse);
FacesMessage accessViolationMessage = new FacesMessage("Access Denied; User is not logged in", "Access Denied; User is not logged in!");
accessViolationMessage.setSeverity(FacesMessage.SEVERITY_ERROR);
fc.addMessage(null, accessViolationMessage);
httpServletResponse.sendRedirect(NOT_LOGGED_IN_URL);
} else {
filterChain.doFilter(servletRequest, servletResponse);
}
this.logger.info("<< =========== doFilter");

I am looking forward to re-write as this works but is ugly (I miss Seam 2):

@WebFilter(urlPatterns = "/*")
public class LoggedInFilter implements Filter {

private Logger logger = Logger.getLogger(LoggedInFilter.class.getName());

@Inject
Identity identity;

@Inject
FacesContextBuilder facesContextBuilder;

private List<String> allowedPagesWhenNotLoggedIn;
static final String NOT_LOGGED_IN_URL = "/entermyevents/";

@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;

boolean bypassCheck = false;
boolean passedLoginTrap = false;

this.logger.info(">> =========== doFilter");
if (this.identity.isLoggedIn()) {
this.logger.info("User: " + this.identity.getName() + "(ID:" + this.identity.getLoggedInUserId() + ")");
passedLoginTrap = true;
} else {
this.logger.info("User not logged in");
String pageHeadingFor = httpServletRequest.getRequestURI();
this.logger.info("heading to page: " + pageHeadingFor);
if (pageHeadingFor.contains("javax.faces.resource")) {
this.logger.info("It's a resource so ignore...");
bypassCheck = true;
} else if (pageHeadingFor.contains("img")) {
this.logger.info("It's a resource so ignore...");
bypassCheck = true;
} else {
LOGIN_PAGE_TRAP: for (String eachPage : this.allowedPagesWhenNotLoggedIn) {
if (pageHeadingFor.equalsIgnoreCase(eachPage)) {
this.logger.info("Passed Login Trap...");
passedLoginTrap = true;
break LOGIN_PAGE_TRAP;
}
}
}
}

if (!passedLoginTrap && !bypassCheck) {
this.logger.info("Redirecting...");
FacesContext fc = this.facesContextBuilder.getFacesContext(httpServletRequest, httpServletResponse);
FacesMessage accessViolationMessage = new FacesMessage("Access Denied; User is not logged in", "Access Denied; User is not logged in!");
accessViolationMessage.setSeverity(FacesMessage.SEVERITY_ERROR);
fc.addMessage(null, accessViolationMessage);
httpServletResponse.sendRedirect(NOT_LOGGED_IN_URL);
} else {
filterChain.doFilter(servletRequest, servletResponse);
}
this.logger.info("<< =========== doFilter");
}

@Override
public void init(FilterConfig arg0) throws ServletException {
this.logger.info(">> =========== init");
this.allowedPagesWhenNotLoggedIn = new ArrayList<String>();
this.allowedPagesWhenNotLoggedIn.add(NOT_LOGGED_IN_URL);
this.allowedPagesWhenNotLoggedIn.add("/entermyevents/users/register");
this.allowedPagesWhenNotLoggedIn.add("/entermyevents/error");
this.allowedPagesWhenNotLoggedIn.add("/entermyevents/viewExpired");
this.logger.info("<< =========== init");
}

@Override
public void destroy() {
this.logger.info(">> =========== destroy");
this.logger.info("<< =========== destroy");
}
}