@Test @Features("issues/292") public void testResetTopSql() throws Exception { Sniffy.getGlobalSqlStats().clear(); executeStatement(); MockHttpServletResponse response = new MockHttpServletResponse(); MockHttpServletRequest request = MockMvcRequestBuilders. delete("/petclinic/" + SniffyServlet.TOP_SQL_URI_PREFIX). buildRequest(servletContext); request.setContextPath("/petclinic"); sniffyServlet.service(request, response); assertEquals(HttpServletResponse.SC_CREATED, response.getStatus()); assertTrue(Sniffy.getGlobalSqlStats().isEmpty()); }
@Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String requestURI = request.getRequestURI(); int ix = requestURI.indexOf(SNIFFY_URI_PREFIX); if (ix < 0) return; String path = requestURI.substring(ix); if (SniffyFilter.JAVASCRIPT_URI.equals(path)) { addCorsHeaders(response); serveContent(response, JAVASCRIPT_MIME_TYPE, javascript); } else if (SniffyFilter.JAVASCRIPT_SOURCE_URI.equals(path)) { addCorsHeaders(response); serveContent(response, JAVASCRIPT_MIME_TYPE, javascriptSource); } else if (SniffyFilter.JAVASCRIPT_MAP_URI.equals(path)) { addCorsHeaders(response); serveContent(response, JAVASCRIPT_MIME_TYPE, javascriptMap); } else if (path.startsWith(SniffyFilter.REQUEST_URI_PREFIX)) { addCorsHeaders(response); byte[] requestStatsJson = getRequestStatsJson(path.substring(SniffyFilter.REQUEST_URI_PREFIX.length())); if (null == requestStatsJson) { response.setStatus(HttpServletResponse.SC_OK); response.setContentType(JAVASCRIPT_MIME_TYPE); response.flushBuffer(); } else { response.setContentType(JAVASCRIPT_MIME_TYPE); response.setContentLength(requestStatsJson.length); ServletOutputStream outputStream = response.getOutputStream(); outputStream.write(requestStatsJson); outputStream.flush(); } } else if (path.equals(CONNECTION_REGISTRY_URI_PREFIX)) { addCorsHeaders(response); response.setStatus(HttpServletResponse.SC_OK); response.setContentType(JAVASCRIPT_MIME_TYPE); ConnectionsRegistry.INSTANCE.writeTo(response.getWriter()); } else if (path.startsWith(CONNECTION_REGISTRY_URI_PREFIX)) { addCorsHeaders(response); Integer status = null; if ("POST".equalsIgnoreCase(request.getMethod())) { try { status = Integer.parseInt(request.getReader().readLine()); } catch (Exception e) { status = 0; } } else if ("DELETE".equalsIgnoreCase(request.getMethod())) { status = -1; } if (path.startsWith(SOCKET_REGISTRY_URI_PREFIX)) { String connectionString = path.substring(SOCKET_REGISTRY_URI_PREFIX.length()); String[] split = splitBySlashAndDecode(connectionString); ConnectionsRegistry.INSTANCE.setSocketAddressStatus(split[0], Integer.parseInt(split[1]), status); } else if (path.startsWith(DATASOURCE_REGISTRY_URI_PREFIX)) { String connectionString = path.substring(DATASOURCE_REGISTRY_URI_PREFIX.length()); String[] split = splitBySlashAndDecode(connectionString); ConnectionsRegistry.INSTANCE.setDataSourceStatus(split[0], split[1], status); } else if (path.startsWith(PERSISTENT_REGISTRY_URI_PREFIX)) { if (null != status && status >= 0) { ConnectionsRegistry.INSTANCE.setPersistRegistry(true); ConnectionsRegistryStorage.INSTANCE.storeConnectionsRegistry(ConnectionsRegistry.INSTANCE); } else { ConnectionsRegistry.INSTANCE.setPersistRegistry(false); } } response.setStatus(HttpServletResponse.SC_CREATED); response.flushBuffer(); } else if (path.equals(TOP_SQL_URI_PREFIX)) { addCorsHeaders(response); if ("DELETE".equalsIgnoreCase(request.getMethod())) { Sniffy.getGlobalSqlStats().clear(); response.setStatus(HttpServletResponse.SC_CREATED); response.flushBuffer(); } else { response.setStatus(HttpServletResponse.SC_OK); response.setContentType(JAVASCRIPT_MIME_TYPE); JsonArray arrayJson = new JsonArray(); for (Map.Entry<String, Timer> entry : Sniffy.getGlobalSqlStats().entrySet()) { Timer timer = entry.getValue(); JsonObject timerJson = new JsonObject(); timerJson.add("count", timer.getCount()); timerJson.add("min", timer.getSnapshot().getMin()); timerJson.add("median", timer.getSnapshot().getMedian()); timerJson.add("mean", timer.getSnapshot().getMean()); timerJson.add("max", timer.getSnapshot().getMax()); timerJson.add("p75", timer.getSnapshot().get75thPercentile()); timerJson.add("p95", timer.getSnapshot().get95thPercentile()); timerJson.add("p99", timer.getSnapshot().get99thPercentile()); JsonObject objectJson = new JsonObject(); objectJson.add("sql", entry.getKey()); objectJson.add("timer", timerJson); arrayJson.add(objectJson); } arrayJson.writeTo(response.getWriter()); response.flushBuffer(); } } }
SniffyServlet extends HttpServlet { @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String requestURI = request.getRequestURI(); int ix = requestURI.indexOf(SNIFFY_URI_PREFIX); if (ix < 0) return; String path = requestURI.substring(ix); if (SniffyFilter.JAVASCRIPT_URI.equals(path)) { addCorsHeaders(response); serveContent(response, JAVASCRIPT_MIME_TYPE, javascript); } else if (SniffyFilter.JAVASCRIPT_SOURCE_URI.equals(path)) { addCorsHeaders(response); serveContent(response, JAVASCRIPT_MIME_TYPE, javascriptSource); } else if (SniffyFilter.JAVASCRIPT_MAP_URI.equals(path)) { addCorsHeaders(response); serveContent(response, JAVASCRIPT_MIME_TYPE, javascriptMap); } else if (path.startsWith(SniffyFilter.REQUEST_URI_PREFIX)) { addCorsHeaders(response); byte[] requestStatsJson = getRequestStatsJson(path.substring(SniffyFilter.REQUEST_URI_PREFIX.length())); if (null == requestStatsJson) { response.setStatus(HttpServletResponse.SC_OK); response.setContentType(JAVASCRIPT_MIME_TYPE); response.flushBuffer(); } else { response.setContentType(JAVASCRIPT_MIME_TYPE); response.setContentLength(requestStatsJson.length); ServletOutputStream outputStream = response.getOutputStream(); outputStream.write(requestStatsJson); outputStream.flush(); } } else if (path.equals(CONNECTION_REGISTRY_URI_PREFIX)) { addCorsHeaders(response); response.setStatus(HttpServletResponse.SC_OK); response.setContentType(JAVASCRIPT_MIME_TYPE); ConnectionsRegistry.INSTANCE.writeTo(response.getWriter()); } else if (path.startsWith(CONNECTION_REGISTRY_URI_PREFIX)) { addCorsHeaders(response); Integer status = null; if ("POST".equalsIgnoreCase(request.getMethod())) { try { status = Integer.parseInt(request.getReader().readLine()); } catch (Exception e) { status = 0; } } else if ("DELETE".equalsIgnoreCase(request.getMethod())) { status = -1; } if (path.startsWith(SOCKET_REGISTRY_URI_PREFIX)) { String connectionString = path.substring(SOCKET_REGISTRY_URI_PREFIX.length()); String[] split = splitBySlashAndDecode(connectionString); ConnectionsRegistry.INSTANCE.setSocketAddressStatus(split[0], Integer.parseInt(split[1]), status); } else if (path.startsWith(DATASOURCE_REGISTRY_URI_PREFIX)) { String connectionString = path.substring(DATASOURCE_REGISTRY_URI_PREFIX.length()); String[] split = splitBySlashAndDecode(connectionString); ConnectionsRegistry.INSTANCE.setDataSourceStatus(split[0], split[1], status); } else if (path.startsWith(PERSISTENT_REGISTRY_URI_PREFIX)) { if (null != status && status >= 0) { ConnectionsRegistry.INSTANCE.setPersistRegistry(true); ConnectionsRegistryStorage.INSTANCE.storeConnectionsRegistry(ConnectionsRegistry.INSTANCE); } else { ConnectionsRegistry.INSTANCE.setPersistRegistry(false); } } response.setStatus(HttpServletResponse.SC_CREATED); response.flushBuffer(); } else if (path.equals(TOP_SQL_URI_PREFIX)) { addCorsHeaders(response); if ("DELETE".equalsIgnoreCase(request.getMethod())) { Sniffy.getGlobalSqlStats().clear(); response.setStatus(HttpServletResponse.SC_CREATED); response.flushBuffer(); } else { response.setStatus(HttpServletResponse.SC_OK); response.setContentType(JAVASCRIPT_MIME_TYPE); JsonArray arrayJson = new JsonArray(); for (Map.Entry<String, Timer> entry : Sniffy.getGlobalSqlStats().entrySet()) { Timer timer = entry.getValue(); JsonObject timerJson = new JsonObject(); timerJson.add("count", timer.getCount()); timerJson.add("min", timer.getSnapshot().getMin()); timerJson.add("median", timer.getSnapshot().getMedian()); timerJson.add("mean", timer.getSnapshot().getMean()); timerJson.add("max", timer.getSnapshot().getMax()); timerJson.add("p75", timer.getSnapshot().get75thPercentile()); timerJson.add("p95", timer.getSnapshot().get95thPercentile()); timerJson.add("p99", timer.getSnapshot().get99thPercentile()); JsonObject objectJson = new JsonObject(); objectJson.add("sql", entry.getKey()); objectJson.add("timer", timerJson); arrayJson.add(objectJson); } arrayJson.writeTo(response.getWriter()); response.flushBuffer(); } } } }
SniffyServlet extends HttpServlet { @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String requestURI = request.getRequestURI(); int ix = requestURI.indexOf(SNIFFY_URI_PREFIX); if (ix < 0) return; String path = requestURI.substring(ix); if (SniffyFilter.JAVASCRIPT_URI.equals(path)) { addCorsHeaders(response); serveContent(response, JAVASCRIPT_MIME_TYPE, javascript); } else if (SniffyFilter.JAVASCRIPT_SOURCE_URI.equals(path)) { addCorsHeaders(response); serveContent(response, JAVASCRIPT_MIME_TYPE, javascriptSource); } else if (SniffyFilter.JAVASCRIPT_MAP_URI.equals(path)) { addCorsHeaders(response); serveContent(response, JAVASCRIPT_MIME_TYPE, javascriptMap); } else if (path.startsWith(SniffyFilter.REQUEST_URI_PREFIX)) { addCorsHeaders(response); byte[] requestStatsJson = getRequestStatsJson(path.substring(SniffyFilter.REQUEST_URI_PREFIX.length())); if (null == requestStatsJson) { response.setStatus(HttpServletResponse.SC_OK); response.setContentType(JAVASCRIPT_MIME_TYPE); response.flushBuffer(); } else { response.setContentType(JAVASCRIPT_MIME_TYPE); response.setContentLength(requestStatsJson.length); ServletOutputStream outputStream = response.getOutputStream(); outputStream.write(requestStatsJson); outputStream.flush(); } } else if (path.equals(CONNECTION_REGISTRY_URI_PREFIX)) { addCorsHeaders(response); response.setStatus(HttpServletResponse.SC_OK); response.setContentType(JAVASCRIPT_MIME_TYPE); ConnectionsRegistry.INSTANCE.writeTo(response.getWriter()); } else if (path.startsWith(CONNECTION_REGISTRY_URI_PREFIX)) { addCorsHeaders(response); Integer status = null; if ("POST".equalsIgnoreCase(request.getMethod())) { try { status = Integer.parseInt(request.getReader().readLine()); } catch (Exception e) { status = 0; } } else if ("DELETE".equalsIgnoreCase(request.getMethod())) { status = -1; } if (path.startsWith(SOCKET_REGISTRY_URI_PREFIX)) { String connectionString = path.substring(SOCKET_REGISTRY_URI_PREFIX.length()); String[] split = splitBySlashAndDecode(connectionString); ConnectionsRegistry.INSTANCE.setSocketAddressStatus(split[0], Integer.parseInt(split[1]), status); } else if (path.startsWith(DATASOURCE_REGISTRY_URI_PREFIX)) { String connectionString = path.substring(DATASOURCE_REGISTRY_URI_PREFIX.length()); String[] split = splitBySlashAndDecode(connectionString); ConnectionsRegistry.INSTANCE.setDataSourceStatus(split[0], split[1], status); } else if (path.startsWith(PERSISTENT_REGISTRY_URI_PREFIX)) { if (null != status && status >= 0) { ConnectionsRegistry.INSTANCE.setPersistRegistry(true); ConnectionsRegistryStorage.INSTANCE.storeConnectionsRegistry(ConnectionsRegistry.INSTANCE); } else { ConnectionsRegistry.INSTANCE.setPersistRegistry(false); } } response.setStatus(HttpServletResponse.SC_CREATED); response.flushBuffer(); } else if (path.equals(TOP_SQL_URI_PREFIX)) { addCorsHeaders(response); if ("DELETE".equalsIgnoreCase(request.getMethod())) { Sniffy.getGlobalSqlStats().clear(); response.setStatus(HttpServletResponse.SC_CREATED); response.flushBuffer(); } else { response.setStatus(HttpServletResponse.SC_OK); response.setContentType(JAVASCRIPT_MIME_TYPE); JsonArray arrayJson = new JsonArray(); for (Map.Entry<String, Timer> entry : Sniffy.getGlobalSqlStats().entrySet()) { Timer timer = entry.getValue(); JsonObject timerJson = new JsonObject(); timerJson.add("count", timer.getCount()); timerJson.add("min", timer.getSnapshot().getMin()); timerJson.add("median", timer.getSnapshot().getMedian()); timerJson.add("mean", timer.getSnapshot().getMean()); timerJson.add("max", timer.getSnapshot().getMax()); timerJson.add("p75", timer.getSnapshot().get75thPercentile()); timerJson.add("p95", timer.getSnapshot().get95thPercentile()); timerJson.add("p99", timer.getSnapshot().get99thPercentile()); JsonObject objectJson = new JsonObject(); objectJson.add("sql", entry.getKey()); objectJson.add("timer", timerJson); arrayJson.add(objectJson); } arrayJson.writeTo(response.getWriter()); response.flushBuffer(); } } } SniffyServlet(Map<String, RequestStats> cache); }
SniffyServlet extends HttpServlet { @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String requestURI = request.getRequestURI(); int ix = requestURI.indexOf(SNIFFY_URI_PREFIX); if (ix < 0) return; String path = requestURI.substring(ix); if (SniffyFilter.JAVASCRIPT_URI.equals(path)) { addCorsHeaders(response); serveContent(response, JAVASCRIPT_MIME_TYPE, javascript); } else if (SniffyFilter.JAVASCRIPT_SOURCE_URI.equals(path)) { addCorsHeaders(response); serveContent(response, JAVASCRIPT_MIME_TYPE, javascriptSource); } else if (SniffyFilter.JAVASCRIPT_MAP_URI.equals(path)) { addCorsHeaders(response); serveContent(response, JAVASCRIPT_MIME_TYPE, javascriptMap); } else if (path.startsWith(SniffyFilter.REQUEST_URI_PREFIX)) { addCorsHeaders(response); byte[] requestStatsJson = getRequestStatsJson(path.substring(SniffyFilter.REQUEST_URI_PREFIX.length())); if (null == requestStatsJson) { response.setStatus(HttpServletResponse.SC_OK); response.setContentType(JAVASCRIPT_MIME_TYPE); response.flushBuffer(); } else { response.setContentType(JAVASCRIPT_MIME_TYPE); response.setContentLength(requestStatsJson.length); ServletOutputStream outputStream = response.getOutputStream(); outputStream.write(requestStatsJson); outputStream.flush(); } } else if (path.equals(CONNECTION_REGISTRY_URI_PREFIX)) { addCorsHeaders(response); response.setStatus(HttpServletResponse.SC_OK); response.setContentType(JAVASCRIPT_MIME_TYPE); ConnectionsRegistry.INSTANCE.writeTo(response.getWriter()); } else if (path.startsWith(CONNECTION_REGISTRY_URI_PREFIX)) { addCorsHeaders(response); Integer status = null; if ("POST".equalsIgnoreCase(request.getMethod())) { try { status = Integer.parseInt(request.getReader().readLine()); } catch (Exception e) { status = 0; } } else if ("DELETE".equalsIgnoreCase(request.getMethod())) { status = -1; } if (path.startsWith(SOCKET_REGISTRY_URI_PREFIX)) { String connectionString = path.substring(SOCKET_REGISTRY_URI_PREFIX.length()); String[] split = splitBySlashAndDecode(connectionString); ConnectionsRegistry.INSTANCE.setSocketAddressStatus(split[0], Integer.parseInt(split[1]), status); } else if (path.startsWith(DATASOURCE_REGISTRY_URI_PREFIX)) { String connectionString = path.substring(DATASOURCE_REGISTRY_URI_PREFIX.length()); String[] split = splitBySlashAndDecode(connectionString); ConnectionsRegistry.INSTANCE.setDataSourceStatus(split[0], split[1], status); } else if (path.startsWith(PERSISTENT_REGISTRY_URI_PREFIX)) { if (null != status && status >= 0) { ConnectionsRegistry.INSTANCE.setPersistRegistry(true); ConnectionsRegistryStorage.INSTANCE.storeConnectionsRegistry(ConnectionsRegistry.INSTANCE); } else { ConnectionsRegistry.INSTANCE.setPersistRegistry(false); } } response.setStatus(HttpServletResponse.SC_CREATED); response.flushBuffer(); } else if (path.equals(TOP_SQL_URI_PREFIX)) { addCorsHeaders(response); if ("DELETE".equalsIgnoreCase(request.getMethod())) { Sniffy.getGlobalSqlStats().clear(); response.setStatus(HttpServletResponse.SC_CREATED); response.flushBuffer(); } else { response.setStatus(HttpServletResponse.SC_OK); response.setContentType(JAVASCRIPT_MIME_TYPE); JsonArray arrayJson = new JsonArray(); for (Map.Entry<String, Timer> entry : Sniffy.getGlobalSqlStats().entrySet()) { Timer timer = entry.getValue(); JsonObject timerJson = new JsonObject(); timerJson.add("count", timer.getCount()); timerJson.add("min", timer.getSnapshot().getMin()); timerJson.add("median", timer.getSnapshot().getMedian()); timerJson.add("mean", timer.getSnapshot().getMean()); timerJson.add("max", timer.getSnapshot().getMax()); timerJson.add("p75", timer.getSnapshot().get75thPercentile()); timerJson.add("p95", timer.getSnapshot().get95thPercentile()); timerJson.add("p99", timer.getSnapshot().get99thPercentile()); JsonObject objectJson = new JsonObject(); objectJson.add("sql", entry.getKey()); objectJson.add("timer", timerJson); arrayJson.add(objectJson); } arrayJson.writeTo(response.getWriter()); response.flushBuffer(); } } } SniffyServlet(Map<String, RequestStats> cache);  }
SniffyServlet extends HttpServlet { @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String requestURI = request.getRequestURI(); int ix = requestURI.indexOf(SNIFFY_URI_PREFIX); if (ix < 0) return; String path = requestURI.substring(ix); if (SniffyFilter.JAVASCRIPT_URI.equals(path)) { addCorsHeaders(response); serveContent(response, JAVASCRIPT_MIME_TYPE, javascript); } else if (SniffyFilter.JAVASCRIPT_SOURCE_URI.equals(path)) { addCorsHeaders(response); serveContent(response, JAVASCRIPT_MIME_TYPE, javascriptSource); } else if (SniffyFilter.JAVASCRIPT_MAP_URI.equals(path)) { addCorsHeaders(response); serveContent(response, JAVASCRIPT_MIME_TYPE, javascriptMap); } else if (path.startsWith(SniffyFilter.REQUEST_URI_PREFIX)) { addCorsHeaders(response); byte[] requestStatsJson = getRequestStatsJson(path.substring(SniffyFilter.REQUEST_URI_PREFIX.length())); if (null == requestStatsJson) { response.setStatus(HttpServletResponse.SC_OK); response.setContentType(JAVASCRIPT_MIME_TYPE); response.flushBuffer(); } else { response.setContentType(JAVASCRIPT_MIME_TYPE); response.setContentLength(requestStatsJson.length); ServletOutputStream outputStream = response.getOutputStream(); outputStream.write(requestStatsJson); outputStream.flush(); } } else if (path.equals(CONNECTION_REGISTRY_URI_PREFIX)) { addCorsHeaders(response); response.setStatus(HttpServletResponse.SC_OK); response.setContentType(JAVASCRIPT_MIME_TYPE); ConnectionsRegistry.INSTANCE.writeTo(response.getWriter()); } else if (path.startsWith(CONNECTION_REGISTRY_URI_PREFIX)) { addCorsHeaders(response); Integer status = null; if ("POST".equalsIgnoreCase(request.getMethod())) { try { status = Integer.parseInt(request.getReader().readLine()); } catch (Exception e) { status = 0; } } else if ("DELETE".equalsIgnoreCase(request.getMethod())) { status = -1; } if (path.startsWith(SOCKET_REGISTRY_URI_PREFIX)) { String connectionString = path.substring(SOCKET_REGISTRY_URI_PREFIX.length()); String[] split = splitBySlashAndDecode(connectionString); ConnectionsRegistry.INSTANCE.setSocketAddressStatus(split[0], Integer.parseInt(split[1]), status); } else if (path.startsWith(DATASOURCE_REGISTRY_URI_PREFIX)) { String connectionString = path.substring(DATASOURCE_REGISTRY_URI_PREFIX.length()); String[] split = splitBySlashAndDecode(connectionString); ConnectionsRegistry.INSTANCE.setDataSourceStatus(split[0], split[1], status); } else if (path.startsWith(PERSISTENT_REGISTRY_URI_PREFIX)) { if (null != status && status >= 0) { ConnectionsRegistry.INSTANCE.setPersistRegistry(true); ConnectionsRegistryStorage.INSTANCE.storeConnectionsRegistry(ConnectionsRegistry.INSTANCE); } else { ConnectionsRegistry.INSTANCE.setPersistRegistry(false); } } response.setStatus(HttpServletResponse.SC_CREATED); response.flushBuffer(); } else if (path.equals(TOP_SQL_URI_PREFIX)) { addCorsHeaders(response); if ("DELETE".equalsIgnoreCase(request.getMethod())) { Sniffy.getGlobalSqlStats().clear(); response.setStatus(HttpServletResponse.SC_CREATED); response.flushBuffer(); } else { response.setStatus(HttpServletResponse.SC_OK); response.setContentType(JAVASCRIPT_MIME_TYPE); JsonArray arrayJson = new JsonArray(); for (Map.Entry<String, Timer> entry : Sniffy.getGlobalSqlStats().entrySet()) { Timer timer = entry.getValue(); JsonObject timerJson = new JsonObject(); timerJson.add("count", timer.getCount()); timerJson.add("min", timer.getSnapshot().getMin()); timerJson.add("median", timer.getSnapshot().getMedian()); timerJson.add("mean", timer.getSnapshot().getMean()); timerJson.add("max", timer.getSnapshot().getMax()); timerJson.add("p75", timer.getSnapshot().get75thPercentile()); timerJson.add("p95", timer.getSnapshot().get95thPercentile()); timerJson.add("p99", timer.getSnapshot().get99thPercentile()); JsonObject objectJson = new JsonObject(); objectJson.add("sql", entry.getKey()); objectJson.add("timer", timerJson); arrayJson.add(objectJson); } arrayJson.writeTo(response.getWriter()); response.flushBuffer(); } } } SniffyServlet(Map<String, RequestStats> cache);  static final String JAVASCRIPT_MIME_TYPE; static final String TOP_SQL_URI_PREFIX; static final String CONNECTION_REGISTRY_URI_PREFIX; static final String SOCKET_REGISTRY_URI_PREFIX; static final String DATASOURCE_REGISTRY_URI_PREFIX; static final String PERSISTENT_REGISTRY_URI_PREFIX; }