BAEL-325: Added UML sources. Applied some small improvements.
This commit is contained in:
parent
67e4090b57
commit
2d678d6b32
|
@ -3,22 +3,23 @@ package com.baeldung.patterns.filters;
|
||||||
import com.baeldung.patterns.commands.FrontCommand;
|
import com.baeldung.patterns.commands.FrontCommand;
|
||||||
import com.baeldung.patterns.commands.LoginCommand;
|
import com.baeldung.patterns.commands.LoginCommand;
|
||||||
|
|
||||||
import javax.servlet.FilterChain;
|
import javax.servlet.*;
|
||||||
import javax.servlet.ServletException;
|
|
||||||
import javax.servlet.ServletRequest;
|
|
||||||
import javax.servlet.ServletResponse;
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
import javax.servlet.http.HttpSession;
|
import javax.servlet.http.HttpSession;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
public class AuthenticationFilter extends BaseFilter {
|
public class AuthenticationFilter implements Filter {
|
||||||
private OnIntercept callback;
|
private OnIntercept callback;
|
||||||
|
|
||||||
public AuthenticationFilter(OnIntercept callback) {
|
public AuthenticationFilter(OnIntercept callback) {
|
||||||
this.callback = callback;
|
this.callback = callback;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void init(FilterConfig filterConfig) throws ServletException {
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void doFilter(
|
public void doFilter(
|
||||||
ServletRequest request,
|
ServletRequest request,
|
||||||
|
@ -37,4 +38,8 @@ public class AuthenticationFilter extends BaseFilter {
|
||||||
chain.doFilter(request, response);
|
chain.doFilter(request, response);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void destroy() {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,9 +2,13 @@ package com.baeldung.patterns.filters;
|
||||||
|
|
||||||
import javax.servlet.*;
|
import javax.servlet.*;
|
||||||
import javax.servlet.annotation.WebFilter;
|
import javax.servlet.annotation.WebFilter;
|
||||||
|
import javax.servlet.annotation.WebInitParam;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
@WebFilter(servletNames = "front-controller")
|
@WebFilter(
|
||||||
|
servletNames = {"front-controller"},
|
||||||
|
initParams = {@WebInitParam(name = "encoding", value = "UTF-8")}
|
||||||
|
)
|
||||||
public class EncodingFilter extends BaseFilter {
|
public class EncodingFilter extends BaseFilter {
|
||||||
private String encoding;
|
private String encoding;
|
||||||
|
|
||||||
|
|
|
@ -1,14 +1,11 @@
|
||||||
package com.baeldung.patterns.filters;
|
package com.baeldung.patterns.filters;
|
||||||
|
|
||||||
import javax.servlet.Filter;
|
import javax.servlet.*;
|
||||||
import javax.servlet.ServletException;
|
|
||||||
import javax.servlet.ServletRequest;
|
|
||||||
import javax.servlet.ServletResponse;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
|
||||||
public class FilterChainImpl implements javax.servlet.FilterChain {
|
public class FilterChainImpl implements FilterChain {
|
||||||
private Iterator<Filter> filters;
|
private Iterator<Filter> filters;
|
||||||
|
|
||||||
public FilterChainImpl(Filter... filters) {
|
public FilterChainImpl(Filter... filters) {
|
||||||
|
|
|
@ -0,0 +1,35 @@
|
||||||
|
package com.baeldung.patterns.filters;
|
||||||
|
|
||||||
|
import javax.servlet.FilterChain;
|
||||||
|
import javax.servlet.ServletException;
|
||||||
|
import javax.servlet.ServletRequest;
|
||||||
|
import javax.servlet.ServletResponse;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public abstract class TemplateFilter extends BaseFilter {
|
||||||
|
|
||||||
|
protected abstract void preFilter(
|
||||||
|
HttpServletRequest request,
|
||||||
|
HttpServletResponse response
|
||||||
|
) throws IOException, ServletException;
|
||||||
|
|
||||||
|
protected abstract void postFilter(
|
||||||
|
HttpServletRequest request,
|
||||||
|
HttpServletResponse response
|
||||||
|
) throws IOException, ServletException;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void doFilter(
|
||||||
|
ServletRequest request,
|
||||||
|
ServletResponse response,
|
||||||
|
FilterChain chain
|
||||||
|
) throws IOException, ServletException {
|
||||||
|
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
|
||||||
|
HttpServletResponse httpServletResponse = (HttpServletResponse) response;
|
||||||
|
preFilter(httpServletRequest, httpServletResponse);
|
||||||
|
chain.doFilter(request, response);
|
||||||
|
postFilter(httpServletRequest, httpServletResponse);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,18 +1,19 @@
|
||||||
package com.baeldung.patterns.filters;
|
package com.baeldung.patterns.filters;
|
||||||
|
|
||||||
import javax.servlet.FilterChain;
|
import javax.servlet.*;
|
||||||
import javax.servlet.ServletException;
|
|
||||||
import javax.servlet.ServletRequest;
|
|
||||||
import javax.servlet.ServletResponse;
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpSession;
|
import javax.servlet.http.HttpSession;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
public class VisitorCounterFilter extends BaseFilter {
|
public class VisitorCounterFilter implements Filter {
|
||||||
private static Set<String> users = new HashSet<>();
|
private static Set<String> users = new HashSet<>();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void init(FilterConfig filterConfig) throws ServletException {
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void doFilter(
|
public void doFilter(
|
||||||
ServletRequest request,
|
ServletRequest request,
|
||||||
|
@ -21,10 +22,12 @@ public class VisitorCounterFilter extends BaseFilter {
|
||||||
) throws IOException, ServletException {
|
) throws IOException, ServletException {
|
||||||
HttpSession session = ((HttpServletRequest) request).getSession(false);
|
HttpSession session = ((HttpServletRequest) request).getSession(false);
|
||||||
String username = (String) session.getAttribute("username");
|
String username = (String) session.getAttribute("username");
|
||||||
if (!users.contains(username)) {
|
|
||||||
users.add(username);
|
users.add(username);
|
||||||
}
|
|
||||||
request.setAttribute("counter", users.size());
|
request.setAttribute("counter", users.size());
|
||||||
chain.doFilter(request, response);
|
chain.doFilter(request, response);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void destroy() {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Before Width: | Height: | Size: 5.2 KiB After Width: | Height: | Size: 5.2 KiB |
Binary file not shown.
After Width: | Height: | Size: 17 KiB |
|
@ -0,0 +1,17 @@
|
||||||
|
@startuml
|
||||||
|
scale 1.5
|
||||||
|
|
||||||
|
class Client
|
||||||
|
class FilterManager
|
||||||
|
interface FilterChain
|
||||||
|
interface "Filter 1"
|
||||||
|
interface "Filter 2"
|
||||||
|
interface "Filter 3"
|
||||||
|
|
||||||
|
Client .right.-- FilterManager
|
||||||
|
FilterManager .right.-- Target
|
||||||
|
FilterManager -- FilterChain
|
||||||
|
FilterChain .right.-- "Filter 3"
|
||||||
|
FilterChain .right.-- "Filter 2"
|
||||||
|
FilterChain .right.-- "Filter 1"
|
||||||
|
@enduml
|
Loading…
Reference in New Issue