Skip to content

Commit 6242a78

Browse files
authored
Merge pull request #86 from yma96/2.0.x
Fix Checksum validation Uni async loop with Multi flatMap
2 parents 73eb606 + 085b0cf commit 6242a78

File tree

1 file changed

+31
-31
lines changed

1 file changed

+31
-31
lines changed

src/main/java/org/commonjava/util/sidecar/services/ProxyService.java

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.commonjava.util.sidecar.services;
1717

18+
import io.smallrye.mutiny.Multi;
1819
import io.smallrye.mutiny.Uni;
1920
import io.vertx.core.http.HttpMethod;
2021
import io.vertx.core.http.HttpServerRequest;
@@ -36,6 +37,7 @@
3637
import java.io.IOException;
3738
import java.io.InputStream;
3839
import java.util.LinkedHashMap;
40+
import java.util.List;
3941
import java.util.Map;
4042

4143
import static io.vertx.core.http.HttpMethod.HEAD;
@@ -137,42 +139,40 @@ public Uni<Boolean> validateChecksum( String trackingId, String packageType, Str
137139
HttpServerRequest request )
138140
{
139141
Map<String, String> localChecksums = getChecksums( path );
140-
Uni<Boolean> resultUni = Uni.createFrom().item( false );
141-
142-
for ( String checksumType : localChecksums.keySet() )
143-
{
142+
Multi<Boolean> multiResults = Multi.createFrom().iterable( localChecksums.keySet() ).flatMap( checksumType -> {
144143
String localChecksum = localChecksums.get( checksumType );
145144
if ( localChecksum == null )
146145
{
147-
continue;
146+
return Multi.createFrom().item( false );
148147
}
149148
String checksumUrl = path + "." + checksumType;
150-
resultUni = resultUni.onItem().call( () -> {
151-
try
152-
{
153-
return downloadAndCompareChecksum( trackingId, packageType, type, name, checksumUrl, localChecksum,
154-
request ).onItem().invoke( result -> {
155-
if ( result != null && result )
156-
{
157-
// This is just used to skip loop to avoid unnecessary checksum download
158-
logger.debug(
159-
"Found the valid checksum compare result, stopping further checks, remote path {}",
160-
checksumUrl );
161-
throw new FoundValidChecksumException();
162-
}
163-
} );
164-
}
165-
catch ( Exception e )
166-
{
167-
logger.error( "Checksum download compare error for path: {}", checksumUrl, e );
168-
}
169-
return null;
170-
} );
171-
}
172-
return resultUni.onFailure().recoverWithItem( false ).onItem().transform( result -> {
173-
// If catch FoundValidChecksumException,return true
174-
return true;
175-
} ); // If no valid checksum compare result found, return false
149+
try
150+
{
151+
return downloadAndCompareChecksum( trackingId, packageType, type, name, checksumUrl, localChecksum,
152+
request ).onItem().invoke( result -> {
153+
if ( result != null && result )
154+
{
155+
// This is just used to skip loop to avoid unnecessary checksum download
156+
logger.debug(
157+
"Found the valid checksum compare result, stopping further checks, remote path {}",
158+
checksumUrl );
159+
throw new FoundValidChecksumException();
160+
}
161+
} ).onFailure().recoverWithItem( true ).toMulti();
162+
}
163+
catch ( Exception e )
164+
{
165+
logger.error( "Checksum download compare error for path: {}", checksumUrl, e );
166+
}
167+
return Multi.createFrom().item( false );
168+
} );
169+
170+
Uni<List<Boolean>> collectedResults = multiResults.collect().asList();
171+
return collectedResults.onItem().transform( results -> {
172+
boolean finalResult = results.stream().anyMatch( res -> res );
173+
logger.debug( "FinalResult:{}", finalResult );
174+
return finalResult;
175+
} );
176176
}
177177

178178
private Uni<Boolean> downloadAndCompareChecksum( String trackingId, String packageType, String type, String name,

0 commit comments

Comments
 (0)