2

I have a ServletFilter that I want to use to put my app in maintenance mode by forwarding and I even tried including a facelet page but either way I can not get the facelet page to include the resources (images, css)

I know of this question Browser can't access/find relative resources like CSS, images and links when calling a Servlet which forwards to a JSP , but that answer is using a jsp page and doesn't use h:head h:body, so the things I have tried from that post haven't gotten it to work.

public class OfflineFilter implements Filter {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
    LocalTime now = LocalTime.now();
    LocalTime start = LocalTime.parse("06:45:00");
    LocalTime stop = LocalTime.parse("07:00:00");

    // Want to go into maintenance mode every morning between 6:45 and 7:00 am
        if (now.isAfter(start) && now.isBefore(stop)) {
        // ((HttpServletResponse) response).sendRedirect(((HttpServletRequest) request).getContextPath() + "/maintenance.xhtml");
           request.getRequestDispatcher("maintenance.xhtml").forward(request, response);
        // request.getRequestDispatcher("maintenance.xhtml").include(request, response);
        } else {
           chain.doFilter(request, response);
        }
    }
}

maintenance.xhtml

<h:head>
    <title>Maintenance</title>
    <h:outputStylesheet name="#{pageContext.request.contextPath}/css/screen.css" />
</h:head>

<h:body>
    <h:outputStylesheet name="#{pageContext.request.contextPath}/css/screen.css" />
    test 0<img src="/AppName/resources/gfx/bug.png" />
    test 1<h:graphicImage styleClass="bug_logo" name="gfx/bug.png" />
    test 2<h:graphicImage styleClass="bug_logo" name="/gfx/bug.png" />
    test 3<h:graphicImage styleClass="bug_logo" name="resources/gfx/bug.png" />
    test 4<h:graphicImage styleClass="bug_logo" name="#{pageContext.request.contextPath}/gfx/bug.png" />
    <p:clock pattern="HH:mm:ss a" mode="server" />

even using just plain html I couldn't get things to work.

Also, I don't even think a Primefaces stylesheet is loaded either for my clock widget.

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui" xmlns:o="http://omnifaces.org/ui"
xmlns:of="http://omnifaces.org/functions">

Edit

looking at the pages source I see <img src="/AppName/javax.faces.resource/gfx/bug.png.xhtml"

But my filter is matching *.xhtml Origianlly I was matching url /* changing to *.xhtml fixed things in plain html page but still can't get a facelet to work.

<filter-mapping>
    <filter-name>OfflineFilter</filter-name>
    <url-pattern>*.xhtml</url-pattern>
</filter-mapping>

So there is the problem, <h:graphicImage is tacking on the .xhtml to all my resources and they are getting excluded.

Community
  • 1
  • 1
jeff
  • 3,618
  • 9
  • 48
  • 101
  • If you just want to block JSF pages, a phaselistener might be an idea. Related: http://stackoverflow.com/questions/15704581/redirect-to-page-using-jsf-phaselistener – Jasper de Vries Jan 19 '17 at 15:52
  • Small off-topic remark: custom servlet filters have nothing to do with jsf, so a 'jsf servletfilter' does not exist. And the filter itself does not seem to have to access the resources etc... And what is the path your custom maintenance page tries to use to access the resources? what does the 404 look like and what does the path look like when it does work. – Kukeltje Jan 19 '17 at 16:05
  • @Kukeltje I'm not getting a 404, the page loads but with image placeholders and no css formatting applied. The path of the maintenance page is at the same level as all other pages. I have also modified my title, hopefully it is more appropriate – jeff Jan 19 '17 at 16:12
  • No 404 on the resources? That is weird, then there most be some 'UNABLE_TO_LOAD_RESOURCE' like string in the location of where the resources would be loaded from the xhtml client side. – Kukeltje Jan 19 '17 at 16:15
  • I have experienced the 'UNABLE_TO_LOAD_RESOURCE' when view the src of a webpage, but not this time. If I take my OffLineFilter out of the equation the maintenance.xml page loaded fine when browsing directly to it. The Primefaces clock displayed and all css formatting and images were there. – jeff Jan 19 '17 at 16:18
  • How does the 'view-source' of the maintenance page look like when the filter is used? Are the jsf tags rendered or visible as is? – Kukeltje Jan 19 '17 at 17:30
  • rendered as We are importing records. Please Check back! – jeff Jan 19 '17 at 17:38
  • why would you say that the .xhtml is not parsed? The – jeff Jan 19 '17 at 18:51
  • 1
    Oh sorry, I misread that the ` – Kukeltje Jan 19 '17 at 18:52
  • Use the `value` attributes of the `h:outputStylesheet` and `h:graphicImage`. And does the `p:clock` work now? – Kukeltje Jan 19 '17 at 19:33

1 Answers1

4

Remember to exclude resources!!

However, I'd go for a redirect:

private static final String MAINTENANCE_PAGE = "/maintenance.jsf";

@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException
{
    HttpServletRequest request = (HttpServletRequest) req;
    HttpServletResponse response = (HttpServletResponse) res;

    String servletPath = request.getServletPath();

    LocalTime now = LocalTime.now();
    LocalTime start = LocalTime.parse("06:45:00");
    LocalTime stop = LocalTime.parse("07:00:00");

    // Want to go into maintenance mode every morning between 6:45 and 7:00 am
    if(now.isAfter(start) && now.isBefore(stop) && !servletPath.startsWith(MAINTENANCE_PAGE) && !servletPath.startsWith(ResourceHandler.RESOURCE_IDENTIFIER))
    {
        response.sendRedirect(request.getContextPath() + MAINTENANCE_PAGE);
        return;
    }

    chain.doFilter(req, res);
}

Note that MAINTENANCE_PAGE should be addressed in client side mode; in other words, you're before Faces Servlet, so be aware of its mapping (/faces/* or *.jsf or whatever).

Michele Mariotti
  • 7,372
  • 5
  • 41
  • 73