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

ebourg / jsign / #389

09 Mar 2026 03:21PM UTC coverage: 80.644%. First build
#389

Pull #342

google-labs-jules[bot]
Implement HTTP retry logic and set connection timeout in RESTClient

- Implemented retry logic in `RESTClient` for `SocketTimeoutException`: up to 3 attempts with a 5-second pause between attempts.
- Set the default connection timeout to 30 seconds.
- Refactored `RESTClient.query` into `query` (retry loop) and `queryOnce` (single request logic).
- Added `openConnection(URL)` and `sleep(long)` methods to `RESTClient` to facilitate testing.
- Added `net.jsign.jca.RESTClientTest` to verify the retry logic and timeout.

Co-authored-by: ebourg <54304+ebourg@users.noreply.github.com>
Pull Request #342: Implement HTTP retry logic and set connection timeout in RESTClient

12 of 17 new or added lines in 1 file covered. (70.59%)

4983 of 6179 relevant lines covered (80.64%)

0.81 hits per line

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

91.51
/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
    public RESTClient(String endpoint) {
1✔
52
        this.endpoint = endpoint;
1✔
53
    }
1✔
54

55
    public RESTClient authentication(Consumer<HttpURLConnection>  authenticationHeaderSupplier) {
56
        this.authenticationHandler = (conn, data) -> authenticationHeaderSupplier.accept(conn);
1✔
57
        return this;
1✔
58
    }
59

60
    public RESTClient authentication(BiConsumer<HttpURLConnection, byte[]>  authenticationHeaderSupplier) {
61
        this.authenticationHandler = authenticationHeaderSupplier;
1✔
62
        return this;
1✔
63
    }
64

65
    public RESTClient errorHandler(Function<Map<String, ?>, String> errorHandler) {
66
        this.errorHandler = errorHandler;
1✔
67
        return this;
1✔
68
    }
69

70
    public Map<String, ?> get(String resource) throws IOException {
71
        return query("GET", resource, null, null);
1✔
72
    }
73

74
    public Map<String, ?> post(String resource, String body) throws IOException {
75
        return query("POST", resource, body, null);
1✔
76
    }
77

78
    public Map<String, ?> post(String resource, String body, Map<String, String> headers) throws IOException {
79
        return query("POST", resource, body, headers);
1✔
80
    }
81

82
    public Map<String, ?> post(String resource, Map<String, String> params) throws IOException {
83
        return post(resource, params, false);
1✔
84
    }
85

86
    public Map<String, ?> post(String resource, Map<String, ?> params, boolean multipart) throws IOException {
87
        Map<String, String> headers = new HashMap<>();
1✔
88
        StringBuilder body = new StringBuilder();
1✔
89

90
        if (multipart) {
1✔
91
            String boundary = "------------------------" + Long.toHexString(new Random().nextLong());
1✔
92
            headers.put("Content-Type", "multipart/form-data; boundary=" + boundary);
1✔
93

94
            for (String name : params.keySet()) {
1✔
95
                Object value = params.get(name);
1✔
96

97
                body.append("--" + boundary + "\r\n");
1✔
98
                if (value instanceof byte[]) {
1✔
99
                    body.append("Content-Type: application/octet-stream" + "\r\n");
1✔
100
                    body.append("Content-Disposition: form-data; name=\"" + name + '"' + "; filename=\"" + name + ".data\"\r\n");
1✔
101
                    body.append("\r\n");
1✔
102
                    body.append(new String((byte[]) value, StandardCharsets.UTF_8));
1✔
103
                } else {
104
                    body.append("Content-Disposition: form-data; name=\"" + name + '"' + "\r\n");
1✔
105
                    body.append("\r\n");
1✔
106
                    body.append(params.get(name));
1✔
107
                }
108
                body.append("\r\n");
1✔
109
            }
1✔
110

111
            body.append("--" + boundary + "--");
1✔
112

113
        } else {
1✔
114
            headers.put("Content-Type", "application/x-www-form-urlencoded");
1✔
115

116
            for (Map.Entry<String, ?> param : params.entrySet()) {
1✔
117
                if (body.length() > 0) {
1✔
118
                    body.append('&');
1✔
119
                }
120
                body.append(param.getKey()).append('=').append(URLEncoder.encode(param.getValue().toString(), "UTF-8"));
1✔
121
            }
1✔
122
        }
123

124
        return post(resource, body.toString(), headers);
1✔
125
    }
126

127
    private Map<String, ?> query(String method, String resource, String body, Map<String, String> headers) throws IOException {
128
        int attempts = 0;
1✔
129
        while (true) {
130
            try {
131
                return queryOnce(method, resource, body, headers);
1✔
132
            } catch (SocketTimeoutException e) {
1✔
133
                attempts++;
1✔
134
                if (attempts >= 3) {
1✔
135
                    throw e;
1✔
136
                }
137
                log.warning("Connection timeout, retrying in 5 seconds (attempt " + attempts + "/3)");
1✔
138
                sleep(5000);
1✔
139
            }
1✔
140
        }
141
    }
142

143
    void sleep(long millis) {
144
        try {
NEW
145
            Thread.sleep(millis);
×
NEW
146
        } catch (InterruptedException ie) {
×
NEW
147
            Thread.currentThread().interrupt();
×
NEW
148
        }
×
NEW
149
    }
×
150

151
    protected HttpURLConnection openConnection(URL url) throws IOException {
152
        return (HttpURLConnection) url.openConnection();
1✔
153
    }
154

155
    private Map<String, ?> queryOnce(String method, String resource, String body, Map<String, String> headers) throws IOException {
156
        URL url = new URL(resource.startsWith("http") ? resource : endpoint + resource);
1✔
157
        log.finest(method + " " + url);
1✔
158
        HttpURLConnection conn = openConnection(url);
1✔
159
        conn.setConnectTimeout(30000);
1✔
160
        conn.setRequestMethod(method);
1✔
161
        String userAgent = System.getProperty("http.agent");
1✔
162
        conn.setRequestProperty("User-Agent", "Jsign (https://ebourg.github.io/jsign/)" + (userAgent != null ? " " + userAgent : ""));
1✔
163
        if (headers != null) {
1✔
164
            for (Map.Entry<String, String> header : headers.entrySet()) {
1✔
165
                conn.setRequestProperty(header.getKey(), header.getValue());
1✔
166
            }
1✔
167
        }
168

169
        byte[] data = body != null ? body.getBytes(StandardCharsets.UTF_8) : null;
1✔
170
        if (authenticationHandler != null) {
1✔
171
            authenticationHandler.accept(conn, data);
1✔
172
        }
173
        if (body != null) {
1✔
174
            if (!conn.getRequestProperties().containsKey("Content-Type")) {
1✔
175
                conn.setRequestProperty("Content-Type", "application/json; charset=utf-8");
1✔
176
            }
177
            conn.setRequestProperty("Content-Length", String.valueOf(data.length));
1✔
178
        }
179

180
        if (log.isLoggable(Level.FINEST)) {
1✔
181
            for (String requestHeader : conn.getRequestProperties().keySet()) {
×
182
                List<String> values = conn.getRequestProperties().get(requestHeader);
×
183
                log.finest(requestHeader + ": " + (values.size() == 1 ? values.get(0) : values));
×
184
            }
×
185
        }
186

187
        if (body != null) {
1✔
188
            log.finest("Content:\n" + body);
1✔
189
            conn.setDoOutput(true);
1✔
190
            conn.getOutputStream().write(data);
1✔
191
        }
192
        log.finest("");
1✔
193

194
        int responseCode = conn.getResponseCode();
1✔
195
        String contentType = conn.getHeaderField("Content-Type");
1✔
196
        log.finest("Response Code: " + responseCode);
1✔
197
        log.finest("Content-Type: " + contentType);
1✔
198

199
        if (responseCode < 400) {
1✔
200
            byte[] binaryResponse = IOUtils.toByteArray(conn.getInputStream());
1✔
201
            String response = new String(binaryResponse, StandardCharsets.UTF_8);
1✔
202
            log.finest("Content-Length: " + binaryResponse.length);
1✔
203
            log.finest("Content:\n" + response);
1✔
204
            log.finest("");
1✔
205

206
            Object value = JsonReader.jsonToJava(response);
1✔
207
            if (value instanceof Map) {
1✔
208
                return (Map) value;
1✔
209
            } else {
210
                Map<String, Object> map = new HashMap<>();
1✔
211
                map.put("result", value);
1✔
212
                return map;
1✔
213
            }
214
        } else {
215
            String error = conn.getErrorStream() != null ? IOUtils.toString(conn.getErrorStream(), StandardCharsets.UTF_8) : "";
1✔
216
            if (conn.getErrorStream() != null) {
1✔
217
                log.finest("Error:\n" + error);
1✔
218
            }
219
            if (contentType != null && (contentType.startsWith("application/json") || contentType.startsWith("application/x-amz-json-1.1"))) {
1✔
220
                throw new IOException(errorHandler != null ? errorHandler.apply(JsonReader.jsonToMaps(error)) : error);
1✔
221
            } else {
222
                throw new IOException("HTTP Error " + responseCode + (conn.getResponseMessage() != null ? " - " + conn.getResponseMessage() : "") + " (" + url + ")");
1✔
223
            }
224
        }
225
    }
226
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc