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

ebourg / jsign / #395

09 Mar 2026 06:15PM UTC coverage: 80.692%. First build
#395

push

web-flow
Merge 86676800b 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 void setConnectTimeout(int connectTimeout) {
NEW
88
        this.connectTimeout = connectTimeout;
×
NEW
89
    }
×
90

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

100
    /**
101
     * Sets the number of retries.
102
     *
103
     * @param retries the number of retries
104
     */
105
    public void setRetries(int retries) {
106
        this.retries = retries;
1✔
107
    }
1✔
108

109
    /**
110
     * Sets the pause between retries.
111
     *
112
     * @param retryPause the pause in milliseconds
113
     */
114
    public void setRetryPause(int retryPause) {
115
        this.retryPause = retryPause;
1✔
116
    }
1✔
117

118
    public Map<String, ?> get(String resource) throws IOException {
119
        return query("GET", resource, null, null);
1✔
120
    }
121

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

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

130
    public Map<String, ?> post(String resource, Map<String, String> params) throws IOException {
131
        return post(resource, params, false);
1✔
132
    }
133

134
    public Map<String, ?> post(String resource, Map<String, ?> params, boolean multipart) throws IOException {
135
        Map<String, String> headers = new HashMap<>();
1✔
136
        StringBuilder body = new StringBuilder();
1✔
137

138
        if (multipart) {
1✔
139
            String boundary = "------------------------" + Long.toHexString(new Random().nextLong());
1✔
140
            headers.put("Content-Type", "multipart/form-data; boundary=" + boundary);
1✔
141

142
            for (String name : params.keySet()) {
1✔
143
                Object value = params.get(name);
1✔
144

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

159
            body.append("--" + boundary + "--");
1✔
160

161
        } else {
1✔
162
            headers.put("Content-Type", "application/x-www-form-urlencoded");
1✔
163

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

172
        return post(resource, body.toString(), headers);
1✔
173
    }
174

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

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

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

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

228
        if (body != null) {
1✔
229
            log.finest("Content:\n" + body);
1✔
230
            conn.setDoOutput(true);
1✔
231
            conn.getOutputStream().write(data);
1✔
232
        }
233
        log.finest("");
1✔
234

235
        int responseCode = conn.getResponseCode();
1✔
236
        String contentType = conn.getHeaderField("Content-Type");
1✔
237
        log.finest("Response Code: " + responseCode);
1✔
238
        log.finest("Content-Type: " + contentType);
1✔
239

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

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