/
CircuitBreakerUrl.java
268 lines (228 loc) · 10.1 KB
/
CircuitBreakerUrl.java
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package com.dotcms.http;
import com.dotcms.rest.EmptyHttpResponse;
import com.dotcms.rest.api.v1.DotObjectMapperProvider;
import com.dotcms.rest.exception.BadRequestException;
import com.dotcms.util.network.IPUtils;
import com.dotmarketing.business.DotStateException;
import com.dotmarketing.exception.DotRuntimeException;
import com.dotmarketing.util.Config;
import com.dotmarketing.util.Logger;
import com.dotmarketing.util.UtilMethods;
import com.fasterxml.jackson.core.type.TypeReference;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableMap;
import io.vavr.control.Try;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletOutputStream;
import javax.servlet.WriteListener;
import javax.servlet.http.HttpServletResponse;
import net.jodah.failsafe.CircuitBreaker;
import net.jodah.failsafe.Failsafe;
import net.jodah.failsafe.FailsafeException;
import org.apache.commons.io.IOUtils;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
/**
* Defaults to GET requests with 2000 timeout
* <p>
* Usage:
* <pre>
* String pageString = CircuitBreakerUrl.builder()
* .setUrl("https://google.com")
* .setHeaders(ImmutableMap.of("X-CUSTOM-HEADER", "TESTING"))
* .setMethod(CircuitBreakerUrl.Method.POST)
* .setParams(ImmutableMap.of("param1", "12345"))
* .setTimeout(2000)
* .build()
* .doString();
* </pre>
* </p>
*
* @author will
*/
public class CircuitBreakerUrl {
private final String proxyUrl;
private final long timeoutMs;
private final CircuitBreaker circuitBreaker;
private final HttpRequestBase request;
private final boolean verbose;
private final String rawData;
private int response=-1;
private Header[] responseHeaders;
/**
*
* @param proxyUrl
*/
public CircuitBreakerUrl(final String proxyUrl) {
this(proxyUrl, Config.getIntProperty("URL_CONNECTION_TIMEOUT", 2000));
}
/**
* Timeout value in MS
* @param proxyUrl
* @param timeoutMs
*/
public CircuitBreakerUrl(final String proxyUrl, final long timeoutMs) {
this(proxyUrl, timeoutMs, CurcuitBreakerPool.getBreaker(proxyUrl + timeoutMs), false);
}
/**
* Pass in a pre-constructed circuit breaker
* Timeout value in MS
* @param proxyUrl
* @param timeoutMs
* @param circuitBreaker
*/
public CircuitBreakerUrl(final String proxyUrl, final long timeoutMs, final CircuitBreaker circuitBreaker) {
this(proxyUrl, timeoutMs, circuitBreaker, false);
}
public CircuitBreakerUrl(String proxyUrl, long timeoutMs, CircuitBreaker circuitBreaker, boolean verbose) {
this(proxyUrl, timeoutMs, circuitBreaker, new HttpGet(proxyUrl), ImmutableMap.of(),ImmutableMap.of(), verbose, null);
}
/**
* Full featured constructor
*
* @param proxyUrl
* @param timeoutMs
* @param circuitBreaker
* @param request
* @param params
* @param headers
*/
@VisibleForTesting
public CircuitBreakerUrl(String proxyUrl, long timeoutMs, CircuitBreaker circuitBreaker, HttpRequestBase request,
Map<String, String> params, Map<String, String> headers, boolean verbose, final String rawData) {
this.proxyUrl = proxyUrl;
this.timeoutMs = timeoutMs;
this.circuitBreaker = circuitBreaker;
this.verbose=verbose;
this.request = request;
this.rawData=rawData;
for (final String head : headers.keySet()) {
request.addHeader(head, headers.get(head));
}
try {
final URIBuilder uriBuilder = new URIBuilder(this.proxyUrl);
if(this.rawData!=null) {
try {
final String contentType = this.rawData.trim().charAt(0)=='{' ? "application/json" : this.rawData.trim().startsWith("<") ? "application/xml" : "application/x-www-form-urlencoded";
final StringEntity postingString = new StringEntity(rawData, ContentType.create(contentType, "UTF-8"));
if(request instanceof HttpEntityEnclosingRequestBase) {
((HttpEntityEnclosingRequestBase)request).setEntity(postingString);
}
}
catch(Exception e) {
Logger.warnAndDebug(this.getClass(), "unable to set rawData",e);
}
}
for (final String param : params.keySet()) {
uriBuilder.addParameter(param, params.get(param));
}
this.request.setURI(uriBuilder.build());
} catch (URISyntaxException e) {
throw new DotStateException(e);
}
}
public String doString() throws IOException {
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
doOut(out);
return new String(out.toByteArray());
}
}
public void doOut(final OutputStream out ) throws IOException {
doOut(new EmptyHttpResponse() {
@Override
public ServletOutputStream getOutputStream() throws IOException {
return new ServletOutputStream(){
@Override
public void write(int b) throws IOException {
out.write(b);
}
@Override
public boolean isReady() {
return false;
}
@Override
public void setWriteListener(WriteListener writeListener) {
}
};
}
});
}
public void doOut(final HttpServletResponse response) throws IOException {
try (final OutputStream out = response.getOutputStream()) {
if(verbose) {
Logger.info(this.getClass(), "Circuitbreaker to " + request + " is " + circuitBreaker.getState());
}
Failsafe.with(circuitBreaker)
.onSuccess(connection -> {
if(verbose) Logger.info(this, "success to " + this.proxyUrl);
})
.onFailure(failure -> Logger.warn(this, "Connection attempts failed " + failure.getMessage()))
.run(ctx -> {
RequestConfig config = RequestConfig.custom()
.setConnectTimeout(Math.toIntExact(this.timeoutMs))
.setConnectionRequestTimeout(Math.toIntExact(this.timeoutMs))
.setSocketTimeout(Math.toIntExact(this.timeoutMs)).build();
try (CloseableHttpClient httpclient = HttpClientBuilder.create().setDefaultRequestConfig(config).build()) {
if(IPUtils.isIpPrivateSubnet(this.request.getURI().getHost()) && !Config.getBooleanProperty("ALLOW_ACCESS_TO_PRIVATE_SUBNETS", false)){
throw new DotRuntimeException("Remote HttpRequests cannot access private subnets. Set ALLOW_ACCESS_TO_PRIVATE_SUBNETS=true to allow");
}
HttpResponse innerResponse = httpclient.execute(this.request);
this.responseHeaders = innerResponse.getAllHeaders();
copyHeaders(innerResponse, response);
this.response = innerResponse.getStatusLine().getStatusCode();
IOUtils.copy(innerResponse.getEntity().getContent(), out);
// throw an error if the request is bad
if(this.response<200 || this.response>299){
throw new BadRequestException("got invalid response for url: " + this.proxyUrl + " response: " + this.response);
}
}
});
} catch (FailsafeException ee) {
Logger.debug(this.getClass(), ee.getMessage() + " " + toString());
}
}
private void copyHeaders(final HttpResponse innerResponse, final HttpServletResponse response) {
final Header contentTypeHeader = innerResponse.getFirstHeader("Content-Type");
if (UtilMethods.isSet(contentTypeHeader)) {
response.setHeader(contentTypeHeader.getName(), contentTypeHeader.getValue());
}
final Header contentLengthHeader = innerResponse.getFirstHeader("Content-Length");
if (UtilMethods.isSet(contentLengthHeader)) {
response.setHeader(contentLengthHeader.getName(), contentLengthHeader.getValue());
}
}
public int response() {
return this.response;
}
public static CircuitBreakerUrlBuilder builder() {
return new CircuitBreakerUrlBuilder();
}
@Override
public String toString() {
return "CircuitBreakerUrl [proxyUrl=" + proxyUrl + ", timeoutMs=" + timeoutMs + ", circuitBreaker=" + circuitBreaker + "]";
}
final static TypeReference<HashMap<String,Object>> typeRef = new TypeReference<HashMap<String,Object>>() {};
public Map<String,Object> doMap() {
return (Map<String,Object>) Try.of(()-> DotObjectMapperProvider.getInstance().getDefaultObjectMapper().readValue(doString(), typeRef)).onFailure(e->Logger.warnAndDebug(CircuitBreakerUrl.class, e)).getOrElse(HashMap::new);
}
public Header[] getResponseHeaders() {
return responseHeaders;
}
public enum Method {
GET, POST, PUT, DELETE, PATCH;
}
}