1515 */
1616package org .commonjava .util .sidecar .services ;
1717
18- import io .smallrye .mutiny .Multi ;
1918import io .smallrye .mutiny .Uni ;
2019import io .vertx .core .http .HttpMethod ;
2120import io .vertx .core .http .HttpServerRequest ;
2221import kotlin .Pair ;
2322import org .commonjava .util .sidecar .config .ProxyConfiguration ;
2423import org .commonjava .util .sidecar .interceptor .ExceptionHandler ;
25- import org .commonjava .util .sidecar .model .dto .HistoricalEntryDTO ;
2624import org .commonjava .util .sidecar .util .OtelAdapter ;
2725import org .commonjava .util .sidecar .util .ProxyStreamingOutput ;
2826import org .commonjava .util .sidecar .util .UrlUtils ;
3331import javax .enterprise .context .ApplicationScoped ;
3432import javax .inject .Inject ;
3533import javax .ws .rs .core .Response ;
36- import java .io .ByteArrayOutputStream ;
37- import java .io .IOException ;
3834import java .io .InputStream ;
39- import java .util .LinkedHashMap ;
40- import java .util .List ;
41- import java .util .Map ;
4235
4336import static io .vertx .core .http .HttpMethod .HEAD ;
4437import static javax .ws .rs .core .Response .Status .INTERNAL_SERVER_ERROR ;
@@ -135,76 +128,6 @@ public Uni<Response> wrapAsyncCall( WebClientAdapter.CallAdapter asyncCall, Http
135128 return ret .onFailure ().recoverWithItem ( this ::handleProxyException );
136129 }
137130
138- public Uni <Boolean > validateChecksum ( String trackingId , String packageType , String type , String name , String path ,
139- HttpServerRequest request )
140- {
141- Map <String , String > localChecksums = getChecksums ( path );
142- Multi <Boolean > multiResults = Multi .createFrom ().iterable ( localChecksums .keySet () ).flatMap ( checksumType -> {
143- String localChecksum = localChecksums .get ( checksumType );
144- if ( localChecksum == null )
145- {
146- return Multi .createFrom ().item ( false );
147- }
148- String checksumUrl = path + "." + checksumType ;
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- }
177-
178- private Uni <Boolean > downloadAndCompareChecksum ( String trackingId , String packageType , String type , String name ,
179- String checksumUrl , String localChecksum ,
180- HttpServerRequest request )
181- throws Exception
182- {
183- return doGet ( trackingId , packageType , type , name , checksumUrl , request ).onItem ().transform ( response -> {
184- if ( response .getStatus () == Response .Status .OK .getStatusCode () )
185- {
186- ProxyStreamingOutput streamingOutput = (ProxyStreamingOutput ) response .getEntity ();
187- try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream ())
188- {
189- streamingOutput .write ( outputStream );
190- String remoteChecksum = outputStream .toString ();
191- return localChecksum .equals ( remoteChecksum );
192- }
193- catch ( IOException e )
194- {
195- logger .error ( "Error to read remote checksum, path:{}." , checksumUrl , e );
196- return null ;
197- }
198- }
199- else
200- {
201- logger .error ( "Failed to download remote checksum for {}: HTTP {}." , checksumUrl ,
202- response .getStatus () );
203- return null ;
204- }
205- } );
206- }
207-
208131 /**
209132 * Send status 500 with error message body.
210133 * @param t error
@@ -245,45 +168,4 @@ private boolean isHeaderAllowed( Pair<? extends String, ? extends String> header
245168 return !FORBIDDEN_HEADERS .contains ( key .toLowerCase () );
246169 }
247170
248- private Map <String , String > getChecksums ( String path )
249- {
250- Map <String , String > result = new LinkedHashMap <>();
251- HistoricalEntryDTO entryDTO = reportService .getHistoricalContentMap ().get ( path );
252- if ( entryDTO != null )
253- {
254- result .put ( ChecksumType .SHA1 .getValue (), entryDTO .getSha1 () );
255- result .put ( ChecksumType .SHA256 .getValue (), entryDTO .getSha256 () );
256- result .put ( ChecksumType .MD5 .getValue (), entryDTO .getMd5 () );
257- }
258-
259- return result ;
260- }
261-
262- enum ChecksumType
263- {
264- SHA1 ( "sha1" ),
265- SHA256 ( "sha256" ),
266- MD5 ( "md5" );
267-
268- private final String value ;
269-
270- ChecksumType ( String value )
271- {
272- this .value = value ;
273- }
274-
275- public String getValue ()
276- {
277- return value ;
278- }
279- }
280-
281- class FoundValidChecksumException
282- extends RuntimeException
283- {
284- public FoundValidChecksumException ()
285- {
286- super ( "Found a valid checksum, stopping further checks." );
287- }
288- }
289171}
0 commit comments