36
36
import io .netty .handler .codec .http .HttpVersion ;
37
37
import io .netty .handler .codec .http .LastHttpContent ;
38
38
import io .netty .util .ReferenceCountUtil ;
39
-
40
- import javax .annotation .Nullable ;
41
39
import java .nio .charset .StandardCharsets ;
42
40
import java .util .Collections ;
43
41
import java .util .Map ;
44
42
import java .util .concurrent .TimeUnit ;
43
+ import javax .annotation .Nullable ;
45
44
46
45
/**
47
46
* A collection of rejection related utilities useful for failing requests. These are tightly coupled with the channel
@@ -56,9 +55,9 @@ public final class RejectionUtils {
56
55
/**
57
56
* Closes the connection without sending a response, and fires a {@link RequestRejectedEvent} back up the pipeline.
58
57
*
59
- * @param nfStatus the status to use for metric reporting
60
- * @param reason the reason for rejecting the request. This is not sent back to the client.
61
- * @param request the request that is being rejected.
58
+ * @param nfStatus the status to use for metric reporting
59
+ * @param reason the reason for rejecting the request. This is not sent back to the client.
60
+ * @param request the request that is being rejected.
62
61
* @param injectedLatencyMillis optional parameter to delay sending a response. The reject notification is still
63
62
* sent up the pipeline.
64
63
*/
@@ -68,6 +67,10 @@ public static void rejectByClosingConnection(
68
67
String reason ,
69
68
HttpRequest request ,
70
69
@ Nullable Integer injectedLatencyMillis ) {
70
+
71
+ // Notify other handlers before closing the conn.
72
+ notifyHandlers (ctx , nfStatus , REJECT_CLOSING_STATUS , reason , request );
73
+
71
74
if (injectedLatencyMillis != null && injectedLatencyMillis > 0 ) {
72
75
// Delay closing the connection for configured time.
73
76
ctx .executor ()
@@ -83,23 +86,20 @@ public static void rejectByClosingConnection(
83
86
CurrentPassport .fromChannel (ctx .channel ()).add (PassportState .SERVER_CH_REJECTING );
84
87
ctx .close ();
85
88
}
86
-
87
- // Notify other handlers that we've rejected this request.
88
- notifyHandlers (ctx , nfStatus , REJECT_CLOSING_STATUS , reason , request );
89
89
}
90
90
91
91
/**
92
92
* Sends a rejection response back to the client, and fires a {@link RequestRejectedEvent} back up the pipeline.
93
93
*
94
- * @param ctx the channel handler processing the request
95
- * @param nfStatus the status to use for metric reporting
96
- * @param reason the reason for rejecting the request. This is not sent back to the client.
97
- * @param request the request that is being rejected.
94
+ * @param ctx the channel handler processing the request
95
+ * @param nfStatus the status to use for metric reporting
96
+ * @param reason the reason for rejecting the request. This is not sent back to the client.
97
+ * @param request the request that is being rejected.
98
98
* @param injectedLatencyMillis optional parameter to delay sending a response. The reject notification is still
99
99
* sent up the pipeline.
100
- * @param rejectedCode the HTTP code to send back to the client.
101
- * @param rejectedBody the HTTP body to be sent back. It is assumed to be of type text/plain.
102
- * @param rejectionHeaders additional HTTP headers to add to the rejection response
100
+ * @param rejectedCode the HTTP code to send back to the client.
101
+ * @param rejectedBody the HTTP body to be sent back. It is assumed to be of type text/plain.
102
+ * @param rejectionHeaders additional HTTP headers to add to the rejection response
103
103
*/
104
104
public static void sendRejectionResponse (
105
105
ChannelHandlerContext ctx ,
@@ -152,16 +152,16 @@ public static void allowThenClose(ChannelHandlerContext ctx) {
152
152
* Throttle either by sending rejection response message, or by closing the connection now, or just drop the
153
153
* message. Only call this if ThrottleResult.shouldThrottle() returned {@code true}.
154
154
*
155
- * @param ctx the channel handler processing the request
156
- * @param msg the request that is being rejected.
157
- * @param rejectionType the type of rejection
158
- * @param nfStatus the status to use for metric reporting
159
- * @param reason the reason for rejecting the request. This is not sent back to the client.
155
+ * @param ctx the channel handler processing the request
156
+ * @param msg the request that is being rejected.
157
+ * @param rejectionType the type of rejection
158
+ * @param nfStatus the status to use for metric reporting
159
+ * @param reason the reason for rejecting the request. This is not sent back to the client.
160
160
* @param injectedLatencyMillis optional parameter to delay sending a response. The reject notification is still
161
161
* sent up the pipeline.
162
- * @param rejectedCode the HTTP code to send back to the client.
163
- * @param rejectedBody the HTTP body to be sent back. It is assumed to be of type text/plain.
164
- * @param rejectionHeaders additional HTTP headers to add to the rejection response
162
+ * @param rejectedCode the HTTP code to send back to the client.
163
+ * @param rejectedBody the HTTP body to be sent back. It is assumed to be of type text/plain.
164
+ * @param rejectionHeaders additional HTTP headers to add to the rejection response
165
165
*/
166
166
public static void handleRejection (
167
167
ChannelHandlerContext ctx ,
@@ -214,15 +214,15 @@ public static void handleRejection(
214
214
/**
215
215
* Switches on the rejection type to decide how to reject the request and or close the conn.
216
216
*
217
- * @param ctx the channel handler processing the request
218
- * @param rejectionType the type of rejection
219
- * @param nfStatus the status to use for metric reporting
220
- * @param reason the reason for rejecting the request. This is not sent back to the client.
221
- * @param request the request that is being rejected.
217
+ * @param ctx the channel handler processing the request
218
+ * @param rejectionType the type of rejection
219
+ * @param nfStatus the status to use for metric reporting
220
+ * @param reason the reason for rejecting the request. This is not sent back to the client.
221
+ * @param request the request that is being rejected.
222
222
* @param injectedLatencyMillis optional parameter to delay sending a response. The reject notification is still
223
223
* sent up the pipeline.
224
- * @param rejectedCode the HTTP code to send back to the client.
225
- * @param rejectedBody the HTTP body to be sent back. It is assumed to be of type text/plain.
224
+ * @param rejectedCode the HTTP code to send back to the client.
225
+ * @param rejectedBody the HTTP body to be sent back. It is assumed to be of type text/plain.
226
226
*/
227
227
public static void reject (
228
228
ChannelHandlerContext ctx ,
@@ -248,16 +248,16 @@ public static void reject(
248
248
/**
249
249
* Switches on the rejection type to decide how to reject the request and or close the conn.
250
250
*
251
- * @param ctx the channel handler processing the request
252
- * @param rejectionType the type of rejection
253
- * @param nfStatus the status to use for metric reporting
254
- * @param reason the reason for rejecting the request. This is not sent back to the client.
255
- * @param request the request that is being rejected.
251
+ * @param ctx the channel handler processing the request
252
+ * @param rejectionType the type of rejection
253
+ * @param nfStatus the status to use for metric reporting
254
+ * @param reason the reason for rejecting the request. This is not sent back to the client.
255
+ * @param request the request that is being rejected.
256
256
* @param injectedLatencyMillis optional parameter to delay sending a response. The reject notification is still
257
257
* sent up the pipeline.
258
- * @param rejectedCode the HTTP code to send back to the client.
259
- * @param rejectedBody the HTTP body to be sent back. It is assumed to be of type text/plain.
260
- * @param rejectionHeaders additional HTTP headers to add to the rejection response
258
+ * @param rejectedCode the HTTP code to send back to the client.
259
+ * @param rejectedBody the HTTP body to be sent back. It is assumed to be of type text/plain.
260
+ * @param rejectionHeaders additional HTTP headers to add to the rejection response
261
261
*/
262
262
public static void reject (
263
263
ChannelHandlerContext ctx ,
@@ -306,7 +306,7 @@ private static boolean closeConnectionAfterReject(Channel channel) {
306
306
if (channel .hasAttr (HAProxyMessageChannelHandler .ATTR_HAPROXY_VERSION )) {
307
307
return HAProxyProtocolVersion .V2
308
308
== channel .attr (HAProxyMessageChannelHandler .ATTR_HAPROXY_VERSION )
309
- .get ();
309
+ .get ();
310
310
} else {
311
311
return false ;
312
312
}
@@ -330,5 +330,6 @@ private static FullHttpResponse createRejectionResponse(
330
330
return new DefaultFullHttpResponse (HttpVersion .HTTP_1_1 , status , body , headers , EmptyHttpHeaders .INSTANCE );
331
331
}
332
332
333
- private RejectionUtils () {}
333
+ private RejectionUtils () {
334
+ }
334
335
}
0 commit comments