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 package org.apache.hc.core5.http.io.support;
28
29 import java.io.IOException;
30
31 import org.apache.hc.core5.annotation.Contract;
32 import org.apache.hc.core5.annotation.ThreadingBehavior;
33 import org.apache.hc.core5.http.ClassicHttpRequest;
34 import org.apache.hc.core5.http.ClassicHttpResponse;
35 import org.apache.hc.core5.http.Header;
36 import org.apache.hc.core5.http.HeaderElements;
37 import org.apache.hc.core5.http.HttpEntity;
38 import org.apache.hc.core5.http.HttpException;
39 import org.apache.hc.core5.http.HttpHeaders;
40 import org.apache.hc.core5.http.HttpResponse;
41 import org.apache.hc.core5.http.HttpStatus;
42 import org.apache.hc.core5.http.io.HttpFilterChain;
43 import org.apache.hc.core5.http.io.HttpFilterHandler;
44 import org.apache.hc.core5.http.message.BasicClassicHttpResponse;
45 import org.apache.hc.core5.http.protocol.HttpContext;
46
47
48
49
50
51
52
53 @Contract(threading = ThreadingBehavior.STATELESS)
54 public class HttpServerExpectationFilter implements HttpFilterHandler {
55
56
57
58
59
60
61
62
63
64 protected boolean verify(final ClassicHttpRequest request, final HttpContext context) throws HttpException {
65 return true;
66 }
67
68
69
70
71
72
73
74
75
76 protected HttpEntity generateResponseContent(final HttpResponse expectationFailed) throws HttpException {
77 return null;
78 }
79
80 @Override
81 public final void handle(
82 final ClassicHttpRequest request,
83 final HttpFilterChain.ResponseTrigger responseTrigger,
84 final HttpContext context,
85 final HttpFilterChain chain) throws HttpException, IOException {
86 final Header expect = request.getFirstHeader(HttpHeaders.EXPECT);
87 final boolean expectContinue = expect != null && HeaderElements.CONTINUE.equalsIgnoreCase(expect.getValue());
88 if (expectContinue) {
89 final boolean verified = verify(request, context);
90 if (verified) {
91 responseTrigger.sendInformation(new BasicClassicHttpResponse(HttpStatus.SC_CONTINUE));
92 } else {
93 final ClassicHttpResponse expectationFailed = new BasicClassicHttpResponse(HttpStatus.SC_EXPECTATION_FAILED);
94 final HttpEntity responseContent = generateResponseContent(expectationFailed);
95 expectationFailed.setEntity(responseContent);
96 responseTrigger.submitResponse(expectationFailed);
97 return;
98 }
99 }
100 chain.proceed(request, responseTrigger, context);
101 }
102 }