Skip to content

Commit

Permalink
Merge pull request #40 from Terrdi/dev
Browse files Browse the repository at this point in the history
close #5
  • Loading branch information
cybertheye committed Nov 21, 2023
2 parents 2ff0bb0 + eb7efdb commit 2fb8591
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
import java.io.UnsupportedEncodingException;

/**
* 过滤器
*
* @description:
*/

public interface Filter {
boolean doFilter(MTRequest request, MTResponse response) throws UnsupportedEncodingException;
boolean doFilter(MTRequest request, MTResponse response, FilterChain filterChain) throws UnsupportedEncodingException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
* @description:
Expand All @@ -16,6 +18,8 @@ public class FilterChainImpl implements FilterChain {
private FilterNode head = new FilterNode(); //soldier
private FilterNode tail;

private FilterNode currNode = null;

private Servlet targetServlet;

private FilterChainImpl(Servlet targetServlet) {
Expand Down Expand Up @@ -61,15 +65,18 @@ public void addLast(List<Filter> filters) {
*/
@Override
public void start(MTRequest request, MTResponse response) {

FilterNode traveler = head.getNext();
// 获取下一个过滤器
// 如果没有过滤器, 则执行对应的servlet
currNode = Optional.ofNullable(currNode).orElse(head).getNext();
try {
while (traveler != null && traveler.exec(request, response)) {
traveler=traveler.next;
// while (traveler != null && traveler.exec(request, response)) {
// traveler=traveler.next;
// }
if (Objects.isNull(currNode)) {
targetServlet.service(request,response);
} else {
currNode.exec(request, response, this);
}

targetServlet.service(request,response);

} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ public class FilterNode {
Filter filter;
FilterNode next;

public boolean exec(MTRequest request, MTResponse response) throws UnsupportedEncodingException {
return filter.doFilter(request,response);
public boolean exec(MTRequest request, MTResponse response, FilterChain filterChain) throws UnsupportedEncodingException {
// return filter.doFilter(request,response);
// filterChain.start(request, response);
filter.doFilter(request, response, filterChain);
return true;
}

public Filter getFilter() {
Expand Down
7 changes: 5 additions & 2 deletions mimic-tomcat/src/test/java/project/filter/FifthFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.attackonarchitect.filter.WebFilter;
import com.attackonarchitect.filter.chain.Filter;
import com.attackonarchitect.filter.chain.FilterChain;
import com.attackonarchitect.http.MTRequest;
import com.attackonarchitect.http.MTResponse;

Expand All @@ -14,8 +15,10 @@
@WebFilter("/hello/b")
public class FifthFilter implements Filter {
@Override
public boolean doFilter(MTRequest request, MTResponse response) throws UnsupportedEncodingException {
response.write("pass filter fifth /hello/b");

public boolean doFilter(MTRequest request, MTResponse response, FilterChain filterChain) throws UnsupportedEncodingException {
response.write("pass filter /hello/b");
filterChain.start(request, response);
return true;
}
}
8 changes: 6 additions & 2 deletions mimic-tomcat/src/test/java/project/filter/FirstFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.attackonarchitect.filter.WebFilter;
import com.attackonarchitect.filter.chain.Filter;
import com.attackonarchitect.filter.chain.FilterChain;
import com.attackonarchitect.http.MTRequest;
import com.attackonarchitect.http.MTResponse;

Expand All @@ -14,8 +15,11 @@
public class FirstFilter implements Filter {

@Override
public boolean doFilter(MTRequest request, MTResponse response) throws UnsupportedEncodingException {
response.write("pass filter first /*");

public boolean doFilter(MTRequest request, MTResponse response, FilterChain filterChain) throws UnsupportedEncodingException {
response.write("pass filter /*");
filterChain.start(request, response);

return true;
}
}
7 changes: 5 additions & 2 deletions mimic-tomcat/src/test/java/project/filter/FourthFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.attackonarchitect.filter.WebFilter;
import com.attackonarchitect.filter.chain.Filter;
import com.attackonarchitect.filter.chain.FilterChain;
import com.attackonarchitect.http.MTRequest;
import com.attackonarchitect.http.MTResponse;

Expand All @@ -14,10 +15,12 @@
public class FourthFilter implements Filter {

@Override
public boolean doFilter(MTRequest request, MTResponse response) throws UnsupportedEncodingException {
public boolean doFilter(MTRequest request, MTResponse response, FilterChain filterChain) throws UnsupportedEncodingException {

response.write("pass filter fourth /hello/a/b/*");
response.write("pass filter /hello/a/b/*");
filterChain.start(request, response);

response.write("after pass");
return true;
}
}
8 changes: 6 additions & 2 deletions mimic-tomcat/src/test/java/project/filter/SecondFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.attackonarchitect.filter.WebFilter;
import com.attackonarchitect.filter.chain.Filter;
import com.attackonarchitect.filter.chain.FilterChain;
import com.attackonarchitect.http.MTRequest;
import com.attackonarchitect.http.MTResponse;

Expand All @@ -14,10 +15,13 @@
public class SecondFilter implements Filter {

@Override
public boolean doFilter(MTRequest request, MTResponse response) throws UnsupportedEncodingException {
public boolean doFilter(MTRequest request, MTResponse response, FilterChain filterChain) throws UnsupportedEncodingException {
request.setAttribute("china","niubi");

response.write("pass filter second /hello/*");

response.write("pass filter /hello/*");
filterChain.start(request, response);

return true;
}
}
8 changes: 6 additions & 2 deletions mimic-tomcat/src/test/java/project/filter/SixthFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.attackonarchitect.filter.WebFilter;
import com.attackonarchitect.filter.chain.Filter;
import com.attackonarchitect.filter.chain.FilterChain;
import com.attackonarchitect.http.MTRequest;
import com.attackonarchitect.http.MTResponse;

Expand All @@ -13,8 +14,11 @@
@WebFilter(order = 0)
public class SixthFilter implements Filter {
@Override
public boolean doFilter(MTRequest request, MTResponse response) throws UnsupportedEncodingException {
response.write("pass filter sixth /*");

public boolean doFilter(MTRequest request, MTResponse response, FilterChain filterChain) throws UnsupportedEncodingException {
response.write("pass another filter /*");
filterChain.start(request, response);

return true;
}
}
8 changes: 6 additions & 2 deletions mimic-tomcat/src/test/java/project/filter/ThirdFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.attackonarchitect.filter.WebFilter;
import com.attackonarchitect.filter.chain.Filter;
import com.attackonarchitect.filter.chain.FilterChain;
import com.attackonarchitect.http.MTRequest;
import com.attackonarchitect.http.MTResponse;

Expand All @@ -14,8 +15,11 @@
public class ThirdFilter implements Filter {

@Override
public boolean doFilter(MTRequest request, MTResponse response) throws UnsupportedEncodingException {
response.write("pass filter third /hello/a/*");

public boolean doFilter(MTRequest request, MTResponse response, FilterChain filterChain) throws UnsupportedEncodingException {
response.write("pass filter /hello/a/*");
filterChain.start(request, response);

return true;
}
}

0 comments on commit 2fb8591

Please sign in to comment.