Skip to content

Commit f34c907

Browse files
committed
SDK-1278: Refactor exception handling for more concise error reporting
1 parent 564c4e6 commit f34c907

File tree

19 files changed

+113
-168
lines changed

19 files changed

+113
-168
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,13 @@ If you are using Maven, you need to add the following dependency:
104104
<dependency>
105105
<groupId>com.yoti</groupId>
106106
<artifactId>yoti-sdk-impl</artifactId>
107-
<version>2.5.0</version>
107+
<version>2.5.1</version>
108108
</dependency>
109109
```
110110

111111
If you are using Gradle, here is the dependency to add:
112112

113-
`compile group: 'com.yoti', name: 'yoti-sdk-impl', version: '2.5.0'`
113+
`compile group: 'com.yoti', name: 'yoti-sdk-impl', version: '2.5.1'`
114114

115115
You will find all classes packaged under `com.yoti.api`
116116

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<groupId>com.yoti</groupId>
66
<artifactId>yoti-sdk</artifactId>
77
<packaging>pom</packaging>
8-
<version>2.5.0</version>
8+
<version>2.5.1</version>
99
<name>Yoti SDK</name>
1010
<description>Java SDK for simple integration with the Yoti platform</description>
1111
<url>https://github.com/getyoti/yoti-java-sdk</url>

yoti-sdk-api/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<parent>
1212
<groupId>com.yoti</groupId>
1313
<artifactId>yoti-sdk-parent</artifactId>
14-
<version>2.5.0</version>
14+
<version>2.5.1</version>
1515
<relativePath>../yoti-sdk-parent</relativePath>
1616
</parent>
1717

yoti-sdk-impl/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<parent>
1212
<groupId>com.yoti</groupId>
1313
<artifactId>yoti-sdk-parent</artifactId>
14-
<version>2.5.0</version>
14+
<version>2.5.1</version>
1515
<relativePath>../yoti-sdk-parent</relativePath>
1616
</parent>
1717

yoti-sdk-impl/src/main/java/com/yoti/api/client/spi/remote/call/RemoteProfileService.java

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import static com.yoti.api.client.spi.remote.util.Validation.notNull;
1212

1313
import java.io.IOException;
14+
import java.io.UnsupportedEncodingException;
1415
import java.net.URISyntaxException;
1516
import java.security.GeneralSecurityException;
1617
import java.security.KeyPair;
@@ -28,7 +29,6 @@ public final class RemoteProfileService implements ProfileService {
2829
private static final Logger LOG = LoggerFactory.getLogger(RemoteProfileService.class);
2930

3031
private final UnsignedPathFactory unsignedPathFactory;
31-
private final SignedRequestBuilder signedRequestBuilder;
3232
private final String apiUrl;
3333

3434
static {
@@ -37,15 +37,12 @@ public final class RemoteProfileService implements ProfileService {
3737

3838
public static RemoteProfileService newInstance() {
3939
return new RemoteProfileService(
40-
new UnsignedPathFactory(),
41-
SignedRequestBuilder.newInstance()
40+
new UnsignedPathFactory()
4241
);
4342
}
4443

45-
RemoteProfileService(UnsignedPathFactory profilePathFactory,
46-
SignedRequestBuilder signedRequestBuilder) {
44+
RemoteProfileService(UnsignedPathFactory profilePathFactory) {
4745
this.unsignedPathFactory = profilePathFactory;
48-
this.signedRequestBuilder = signedRequestBuilder;
4946

5047
apiUrl = System.getProperty(PROPERTY_YOTI_API_URL, DEFAULT_YOTI_API_URL);
5148
}
@@ -61,18 +58,8 @@ public Receipt getReceipt(KeyPair keyPair, String appId, String connectToken) th
6158
try {
6259
String authKey = base64(keyPair.getPublic().getEncoded());
6360

64-
SignedRequest signedRequest = this.signedRequestBuilder
65-
.withKeyPair(keyPair)
66-
.withBaseUrl(apiUrl)
67-
.withEndpoint(path)
68-
.withHttpMethod(HTTP_GET)
69-
.withHeader(AUTH_KEY_HEADER, authKey)
70-
.build();
61+
SignedRequest signedRequest = createSignedRequest(keyPair, path, authKey);
7162
return fetchReceipt(signedRequest);
72-
} catch (GeneralSecurityException gse) {
73-
throw new ProfileException("Cannot sign request", gse);
74-
} catch (URISyntaxException uriSyntaxException) {
75-
throw new ProfileException("Error creating request", uriSyntaxException);
7663
} catch (IOException ioe) {
7764
throw new ProfileException("Error calling service to get profile", ioe);
7865
}
@@ -97,4 +84,20 @@ private Receipt fetchReceipt(SignedRequest signedRequest) throws IOException, Pr
9784
}
9885
}
9986

87+
SignedRequest createSignedRequest(KeyPair keyPair, String path, String authKey) throws ProfileException {
88+
try {
89+
return SignedRequestBuilder.newInstance()
90+
.withKeyPair(keyPair)
91+
.withBaseUrl(apiUrl)
92+
.withEndpoint(path)
93+
.withHttpMethod(HTTP_GET)
94+
.withHeader(AUTH_KEY_HEADER, authKey)
95+
.build();
96+
} catch (GeneralSecurityException ex) {
97+
throw new ProfileException("Cannot sign request", ex);
98+
} catch (UnsupportedEncodingException | URISyntaxException ex) {
99+
throw new ProfileException("Error creating request", ex);
100+
}
101+
}
102+
100103
}

yoti-sdk-impl/src/main/java/com/yoti/api/client/spi/remote/call/YotiConstants.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ private YotiConstants() {}
1919
public static final String CONTENT_TYPE_JSON = "application/json";
2020

2121
public static final String JAVA = "Java";
22-
public static final String SDK_VERSION = JAVA + "-2.5.0";
22+
public static final String SDK_VERSION = JAVA + "-2.5.1";
2323
public static final String SIGNATURE_ALGORITHM = "SHA256withRSA";
2424
public static final String ASYMMETRIC_CIPHER = "RSA/NONE/PKCS1Padding";
2525
public static final String SYMMETRIC_CIPHER = "AES/CBC/PKCS7Padding";
Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,46 @@
11
package com.yoti.api.client.spi.remote.call.aml;
22

3-
import com.fasterxml.jackson.databind.ObjectMapper;
3+
import static java.net.HttpURLConnection.HTTP_BAD_REQUEST;
4+
import static java.net.HttpURLConnection.HTTP_INTERNAL_ERROR;
5+
import static java.net.HttpURLConnection.HTTP_UNAUTHORIZED;
6+
7+
import static com.yoti.api.client.spi.remote.call.HttpMethod.HTTP_POST;
8+
import static com.yoti.api.client.spi.remote.call.YotiConstants.DEFAULT_CHARSET;
9+
import static com.yoti.api.client.spi.remote.call.YotiConstants.DEFAULT_YOTI_API_URL;
10+
import static com.yoti.api.client.spi.remote.call.YotiConstants.PROPERTY_YOTI_API_URL;
11+
import static com.yoti.api.client.spi.remote.util.Validation.notNull;
12+
13+
import java.io.IOException;
14+
import java.io.UnsupportedEncodingException;
15+
import java.net.URISyntaxException;
16+
import java.security.GeneralSecurityException;
17+
import java.security.KeyPair;
18+
419
import com.yoti.api.client.AmlException;
520
import com.yoti.api.client.aml.AmlProfile;
621
import com.yoti.api.client.spi.remote.call.ResourceException;
722
import com.yoti.api.client.spi.remote.call.SignedRequest;
823
import com.yoti.api.client.spi.remote.call.SignedRequestBuilder;
924
import com.yoti.api.client.spi.remote.call.factory.UnsignedPathFactory;
1025

11-
import java.io.IOException;
12-
import java.net.URISyntaxException;
13-
import java.security.GeneralSecurityException;
14-
import java.security.KeyPair;
15-
16-
import static com.yoti.api.client.spi.remote.call.HttpMethod.HTTP_POST;
17-
import static com.yoti.api.client.spi.remote.call.YotiConstants.*;
18-
import static com.yoti.api.client.spi.remote.util.Validation.notNull;
19-
import static java.net.HttpURLConnection.*;
26+
import com.fasterxml.jackson.databind.ObjectMapper;
2027

2128
public class RemoteAmlService {
2229

2330
private final UnsignedPathFactory unsignedPathFactory;
2431
private final ObjectMapper objectMapper;
25-
private final SignedRequestBuilder signedRequestBuilder;
2632
private final String apiUrl;
2733

2834
public static RemoteAmlService newInstance() {
2935
return new RemoteAmlService(
3036
new UnsignedPathFactory(),
31-
new ObjectMapper(),
32-
SignedRequestBuilder.newInstance()
37+
new ObjectMapper()
3338
);
3439
}
3540

36-
RemoteAmlService(UnsignedPathFactory unsignedPathFactory,
37-
ObjectMapper objectMapper,
38-
SignedRequestBuilder signedRequestBuilder) {
41+
RemoteAmlService(UnsignedPathFactory unsignedPathFactory, ObjectMapper objectMapper) {
3942
this.unsignedPathFactory = unsignedPathFactory;
4043
this.objectMapper = objectMapper;
41-
this.signedRequestBuilder = signedRequestBuilder;
4244

4345
apiUrl = System.getProperty(PROPERTY_YOTI_API_URL, DEFAULT_YOTI_API_URL);
4446
}
@@ -52,20 +54,10 @@ public SimpleAmlResult performCheck(KeyPair keyPair, String appId, AmlProfile am
5254
String resourcePath = unsignedPathFactory.createAmlPath(appId);
5355
byte[] body = objectMapper.writeValueAsString(amlProfile).getBytes(DEFAULT_CHARSET);
5456

55-
SignedRequest signedRequest = this.signedRequestBuilder.withKeyPair(keyPair)
56-
.withBaseUrl(apiUrl)
57-
.withEndpoint(resourcePath)
58-
.withPayload(body)
59-
.withHttpMethod(HTTP_POST)
60-
.build();
61-
57+
SignedRequest signedRequest = createSignedRequest(keyPair, resourcePath, body);
6258
return signedRequest.execute(SimpleAmlResult.class);
6359
} catch (IOException ioException) {
6460
throw new AmlException("Error communicating with AML endpoint", ioException);
65-
} catch (GeneralSecurityException generalSecurityException) {
66-
throw new AmlException("Cannot sign request", generalSecurityException);
67-
} catch (URISyntaxException uriSyntaxException) {
68-
throw new AmlException("Error creating request", uriSyntaxException);
6961
} catch (ResourceException resourceException) {
7062
throw createExceptionFromStatusCode(resourceException);
7163
}
@@ -84,4 +76,20 @@ private AmlException createExceptionFromStatusCode(ResourceException e) {
8476
}
8577
}
8678

79+
SignedRequest createSignedRequest(KeyPair keyPair, String resourcePath, byte[] body) throws AmlException {
80+
try {
81+
return SignedRequestBuilder.newInstance()
82+
.withKeyPair(keyPair)
83+
.withBaseUrl(apiUrl)
84+
.withEndpoint(resourcePath)
85+
.withPayload(body)
86+
.withHttpMethod(HTTP_POST)
87+
.build();
88+
} catch (GeneralSecurityException generalSecurityException) {
89+
throw new AmlException("Cannot sign request", generalSecurityException);
90+
} catch (URISyntaxException | UnsupportedEncodingException uriSyntaxException) {
91+
throw new AmlException("Error creating request", uriSyntaxException);
92+
}
93+
}
94+
8795
}

yoti-sdk-impl/src/main/java/com/yoti/api/client/spi/remote/call/qrcode/DynamicSharingService.java

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import static com.yoti.api.client.spi.remote.util.Validation.notNull;
77

88
import java.io.IOException;
9+
import java.io.UnsupportedEncodingException;
10+
import java.net.URISyntaxException;
911
import java.security.GeneralSecurityException;
1012
import java.security.KeyPair;
1113

@@ -25,25 +27,21 @@ public final class DynamicSharingService {
2527
public static DynamicSharingService newInstance() {
2628
return new DynamicSharingService(
2729
new UnsignedPathFactory(),
28-
new ObjectMapper(),
29-
SignedRequestBuilder.newInstance()
30+
new ObjectMapper()
3031
);
3132
}
3233

3334
private static final Logger LOG = LoggerFactory.getLogger(DynamicSharingService.class);
3435

3536
private final UnsignedPathFactory unsignedPathFactory;
3637
private final ObjectMapper objectMapper;
37-
private final SignedRequestBuilder signedRequestBuilder;
3838

3939
private final String apiUrl;
4040

4141
DynamicSharingService(UnsignedPathFactory unsignedPathFactory,
42-
ObjectMapper objectMapper,
43-
SignedRequestBuilder signedRequestBuilder) {
42+
ObjectMapper objectMapper) {
4443
this.unsignedPathFactory = unsignedPathFactory;
4544
this.objectMapper = objectMapper;
46-
this.signedRequestBuilder = signedRequestBuilder;
4745

4846
apiUrl = System.getProperty(PROPERTY_YOTI_API_URL, DEFAULT_YOTI_API_URL);
4947
}
@@ -59,23 +57,29 @@ public SimpleShareUrlResult createShareUrl(String appId, KeyPair keyPair, Dynami
5957
try {
6058
byte[] body = objectMapper.writeValueAsString(dynamicScenario).getBytes(DEFAULT_CHARSET);
6159

62-
SignedRequest signedRequest = this.signedRequestBuilder
60+
SignedRequest signedRequest = createSignedRequest(keyPair, path, body);
61+
62+
return signedRequest.execute(SimpleShareUrlResult.class);
63+
} catch (ResourceException ex) {
64+
throw new DynamicShareException("Error posting the request: ", ex);
65+
} catch (IOException ex) {
66+
throw new DynamicShareException("Error building the request: ", ex);
67+
}
68+
}
69+
70+
SignedRequest createSignedRequest(KeyPair keyPair, String path, byte[] body) throws DynamicShareException {
71+
try {
72+
return SignedRequestBuilder.newInstance()
6373
.withKeyPair(keyPair)
6474
.withBaseUrl(apiUrl)
6575
.withEndpoint(path)
6676
.withPayload(body)
6777
.withHttpMethod("POST")
6878
.build();
69-
70-
return signedRequest.execute(SimpleShareUrlResult.class);
7179
} catch (GeneralSecurityException ex) {
7280
throw new DynamicShareException("Error signing the request: ", ex);
73-
} catch (ResourceException ex) {
74-
throw new DynamicShareException("Error posting the request: ", ex);
75-
} catch (IOException ex) {
81+
} catch (UnsupportedEncodingException | URISyntaxException ex) {
7682
throw new DynamicShareException("Error building the request: ", ex);
77-
} catch (Exception ex) {
78-
throw new DynamicShareException("Error initiating the share: ", ex);
7983
}
8084
}
8185

0 commit comments

Comments
 (0)