Skip to content

Commit 945d32b

Browse files
authored
23 define and create filter interface (#46)
* initial commit, added interfaces Filter and FilterChain * Added HttpRequest class, groups together all information about a request that the server needs and easier to handle by future filters * added interfaces Filter and FilterChain with Java Servlet Filter architecture. * added FilterChainImpl * Corrected imports from JDKS HttpRequest, to projects HttpRequest class * Changed, params for FilterChain * Updated HttpRequest with attributes, * Revert "Updated HttpRequest with attributes," This reverts commit 0fd490e.
1 parent bcb828c commit 945d32b

4 files changed

Lines changed: 98 additions & 0 deletions

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.example.filter;
2+
3+
import org.example.http.HttpResponseBuilder;
4+
5+
import org.example.httpparser.HttpRequest;
6+
7+
public interface Filter {
8+
void init();
9+
10+
void doFilter(HttpRequest request, HttpResponseBuilder response, FilterChain chain);
11+
12+
void destroy();
13+
14+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.example.filter;
2+
3+
import org.example.http.HttpResponseBuilder;
4+
import org.example.httpparser.HttpRequest;
5+
6+
7+
public interface FilterChain {
8+
9+
void doFilter(HttpRequest request, HttpResponseBuilder response);
10+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.example.filter;
2+
3+
import org.example.http.HttpResponseBuilder;
4+
import org.example.httpparser.HttpRequest;
5+
6+
7+
import java.util.List;
8+
9+
/*
10+
* The default class of FilterChain,
11+
* Contains a list of filters. For each of the filter, will execute the doFilter method.
12+
*
13+
*/
14+
15+
public class FilterChainImpl implements FilterChain {
16+
17+
private final List<Filter> filters;
18+
private int index = 0;
19+
20+
public FilterChainImpl(List<Filter> filters) {
21+
this.filters = filters;
22+
}
23+
24+
@Override
25+
public void doFilter(HttpRequest request, HttpResponseBuilder response) {
26+
if (index < filters.size()) {
27+
Filter next = filters.get(index++);
28+
next.doFilter(request, response, this);
29+
} else {
30+
// TODO: when no more filters, should execute the request
31+
}
32+
}
33+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package org.example.httpparser;
2+
3+
import java.util.Collections;
4+
import java.util.Map;
5+
6+
/*
7+
*
8+
*This class groups together all information about a request that the server needs
9+
*/
10+
11+
public class HttpRequest {
12+
13+
private final String method;
14+
private final String path;
15+
private final String version;
16+
private final Map<String, String> headers;
17+
private final String body;
18+
19+
public HttpRequest(String method,
20+
String path,
21+
String version,
22+
Map<String, String> headers,
23+
String body) {
24+
this.method = method;
25+
this.path = path;
26+
this.version = version;
27+
this.headers = headers != null ? Map.copyOf(headers) : Collections.emptyMap();
28+
this.body = body;
29+
}
30+
31+
public String getMethod() {
32+
return method; }
33+
public String getPath() {
34+
return path; }
35+
public String getVersion() {
36+
return version; }
37+
public Map<String, String> getHeaders() {
38+
return headers; }
39+
public String getBody() {
40+
return body; }
41+
}

0 commit comments

Comments
 (0)