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

ebourg / jsign / #392

09 Mar 2026 04:51PM UTC coverage: 80.692%. First build
#392

push

web-flow
Merge dc450913b into 6f0e7de6f

22 of 26 new or added lines in 1 file covered. (84.62%)

4994 of 6189 relevant lines covered (80.69%)

0.81 hits per line

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

93.1
/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
    /** Connect timeout (in milliseconds) */
52
    private int connectTimeout = 30000;
1✔
53

54
    /** Read timeout (in milliseconds) */
55
    private int readTimeout = 30000;
1✔
56

57
    /** Number of retries */
58
    private int retries = 3;
1✔
59

60
    /** Pause between retries (in milliseconds) */
61
    private int retryPause = 5000;
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
    /**
83
     * Sets the connect timeout.
84
     *
85
     * @param connectTimeout the timeout in milliseconds
86
     */
87
    public RESTClient connectTimeout(int connectTimeout) {
NEW
88
        this.connectTimeout = connectTimeout;
×
NEW
89
        return this;
×
90
    }
91

92
    /**
93
     * Sets the read timeout.
94
     *
95
     * @param readTimeout the timeout in milliseconds
96
     */
97
    public RESTClient readTimeout(int readTimeout) {
98
        this.readTimeout = readTimeout;
1✔
99
        return this;
1✔
100
    }
101

102
    /**
103
     * Sets the number of retries.
104
     *
105
     * @param retries the number of retries
106
     */
107
    public RESTClient retries(int retries) {
108
        this.retries = retries;
1✔
109
        return this;
1✔
110
    }
111

112
    /**
113
     * Sets the pause between retries.
114
     *
115
     * @param retryPause the pause in milliseconds
116
     */
117
    public RESTClient retryPause(int retryPause) {
118
        this.retryPause = retryPause;
1✔
119
        return this;
1✔
120
    }
121

122
    public Map<String, ?> get(String resource) throws IOException {
123
        return query("GET", resource, null, null);
1✔
124
    }
125

126
    public Map<String, ?> post(String resource, String body) throws IOException {
127
        return query("POST", resource, body, null);
1✔
128
    }
129

130
    public Map<String, ?> post(String resource, String body, Map<String, String> headers) throws IOException {
131
        return query("POST", resource, body, headers);
1✔
132
    }
133

134
    public Map<String, ?> post(String resource, Map<String, String> params) throws IOException {
135
        return post(resource, params, false);
1✔
136
    }
137

138
    public Map<String, ?> post(String resource, Map<String, ?> params, boolean multipart) throws IOException {
139
        Map<String, String> headers = new HashMap<>();
1✔
140
        StringBuilder body = new StringBuilder();
1✔
141

142
        if (multipart) {
1✔
143
            String boundary = "------------------------" + Long.toHexString(new Random().nextLong());
1✔
144
            headers.put("Content-Type", "multipart/form-data; boundary=" + boundary);
1✔
145

146
            for (String name : params.keySet()) {
1✔
147
                Object value = params.get(name);
1✔
148

149
                body.append("--" + boundary + "\r\n");
1✔
150
                if (value instanceof byte[]) {
1✔
151
                    body.append("Content-Type: application/octet-stream" + "\r\n");
1✔
152
                    body.append("Content-Disposition: form-data; name=\"" + name + '"' + "; filename=\"" + name + ".data\"\r\n");
1✔
153
                    body.append("\r\n");
1✔
154
                    body.append(new String((byte[]) value, StandardCharsets.UTF_8));
1✔
155
                } else {
156
                    body.append("Content-Disposition: form-data; name=\"" + name + '"' + "\r\n");
1✔
157
                    body.append("\r\n");
1✔
158
                    body.append(params.get(name));
1✔
159
                }
160
                body.append("\r\n");
1✔
161
            }
1✔
162

163
            body.append("--" + boundary + "--");
1✔
164

165
        } else {
1✔
166
            headers.put("Content-Type", "application/x-www-form-urlencoded");
1✔
167

168
            for (Map.Entry<String, ?> param : params.entrySet()) {
1✔
169
                if (body.length() > 0) {
1✔
170
                    body.append('&');
1✔
171
                }
172
                body.append(param.getKey()).append('=').append(URLEncoder.encode(param.getValue().toString(), "UTF-8"));
1✔
173
            }
1✔
174
        }
175

176
        return post(resource, body.toString(), headers);
1✔
177
    }
178

179
    private Map<String, ?> query(String method, String resource, String body, Map<String, String> headers) throws IOException {
180
        int attempts = 0;
1✔
181
        while (true) {
182
            try {
183
                return queryOnce(method, resource, body, headers);
1✔
184
            } catch (SocketTimeoutException e) {
1✔
185
                attempts++;
1✔
186
                if (attempts >= retries) {
1✔
187
                    throw e;
1✔
188
                }
189
                log.warning("Connection timeout, retrying in " + (retryPause / 1000) + " seconds (attempt " + attempts + "/" + retries + ")");
1✔
190
                try {
191
                    Thread.sleep(retryPause);
1✔
NEW
192
                } catch (InterruptedException ie) {
×
NEW
193
                    Thread.currentThread().interrupt();
×
194
                }
1✔
195
            }
1✔
196
        }
197
    }
198

199
    protected Map<String, ?> queryOnce(String method, String resource, String body, Map<String, String> headers) throws IOException {
200
        URL url = new URL(resource.startsWith("http") ? resource : endpoint + resource);
1✔
201
        log.finest(method + " " + url);
1✔
202
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
1✔
203
        conn.setConnectTimeout(connectTimeout);
1✔
204
        conn.setReadTimeout(readTimeout);
1✔
205
        conn.setRequestMethod(method);
1✔
206
        String userAgent = System.getProperty("http.agent");
1✔
207
        conn.setRequestProperty("User-Agent", "Jsign (https://ebourg.github.io/jsign/)" + (userAgent != null ? " " + userAgent : ""));
1✔
208
        if (headers != null) {
1✔
209
            for (Map.Entry<String, String> header : headers.entrySet()) {
1✔
210
                conn.setRequestProperty(header.getKey(), header.getValue());
1✔
211
            }
1✔
212
        }
213

214
        byte[] data = body != null ? body.getBytes(StandardCharsets.UTF_8) : null;
1✔
215
        if (authenticationHandler != null) {
1✔
216
            authenticationHandler.accept(conn, data);
1✔
217
        }
218
        if (body != null) {
1✔
219
            if (!conn.getRequestProperties().containsKey("Content-Type")) {
1✔
220
                conn.setRequestProperty("Content-Type", "application/json; charset=utf-8");
1✔
221
            }
222
            conn.setRequestProperty("Content-Length", String.valueOf(data.length));
1✔
223
        }
224

225
        if (log.isLoggable(Level.FINEST)) {
1✔
226
            for (String requestHeader : conn.getRequestProperties().keySet()) {
×
227
                List<String> values = conn.getRequestProperties().get(requestHeader);
×
228
                log.finest(requestHeader + ": " + (values.size() == 1 ? values.get(0) : values));
×
229
            }
×
230
        }
231

232
        if (body != null) {
1✔
233
            log.finest("Content:\n" + body);
1✔
234
            conn.setDoOutput(true);
1✔
235
            conn.getOutputStream().write(data);
1✔
236
        }
237
        log.finest("");
1✔
238

239
        int responseCode = conn.getResponseCode();
1✔
240
        String contentType = conn.getHeaderField("Content-Type");
1✔
241
        log.finest("Response Code: " + responseCode);
1✔
242
        log.finest("Content-Type: " + contentType);
1✔
243

244
        if (responseCode < 400) {
1✔
245
            byte[] binaryResponse = IOUtils.toByteArray(conn.getInputStream());
1✔
246
            String response = new String(binaryResponse, StandardCharsets.UTF_8);
1✔
247
            log.finest("Content-Length: " + binaryResponse.length);
1✔
248
            log.finest("Content:\n" + response);
1✔
249
            log.finest("");
1✔
250

251
            Object value = JsonReader.jsonToJava(response);
1✔
252
            if (value instanceof Map) {
1✔
253
                return (Map) value;
1✔
254
            } else {
255
                Map<String, Object> map = new HashMap<>();
1✔
256
                map.put("result", value);
1✔
257
                return map;
1✔
258
            }
259
        } else {
260
            String error = conn.getErrorStream() != null ? IOUtils.toString(conn.getErrorStream(), StandardCharsets.UTF_8) : "";
1✔
261
            if (conn.getErrorStream() != null) {
1✔
262
                log.finest("Error:\n" + error);
1✔
263
            }
264
            if (contentType != null && (contentType.startsWith("application/json") || contentType.startsWith("application/x-amz-json-1.1"))) {
1✔
265
                throw new IOException(errorHandler != null ? errorHandler.apply(JsonReader.jsonToMaps(error)) : error);
1✔
266
            } else {
267
                throw new IOException("HTTP Error " + responseCode + (conn.getResponseMessage() != null ? " - " + conn.getResponseMessage() : "") + " (" + url + ")");
1✔
268
            }
269
        }
270
    }
271
}
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