0
# Valves and Pipeline
1
2
Request processing pipeline using chain-of-responsibility pattern with valves for cross-cutting concerns including access logging, authentication, error handling, compression, URL rewriting, remote address filtering, and custom request/response processing.
3
4
## Capabilities
5
6
### Valve Interface
7
8
Base interface for all valves in the processing pipeline.
9
10
```java { .api }
11
public interface Valve {
12
// Next valve in chain
13
Valve getNext();
14
void setNext(Valve valve);
15
16
// Processing
17
void invoke(Request request, Response response) throws IOException, ServletException;
18
boolean isAsyncSupported();
19
20
// Background processing
21
void backgroundProcess();
22
}
23
```
24
25
### Pipeline Interface
26
27
Container for managing valve chains.
28
29
```java { .api }
30
public interface Pipeline extends Contained {
31
// Basic valve (end of chain)
32
Valve getBasic();
33
void setBasic(Valve valve);
34
35
// Valve management
36
void addValve(Valve valve);
37
Valve[] getValves();
38
void removeValve(Valve valve);
39
Valve getFirst();
40
41
// Async support
42
boolean isAsyncSupported();
43
void findNonAsyncValves(Set<String> result);
44
}
45
```
46
47
### Common Valve Types
48
49
Standard valves provided by Tomcat.
50
51
```java { .api }
52
// Access logging valve
53
public class AccessLogValve extends ValveBase implements AccessLog {
54
// Log pattern configuration
55
public void setPattern(String pattern);
56
public String getPattern();
57
58
// File configuration
59
public void setDirectory(String directory);
60
public String getDirectory();
61
public void setPrefix(String prefix);
62
public String getPrefix();
63
public void setSuffix(String suffix);
64
public String getSuffix();
65
66
// Rotation
67
public void setRotatable(boolean rotatable);
68
public boolean isRotatable();
69
public void setFileDateFormat(String fileDateFormat);
70
public String getFileDateFormat();
71
72
// Buffering
73
public void setBuffered(boolean buffered);
74
public boolean isBuffered();
75
76
// Request attributes
77
public void setRequestAttributesEnabled(boolean requestAttributesEnabled);
78
public boolean getRequestAttributesEnabled();
79
80
@Override
81
public void invoke(Request request, Response response) throws IOException, ServletException;
82
public void log(Request request, Response response, long time);
83
}
84
85
// Error report valve
86
public class ErrorReportValve extends ValveBase {
87
public void setShowReport(boolean showReport);
88
public boolean isShowReport();
89
public void setShowServerInfo(boolean showServerInfo);
90
public boolean isShowServerInfo();
91
92
@Override
93
public void invoke(Request request, Response response) throws IOException, ServletException;
94
}
95
96
// Remote address filter valve
97
public class RemoteAddrValve extends RequestFilterValve {
98
@Override
99
public void invoke(Request request, Response response) throws IOException, ServletException;
100
}
101
102
// Remote host filter valve
103
public class RemoteHostValve extends RequestFilterValve {
104
@Override
105
public void invoke(Request request, Response response) throws IOException, ServletException;
106
}
107
108
// Remote IP valve (X-Forwarded-For handling)
109
public class RemoteIpValve extends ValveBase {
110
public void setRemoteIpHeader(String header);
111
public void setProxiesHeader(String header);
112
public void setTrustedProxies(String trustedProxies);
113
@Override
114
public void invoke(Request request, Response response) throws IOException, ServletException;
115
}
116
117
// SSL valve (SSL termination support)
118
public class SSLValve extends ValveBase {
119
public void setSslCipherHeader(String header);
120
public void setSslSessionIdHeader(String header);
121
public void setSslClientCertHeader(String header);
122
@Override
123
public void invoke(Request request, Response response) throws IOException, ServletException;
124
}
125
126
// Stuck thread detection valve
127
public class StuckThreadDetectionValve extends ValveBase {
128
public void setThreshold(int threshold);
129
public void setInterruptThreadThreshold(int interruptThreadThreshold);
130
@Override
131
public void invoke(Request request, Response response) throws IOException, ServletException;
132
}
133
```
134
135
## Usage Examples
136
137
### Adding Custom Valve
138
139
```java
140
import org.apache.catalina.connector.Request;
141
import org.apache.catalina.connector.Response;
142
import org.apache.catalina.valves.ValveBase;
143
import org.apache.catalina.startup.Tomcat;
144
import org.apache.catalina.Context;
145
import jakarta.servlet.ServletException;
146
import java.io.IOException;
147
148
public class CustomValveExample extends ValveBase {
149
@Override
150
public void invoke(Request request, Response response) throws IOException, ServletException {
151
// Pre-processing
152
long startTime = System.currentTimeMillis();
153
System.out.println("Request: " + request.getRequestURI());
154
155
// Continue chain
156
getNext().invoke(request, response);
157
158
// Post-processing
159
long duration = System.currentTimeMillis() - startTime;
160
System.out.println("Completed in " + duration + "ms");
161
}
162
163
public static void main(String[] args) throws Exception {
164
Tomcat tomcat = new Tomcat();
165
tomcat.setPort(8080);
166
167
Context ctx = tomcat.addContext("", System.getProperty("java.io.tmpdir"));
168
169
// Add custom valve
170
ctx.getPipeline().addValve(new CustomValveExample());
171
172
tomcat.start();
173
tomcat.getServer().await();
174
}
175
}
176
```
177