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

ebourg / jsign / #406

07 Jul 2026 03:38PM UTC coverage: 80.728% (+0.04%) from 80.687%
#406

push

ebourg
Retry RESTClient connections when a timeout occurs (Fixes #341)

52 of 60 new or added lines in 1 file covered. (86.67%)

5102 of 6320 relevant lines covered (80.73%)

0.81 hits per line

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

93.8
/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
    /** Pause between retries in milliseconds */
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) {
NEW
93
        this.connectTimeout = connectTimeout;
×
NEW
94
        return this;
×
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✔
NEW
187
                for (String requestHeader : conn.getRequestProperties().keySet()) {
×
NEW
188
                    List<String> values = conn.getRequestProperties().get(requestHeader);
×
NEW
189
                    log.finest(requestHeader + ": " + (values.size() == 1 ? values.get(0) : values));
×
NEW
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 a timeout 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
        boolean retry = true;
1✔
245
        int attempt = 1;
1✔
246

247
        HttpURLConnection conn = null;
1✔
248

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

254
                conn.getResponseCode();
1✔
255
                retry = false;
1✔
256
            } catch (SocketTimeoutException e) {
1✔
257
                if (attempt++ < retries) {
1✔
258
                    try {
259
                        Thread.sleep(retryWait);
1✔
260
                        log.fine("Connection to " + url + " timed out, attempt " + attempt + " of " + retries);
1✔
NEW
261
                    } catch (InterruptedException ie) {
×
NEW
262
                        Thread.currentThread().interrupt();
×
263
                    }
1✔
264
                } else {
265
                    throw (IOException) new SocketTimeoutException("Unable to connect to " + url + " after " + retries + " attempts").initCause(e);
1✔
266
                }
267
            }
1✔
268
        }
269

270
        return conn;
1✔
271
    }
272

273
    private interface HttpURLConnectionHandler {
274
        void handle(HttpURLConnection conn) throws IOException;
275
    }
276
}
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