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 package org.apache.hc.client5.http.examples;
29
30 import java.io.IOException;
31 import java.util.concurrent.atomic.AtomicLong;
32
33 import org.apache.hc.client5.http.classic.ExecChain;
34 import org.apache.hc.client5.http.classic.ExecChainHandler;
35 import org.apache.hc.client5.http.classic.methods.HttpGet;
36 import org.apache.hc.client5.http.impl.ChainElement;
37 import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
38 import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
39 import org.apache.hc.client5.http.impl.classic.HttpClients;
40 import org.apache.hc.core5.http.ClassicHttpRequest;
41 import org.apache.hc.core5.http.ClassicHttpResponse;
42 import org.apache.hc.core5.http.ContentType;
43 import org.apache.hc.core5.http.EntityDetails;
44 import org.apache.hc.core5.http.Header;
45 import org.apache.hc.core5.http.HttpException;
46 import org.apache.hc.core5.http.HttpRequest;
47 import org.apache.hc.core5.http.HttpRequestInterceptor;
48 import org.apache.hc.core5.http.HttpStatus;
49 import org.apache.hc.core5.http.io.entity.EntityUtils;
50 import org.apache.hc.core5.http.io.entity.StringEntity;
51 import org.apache.hc.core5.http.message.BasicClassicHttpResponse;
52 import org.apache.hc.core5.http.protocol.HttpContext;
53
54
55
56
57
58 public class ClientInterceptors {
59
60 public static void main(final String[] args) throws Exception {
61 try (final CloseableHttpClient httpclient = HttpClients.custom()
62
63
64
65 .addRequestInterceptorFirst(new HttpRequestInterceptor() {
66
67 private final AtomicLong count = new AtomicLong(0);
68
69 @Override
70 public void process(
71 final HttpRequest request,
72 final EntityDetails entity,
73 final HttpContext context) throws HttpException, IOException {
74 request.setHeader("request-id", Long.toString(count.incrementAndGet()));
75 }
76 })
77
78
79
80 .addExecInterceptorAfter(ChainElement.PROTOCOL.name(), "custom", new ExecChainHandler() {
81
82 @Override
83 public ClassicHttpResponse execute(
84 final ClassicHttpRequest request,
85 final ExecChain.Scope scope,
86 final ExecChain chain) throws IOException, HttpException {
87
88 final Header idHeader = request.getFirstHeader("request-id");
89 if (idHeader != null && "13".equalsIgnoreCase(idHeader.getValue())) {
90 final ClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.SC_NOT_FOUND, "Oppsie");
91 response.setEntity(new StringEntity("bad luck", ContentType.TEXT_PLAIN));
92 return response;
93 } else {
94 return chain.proceed(request, scope);
95 }
96 }
97
98 })
99 .build()) {
100
101 for (int i = 0; i < 20; i++) {
102 final HttpGet httpget = new HttpGet("http://httpbin.org/get");
103
104 System.out.println("Executing request " + httpget.getMethod() + " " + httpget.getUri());
105
106 try (final CloseableHttpResponse response = httpclient.execute(httpget)) {
107 System.out.println("----------------------------------------");
108 System.out.println(response.getCode() + " " + response.getReasonPhrase());
109 System.out.println(EntityUtils.toString(response.getEntity()));
110 }
111 }
112 }
113 }
114
115 }
116