3030import java .util .UUID ;
3131import java .util .concurrent .CompletableFuture ;
3232import java .util .concurrent .ExecutionException ;
33+ import java .util .concurrent .ExecutorService ;
3334import java .util .concurrent .TimeUnit ;
3435import java .util .concurrent .TimeoutException ;
3536import java .util .function .Function ;
@@ -155,45 +156,47 @@ private static String encodeParams(Map<String, String> params) {
155156 return encodedParams .toString ();
156157 }
157158
159+ private static CompletableFuture <VaasVerdict > parseVaasError (HttpResponse response )
160+ throws VaasClientException , VaasServerException , VaasAuthenticationException {
161+ var problemDetails = response .body () != null ? ProblemDetails .fromJson (response .body ().toString ()) : new ProblemDetails ();
162+ switch (response .statusCode ()) {
163+ case 400 :
164+ return CompletableFuture .failedFuture (new VaasClientException (problemDetails .detail ));
165+ case 401 :
166+ return CompletableFuture .failedFuture (new VaasAuthenticationException ());
167+ default :
168+ return CompletableFuture .failedFuture (new VaasServerException (problemDetails .detail ));
169+ }
170+ }
171+
158172 private static CompletableFuture <VaasVerdict > sendFileWithRetry (HttpClient httpClient , HttpRequest request ) {
159173 return httpClient .sendAsync (request , HttpResponse .BodyHandlers .ofString ())
160- .thenCompose (response -> {
174+ .thenCompose (handleException ( response -> {
161175 switch (response .statusCode ()) {
162176 case 200 :
163177 var fileReport = FileReport .fromJson (response .body ());
164- System .out .println ("Get filereport with Verdict" + fileReport .getVerdict ());
165178 return CompletableFuture .completedFuture (VaasVerdict .From (fileReport ));
166179 case 201 :
167- System .out .println ("Request acceptet" );
168180 return sendFileWithRetry (httpClient , request );
169- case 400 :
170- return CompletableFuture .failedFuture (new VaasClientException (response .body ()));
171- case 401 :
172- return CompletableFuture .failedFuture (new VaasAuthenticationException ());
173181 default :
174- return CompletableFuture . failedFuture ( new VaasServerException ( response . body ()) );
182+ return parseVaasError ( response );
175183 }
176- });
184+ })) ;
177185 }
178-
179186
180187 private static CompletableFuture <VaasVerdict > sendUrlWithRetry (HttpClient httpClient , HttpRequest request ) {
181188 return httpClient .sendAsync (request , HttpResponse .BodyHandlers .ofString ())
182- .thenCompose (response -> {
189+ .thenCompose (handleException ( response -> {
183190 switch (response .statusCode ()) {
184191 case 200 :
185192 var urlReport = UrlReport .fromJson (response .body ());
186193 return CompletableFuture .completedFuture (VaasVerdict .From (urlReport ));
187194 case 201 :
188195 return sendUrlWithRetry (httpClient , request );
189- case 400 :
190- return CompletableFuture .failedFuture (new VaasClientException ("foobar" ));
191- case 401 :
192- return CompletableFuture .failedFuture (new VaasAuthenticationException ());
193196 default :
194- return CompletableFuture . failedFuture ( new VaasServerException ( "foobar2" ) );
197+ return parseVaasError ( response );
195198 }
196- });
199+ })) ;
197200 }
198201
199202 public HttpRequest .Builder CreateHttpRequestBuilderWithHeaders (URI uri , String requestId )
@@ -218,16 +221,11 @@ public CompletableFuture<VaasVerdict> forSha256(Sha256 sha256)
218221 @ Override
219222 public CompletableFuture <VaasVerdict > forSha256 (Sha256 sha256 , ForSha256Options options )
220223 throws IOException , InterruptedException , VaasAuthenticationException {
221-
222- System .out .println ("Start processing SHA256" + sha256 );
223224 var params = Map .of (
224225 "useCache" , String .valueOf (options .isUseCache ()),
225226 "useHashLookup" , String .valueOf (options .isUseHashLookup ()));
226-
227227 var filesReportUri = this .config .getUrl () + String .format ("/files/%s/report?" , sha256 .getValue ())
228228 + encodeParams (params );
229-
230- System .out .println ("Create request with url " + filesReportUri );
231229 var request = CreateHttpRequestBuilderWithHeaders (URI .create (filesReportUri ), options .getVaasRequestId ())
232230 .GET ()
233231 .build ();
@@ -241,7 +239,8 @@ public CompletableFuture<VaasVerdict> forSha256(Sha256 sha256, ForSha256Options
241239
242240 @ Override
243241 public CompletableFuture <VaasVerdict > forFile (Path file )
244- throws NoSuchAlgorithmException , IOException , URISyntaxException , InterruptedException , VaasAuthenticationException {
242+ throws NoSuchAlgorithmException , IOException , URISyntaxException , InterruptedException ,
243+ VaasAuthenticationException {
245244 var forFileOptions = new ForFileOptions ();
246245 forFileOptions .setUseCache (true );
247246 forFileOptions .setUseHashLookup (true );
@@ -298,14 +297,14 @@ public CompletableFuture<VaasVerdict> forStream(InputStream inputStream, long co
298297 contentLength );
299298 var request = CreateHttpRequestBuilderWithHeaders (URI .create (filesUri ), options .getVaasRequestId ())
300299 .POST (bodyPublisher )
301- .header ("Content-Type" , "application/octet-stream " )
300+ .header ("Content-Type" , "application/octet-stream" )
302301 .build ();
303302
304303 return httpClient .sendAsync (request , HttpResponse .BodyHandlers .ofString ())
305304 .thenCompose (handleException (response -> {
306305 var statusCode = response .statusCode ();
307306 if (statusCode < 200 || statusCode >= 300 ) {
308- throw new RuntimeException ( "Unexpected response status: " + statusCode );
307+ return parseVaasError ( response );
309308 }
310309 var fileResponseStarted = FileAnalysisStarted .fromJson (response .body ());
311310 var sha256 = new Sha256 (fileResponseStarted .getSha256 ());
@@ -318,32 +317,33 @@ public CompletableFuture<VaasVerdict> forStream(InputStream inputStream, long co
318317 }
319318
320319 @ Override
321- public CompletableFuture <VaasVerdict > forUrl (URL url ) throws URISyntaxException , IOException , InterruptedException , VaasAuthenticationException {
320+ public CompletableFuture <VaasVerdict > forUrl (URL url ) throws URISyntaxException , IOException , InterruptedException ,
321+ VaasAuthenticationException , VaasClientException , VaasServerException {
322322 var forUrlOptions = new ForUrlOptions ();
323323 forUrlOptions .setUseHashLookup (true );
324324 return forUrl (url , forUrlOptions );
325325 }
326326
327327 @ Override
328328 public CompletableFuture <VaasVerdict > forUrl (URL url , ForUrlOptions options )
329- throws IOException , InterruptedException , VaasAuthenticationException {
329+ throws IOException , InterruptedException , VaasAuthenticationException , VaasClientException ,
330+ VaasServerException {
330331 var params = Map .of ("useHashLookup" , String .valueOf (options .isUseHashLookup ()));
331- var urlsUri = this .config .getUrl () + "/urls?" + encodeParams ( params ) ;
332+ var urlsUri = this .config .getUrl () + "/urls" ;
332333 var urlAnalysisRequest = new UrlAnalysisRequest (url .toString (), options .isUseHashLookup ());
333334 var request = CreateHttpRequestBuilderWithHeaders (URI .create (urlsUri ), options .getVaasRequestId ())
334335 .POST (HttpRequest .BodyPublishers .ofString (UrlAnalysisRequest .ToJson (urlAnalysisRequest )))
335336 .header ("Content-Type" , "application/json" )
336337 .build ();
337338
338- // Return a fully resolved CompletableFuture
339339 return httpClient .sendAsync (request , HttpResponse .BodyHandlers .ofString ())
340- .thenApply (response -> {
340+ .thenApply (handleException ( response -> {
341341 var statusCode = response .statusCode ();
342342 if (statusCode < 200 || statusCode >= 300 ) {
343- throw new RuntimeException ( "Unexpected response status: " + statusCode );
343+ parseVaasError ( response );
344344 }
345345 return UrlAnalysisStarted .fromJson (response .body ());
346- })
346+ }))
347347 .thenCompose (handleException (urlAnalysisStarted -> {
348348 var urlsReportUri = this .config .getUrl ()
349349 + String .format ("/urls/%s/report?" , urlAnalysisStarted .getId ()) + encodeParams (params );
0 commit comments