filters;
+
+ public FilterChainImpl(Filter... filters) {
+ this.filters = Arrays.asList(filters).iterator();
+ }
+
+ @Override
+ public void doFilter(
+ ServletRequest request,
+ ServletResponse response
+ ) throws IOException, ServletException {
+ if (filters.hasNext()) {
+ Filter filter = filters.next();
+ filter.doFilter(request, response, this);
+ }
+ }
+}
diff --git a/enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/filters/FilterManager.java b/enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/filters/FilterManager.java
new file mode 100644
index 0000000000..efa8511f2a
--- /dev/null
+++ b/enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/filters/FilterManager.java
@@ -0,0 +1,21 @@
+package com.baeldung.enterprise.patterns.front.controller.filters;
+
+import javax.servlet.FilterChain;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+
+public class FilterManager {
+ public static void process(
+ HttpServletRequest request,
+ HttpServletResponse response,
+ OnIntercept callback
+ ) throws ServletException, IOException {
+ FilterChain filterChain = new FilterChainImpl(
+ new AuthenticationFilter(callback),
+ new AuditFilter()
+ );
+ filterChain.doFilter(request, response);
+ }
+}
diff --git a/enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/filters/LoggingFilter.java b/enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/filters/LoggingFilter.java
new file mode 100644
index 0000000000..089a7807cf
--- /dev/null
+++ b/enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/filters/LoggingFilter.java
@@ -0,0 +1,39 @@
+package com.baeldung.enterprise.patterns.front.controller.filters;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.servlet.FilterChain;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import javax.servlet.annotation.WebFilter;
+import javax.servlet.http.HttpServletRequest;
+import java.io.IOException;
+
+@WebFilter(servletNames = "front-controller")
+public class LoggingFilter extends BaseFilter {
+ private static final Logger log = LoggerFactory.getLogger(LoggingFilter.class);
+
+ @Override
+ public void doFilter(
+ ServletRequest request,
+ ServletResponse response,
+ FilterChain chain
+ ) throws IOException, ServletException {
+ chain.doFilter(request, response);
+ HttpServletRequest httpServletRequest = (HttpServletRequest) request;
+ String username = (String) httpServletRequest.getAttribute("username");
+ if (username != null && username.length() > 0) {
+ username = username.concat("@");
+ } else {
+ username = "";
+ }
+ String intro = "Request";
+ if (username.length() > 0) {
+ intro = "Authenticated request";
+ }
+ log.info("{} from '{}{}': {}?{}", intro, username, request.getRemoteAddr(),
+ httpServletRequest.getRequestURI(), request.getParameterMap());
+ }
+}
diff --git a/enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/filters/OnIntercept.java b/enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/filters/OnIntercept.java
new file mode 100644
index 0000000000..54da744e78
--- /dev/null
+++ b/enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/filters/OnIntercept.java
@@ -0,0 +1,5 @@
+package com.baeldung.enterprise.patterns.front.controller.filters;
+
+public interface OnIntercept {
+ void intercept();
+}
diff --git a/enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/filters/VisitorCounterFilter.java b/enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/filters/VisitorCounterFilter.java
new file mode 100644
index 0000000000..0ae7cd73fd
--- /dev/null
+++ b/enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/filters/VisitorCounterFilter.java
@@ -0,0 +1,23 @@
+package com.baeldung.enterprise.patterns.front.controller.filters;
+
+import javax.servlet.FilterChain;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import javax.servlet.annotation.WebFilter;
+import java.io.IOException;
+
+@WebFilter(servletNames = "front-controller")
+public class VisitorCounterFilter extends BaseFilter {
+ private int counter;
+
+ @Override
+ public void doFilter(
+ ServletRequest request,
+ ServletResponse response,
+ FilterChain chain
+ ) throws IOException, ServletException {
+ request.setAttribute("counter", ++counter);
+ chain.doFilter(request, response);
+ }
+}
diff --git a/enterprise-patterns/intercepting-filter-pattern/src/main/webapp/WEB-INF/jsp/book-found.jsp b/enterprise-patterns/intercepting-filter-pattern/src/main/webapp/WEB-INF/jsp/book-found.jsp
new file mode 100644
index 0000000000..36f94b0933
--- /dev/null
+++ b/enterprise-patterns/intercepting-filter-pattern/src/main/webapp/WEB-INF/jsp/book-found.jsp
@@ -0,0 +1,13 @@
+
+
+
+ Bookshelf: Title found
+
+
+ Our Bookshelf contains this title:
+ ${book.getTitle()}
+ Author: ${book.getAuthor()}
+
+ <%@include file="visitor-counter.jsp"%>
+
+
diff --git a/enterprise-patterns/intercepting-filter-pattern/src/main/webapp/WEB-INF/jsp/book-notfound.jsp b/enterprise-patterns/intercepting-filter-pattern/src/main/webapp/WEB-INF/jsp/book-notfound.jsp
new file mode 100644
index 0000000000..02d1307e00
--- /dev/null
+++ b/enterprise-patterns/intercepting-filter-pattern/src/main/webapp/WEB-INF/jsp/book-notfound.jsp
@@ -0,0 +1,11 @@
+
+
+
+ Bookshelf: Title not found
+
+
+ Our Bookshelf doesn't contains this title:
+ ${param.get("title")}
+ <%@include file="visitor-counter.jsp"%>
+
+
diff --git a/enterprise-patterns/intercepting-filter-pattern/src/main/webapp/WEB-INF/jsp/login.jsp b/enterprise-patterns/intercepting-filter-pattern/src/main/webapp/WEB-INF/jsp/login.jsp
new file mode 100644
index 0000000000..a1609ecaff
--- /dev/null
+++ b/enterprise-patterns/intercepting-filter-pattern/src/main/webapp/WEB-INF/jsp/login.jsp
@@ -0,0 +1,15 @@
+
+
+
+ Bookshelf: Login
+
+
+ Please input a username:
+ Login
+
+
+
diff --git a/enterprise-patterns/intercepting-filter-pattern/src/main/webapp/WEB-INF/jsp/unknown.jsp b/enterprise-patterns/intercepting-filter-pattern/src/main/webapp/WEB-INF/jsp/unknown.jsp
new file mode 100644
index 0000000000..010cb7ffb4
--- /dev/null
+++ b/enterprise-patterns/intercepting-filter-pattern/src/main/webapp/WEB-INF/jsp/unknown.jsp
@@ -0,0 +1,10 @@
+
+
+
+ Bookshelf: Command unknown
+
+
+ Sorry, this command is not known!
+ <%@include file="visitor-counter.jsp"%>
+
+
diff --git a/enterprise-patterns/intercepting-filter-pattern/src/main/webapp/WEB-INF/jsp/visitor-counter.jsp b/enterprise-patterns/intercepting-filter-pattern/src/main/webapp/WEB-INF/jsp/visitor-counter.jsp
new file mode 100644
index 0000000000..7dc1d78196
--- /dev/null
+++ b/enterprise-patterns/intercepting-filter-pattern/src/main/webapp/WEB-INF/jsp/visitor-counter.jsp
@@ -0,0 +1,2 @@
+
+You are visitor #${counter}!