@Test void doFilterActionDispatcherIsTrue() throws Exception { ServletRequest request = mock(HttpServletRequest.class); ServletResponse response = mock(ServletResponse.class); FilterChain chain = mock(FilterChain.class); when(request.getAttribute(ActionFilter.class.getName() + ".actionDispatched")).thenReturn("true"); filter.doFilter(request, response, chain); verify(request, never()).getParameterValues("xaction"); verify(chain).doFilter(request, response); }
@SuppressWarnings("unchecked") @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { if (request instanceof HttpServletRequest && !Boolean.parseBoolean((String) request.getAttribute(ATTRIBUTE_ACTION_DISPATCHED))) { HttpServletRequest hrequest = (HttpServletRequest) request; Enumeration<String> parameterNames = hrequest.getParameterNames(); while (parameterNames.hasMoreElements()) { String parameter = parameterNames.nextElement(); String[] xactions = request.getParameterValues("xaction"); if (parameter.startsWith(ACTION_PREFIX) && (xactions == null || Stream.of(xactions) .anyMatch(it -> Objects.equals(parameter, String.format("action_%s", it))))) { String targetURL = getTargetURL(hrequest, parameter); RequestDispatcher dispatcher = hrequest.getRequestDispatcher(targetURL); if (dispatcher != null) { LOGGER.debug("Forwarding request to " + targetURL); request.setAttribute(ATTRIBUTE_ACTION_DISPATCHED, "true"); dispatcher.forward(hrequest, response); request.removeAttribute(ATTRIBUTE_ACTION_DISPATCHED); return; } } } } chain.doFilter(request, response); }
ActionFilter implements Filter { @SuppressWarnings("unchecked") @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { if (request instanceof HttpServletRequest && !Boolean.parseBoolean((String) request.getAttribute(ATTRIBUTE_ACTION_DISPATCHED))) { HttpServletRequest hrequest = (HttpServletRequest) request; Enumeration<String> parameterNames = hrequest.getParameterNames(); while (parameterNames.hasMoreElements()) { String parameter = parameterNames.nextElement(); String[] xactions = request.getParameterValues("xaction"); if (parameter.startsWith(ACTION_PREFIX) && (xactions == null || Stream.of(xactions) .anyMatch(it -> Objects.equals(parameter, String.format("action_%s", it))))) { String targetURL = getTargetURL(hrequest, parameter); RequestDispatcher dispatcher = hrequest.getRequestDispatcher(targetURL); if (dispatcher != null) { LOGGER.debug("Forwarding request to " + targetURL); request.setAttribute(ATTRIBUTE_ACTION_DISPATCHED, "true"); dispatcher.forward(hrequest, response); request.removeAttribute(ATTRIBUTE_ACTION_DISPATCHED); return; } } } } chain.doFilter(request, response); } }
ActionFilter implements Filter { @SuppressWarnings("unchecked") @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { if (request instanceof HttpServletRequest && !Boolean.parseBoolean((String) request.getAttribute(ATTRIBUTE_ACTION_DISPATCHED))) { HttpServletRequest hrequest = (HttpServletRequest) request; Enumeration<String> parameterNames = hrequest.getParameterNames(); while (parameterNames.hasMoreElements()) { String parameter = parameterNames.nextElement(); String[] xactions = request.getParameterValues("xaction"); if (parameter.startsWith(ACTION_PREFIX) && (xactions == null || Stream.of(xactions) .anyMatch(it -> Objects.equals(parameter, String.format("action_%s", it))))) { String targetURL = getTargetURL(hrequest, parameter); RequestDispatcher dispatcher = hrequest.getRequestDispatcher(targetURL); if (dispatcher != null) { LOGGER.debug("Forwarding request to " + targetURL); request.setAttribute(ATTRIBUTE_ACTION_DISPATCHED, "true"); dispatcher.forward(hrequest, response); request.removeAttribute(ATTRIBUTE_ACTION_DISPATCHED); return; } } } } chain.doFilter(request, response); }  }
ActionFilter implements Filter { @SuppressWarnings("unchecked") @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { if (request instanceof HttpServletRequest && !Boolean.parseBoolean((String) request.getAttribute(ATTRIBUTE_ACTION_DISPATCHED))) { HttpServletRequest hrequest = (HttpServletRequest) request; Enumeration<String> parameterNames = hrequest.getParameterNames(); while (parameterNames.hasMoreElements()) { String parameter = parameterNames.nextElement(); String[] xactions = request.getParameterValues("xaction"); if (parameter.startsWith(ACTION_PREFIX) && (xactions == null || Stream.of(xactions) .anyMatch(it -> Objects.equals(parameter, String.format("action_%s", it))))) { String targetURL = getTargetURL(hrequest, parameter); RequestDispatcher dispatcher = hrequest.getRequestDispatcher(targetURL); if (dispatcher != null) { LOGGER.debug("Forwarding request to " + targetURL); request.setAttribute(ATTRIBUTE_ACTION_DISPATCHED, "true"); dispatcher.forward(hrequest, response); request.removeAttribute(ATTRIBUTE_ACTION_DISPATCHED); return; } } } } chain.doFilter(request, response); }  @Override void init(FilterConfig filterConfig); @SuppressWarnings("unchecked") @Override void doFilter(ServletRequest request, ServletResponse response, FilterChain chain); @Override void destroy(); }
ActionFilter implements Filter { @SuppressWarnings("unchecked") @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { if (request instanceof HttpServletRequest && !Boolean.parseBoolean((String) request.getAttribute(ATTRIBUTE_ACTION_DISPATCHED))) { HttpServletRequest hrequest = (HttpServletRequest) request; Enumeration<String> parameterNames = hrequest.getParameterNames(); while (parameterNames.hasMoreElements()) { String parameter = parameterNames.nextElement(); String[] xactions = request.getParameterValues("xaction"); if (parameter.startsWith(ACTION_PREFIX) && (xactions == null || Stream.of(xactions) .anyMatch(it -> Objects.equals(parameter, String.format("action_%s", it))))) { String targetURL = getTargetURL(hrequest, parameter); RequestDispatcher dispatcher = hrequest.getRequestDispatcher(targetURL); if (dispatcher != null) { LOGGER.debug("Forwarding request to " + targetURL); request.setAttribute(ATTRIBUTE_ACTION_DISPATCHED, "true"); dispatcher.forward(hrequest, response); request.removeAttribute(ATTRIBUTE_ACTION_DISPATCHED); return; } } } } chain.doFilter(request, response); }  @Override void init(FilterConfig filterConfig); @SuppressWarnings("unchecked") @Override void doFilter(ServletRequest request, ServletResponse response, FilterChain chain); @Override void destroy();  }