Skip to content

Commit 433e80f

Browse files
Fix
1 parent 07a3abf commit 433e80f

File tree

1 file changed

+29
-51
lines changed

1 file changed

+29
-51
lines changed

java/src/main/java/de/gdata/vaas/Vaas.java

Lines changed: 29 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package de.gdata.vaas;
22

3-
import com.fasterxml.jackson.core.JsonProcessingException;
4-
import com.fasterxml.jackson.core.type.TypeReference;
5-
import com.fasterxml.jackson.databind.ObjectMapper;
3+
import com.google.gson.Gson;
64
import de.gdata.vaas.authentication.IAuthenticator;
75
import de.gdata.vaas.exceptions.VaasAuthenticationException;
86
import de.gdata.vaas.exceptions.VaasClientException;
@@ -37,7 +35,8 @@
3735
import java.util.concurrent.CompletionException;
3836
import java.util.concurrent.ExecutionException;
3937
import java.util.concurrent.TimeUnit;
40-
import java.util.function.Function;
38+
39+
import static de.gdata.vaas.CompletableFutureExceptionHandler.handleException;
4140

4241
public class Vaas implements IVaas {
4342
private static final String userAgent = "Java/9.0.0";
@@ -96,20 +95,19 @@ private static CompletableFuture<VaasVerdict> sendUrlWithRetry(HttpClient httpCl
9695
private static Exception parseVaasError(HttpResponse<String> response) {
9796
String responseBody = response.body();
9897
try {
99-
var objectMapper = new ObjectMapper();
100-
var problemDetails = objectMapper.readValue(responseBody, new TypeReference<>() {
101-
});
102-
String type = (String) problemDetails.getOrDefault("type", "");
103-
String detail = (String) problemDetails.getOrDefault("detail", "Unknown error");
104-
105-
if (type.equals("VaasClientException")) {
106-
return new VaasClientException(detail);
107-
} else if (type.equals("VaasAuthenticationException")) {
108-
return new VaasAuthenticationException(detail);
98+
Map<String, Object> problemDetails = new Gson().fromJson(responseBody, Map.class);
99+
if (problemDetails != null) {
100+
String type = (String) problemDetails.getOrDefault("type", "");
101+
String detail = (String) problemDetails.getOrDefault("detail", "Unknown error");
102+
if (type.equals("VaasClientException")) {
103+
return new VaasClientException(detail);
104+
} else if (type.equals("VaasAuthenticationException")) {
105+
return new VaasAuthenticationException(detail);
106+
}
107+
return new VaasServerException(detail);
108+
} else {
109+
return new VaasServerException("Invalid JSON error response from server");
109110
}
110-
return new VaasServerException(detail);
111-
} catch (JsonProcessingException e) {
112-
return new VaasServerException("Invalid JSON error response from server");
113111
} catch (Exception e) {
114112
if (response.statusCode() == 401) {
115113
return new VaasAuthenticationException(
@@ -123,7 +121,7 @@ private static Exception parseVaasError(HttpResponse<String> response) {
123121
}
124122
}
125123

126-
private CompletableFuture<VaasVerdict> sendFileWithRetry(HttpClient httpClient, URI uri, String vaasRequestId) throws IOException, VaasAuthenticationException, InterruptedException {
124+
private CompletableFuture<VaasVerdict> sendFileWithRetry(HttpClient httpClient, URI uri, String vaasRequestId) throws VaasAuthenticationException {
127125
var request = CreateHttpRequestBuilderWithHeaders(uri, vaasRequestId)
128126
.GET()
129127
.build();
@@ -139,7 +137,7 @@ private CompletableFuture<VaasVerdict> sendFileWithRetry(HttpClient httpClient,
139137
}
140138

141139
private HttpRequest.Builder CreateHttpRequestBuilderWithHeaders(URI uri, String requestId)
142-
throws IOException, InterruptedException, VaasAuthenticationException {
140+
throws VaasAuthenticationException {
143141
var token = this.authenticator.getToken();
144142
var httpRequestBuilder = HttpRequest.newBuilder()
145143
.uri(uri)
@@ -159,14 +157,11 @@ private HttpRequest.Builder CreateHttpRequestBuilderWithHeaders(URI uri, String
159157
* @param sha256 the SHA-256 hash to retrieve the verdict for
160158
* @return a {@link CompletableFuture} containing the {@link VaasVerdict} for
161159
* the hash
162-
* @throws IOException If an I/O error occurs during the
163-
* request.
164-
* @throws InterruptedException If the operation is interrupted.
165160
* @throws VaasAuthenticationException If there is an authentication error.
166161
*/
167162
@Override
168163
public CompletableFuture<VaasVerdict> forSha256Async(Sha256 sha256)
169-
throws IOException, InterruptedException, VaasAuthenticationException {
164+
throws VaasAuthenticationException {
170165
return this.forSha256Async(sha256, ForSha256Options.fromVaasConfig(this.config));
171166
}
172167

@@ -179,14 +174,11 @@ public CompletableFuture<VaasVerdict> forSha256Async(Sha256 sha256)
179174
* and hash lookup.
180175
* @return a {@link CompletableFuture} containing the {@link VaasVerdict} for
181176
* the hash
182-
* @throws IOException If an I/O error occurs during the
183-
* request.
184-
* @throws InterruptedException If the operation is interrupted.
185177
* @throws VaasAuthenticationException If there is an authentication error.
186178
*/
187179
@Override
188180
public CompletableFuture<VaasVerdict> forSha256Async(Sha256 sha256, ForSha256Options options)
189-
throws IOException, InterruptedException, VaasAuthenticationException {
181+
throws VaasAuthenticationException {
190182
var params = Map.of(
191183
"useCache", String.valueOf(options.isUseCache()),
192184
"useHashLookup", String.valueOf(options.isUseHashLookup()));
@@ -206,12 +198,11 @@ public CompletableFuture<VaasVerdict> forSha256Async(Sha256 sha256, ForSha256Opt
206198
* @throws InterruptedException if the thread is interrupted while
207199
* waiting for the result
208200
* @throws ExecutionException if the computation threw an exception
209-
* @throws IOException if an I/O error occurs
210201
* @throws VaasAuthenticationException if there is an authentication error
211202
*/
212203
@Override
213204
public VaasVerdict forSha256(Sha256 sha256)
214-
throws InterruptedException, ExecutionException, IOException, VaasAuthenticationException {
205+
throws InterruptedException, ExecutionException, VaasAuthenticationException {
215206
return forSha256Async(sha256).get();
216207
}
217208

@@ -226,12 +217,11 @@ public VaasVerdict forSha256(Sha256 sha256)
226217
* @throws InterruptedException if the thread is interrupted while
227218
* waiting for the result
228219
* @throws ExecutionException if the computation threw an exception
229-
* @throws IOException if an I/O error occurs
230220
* @throws VaasAuthenticationException if there is an authentication error
231221
*/
232222
@Override
233223
public VaasVerdict forSha256(Sha256 sha256, ForSha256Options options)
234-
throws InterruptedException, ExecutionException, IOException, VaasAuthenticationException {
224+
throws InterruptedException, ExecutionException, VaasAuthenticationException {
235225
return forSha256Async(sha256, options).get();
236226
}
237227

@@ -245,14 +235,13 @@ public VaasVerdict forSha256(Sha256 sha256, ForSha256Options options)
245235
* @return a {@link CompletableFuture} containing the {@link VaasVerdict} for
246236
* the file
247237
* @throws IOException if an I/O error occurs
248-
* @throws InterruptedException if the operation is interrupted
249238
* @throws VaasAuthenticationException if authentication fails
250239
* @throws NoSuchAlgorithmException if the algorithm for hash lookup is not
251240
* available
252241
*/
253242
@Override
254243
public CompletableFuture<VaasVerdict> forFileAsync(Path file)
255-
throws IOException, InterruptedException, VaasAuthenticationException, NoSuchAlgorithmException {
244+
throws IOException, VaasAuthenticationException, NoSuchAlgorithmException {
256245
return forFileAsync(file, ForFileOptions.fromVaaSConfig(this.config));
257246
}
258247

@@ -268,14 +257,13 @@ public CompletableFuture<VaasVerdict> forFileAsync(Path file)
268257
* @return a {@link CompletableFuture} containing the {@link VaasVerdict} for
269258
* the file
270259
* @throws IOException if an I/O error occurs
271-
* @throws InterruptedException if the operation is interrupted
272260
* @throws VaasAuthenticationException if authentication fails
273261
* @throws NoSuchAlgorithmException if the algorithm for hash lookup is not
274262
* available
275263
*/
276264
@Override
277265
public CompletableFuture<VaasVerdict> forFileAsync(Path file, ForFileOptions options)
278-
throws IOException, InterruptedException, VaasAuthenticationException, NoSuchAlgorithmException {
266+
throws IOException, VaasAuthenticationException, NoSuchAlgorithmException {
279267
var sha256 = new Sha256(file);
280268
var contentLength = Files.size(file);
281269
var forSha256Options = new ForSha256Options(options.isUseCache(), options.isUseHashLookup(),
@@ -362,12 +350,11 @@ public VaasVerdict forFile(Path file, ForFileOptions options) throws NoSuchAlgor
362350
* @param contentLength the length of the content in the input stream
363351
* @return a {@link CompletableFuture} containing the {@link VaasVerdict}
364352
* @throws IOException if an I/O error occurs
365-
* @throws InterruptedException if the operation is interrupted
366353
* @throws VaasAuthenticationException if authentication fails
367354
*/
368355
@Override
369356
public CompletableFuture<VaasVerdict> forStreamAsync(InputStream stream, long contentLength)
370-
throws IOException, InterruptedException, VaasAuthenticationException {
357+
throws IOException, VaasAuthenticationException {
371358
return forStreamAsync(stream, contentLength, ForStreamOptions.fromVaasConfig(this.config));
372359
}
373360

@@ -382,12 +369,11 @@ public CompletableFuture<VaasVerdict> forStreamAsync(InputStream stream, long co
382369
* hash lookup.
383370
* @return a {@link CompletableFuture} containing the {@link VaasVerdict}
384371
* @throws IOException if an I/O error occurs
385-
* @throws InterruptedException if the operation is interrupted
386372
* @throws VaasAuthenticationException if authentication fails
387373
*/
388374
@Override
389375
public CompletableFuture<VaasVerdict> forStreamAsync(InputStream inputStream, long contentLength,
390-
ForStreamOptions options) throws IOException, InterruptedException, VaasAuthenticationException {
376+
ForStreamOptions options) throws IOException, VaasAuthenticationException {
391377
var params = Map.of("useHashLookup", String.valueOf(options.isUseHashLookup()));
392378

393379
var filesUri = this.config.getUrl() + "/files?" + encodeParams(params);
@@ -461,14 +447,11 @@ public VaasVerdict forStream(InputStream stream, long contentLength, ForStreamOp
461447
* @return a {@link CompletableFuture} containing the {@link VaasVerdict} for
462448
* the
463449
* URL
464-
* @throws IOException If an I/O error occurs during the
465-
* request.
466-
* @throws InterruptedException If the operation is interrupted.
467450
* @throws VaasAuthenticationException If there is an authentication error.
468451
*/
469452
@Override
470453
public CompletableFuture<VaasVerdict> forUrlAsync(URL url)
471-
throws IOException, InterruptedException, VaasAuthenticationException {
454+
throws VaasAuthenticationException {
472455
return forUrlAsync(url, ForUrlOptions.fromVaasConfig(this.config));
473456
}
474457

@@ -482,14 +465,11 @@ public CompletableFuture<VaasVerdict> forUrlAsync(URL url)
482465
* @return a {@link CompletableFuture} containing the {@link VaasVerdict} for
483466
* the
484467
* URL
485-
* @throws IOException If an I/O error occurs during the
486-
* request.
487-
* @throws InterruptedException If the operation is interrupted.
488468
* @throws VaasAuthenticationException If there is an authentication error.
489469
*/
490470
@Override
491471
public CompletableFuture<VaasVerdict> forUrlAsync(URL url, ForUrlOptions options)
492-
throws IOException, InterruptedException, VaasAuthenticationException {
472+
throws VaasAuthenticationException {
493473
var params = Map.of("useHashLookup", String.valueOf(options.isUseHashLookup()));
494474
var urlsUri = this.config.getUrl() + "/urls";
495475
var urlAnalysisRequest = new UrlAnalysisRequest(url.toString(), options.isUseHashLookup());
@@ -530,12 +510,11 @@ public CompletableFuture<VaasVerdict> forUrlAsync(URL url, ForUrlOptions options
530510
* waiting
531511
* for the result
532512
* @throws ExecutionException if the computation threw an exception
533-
* @throws IOException if an I/O error occurs
534513
* @throws VaasAuthenticationException if there is an authentication error
535514
*/
536515
@Override
537516
public VaasVerdict forUrl(URL url)
538-
throws InterruptedException, ExecutionException, IOException, VaasAuthenticationException {
517+
throws InterruptedException, ExecutionException, VaasAuthenticationException {
539518
return forUrlAsync(url).get();
540519
}
541520

@@ -550,12 +529,11 @@ public VaasVerdict forUrl(URL url)
550529
* waiting
551530
* for the result
552531
* @throws ExecutionException if the computation threw an exception
553-
* @throws IOException if an I/O error occurs
554532
* @throws VaasAuthenticationException if there is an authentication error
555533
*/
556534
@Override
557535
public VaasVerdict forUrl(URL url, ForUrlOptions options)
558-
throws InterruptedException, ExecutionException, IOException, VaasAuthenticationException {
536+
throws InterruptedException, ExecutionException, VaasAuthenticationException {
559537
return forUrlAsync(url, options).get();
560538
}
561539

0 commit comments

Comments
 (0)