-
Notifications
You must be signed in to change notification settings - Fork 78
/
HttpFilter.java
129 lines (120 loc) · 5.33 KB
/
HttpFilter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
* Copyright (c) 1997, 2021 Oracle and/or its affiliates and others.
* All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package jakarta.servlet.http;
import jakarta.servlet.FilterChain;
import jakarta.servlet.GenericFilter;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import java.io.IOException;
/**
*
* <p>
* Provides an abstract class to be subclassed to create an HTTP filter suitable for a Web site. A subclass of
* <code>HttpFilter</code> should override
* {@link #doFilter(jakarta.servlet.http.HttpServletRequest, jakarta.servlet.http.HttpServletResponse, jakarta.servlet.FilterChain) }.
* </p>
*
* <p>
* Filters typically run on multithreaded servers, so be aware that a filter must handle concurrent requests and be
* careful to synchronize access to shared resources. Shared resources include in-memory data such as instance or class
* variables and external objects such as files, database connections, and network connections. See the
* <a href="https://docs.oracle.com/javase/tutorial/essential/concurrency/"> Java Tutorial on Multithreaded
* Programming</a> for more information on handling multiple threads in a Java program.
*
* @author Various
*
* @since Servlet 4.0
*/
public abstract class HttpFilter extends GenericFilter {
private static final long serialVersionUID = 7478463438252262094L;
/**
* <p>
* Does nothing, because this is an abstract class.
* </p>
*
* @since Servlet 4.0
*/
public HttpFilter() {
}
/**
*
* <p>
* The <code>doFilter</code> method of the Filter is called by the container each time a request/response pair is passed
* through the chain due to a client request for a resource at the end of the chain. The FilterChain passed in to this
* method allows the Filter to pass on the request and response to the next entity in the chain. There's no need to
* override this method.
* </p>
*
* <p>
* The default implementation inspects the incoming {@code req} and {@code res} objects to determine if they are
* instances of {@link HttpServletRequest} and {@link HttpServletResponse}, respectively. If not, a
* {@link ServletException} is thrown. Otherwise, the protected
* {@link #doFilter(jakarta.servlet.http.HttpServletRequest, jakarta.servlet.http.HttpServletResponse, jakarta.servlet.FilterChain)}
* method is called.
* </p>
*
* @param req a {@link ServletRequest} object that contains the request the client has made of the filter
*
* @param res a {@link ServletResponse} object that contains the response the filter sends to the client
*
* @param chain the <code>FilterChain</code> for invoking the next filter or the resource
*
* @throws IOException if an input or output error is detected when the filter handles the request
*
* @throws ServletException if the request for the could not be handled or either parameter is not an instance of the
* respective {@link HttpServletRequest} or {@link HttpServletResponse}.
*
* @since Servlet 4.0
*/
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
throws IOException, ServletException {
if (!(req instanceof HttpServletRequest && res instanceof HttpServletResponse)) {
throw new ServletException("non-HTTP request or response");
}
this.doFilter((HttpServletRequest) req, (HttpServletResponse) res, chain);
}
/**
*
* <p>
* The <code>doFilter</code> method of the Filter is called by the container each time a request/response pair is passed
* through the chain due to a client request for a resource at the end of the chain. The FilterChain passed in to this
* method allows the Filter to pass on the request and response to the next entity in the chain.
* </p>
*
* <p>
* The default implementation simply calls {@link FilterChain#doFilter}
* </p>
*
* @param req a {@link HttpServletRequest} object that contains the request the client has made of the filter
*
* @param res a {@link HttpServletResponse} object that contains the response the filter sends to the client
*
* @param chain the <code>FilterChain</code> for invoking the next filter or the resource
*
* @throws IOException if an input or output error is detected when the filter handles the request
*
* @throws ServletException if the request for the could not be handled
*
* @since Servlet 4.0
*/
protected void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain chain)
throws IOException, ServletException {
chain.doFilter(req, res);
}
}