From fb3645d1e88bd9e716e2010d9bab44db5fba0675 Mon Sep 17 00:00:00 2001 From: DOHA Date: Sun, 29 Mar 2015 14:35:27 +0200 Subject: [PATCH] Move boot metric to rest full --- .../org/baeldung/SampleLDAPApplication.java | 1 - .../org/baeldung/controller/MyController.java | 13 ------ .../org/baeldung/metric/MetricFilter.java | 36 --------------- .../src/main/resources/templates/graph.html | 42 ----------------- spring-security-rest-full/pom.xml | 46 ++++++++----------- .../java/org/baeldung/spring/Application.java | 23 ++++++++++ .../web/controller/RootController.java | 10 ++++ .../web/metric/ActuatorMetricService.java | 8 ++-- .../web/metric/IActuatorMetricService.java | 4 +- .../org/baeldung/web/metric/MetricFilter.java | 12 +++-- 10 files changed, 68 insertions(+), 127 deletions(-) delete mode 100644 spring-security-mvc-ldap/src/main/java/org/baeldung/metric/MetricFilter.java delete mode 100644 spring-security-mvc-ldap/src/main/resources/templates/graph.html create mode 100644 spring-security-rest-full/src/main/java/org/baeldung/spring/Application.java rename spring-security-mvc-ldap/src/main/java/org/baeldung/metric/MetricService.java => spring-security-rest-full/src/main/java/org/baeldung/web/metric/ActuatorMetricService.java (94%) rename spring-security-mvc-ldap/src/main/java/org/baeldung/metric/IMetricService.java => spring-security-rest-full/src/main/java/org/baeldung/web/metric/IActuatorMetricService.java (50%) diff --git a/spring-security-mvc-ldap/src/main/java/org/baeldung/SampleLDAPApplication.java b/spring-security-mvc-ldap/src/main/java/org/baeldung/SampleLDAPApplication.java index 748d9c703e..454e52d1d5 100644 --- a/spring-security-mvc-ldap/src/main/java/org/baeldung/SampleLDAPApplication.java +++ b/spring-security-mvc-ldap/src/main/java/org/baeldung/SampleLDAPApplication.java @@ -24,7 +24,6 @@ public class SampleLDAPApplication extends WebMvcConfigurerAdapter { @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/login").setViewName("login"); - registry.addViewController("/graph").setViewName("graph"); } } \ No newline at end of file diff --git a/spring-security-mvc-ldap/src/main/java/org/baeldung/controller/MyController.java b/spring-security-mvc-ldap/src/main/java/org/baeldung/controller/MyController.java index a3fbb129cb..e347bd8633 100644 --- a/spring-security-mvc-ldap/src/main/java/org/baeldung/controller/MyController.java +++ b/spring-security-mvc-ldap/src/main/java/org/baeldung/controller/MyController.java @@ -7,15 +7,11 @@ import java.util.HashSet; import java.util.Map; import java.util.Set; -import org.baeldung.metric.IMetricService; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.core.Authentication; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.userdetails.UserDetails; 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; /** * Spring Controller Definitions. @@ -23,9 +19,6 @@ import org.springframework.web.bind.annotation.ResponseBody; @Controller public class MyController { - @Autowired - private IMetricService metricService; - @RequestMapping("/") public String init(Map model, Principal principal) { model.put("title", "PUBLIC AREA"); @@ -44,12 +37,6 @@ public class MyController { return "home"; } - @RequestMapping(value = "/metric-graph-data", method = RequestMethod.GET) - @ResponseBody - public Object[][] getMetricData() { - return metricService.getGraphData(); - } - private String getUserName(Principal principal) { if (principal == null) { return "anonymous"; diff --git a/spring-security-mvc-ldap/src/main/java/org/baeldung/metric/MetricFilter.java b/spring-security-mvc-ldap/src/main/java/org/baeldung/metric/MetricFilter.java deleted file mode 100644 index 4c00fda530..0000000000 --- a/spring-security-mvc-ldap/src/main/java/org/baeldung/metric/MetricFilter.java +++ /dev/null @@ -1,36 +0,0 @@ -package org.baeldung.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.HttpServletResponse; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; - -@Component -public class MetricFilter implements Filter { - - @Autowired - private IMetricService metricService; - - @Override - public void init(final FilterConfig config) throws ServletException { - } - - @Override - public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) throws java.io.IOException, ServletException { - chain.doFilter(request, response); - - final int status = ((HttpServletResponse) response).getStatus(); - metricService.increaseCount(status); - } - - @Override - public void destroy() { - - } -} diff --git a/spring-security-mvc-ldap/src/main/resources/templates/graph.html b/spring-security-mvc-ldap/src/main/resources/templates/graph.html deleted file mode 100644 index 1064068f06..0000000000 --- a/spring-security-mvc-ldap/src/main/resources/templates/graph.html +++ /dev/null @@ -1,42 +0,0 @@ - - -Metric Graph - - - - - -
- - \ No newline at end of file diff --git a/spring-security-rest-full/pom.xml b/spring-security-rest-full/pom.xml index a1c688aea6..664e47bb1f 100644 --- a/spring-security-rest-full/pom.xml +++ b/spring-security-rest-full/pom.xml @@ -9,17 +9,35 @@ + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-actuator + + + + org.aspectj + aspectjweaver + + + org.springframework.security spring-security-web - ${org.springframework.security.version} org.springframework.security spring-security-config - ${org.springframework.security.version} @@ -27,7 +45,6 @@ org.springframework spring-core - ${org.springframework.version} commons-logging @@ -38,43 +55,35 @@ org.springframework spring-context - ${org.springframework.version} org.springframework spring-jdbc - ${org.springframework.version} org.springframework spring-beans - ${org.springframework.version} org.springframework spring-aop - ${org.springframework.version} org.springframework spring-tx - ${org.springframework.version} org.springframework spring-expression - ${org.springframework.version} org.springframework spring-web - ${org.springframework.version} org.springframework spring-webmvc - ${org.springframework.version} @@ -113,17 +122,14 @@ org.springframework spring-orm - ${org.springframework.version} org.springframework.data spring-data-jpa - ${spring-data-jpa.version} org.hibernate hibernate-entitymanager - ${hibernate.version} xml-apis @@ -133,12 +139,10 @@ org.javassist javassist - 3.19.0-GA mysql mysql-connector-java - ${mysql-connector-java.version} runtime @@ -147,7 +151,6 @@ javax.servlet javax.servlet-api - 3.0.1 provided @@ -162,7 +165,6 @@ com.fasterxml.jackson.core jackson-databind - ${jackson.version} @@ -184,24 +186,20 @@ org.slf4j slf4j-api - ${org.slf4j.version} ch.qos.logback logback-classic - ${logback.version} org.slf4j jcl-over-slf4j - ${org.slf4j.version} org.slf4j log4j-over-slf4j - ${org.slf4j.version} @@ -209,7 +207,6 @@ org.springframework spring-test - ${org.springframework.version} test @@ -266,7 +263,6 @@ org.apache.maven.plugins maven-compiler-plugin - ${maven-compiler-plugin.version} 1.7 1.7 @@ -276,13 +272,11 @@ org.apache.maven.plugins maven-war-plugin - ${maven-war-plugin.version} org.apache.maven.plugins maven-surefire-plugin - ${maven-surefire-plugin.version} **/*IntegrationTest.java diff --git a/spring-security-rest-full/src/main/java/org/baeldung/spring/Application.java b/spring-security-rest-full/src/main/java/org/baeldung/spring/Application.java new file mode 100644 index 0000000000..58a6ad02d8 --- /dev/null +++ b/spring-security-rest-full/src/main/java/org/baeldung/spring/Application.java @@ -0,0 +1,23 @@ +package org.baeldung.spring; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.scheduling.annotation.EnableScheduling; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; + +/** + * Main Application Class - uses Spring Boot. Just run this as a normal Java + * class to run up a Jetty Server (on http://localhost:8080) + * + */ +@EnableScheduling +@EnableAutoConfiguration +@ComponentScan("org.baeldung") +public class Application extends WebMvcConfigurerAdapter { + + public static void main(final String[] args) { + SpringApplication.run(Application.class, args); + } + +} \ No newline at end of file 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 fdef43de7d..42fc78f611 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 @@ -6,6 +6,7 @@ import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import org.baeldung.web.metric.IActuatorMetricService; import org.baeldung.web.metric.IMetricService; import org.baeldung.web.util.LinkUtil; import org.springframework.beans.factory.annotation.Autowired; @@ -23,6 +24,9 @@ public class RootController { @Autowired private IMetricService metricService; + @Autowired + private IActuatorMetricService actMetricService; + public RootController() { super(); } @@ -62,4 +66,10 @@ public class RootController { } return result; } + + @RequestMapping(value = "/metric-graph-data", method = RequestMethod.GET) + @ResponseBody + public Object[][] getActuatorMetricData() { + return actMetricService.getGraphData(); + } } diff --git a/spring-security-mvc-ldap/src/main/java/org/baeldung/metric/MetricService.java b/spring-security-rest-full/src/main/java/org/baeldung/web/metric/ActuatorMetricService.java similarity index 94% rename from spring-security-mvc-ldap/src/main/java/org/baeldung/metric/MetricService.java rename to spring-security-rest-full/src/main/java/org/baeldung/web/metric/ActuatorMetricService.java index 6767f1b540..ea28c1e5f4 100644 --- a/spring-security-mvc-ldap/src/main/java/org/baeldung/metric/MetricService.java +++ b/spring-security-rest-full/src/main/java/org/baeldung/web/metric/ActuatorMetricService.java @@ -1,4 +1,4 @@ -package org.baeldung.metric; +package org.baeldung.web.metric; import java.text.SimpleDateFormat; import java.util.ArrayList; @@ -13,7 +13,7 @@ import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; @Service -public class MetricService implements IMetricService { +public class ActuatorMetricService implements IActuatorMetricService { @Autowired private MetricRepository repo; @@ -25,7 +25,7 @@ public class MetricService implements IMetricService { private final List statusList; private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm"); - public MetricService() { + public ActuatorMetricService() { super(); statusMetric = new ArrayList>(); statusList = new ArrayList(); @@ -88,4 +88,4 @@ public class MetricService implements IMetricService { } statusMetric.add(statusCount); } -} +} \ No newline at end of file diff --git a/spring-security-mvc-ldap/src/main/java/org/baeldung/metric/IMetricService.java b/spring-security-rest-full/src/main/java/org/baeldung/web/metric/IActuatorMetricService.java similarity index 50% rename from spring-security-mvc-ldap/src/main/java/org/baeldung/metric/IMetricService.java rename to spring-security-rest-full/src/main/java/org/baeldung/web/metric/IActuatorMetricService.java index 6814573138..b7528ce372 100644 --- a/spring-security-mvc-ldap/src/main/java/org/baeldung/metric/IMetricService.java +++ b/spring-security-rest-full/src/main/java/org/baeldung/web/metric/IActuatorMetricService.java @@ -1,6 +1,6 @@ -package org.baeldung.metric; +package org.baeldung.web.metric; -public interface IMetricService { +public interface IActuatorMetricService { void increaseCount(final int status); 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 index 13988858b4..f892aa4ffe 100644 --- 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 @@ -9,15 +9,20 @@ import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import org.springframework.web.context.support.WebApplicationContextUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +@Component public class MetricFilter implements Filter { - private MetricService metricService; + @Autowired + private IMetricService metricService; + + @Autowired + private IActuatorMetricService actMetricService; @Override public void init(final FilterConfig config) throws ServletException { - metricService = (MetricService) WebApplicationContextUtils.getRequiredWebApplicationContext(config.getServletContext()).getBean("metricService"); } @Override @@ -29,6 +34,7 @@ public class MetricFilter implements Filter { final int status = ((HttpServletResponse) response).getStatus(); metricService.increaseCount(req, status); + actMetricService.increaseCount(status); } @Override