From 7f07c602f8c14a072f1ac3f5aa3923fad482cf0e Mon Sep 17 00:00:00 2001 From: DOHA Date: Sat, 21 Mar 2015 20:11:50 +0200 Subject: [PATCH] add metric filter --- .../web/controller/RootController.java | 11 ++++++ .../org/baeldung/web/metric/MetricFilter.java | 36 +++++++++++++++++++ .../baeldung/web/metric/MetricService.java | 35 ++++++++++++++++++ .../src/main/webapp/WEB-INF/web.xml | 11 ++++++ 4 files changed, 93 insertions(+) create mode 100644 spring-security-rest-full/src/main/java/org/baeldung/web/metric/MetricFilter.java create mode 100644 spring-security-rest-full/src/main/java/org/baeldung/web/metric/MetricService.java diff --git a/spring-security-rest-full/src/main/java/org/baeldung/web/controller/RootController.java b/spring-security-rest-full/src/main/java/org/baeldung/web/controller/RootController.java index 0f35a29338..88399eaa9d 100644 --- a/spring-security-rest-full/src/main/java/org/baeldung/web/controller/RootController.java +++ b/spring-security-rest-full/src/main/java/org/baeldung/web/controller/RootController.java @@ -5,17 +5,23 @@ import java.net.URI; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import org.baeldung.web.metric.MetricService; import org.baeldung.web.util.LinkUtil; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.util.UriTemplate; @Controller public class RootController { + @Autowired + private MetricService metricService; + public RootController() { super(); } @@ -34,4 +40,9 @@ public class RootController { response.addHeader("Link", linkToFoo); } + @RequestMapping(value = "/metric", method = RequestMethod.GET) + @ResponseBody + public String getMetric() { + return metricService.getFullMetric(); + } } diff --git a/spring-security-rest-full/src/main/java/org/baeldung/web/metric/MetricFilter.java b/spring-security-rest-full/src/main/java/org/baeldung/web/metric/MetricFilter.java new file mode 100644 index 0000000000..e915a54465 --- /dev/null +++ b/spring-security-rest-full/src/main/java/org/baeldung/web/metric/MetricFilter.java @@ -0,0 +1,36 @@ +package org.baeldung.web.metric; + +import javax.servlet.Filter; +import javax.servlet.FilterChain; +import javax.servlet.FilterConfig; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +public class MetricFilter implements Filter { + + private MetricService metricService; + + @Override + public void init(final FilterConfig config) throws ServletException { + metricService = new MetricService(); + } + + @Override + public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) throws java.io.IOException, ServletException { + final HttpServletRequest httpRequest = ((HttpServletRequest) request); + final String req = httpRequest.getMethod() + " " + httpRequest.getRequestURI(); + + chain.doFilter(request, response); + + final int status = ((HttpServletResponse) response).getStatus(); + metricService.increaseCount(req, status); + } + + @Override + public void destroy() { + + } +} diff --git a/spring-security-rest-full/src/main/java/org/baeldung/web/metric/MetricService.java b/spring-security-rest-full/src/main/java/org/baeldung/web/metric/MetricService.java new file mode 100644 index 0000000000..42d150d2ed --- /dev/null +++ b/spring-security-rest-full/src/main/java/org/baeldung/web/metric/MetricService.java @@ -0,0 +1,35 @@ +package org.baeldung.web.metric; + +import java.util.HashMap; + +import org.springframework.stereotype.Service; + +@Service +public class MetricService { + + private static HashMap> metricMap = new HashMap>(); + + public MetricService() { + super(); + } + + public void increaseCount(final String request, final int status) { + HashMap statusMap = metricMap.get(request); + if (statusMap == null) { + statusMap = new HashMap(); + } + + Integer count = statusMap.get(status); + if (count == null) { + count = 1; + } else { + count++; + } + statusMap.put(status, count); + metricMap.put(request, statusMap); + } + + public String getFullMetric() { + return metricMap.entrySet().toString(); + } +} diff --git a/spring-security-rest-full/src/main/webapp/WEB-INF/web.xml b/spring-security-rest-full/src/main/webapp/WEB-INF/web.xml index 4232428520..db23624c3c 100644 --- a/spring-security-rest-full/src/main/webapp/WEB-INF/web.xml +++ b/spring-security-rest-full/src/main/webapp/WEB-INF/web.xml @@ -51,6 +51,17 @@ springSecurityFilterChain /* + + + + metricFilter + org.baeldung.web.metric.MetricFilter + + + + metricFilter + /* + index.html