-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
yao.fei
committed
Dec 7, 2023
1 parent
961ac67
commit 3256c55
Showing
7 changed files
with
121 additions
and
125 deletions.
There are no files selected for viewing
12 changes: 0 additions & 12 deletions
12
mimic-tomcat/src/main/java/com/attackonarchitect/filter/chain/Chain.java
This file was deleted.
Oops, something went wrong.
105 changes: 105 additions & 0 deletions
105
mimic-tomcat/src/main/java/com/attackonarchitect/filter/chain/DefaultFilterChain.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package com.attackonarchitect.filter.chain; | ||
|
||
import com.attackonarchitect.http.MTRequest; | ||
import com.attackonarchitect.http.MTResponse; | ||
import com.attackonarchitect.servlet.Servlet; | ||
|
||
import java.io.UnsupportedEncodingException; | ||
import java.util.Collections; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
|
||
/** | ||
* @description: | ||
*/ | ||
public class DefaultFilterChain implements FilterChain { | ||
|
||
//servlet | ||
private Servlet targetServlet; | ||
|
||
//当前filter | ||
private Filter currentFilter; | ||
|
||
//filters | ||
private List<Filter> filters; | ||
|
||
private DefaultFilterChain chain; | ||
|
||
public DefaultFilterChain(Servlet targetServlet, List<Filter> filters) { | ||
this.targetServlet = targetServlet; | ||
initChain(filters); | ||
} | ||
|
||
public DefaultFilterChain(Servlet targetServlet, List<Filter> invokerFilters, Filter currentFilter, DefaultFilterChain chain) { | ||
this.targetServlet = targetServlet; | ||
this.filters = invokerFilters; | ||
this.currentFilter = currentFilter; | ||
this.chain = chain; | ||
} | ||
|
||
//初始化filter链 | ||
private DefaultFilterChain initChain(List<Filter> filters) { | ||
DefaultFilterChain chain = new DefaultFilterChain(this.targetServlet, filters, null, null); | ||
if (!isEmpty(filters)) { | ||
Collections.reverse(filters); | ||
Iterator<Filter> iterator = filters.iterator(); | ||
while (iterator.hasNext()) { | ||
chain = new DefaultFilterChain(null, filters, iterator.next(), chain); | ||
} | ||
} | ||
return chain; | ||
} | ||
|
||
@Override | ||
public void addFirst(List<Filter> filters) { | ||
|
||
if (isEmpty(this.filters)) { | ||
this.filters = filters; | ||
} else { | ||
this.filters.addAll(filters); | ||
} | ||
initChain(this.filters); | ||
|
||
} | ||
|
||
@Override | ||
public void addLast(List<Filter> filters) { | ||
|
||
if (isEmpty(this.filters)) { | ||
this.filters = filters; | ||
} else { | ||
this.filters.addAll(this.filters.size() - 1, filters); | ||
} | ||
initChain(this.filters); | ||
|
||
} | ||
|
||
@Override | ||
public void doFilter(MTRequest request, MTResponse response) { | ||
|
||
if (this.currentFilter != null) { | ||
try { | ||
this.currentFilter.doFilter(request, response); | ||
} catch (UnsupportedEncodingException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
if (this.chain != null) { | ||
this.chain.doFilter(request, response); | ||
} | ||
|
||
if (this.targetServlet!= null) { | ||
try { | ||
this.targetServlet.service(request, response); | ||
} catch (UnsupportedEncodingException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} | ||
|
||
private boolean isEmpty(List<Filter> filters) { | ||
return filters == null || filters.size() == 0; | ||
} | ||
|
||
} |
9 changes: 8 additions & 1 deletion
9
mimic-tomcat/src/main/java/com/attackonarchitect/filter/chain/FilterChain.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,20 @@ | ||
package com.attackonarchitect.filter.chain; | ||
|
||
import com.attackonarchitect.http.MTRequest; | ||
import com.attackonarchitect.http.MTResponse; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* @description: | ||
*/ | ||
|
||
public interface FilterChain extends Chain{ | ||
public interface FilterChain { | ||
|
||
void addFirst(List<Filter> filters); | ||
|
||
void addLast(List<Filter> filters); | ||
|
||
void doFilter(MTRequest request, MTResponse response); | ||
|
||
} |
71 changes: 0 additions & 71 deletions
71
mimic-tomcat/src/main/java/com/attackonarchitect/filter/chain/FilterChainImpl.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 0 additions & 34 deletions
34
mimic-tomcat/src/main/java/com/attackonarchitect/filter/chain/FilterNode.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters