• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ebourg / jsign / #424

07 Sep 2026 03:43PM UTC coverage: 82.076% (+0.07%) from 82.005%
#424

push

ebourg
Retry failed connections caused by HTTP 429, 500, 502, 503 and 504 errors (Fixes #361)

29 of 31 new or added lines in 1 file covered. (93.55%)

6278 of 7649 relevant lines covered (82.08%)

0.82 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.95
/jsign-crypto/src/main/java/net/jsign/jca/RESTClient.java
1
/*
2
 * Copyright 2021 Emmanuel Bourg
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16

17
package net.jsign.jca;
18

19
import java.io.IOException;
20
import java.net.HttpURLConnection;
21
import java.net.SocketTimeoutException;
22
import java.net.URL;
23
import java.net.URLEncoder;
24
import java.nio.charset.StandardCharsets;
25
import java.util.HashMap;
26
import java.util.List;
27
import java.util.Map;
28
import java.util.Random;
29
import java.util.function.BiConsumer;
30
import java.util.function.Consumer;
31
import java.util.function.Function;
32
import java.util.logging.Level;
33
import java.util.logging.Logger;
34

35
import com.cedarsoftware.util.io.JsonReader;
36
import org.apache.commons.io.IOUtils;
37

38
class RESTClient {
39

40
    private final Logger log = Logger.getLogger(getClass().getName());
1 ✔
41

42
    /** Base URL of the REST service for relative resources */
43
    private final String endpoint;
44

45
    /** Callback setting the authentication headers for the request */
46
    private BiConsumer<HttpURLConnection, byte[]> authenticationHandler;
47

48
    /** Callback building an error message from the JSON formatted error response */
49
    private Function<Map<String, ?>, String> errorHandler;
50

51
    /** Number of attempts for a request */
52
    private int retries = 5;
1 ✔
53

54
    /** Initial pause between retries in milliseconds, doubled after each attempt on HTTP error (not on timeouts) */
55
    private int retryWait = 1000;
1 ✔
56

57
    /** Connect timeout in milliseconds */
58
    private int connectTimeout = 30000;
1 ✔
59

60
    /** Read timeout in milliseconds */
61
    private int readTimeout = 30000;
1 ✔
62

63
    public RESTClient(String endpoint) {
1 ✔
64
        this.endpoint = endpoint;
1 ✔
65
    }
1 ✔
66

67
    public RESTClient authentication(Consumer<HttpURLConnection>  authenticationHeaderSupplier) {
68
        this.authenticationHandler = (conn, data) -> authenticationHeaderSupplier.accept(conn);
1 ✔
69
        return this;
1 ✔
70
    }
71

72
    public RESTClient authentication(BiConsumer<HttpURLConnection, byte[]>  authenticationHeaderSupplier) {
73
        this.authenticationHandler = authenticationHeaderSupplier;
1 ✔
74
        return this;
1 ✔
75
    }
76

77
    public RESTClient errorHandler(Function<Map<String, ?>, String> errorHandler) {
78
        this.errorHandler = errorHandler;
1 ✔
79
        return this;
1 ✔
80
    }
81

82
    public RESTClient retries(int retries) {
83
        this.retries = retries;
1 ✔
84
        return this;
1 ✔
85
    }
86

87
    public RESTClient retryWait(int retryWait) {
88
        this.retryWait = retryWait;
1 ✔
89
        return this;
1 ✔
90
    }
91

92
    public RESTClient connectTimeout(int connectTimeout) {
93
        this.connectTimeout = connectTimeout;
1 ✔
94
        return this;
1 ✔
95
    }
96

97
    public RESTClient readTimeout(int readTimeout) {
98
        this.readTimeout = readTimeout;
1 ✔
99
        return this;
1 ✔
100
    }
101

102
    public Map<String, ?> get(String resource) throws IOException {
103
        return query("GET", resource, null, null);
1 ✔
104
    }
105

106
    public Map<String, ?> post(String resource, String body) throws IOException {
107
        return query("POST", resource, body, null);
1 ✔
108
    }
109

110
    public Map<String, ?> post(String resource, String body, Map<String, String> headers) throws IOException {
111
        return query("POST", resource, body, headers);
1 ✔
112
    }
113

114
    public Map<String, ?> post(String resource, Map<String, String> params) throws IOException {
115
        return post(resource, params, false);
1 ✔
116
    }
117

118
    public Map<String, ?> post(String resource, Map<String, ?> params, boolean multipart) throws IOException {
119
        Map<String, String> headers = new HashMap<>();
1 ✔
120
        StringBuilder body = new StringBuilder();
1 ✔
121

122
        if (multipart) {
1 ✔
123
            String boundary = "------------------------" + Long.toHexString(new Random().nextLong());
1 ✔
124
            headers.put("Content-Type", "multipart/form-data; boundary=" + boundary);
1 ✔
125

126
            for (String name : params.keySet()) {
1 ✔
127
                Object value = params.get(name);
1 ✔
128

129
                body.append("--" + boundary + "\r\n");
1 ✔
130
                if (value instanceof byte[]) {
1 ✔
131
                    body.append("Content-Type: application/octet-stream" + "\r\n");
1 ✔
132
                    body.append("Content-Disposition: form-data; name=\"" + name + '"' + "; filename=\"" + name + ".data\"\r\n");
1 ✔
133
                    body.append("\r\n");
1 ✔
134
                    body.append(new String((byte[]) value, StandardCharsets.UTF_8));
1 ✔
135
                } else {
136
                    body.append("Content-Disposition: form-data; name=\"" + name + '"' + "\r\n");
1 ✔
137
                    body.append("\r\n");
1 ✔
138
                    body.append(params.get(name));
1 ✔
139
                }
140
                body.append("\r\n");
1 ✔
141
            }
1 ✔
142

143
            body.append("--" + boundary + "--");
1 ✔
144

145
        } else {
1 ✔
146
            headers.put("Content-Type", "application/x-www-form-urlencoded");
1 ✔
147

148
            for (Map.Entry<String, ?> param : params.entrySet()) {
1 ✔
149
                if (body.length() > 0) {
1 ✔
150
                    body.append('&');
1 ✔
151
                }
152
                body.append(param.getKey()).append('=').append(URLEncoder.encode(param.getValue().toString(), "UTF-8"));
1 ✔
153
            }
1 ✔
154
        }
155

156
        return post(resource, body.toString(), headers);
1 ✔
157
    }
158

159
    private Map<String, ?> query(String method, String resource, String body, Map<String, String> headers) throws IOException {
160
        URL url = new URL(resource.startsWith("http") ? resource : endpoint + resource);
1 ✔
161
        log.finest(method + " " + url);
1 ✔
162
        HttpURLConnection c = open(url, conn -> {
1 ✔
163
            conn.setConnectTimeout(connectTimeout);
1 ✔
164
            conn.setReadTimeout(readTimeout);
1 ✔
165
            conn.setRequestMethod(method);
1 ✔
166
            String userAgent = System.getProperty("http.agent");
1 ✔
167
            conn.setRequestProperty("User-Agent", "Jsign (https://ebourg.github.io/jsign/)" + (userAgent != null ? " " + userAgent : ""));
1 ✔
168
            if (headers != null) {
1 ✔
169
                for (Map.Entry<String, String> header : headers.entrySet()) {
1 ✔
170
                    conn.setRequestProperty(header.getKey(), header.getValue());
1 ✔
171
                }
1 ✔
172
            }
173

174
            byte[] data = body != null ? body.getBytes(StandardCharsets.UTF_8) : null;
1 ✔
175
            if (authenticationHandler != null) {
1 ✔
176
                authenticationHandler.accept(conn, data);
1 ✔
177
            }
178
            if (body != null) {
1 ✔
179
                if (!conn.getRequestProperties().containsKey("Content-Type")) {
1 ✔
180
                    conn.setRequestProperty("Content-Type", "application/json; charset=utf-8");
1 ✔
181
                }
182
                conn.setRequestProperty("Content-Length", String.valueOf(data.length));
1 ✔
183
                conn.setRequestProperty("Accept", "*/*");
1 ✔
184
            }
185

186
            if (log.isLoggable(Level.FINEST)) {
1 ✔
187
                for (String requestHeader : conn.getRequestProperties().keySet()) {
×
188
                    List<String> values = conn.getRequestProperties().get(requestHeader);
×
189
                    log.finest(requestHeader + ": " + (values.size() == 1 ? values.get(0) : values));
×
190
                }
×
191
            }
192

193
            if (body != null) {
1 ✔
194
                log.finest("Content:\n" + body);
1 ✔
195
                conn.setDoOutput(true);
1 ✔
196
                conn.getOutputStream().write(data);
1 ✔
197
            }
198
            log.finest("");
1 ✔
199
        });
1 ✔
200

201
        HttpURLConnection conn = c;
1 ✔
202
        int responseCode = conn.getResponseCode();
1 ✔
203
        String contentType = conn.getHeaderField("Content-Type");
1 ✔
204
        log.finest("Response Code: " + responseCode);
1 ✔
205
        log.finest("Content-Type: " + contentType);
1 ✔
206

207
        if (responseCode < 400) {
1 ✔
208
            byte[] binaryResponse = IOUtils.toByteArray(conn.getInputStream());
1 ✔
209
            String response = new String(binaryResponse, StandardCharsets.UTF_8);
1 ✔
210
            log.finest("Content-Length: " + binaryResponse.length);
1 ✔
211
            log.finest("Content:\n" + response);
1 ✔
212
            log.finest("");
1 ✔
213

214
            Object value = JsonReader.jsonToJava(response);
1 ✔
215
            if (value instanceof Map) {
1 ✔
216
                return (Map) value;
1 ✔
217
            } else if (value instanceof Object[]) {
1 ✔
218
                Map<String, Object> map = new HashMap<>();
1 ✔
219
                map.put("result", value);
1 ✔
220
                return map;
1 ✔
221
            } else {
222
                Map<String, Object> map = new HashMap<>();
1 ✔
223
                map.put("result", response);
1 ✔
224
                return map;  
1 ✔
225
            }
226
        } else {
227
            String error = conn.getErrorStream() != null ? IOUtils.toString(conn.getErrorStream(), StandardCharsets.UTF_8) : "";
1 ✔
228
            if (conn.getErrorStream() != null) {
1 ✔
229
                log.finest("Error:\n" + error);
1 ✔
230
            }
231
            if (contentType != null && (contentType.startsWith("application/json") || contentType.startsWith("application/x-amz-json-1.1"))) {
1 ✔
232
                throw new IOException(errorHandler != null ? errorHandler.apply(JsonReader.jsonToMaps(error)) : error);
1 ✔
233
            } else {
234
                throw new IOException("HTTP Error " + responseCode + (conn.getResponseMessage() != null ? " - " + conn.getResponseMessage() : "") + " (" + url + ")");
1 ✔
235
            }
236
        }
237
    }
238

239
    /**
240
     * Opens a connection to the specified URL and makes several attempts if an error occurs.
241
     * The provided configurator is used to set up the connection before making the request.
242
     */
243
    private HttpURLConnection open(URL url, HttpURLConnectionHandler configurator) throws IOException {
244
        int attempt = 1;
1 ✔
245

246
        HttpURLConnection conn;
247

248
        while (true) {
249
            try {
250
                conn = (HttpURLConnection) url.openConnection();
1 ✔
251
                configurator.handle(conn);
1 ✔
252

253
                int responseCode = conn.getResponseCode();
1 ✔
254
                if (isRetryableError(responseCode) && attempt < retries) {
1 ✔
255
                    if (conn.getErrorStream() != null) {
1 ✔
256
                        conn.getErrorStream().close();
1 ✔
257
                    }
258
                    long delay = getRetryDelay(conn, attempt);
1 ✔
259
                    log.fine("Connection attempt " + attempt + " of " + retries + " to " + url + " failed with HTTP error " + responseCode + ", retrying in " + delay + " ms");
1 ✔
260
                    pause(delay);
1 ✔
261
                } else {
1 ✔
262
                    break;
1 ✔
263
                }
264

265
            } catch (SocketTimeoutException e) {
1 ✔
266
                if (attempt < retries) {
1 ✔
267
                    log.fine("Connection attempt " + attempt + " of " + retries + " to " + url + " timed out, retrying in " + retryWait + " ms");
1 ✔
268
                    pause(retryWait);
1 ✔
269
                } else {
270
                    throw (IOException) new SocketTimeoutException("Unable to connect to " + url + " after " + retries + " attempts").initCause(e);
1 ✔
271
                }
272
            }
1 ✔
273
            attempt++;
1 ✔
274
        }
275

276
        return conn;
1 ✔
277
    }
278

279
    private boolean isRetryableError(int responseCode) {
280
        return responseCode == 429 // Too Many Requests
1 ✔
281
                || responseCode == HttpURLConnection.HTTP_INTERNAL_ERROR
282
                || responseCode == HttpURLConnection.HTTP_BAD_GATEWAY
283
                || responseCode == HttpURLConnection.HTTP_UNAVAILABLE
284
                || responseCode == HttpURLConnection.HTTP_GATEWAY_TIMEOUT;
285
    }
286

287
    /**
288
     * Returns the retry delay in milliseconds, either the value specified in the <code>Retry-After</code> header, or an
289
     * exponential backoff delay for the specified attempt capped to the connect timeout.
290
     */
291
    private long getRetryDelay(HttpURLConnection conn, int attempt) {
292
        Long retryAfter = getRetryAfter(conn);
1 ✔
293
        if (retryAfter != null) {
1 ✔
294
            return retryAfter;
1 ✔
295
        } else {
296
            return (long) Math.min(connectTimeout, retryWait * Math.pow(2, attempt - 1));
1 ✔
297
        }
298
    }
299

300
    /**
301
     * Returns the pause in milliseconds requested by the server with the <code>Retry-After</code> header,
302
     * or <code>null</code> if the header is absent or unparseable.
303
     */
304
    private Long getRetryAfter(HttpURLConnection conn) {
305
        String retryAfter = conn.getHeaderField("Retry-After");
1 ✔
306
        if (retryAfter == null) {
1 ✔
307
            return null;
1 ✔
308
        }
309

310
        try {
311
            return Long.parseLong(retryAfter.trim()) * 1000;
1 ✔
312
        } catch (NumberFormatException e) {
1 ✔
313
            long date = conn.getHeaderFieldDate("Retry-After", 0);
1 ✔
314
            return date > 0 ? Math.max(0, date - System.currentTimeMillis()) : null;
1 ✔
315
        }
316
    }
317

318
    private void pause(long millis) {
319
        try {
320
            Thread.sleep(millis);
1 ✔
NEW
321
        } catch (InterruptedException e) {
×
NEW
322
            Thread.currentThread().interrupt();
×
323
        }
1 ✔
324
    }
1 ✔
325

326
    private interface HttpURLConnectionHandler {
327
        void handle(HttpURLConnection conn) throws IOException;
328
    }
329
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc