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

hyperwallet / java-sdk / 753

pending completion
753

Pull #117

travis-ci

web-flow
Merge 18d64ca09 into c509957a6
Pull Request #117: Feature v3/dtrunetwo 380 v3 javasdk remove jerseydependency multipart upload

163 of 163 new or added lines in 6 files covered. (100.0%)

4304 of 4449 relevant lines covered (96.74%)

63.07 hits per line

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

91.0
/src/main/java/com/hyperwallet/clientsdk/util/HyperwalletApiClient.java
1
package com.hyperwallet.clientsdk.util;
2

3
import cc.protea.util.http.Request;
4
import cc.protea.util.http.Response;
5
import com.fasterxml.jackson.core.type.TypeReference;
6
import com.hyperwallet.clientsdk.HyperwalletException;
7
import com.hyperwallet.clientsdk.model.HyperwalletErrorList;
8
import com.nimbusds.jose.JOSEException;
9

10
import javax.xml.bind.DatatypeConverter;
11
import java.io.IOException;
12
import java.text.ParseException;
13
import java.util.HashMap;
14
import java.util.UUID;
15

16
public class HyperwalletApiClient {
17

18
    private static final String CONTENT_TYPE_HEADER = "Content-Type";
19
    private static final String VALID_JSON_CONTENT_TYPE = "application/json";
20
    private static final String VALID_JSON_JOSE_CONTENT_TYPE = "application/jose+json";
21
    private static final String SDK_TYPE = "java";
22

23
    private final String username;
24
    private final String password;
25
    private final String version;
26
    private final HyperwalletEncryption hyperwalletEncryption;
27
    private final boolean isEncrypted;
28
    private final String contextId;
29

30

31
    public HyperwalletApiClient(final String username, final String password, final String version) {
32
        this(username, password, version, null);
×
33
    }
×
34

35
    public HyperwalletApiClient(final String username, final String password, final String version,
36
                                HyperwalletEncryption hyperwalletEncryption) {
442✔
37
        this.username = username;
442✔
38
        this.password = password;
442✔
39
        this.version = version;
442✔
40
        this.hyperwalletEncryption = hyperwalletEncryption;
442✔
41
        this.isEncrypted = hyperwalletEncryption != null;
442✔
42
        this.contextId = String.valueOf(UUID.randomUUID());
442✔
43

44
        // TLS fix
45
        if (System.getProperty("java.version").startsWith("1.7.")) {
442✔
46
            System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2");
×
47
        }
48
    }
442✔
49

50
    public <T> T get(final String url, final Class<T> type) {
51
        Response response = null;
6✔
52
        try {
53
            response = getService(url, true).getResource();
6✔
54
            return processResponse(response, type);
5✔
55
        } catch (IOException | JOSEException | ParseException e) {
1✔
56
            throw new HyperwalletException(e);
1✔
57
        }
58
    }
59

60
    public <T> T get(final String url, final TypeReference<T> type) {
61
        Response response = null;
7✔
62
        try {
63
            response = getService(url, true).getResource();
7✔
64
            return processResponse(response, type);
6✔
65
        } catch (IOException | JOSEException | ParseException e) {
1✔
66
            throw new HyperwalletException(e);
1✔
67
        }
68
    }
69

70
    public <T> T put(final String url, final Object bodyObject, final Class<T> type) {
71
        Response response = null;
5✔
72
        try {
73
            String body = convert(bodyObject);
5✔
74
            response = getService(url, false).setBody(encrypt(body)).putResource();
5✔
75
            return processResponse(response, type);
4✔
76
        } catch (IOException | JOSEException | ParseException e) {
1✔
77
            throw new HyperwalletException(e);
1✔
78
        }
79
    }
80

81
    public <T> T put(final String url, Multipart uploadData, final Class<T> type) {
82
        Response response = null;
×
83
        try {
84
            response = getMultipartService(url, uploadData).putResource();
×
85
            return processResponse(response, type);
×
86
        } catch (IOException | JOSEException | ParseException e) {
×
87
            throw new HyperwalletException(e);
×
88
        }
89
    }
90

91
    public <T> T post(final String url, final Object bodyObject, final Class<T> type) {
92
        Response response = null;
17✔
93
        try {
94
            Request request = getService(url, false);
17✔
95
            String body = bodyObject != null ? encrypt(convert(bodyObject)) : "";
17✔
96
            request.setBody(body);
17✔
97
            response = request.postResource();
17✔
98
            return processResponse(response, type);
16✔
99
        } catch (IOException | JOSEException | ParseException e) {
1✔
100
            throw new HyperwalletException(e);
1✔
101
        }
102
    }
103

104
    public <T> T post(final String url, final Object bodyObject, final Class<T> type, HashMap<String,String> header) {
105
        Response response = null;
4✔
106
        try {
107
            String body = convert(bodyObject);
4✔
108
            Request request = getService(url, false).setBody(encrypt(body));
4✔
109
            if (header != null) {
4✔
110
                for (String key : header.keySet()) {
3✔
111
                    request = request.addHeader(key, header.get(key));
3✔
112
                }
3✔
113
            }
114

115
            response = request.postResource();
4✔
116
            return processResponse(response, type);
3✔
117
        } catch (IOException | JOSEException | ParseException e) {
1✔
118
            throw new HyperwalletException(e);
1✔
119
        }
120
    }
121

122
    protected <T> T processResponse(final Response response, final Class<T> type)
123
            throws ParseException, JOSEException, IOException {
124
        checkErrorResponse(response);
28✔
125
        checkResponseHeader(response);
19✔
126
        if (response.getResponseCode() == 204) {
17✔
127
            return convert("{}", type);
5✔
128
        } else {
129
            return convert(decryptResponse(response.getBody()), type);
12✔
130
        }
131
    }
132

133
    protected <T> T processResponse(final Response response, final TypeReference<T> type)
134
            throws ParseException, JOSEException, IOException {
135
        checkErrorResponse(response);
6✔
136
        checkResponseHeader(response);
3✔
137
        if (response.getResponseCode() == 204) {
3✔
138
            return convert("{}", type);
1✔
139
        } else {
140
            return convert(decryptResponse(response.getBody()), type);
2✔
141
        }
142
    }
143

144
    protected void checkErrorResponse(final Response response) throws ParseException, JOSEException, IOException {
145
        HyperwalletErrorList errorList = null;
35✔
146
        if (response.getResponseCode() >= 400) {
35✔
147
            errorList = convert(decryptResponse(response.getBody()), HyperwalletErrorList.class);
13✔
148
            if (errorList != null) {
13✔
149
                throw new HyperwalletException(response, errorList);
9✔
150
            } else {//unmapped errors
151
                throw new HyperwalletException(response, response.getResponseCode(), response.getResponseMessage());
4✔
152
            }
153
        }
154
    }
22✔
155

156
    private void checkResponseHeader(Response response) {
157
        String contentTypeHeader = response.getHeader(CONTENT_TYPE_HEADER);
22✔
158
        String expectedContentType = isEncrypted ? VALID_JSON_JOSE_CONTENT_TYPE : VALID_JSON_CONTENT_TYPE;
22✔
159
        boolean invalidContentType = response.getResponseCode() != 204 && contentTypeHeader != null
22✔
160
                && !contentTypeHeader.contains(expectedContentType);
16✔
161
        if (invalidContentType) {
22✔
162
            throw new HyperwalletException("Invalid Content-Type specified in Response Header");
2✔
163
        }
164
    }
20✔
165

166
    private String getAuthorizationHeader() {
167
        final String pair = this.username + ":" + this.password;
39✔
168
        final String base64 = DatatypeConverter.printBase64Binary(pair.getBytes());
39✔
169
        return "Basic " + base64;
39✔
170
    }
171

172
    private Request getService(final String url, boolean isHttpGet) {
173
        String contentType = "application/" + ((isEncrypted) ? "jose+json" : "json");
39✔
174
        Request request = new Request(url)
39✔
175
                .addHeader("Authorization", getAuthorizationHeader())
39✔
176
                .addHeader("Accept", contentType)
39✔
177
                .addHeader("User-Agent", "Hyperwallet Java SDK v" + this.version)
39✔
178
                .addHeader("x-sdk-version", this.version)
39✔
179
                .addHeader("x-sdk-type", SDK_TYPE)
39✔
180
                .addHeader("x-sdk-contextId", this.contextId);
39✔
181
        if (!isHttpGet) {
39✔
182
            request.addHeader("Content-Type", contentType);
26✔
183
        }
184
        return request;
39✔
185
    }
186

187
    private <T> T convert(final String responseBody, final Class<T> type) {
188
        if (responseBody == null) {
30✔
189
            return null;
4✔
190
        }
191
        return HyperwalletJsonUtil.fromJson(responseBody, type);
26✔
192
    }
193

194
    private <T> T convert(final String responseBody, final TypeReference<T> type) {
195
        return HyperwalletJsonUtil.fromJson(responseBody, type);
3✔
196
    }
197

198
    private String convert(final Object object) {
199
        return HyperwalletJsonUtil.toJson(object);
26✔
200
    }
201

202
    private String encrypt(String body) throws JOSEException, IOException, ParseException {
203
        return isEncrypted ? hyperwalletEncryption.encrypt(body) : body;
26✔
204
    }
205

206
    private String decryptResponse(String responseBody) throws ParseException, IOException, JOSEException {
207
        if (responseBody == null) {
27✔
208
            return null;
4✔
209
        }
210
        return isEncrypted ? hyperwalletEncryption.decrypt(responseBody) : responseBody;
23✔
211
    }
212
    private MultipartRequest getMultipartService(String requestURL, Multipart multipartData)
213
            throws IOException {
214
        return new MultipartRequest(requestURL, multipartData,  username,  password);
×
215
    }
216
}
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

© 2025 Coveralls, Inc