diff --git a/spring-boot/src/main/java/com/baeldung/bootcustomfilters/SpringBootFiltersApplication.java b/spring-boot/src/main/java/com/baeldung/bootcustomfilters/SpringBootFiltersApplication.java new file mode 100644 index 0000000000..c4653932c0 --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/bootcustomfilters/SpringBootFiltersApplication.java @@ -0,0 +1,17 @@ +package com.baeldung.bootcustomfilters; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * Boot application + * @author hemant + * + */ +@SpringBootApplication +public class SpringBootFiltersApplication { + + public static void main(String[] args) { + SpringApplication.run(SpringBootFiltersApplication.class, args); + } +} diff --git a/spring-boot/src/main/java/com/baeldung/bootcustomfilters/controller/UserController.java b/spring-boot/src/main/java/com/baeldung/bootcustomfilters/controller/UserController.java new file mode 100644 index 0000000000..9dfab1192d --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/bootcustomfilters/controller/UserController.java @@ -0,0 +1,34 @@ +package com.baeldung.bootcustomfilters.controller; + +import java.util.Arrays; +import java.util.List; +import java.util.UUID; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import com.baeldung.bootcustomfilters.model.User; + +/** + * Rest controller for User + * @author hemant + * + */ +@RestController +@RequestMapping("/users") +public class UserController { + + private static final Logger LOG = LoggerFactory.getLogger(UserController.class); + + @RequestMapping("") + public List getAllUsers() { + LOG.info("Fetching all the users"); + return Arrays.asList( + new User(UUID.randomUUID().toString(), "User1", "user1@test.com"), + new User(UUID.randomUUID().toString(), "User1", "user1@test.com"), + new User(UUID.randomUUID().toString(), "User1", "user1@test.com")); + } + +} diff --git a/spring-boot/src/main/java/com/baeldung/bootcustomfilters/filters/RequestResponseLoggingFilter.java b/spring-boot/src/main/java/com/baeldung/bootcustomfilters/filters/RequestResponseLoggingFilter.java new file mode 100644 index 0000000000..e42ea7d2dd --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/bootcustomfilters/filters/RequestResponseLoggingFilter.java @@ -0,0 +1,50 @@ +package com.baeldung.bootcustomfilters.filters; + +import java.io.IOException; + +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; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.core.annotation.Order; +import org.springframework.stereotype.Component; + +/** + * A servlet filter to log request and response + * The logging implementation is pretty native and for demonstration only + * @author hemant + * + */ +@Component +@Order(2) +public class RequestResponseLoggingFilter implements Filter { + + private final static Logger LOG = LoggerFactory.getLogger(RequestResponseLoggingFilter.class); + + @Override + public void init(final FilterConfig filterConfig) throws ServletException { + LOG.info("Initializing filter :{}", this); + } + + @Override + public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) + throws IOException, ServletException { + HttpServletRequest req = (HttpServletRequest) request; + HttpServletResponse res = (HttpServletResponse) response; + LOG.info("Logging Request {} : {}", req.getMethod(), req.getRequestURI()); + chain.doFilter(request, response); + LOG.info("Logging Response :{}", res.getContentType()); + } + + @Override + public void destroy() { + LOG.warn("Destructing filter :{}", this); + } +} diff --git a/spring-boot/src/main/java/com/baeldung/bootcustomfilters/filters/TransactionFilter.java b/spring-boot/src/main/java/com/baeldung/bootcustomfilters/filters/TransactionFilter.java new file mode 100644 index 0000000000..d92b723e73 --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/bootcustomfilters/filters/TransactionFilter.java @@ -0,0 +1,47 @@ +package com.baeldung.bootcustomfilters.filters; + +import java.io.IOException; + +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 org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.core.annotation.Order; +import org.springframework.stereotype.Component; + +/** + * A filter to create transaction before and commit it once request completes + * The current implemenatation is just for demo + * @author hemant + * + */ +@Component +@Order(1) +public class TransactionFilter implements Filter { + + private final static Logger LOG = LoggerFactory.getLogger(TransactionFilter.class); + + @Override + public void init(final FilterConfig filterConfig) throws ServletException { + LOG.info("Initializing filter :{}", this); + } + + @Override + public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException { + HttpServletRequest req = (HttpServletRequest) request; + LOG.info("Starting Transaction for req :{}", req.getRequestURI()); + chain.doFilter(request, response); + LOG.info("Committing Transaction for req :{}", req.getRequestURI()); + } + + @Override + public void destroy() { + LOG.warn("Destructing filter :{}", this); + } +} diff --git a/spring-boot/src/main/java/com/baeldung/bootcustomfilters/model/User.java b/spring-boot/src/main/java/com/baeldung/bootcustomfilters/model/User.java new file mode 100644 index 0000000000..ab8e6c8206 --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/bootcustomfilters/model/User.java @@ -0,0 +1,45 @@ +package com.baeldung.bootcustomfilters.model; + +/** + * User model + * @author hemant + * + */ +public class User { + + private String id; + private String name; + private String email; + + public User(String id, String name, String email) { + super(); + this.id = id; + this.name = name; + this.email = email; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + +}