0
# Request Processing
1
2
Components for parsing and extracting parameters from Spring WebFlux ServerWebExchange objects, enabling parameter-based flow control rules and request analysis.
3
4
## Capabilities
5
6
### ServerWebExchangeItemParser
7
8
Parses request items from Spring WebFlux ServerWebExchange, extracting various request attributes for use in Sentinel flow control rules.
9
10
```java { .api }
11
/**
12
* Parses request items from Spring WebFlux ServerWebExchange
13
* Implements RequestItemParser<ServerWebExchange> interface
14
*/
15
public class ServerWebExchangeItemParser implements RequestItemParser<ServerWebExchange> {
16
/**
17
* Default constructor
18
*/
19
public ServerWebExchangeItemParser();
20
21
/**
22
* Extracts the request path from the exchange
23
* @param exchange - Server web exchange
24
* @return String request path value
25
*/
26
public String getPath(ServerWebExchange exchange);
27
28
/**
29
* Extracts the remote client IP address from the exchange
30
* @param exchange - Server web exchange
31
* @return String remote address or null if not available
32
*/
33
public String getRemoteAddress(ServerWebExchange exchange);
34
35
/**
36
* Extracts a specific header value from the request
37
* @param exchange - Server web exchange
38
* @param key - Header name to extract
39
* @return String header value or null if not present
40
*/
41
public String getHeader(ServerWebExchange exchange, String key);
42
43
/**
44
* Extracts a URL query parameter value from the request
45
* @param exchange - Server web exchange
46
* @param paramName - Parameter name to extract
47
* @return String parameter value or null if not present
48
*/
49
public String getUrlParam(ServerWebExchange exchange, String paramName);
50
51
/**
52
* Extracts a cookie value from the request
53
* @param exchange - Server web exchange
54
* @param cookieName - Cookie name to extract
55
* @return String cookie value or null if not present
56
*/
57
public String getCookieValue(ServerWebExchange exchange, String cookieName);
58
}
59
```
60
61
**Usage Examples:**
62
63
```java
64
import com.alibaba.csp.sentinel.adapter.gateway.sc.ServerWebExchangeItemParser;
65
66
// Create parser instance
67
ServerWebExchangeItemParser parser = new ServerWebExchangeItemParser();
68
69
// Extract request information (typically done within flow control rules)
70
String path = parser.getPath(exchange);
71
String clientIp = parser.getRemoteAddress(exchange);
72
String userAgent = parser.getHeader(exchange, "User-Agent");
73
String userId = parser.getUrlParam(exchange, "userId");
74
String sessionId = parser.getCookieValue(exchange, "JSESSIONID");
75
76
// Use in custom SentinelGatewayFilter
77
@Bean
78
public GlobalFilter customSentinelFilter() {
79
ServerWebExchangeItemParser customParser = new ServerWebExchangeItemParser();
80
return new SentinelGatewayFilter(customParser);
81
}
82
```
83
84
## Integration with Flow Control Rules
85
86
### Parameter-Based Rules
87
The extracted request parameters can be used in Sentinel flow control rules for fine-grained traffic control:
88
89
```java
90
// Example: Configure parameter-based flow rule using extracted values
91
ParamFlowRule rule = new ParamFlowRule()
92
.setResource("api-gateway")
93
.setParamIdx(0) // First parameter (often path or client IP)
94
.setCount(10) // Limit to 10 requests
95
.setDurationInSec(1);
96
97
ParamFlowRuleManager.loadRules(Collections.singletonList(rule));
98
```
99
100
### Custom Parameter Extraction
101
You can extend `ServerWebExchangeItemParser` for custom parameter extraction logic:
102
103
```java
104
public class CustomServerWebExchangeItemParser extends ServerWebExchangeItemParser {
105
106
@Override
107
public String getHeader(ServerWebExchange exchange, String key) {
108
// Custom header processing logic
109
String headerValue = super.getHeader(exchange, key);
110
return processCustomHeader(headerValue);
111
}
112
113
// Add custom extraction methods
114
public String getCustomAttribute(ServerWebExchange exchange, String attributeName) {
115
return exchange.getAttribute(attributeName);
116
}
117
118
private String processCustomHeader(String headerValue) {
119
// Custom processing logic
120
return headerValue != null ? headerValue.toLowerCase() : null;
121
}
122
}
123
```
124
125
## Request Analysis Capabilities
126
127
### Path Analysis
128
- Extracts clean request path without query parameters
129
- Useful for path-based routing and flow control rules
130
- Returns normalized path value from Spring Cloud Gateway route processing
131
132
### Client Identification
133
- Extracts remote client IP address from request
134
- Handles proxy scenarios through Spring WebFlux's remote address resolution
135
- Returns null if remote address cannot be determined
136
137
### Header Processing
138
- Extracts any HTTP header by name
139
- Case-insensitive header name matching
140
- Returns first header value if multiple values exist
141
142
### Query Parameter Extraction
143
- Extracts URL query parameters by name
144
- Handles URL-encoded parameter values
145
- Returns first parameter value if multiple values exist
146
147
### Cookie Management
148
- Extracts cookie values by cookie name
149
- Integrates with Spring WebFlux cookie handling
150
- Provides null-safe cookie value extraction
151
152
## Error Handling
153
154
### Null Safety
155
All methods are designed to handle null input gracefully:
156
- Return `null` when requested information is not available
157
- Do not throw exceptions for missing headers, parameters, or cookies
158
- Safe to use in flow control rule conditions
159
160
### Exception Scenarios
161
- **Network Issues**: `getRemoteAddress()` returns `null` if remote address cannot be determined
162
- **Missing Data**: All extraction methods return `null` for missing data rather than throwing exceptions
163
- **Malformed Requests**: Parser handles malformed requests gracefully without disrupting gateway processing