|
15 | 15 | */ |
16 | 16 | package org.commonjava.util.sidecar.services; |
17 | 17 |
|
| 18 | +import io.smallrye.mutiny.Multi; |
18 | 19 | import io.smallrye.mutiny.Uni; |
19 | 20 | import io.vertx.core.http.HttpMethod; |
20 | 21 | import io.vertx.core.http.HttpServerRequest; |
|
36 | 37 | import java.io.IOException; |
37 | 38 | import java.io.InputStream; |
38 | 39 | import java.util.LinkedHashMap; |
| 40 | +import java.util.List; |
39 | 41 | import java.util.Map; |
40 | 42 |
|
41 | 43 | import static io.vertx.core.http.HttpMethod.HEAD; |
@@ -137,42 +139,40 @@ public Uni<Boolean> validateChecksum( String trackingId, String packageType, Str |
137 | 139 | HttpServerRequest request ) |
138 | 140 | { |
139 | 141 | 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 -> { |
144 | 143 | String localChecksum = localChecksums.get( checksumType ); |
145 | 144 | if ( localChecksum == null ) |
146 | 145 | { |
147 | | - continue; |
| 146 | + return Multi.createFrom().item( false ); |
148 | 147 | } |
149 | 148 | 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 | + } ); |
176 | 176 | } |
177 | 177 |
|
178 | 178 | private Uni<Boolean> downloadAndCompareChecksum( String trackingId, String packageType, String type, String name, |
|
0 commit comments