MonitoringFilter implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { if (!(request instanceof HttpServletRequest) || !(response instanceof HttpServletResponse) || monitoringDisabled || !instanceEnabled) { chain.doFilter(request, response); return; } final HttpServletRequest httpRequest = (HttpServletRequest) request; final HttpServletResponse httpResponse = (HttpServletResponse) response; if (httpRequest.getRequestURI().equals(getMonitoringUrl(httpRequest))) { doMonitoring(httpRequest, httpResponse); return; } if (!httpCounter.isDisplayed() || isRequestExcluded((HttpServletRequest) request)) { chain.doFilter(request, response); return; } doFilter(chain, httpRequest, httpResponse); } MonitoringFilter(); String getApplicationType(); void setApplicationType(final String applicationType); @Override void init(FilterConfig config); @Override void destroy(); @Override void doFilter(ServletRequest request, ServletResponse response, FilterChain chain); static void registerApplicationNodeInCollectServer(String applicationName,
			URL collectServerUrl, URL applicationNodeUrl); static void unregisterApplicationNodeInCollectServer();  }
@Test public void testDoFilterNoHttp() throws ServletException, IOException { final FilterChain servletChain = createNiceMock(FilterChain.class); final ServletRequest servletRequest = createNiceMock(ServletRequest.class); final ServletResponse servletResponse = createNiceMock(ServletResponse.class); replay(servletRequest); replay(servletResponse); replay(servletChain); monitoringFilter.doFilter(servletRequest, servletResponse, servletChain); verify(servletRequest); verify(servletResponse); verify(servletChain); final FilterChain servletChain2 = createNiceMock(FilterChain.class); final HttpServletRequest servletRequest2 = createNiceMock(HttpServletRequest.class); final ServletResponse servletResponse2 = createNiceMock(ServletResponse.class); replay(servletRequest2); replay(servletResponse2); replay(servletChain2); monitoringFilter.doFilter(servletRequest2, servletResponse2, servletChain2); verify(servletRequest2); verify(servletResponse2); verify(servletChain2); }
@Test public void testDoFilter() throws ServletException, IOException { setProperty(Parameter.DISPLAYED_COUNTERS, "sql"); try { setUp(); HttpServletRequest request = createNiceMock(HttpServletRequest.class); doFilter(request); setProperty(Parameter.DISPLAYED_COUNTERS, ""); setUp(); request = createNiceMock(HttpServletRequest.class); doFilter(request); setProperty(Parameter.DISPLAYED_COUNTERS, "unknown"); try { setUp(); request = createNiceMock(HttpServletRequest.class); doFilter(request); } catch (final IllegalArgumentException e) { assertNotNull("ok", e); } } finally { setProperty(Parameter.DISPLAYED_COUNTERS, null); } setProperty(Parameter.URL_EXCLUDE_PATTERN, ".*"); try { setUp(); final HttpServletRequest request = createNiceMock(HttpServletRequest.class); doFilter(request); } finally { setProperty(Parameter.URL_EXCLUDE_PATTERN, ""); } setUp(); HttpServletRequest request = createNiceMock(HttpServletRequest.class); doFilter(request); setUp(); setProperty(Parameter.LOG, TRUE); try { ((Logger) org.slf4j.LoggerFactory.getLogger(FILTER_NAME)).setLevel(Level.WARN); request = createNiceMock(HttpServletRequest.class); doFilter(request); ((Logger) org.slf4j.LoggerFactory.getLogger(FILTER_NAME)).setLevel(Level.DEBUG); request = createNiceMock(HttpServletRequest.class); doFilter(request); request = createNiceMock(HttpServletRequest.class); expect(request.getHeader("X-Forwarded-For")).andReturn("me").anyTimes(); expect(request.getQueryString()).andReturn("param1=1").anyTimes(); doFilter(request); } finally { setProperty(Parameter.LOG, null); } request = createNiceMock(HttpServletRequest.class); expect(request.getHeader("X-Requested-With")).andReturn("XMLHttpRequest"); doFilter(request); request = createNiceMock(HttpServletRequest.class); expect(request .getAttribute("org.springframework.web.servlet.HandlerMapping.bestMatchingPattern")) .andReturn("/testspringmvc").anyTimes(); doFilter(request); setProperty(Parameter.LOG, TRUE); try { final String test = "test"; request = createNiceMock(HttpServletRequest.class); doFilter(request, new UnknownError(test)); request = createNiceMock(HttpServletRequest.class); doFilter(request, new IllegalStateException(test)); } finally { setProperty(Parameter.LOG, null); } }
@Test public void testDoFilterWithSession() throws ServletException, IOException { final HttpServletRequest request = createNiceMock(HttpServletRequest.class); final HttpSession session = createNiceMock(HttpSession.class); expect(request.getSession(false)).andReturn(session).anyTimes(); expect(request.getLocale()).andReturn(Locale.FRANCE); replay(session); doFilter(request); verify(session); }
@Test public void testDoFilterWithSessionBis() throws ServletException, IOException { final HttpServletRequest request = createNiceMock(HttpServletRequest.class); final HttpSession session = createNiceMock(HttpSession.class); expect(request.getSession(false)).andReturn(session); expect(request.getLocale()).andReturn(Locale.FRENCH).anyTimes(); expect(request.getHeader("X-Forwarded-For")).andReturn("somewhere").anyTimes(); expect(request.getRemoteUser()).andReturn("me").anyTimes(); replay(session); doFilter(request); verify(session); }
@Test public void testDoFilterWithSessionTer() throws ServletException, IOException { final HttpServletRequest request = createNiceMock(HttpServletRequest.class); final HttpSession session = createNiceMock(HttpSession.class); expect(request.getSession(false)).andReturn(session); expect(session.getAttribute(SessionListener.SESSION_COUNTRY_KEY)) .andReturn(Locale.FRANCE.getCountry()).anyTimes(); expect(session.getAttribute(SessionListener.SESSION_REMOTE_ADDR)).andReturn("somewhere") .anyTimes(); expect(session.getAttribute(SessionListener.SESSION_REMOTE_USER)).andReturn("me") .anyTimes(); replay(session); doFilter(request); verify(session); }
@Test public void testDoFilterWithGWT() throws ServletException, IOException { final HttpServletRequest request = createNiceMock(HttpServletRequest.class); final String textGwtRpc = "text/x-gwt-rpc"; expect(request.getContentType()).andReturn(textGwtRpc).anyTimes(); expect(request.getInputStream()) .andReturn(createInputStreamForString("1|2|3|4|5|6|7|8|9|10")).anyTimes(); doFilter(request); final HttpServletRequest request2a = createNiceMock(HttpServletRequest.class); expect(request2a.getContentType()).andReturn("not/x-gwt-rpc").anyTimes(); expect(request2a.getInputStream()) .andReturn(createInputStreamForString("1|2|3|4|5|6|7|8|9|10")).anyTimes(); doFilter(request2a); final HttpServletRequest request2b = createNiceMock(HttpServletRequest.class); expect(request2b.getContentType()).andReturn(textGwtRpc).anyTimes(); expect(request2b.getInputStream()).andReturn(createInputStreamForString("1|2|3|4|5|6")) .anyTimes(); expect(request2b.getReader()).andReturn(new BufferedReader(new StringReader("1|2|3|4|5|6"))) .anyTimes(); replay(request2b); final PayloadNameRequestWrapper wrapper2b = new PayloadNameRequestWrapper(request2b); wrapper2b.getInputStream().read(); wrapper2b.getReader().read(); verify(request2b); final HttpServletRequest request2 = createNiceMock(HttpServletRequest.class); expect(request2.getContentType()).andReturn(textGwtRpc).anyTimes(); expect(request2.getInputStream()) .andReturn(createInputStreamForString("1|2|3|4|5|6||8|9|10")).anyTimes(); expect(request2.getReader()).andReturn(new BufferedReader(new StringReader("1|2|3|4|5|6"))) .anyTimes(); replay(request2); final PayloadNameRequestWrapper wrapper2 = new PayloadNameRequestWrapper(request2); wrapper2.getInputStream().read(); wrapper2.getReader().read(); verify(request2); final HttpServletRequest request3 = createNiceMock(HttpServletRequest.class); expect(request3.getContentType()).andReturn(textGwtRpc).anyTimes(); expect(request3.getCharacterEncoding()).andReturn("utf-8").anyTimes(); expect(request3.getInputStream()) .andReturn(createInputStreamForString("1|2|3|4|5|6||8|9|10")).anyTimes(); expect(request3.getReader()).andReturn(new BufferedReader(new StringReader("1|2|3|4|5|6"))) .anyTimes(); replay(request3); final PayloadNameRequestWrapper wrapper3 = new PayloadNameRequestWrapper(request3); wrapper3.getInputStream().read(); wrapper3.getInputStream().read(); wrapper3.getReader().read(); wrapper3.getReader().read(); verify(request3); }
@Test public void testDoMonitoringWithRum() throws ServletException, IOException { try { setProperty(Parameter.RUM_ENABLED, Boolean.TRUE.toString()); setUp(); final HttpServletRequest requestForRum = createNiceMock(HttpServletRequest.class); expect(requestForRum.getHeader("accept")).andReturn("text/html"); expect(requestForRum.getInputStream()) .andReturn(createInputStreamForString("<html><body>test</body></html>")) .anyTimes(); doFilter(requestForRum); monitoring(Collections.singletonMap(HttpParameter.RESOURCE, "boomerang.min.js")); monitoring(Collections.<HttpParameter, String> emptyMap()); monitoring(Collections.singletonMap(HttpParameter.PART, HttpPart.RUM.getName()), false); final Map<String, String> rumMap = new HashMap<String, String>(); rumMap.put(HttpParameter.PART.getName(), HttpPart.RUM.getName()); rumMap.put("requestName", TEST_REQUEST + " GET"); rumMap.put("serverTime", "100"); rumMap.put("timeToFirstByte", "100"); rumMap.put("domProcessing", "50"); rumMap.put("pageRendering", "50"); monitoring0(rumMap, false); final Map<HttpParameter, String> graphMap = new HashMap<HttpParameter, String>(); graphMap.put(HttpParameter.PART, HttpPart.GRAPH.getName()); final String requestId = new CounterRequest(TEST_REQUEST + " GET", Counter.HTTP_COUNTER_NAME).getId(); graphMap.put(HttpParameter.GRAPH, requestId); monitoring(graphMap); graphMap.put(HttpParameter.PERIOD, Period.TOUT.getCode()); monitoring(graphMap); graphMap.put(HttpParameter.FORMAT, "pdf"); monitoring(graphMap); } finally { setProperty(Parameter.RUM_ENABLED, null); } }