commit
90ea448444
|
@ -5,17 +5,23 @@ import java.net.URI;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
|
import org.baeldung.web.metric.MetricService;
|
||||||
import org.baeldung.web.util.LinkUtil;
|
import org.baeldung.web.util.LinkUtil;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMethod;
|
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.bind.annotation.ResponseStatus;
|
||||||
import org.springframework.web.util.UriTemplate;
|
import org.springframework.web.util.UriTemplate;
|
||||||
|
|
||||||
@Controller
|
@Controller
|
||||||
public class RootController {
|
public class RootController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private MetricService metricService;
|
||||||
|
|
||||||
public RootController() {
|
public RootController() {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
@ -34,4 +40,9 @@ public class RootController {
|
||||||
response.addHeader("Link", linkToFoo);
|
response.addHeader("Link", linkToFoo);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@RequestMapping(value = "/metric", method = RequestMethod.GET)
|
||||||
|
@ResponseBody
|
||||||
|
public String getMetric() {
|
||||||
|
return metricService.getFullMetric();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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() {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -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<String, HashMap<Integer, Integer>> metricMap = new HashMap<String, HashMap<Integer, Integer>>();
|
||||||
|
|
||||||
|
public MetricService() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void increaseCount(final String request, final int status) {
|
||||||
|
HashMap<Integer, Integer> statusMap = metricMap.get(request);
|
||||||
|
if (statusMap == null) {
|
||||||
|
statusMap = new HashMap<Integer, Integer>();
|
||||||
|
}
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
|
@ -52,6 +52,17 @@
|
||||||
<url-pattern>/*</url-pattern>
|
<url-pattern>/*</url-pattern>
|
||||||
</filter-mapping>
|
</filter-mapping>
|
||||||
|
|
||||||
|
<!-- Metric filter -->
|
||||||
|
<filter>
|
||||||
|
<filter-name>metricFilter</filter-name>
|
||||||
|
<filter-class>org.baeldung.web.metric.MetricFilter</filter-class>
|
||||||
|
</filter>
|
||||||
|
|
||||||
|
<filter-mapping>
|
||||||
|
<filter-name>metricFilter</filter-name>
|
||||||
|
<url-pattern>/*</url-pattern>
|
||||||
|
</filter-mapping>
|
||||||
|
|
||||||
<welcome-file-list>
|
<welcome-file-list>
|
||||||
<welcome-file>index.html</welcome-file>
|
<welcome-file>index.html</welcome-file>
|
||||||
</welcome-file-list>
|
</welcome-file-list>
|
||||||
|
|
Loading…
Reference in New Issue