Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Optional;

Expand Down Expand Up @@ -92,39 +91,11 @@ public Uni<Response> get( @Parameter( in = PATH, required = true ) @PathParam( "
Optional<File> download = archiveService.getLocally( path );
if ( download.isPresent() && download.get().isFile() && reportService.checkValidHistoricalEntryMeta( path ) )
{
Uni<Boolean> checksumValidation =
proxyService.validateChecksum( id, packageType, type, name, path, request );
return checksumValidation.onItem().transform( result -> {
if ( result != null && result )
{
try
{
InputStream inputStream = FileUtils.openInputStream( download.get() );
final Response.ResponseBuilder builder =
Response.ok( new TransferStreamingOutput( inputStream ) );
logger.debug( "Download path: {} from historical archive.", path );
publishTrackingEvent( path, id );
return Uni.createFrom().item( builder.build() );
}
catch ( IOException e )
{
logger.error( "IO error for local file, path {}.", path, e );
}
}
else
{
try
{
logger.debug( "Checksum validation failed, download from proxy: {}.", path );
return proxyService.doGet( id, packageType, type, name, path, request );
}
catch ( Exception e )
{
logger.error( "Error for proxy download, path {}.", path, e );
}
}
return null;
} ).flatMap( response -> response );
InputStream inputStream = FileUtils.openInputStream( download.get() );
final Response.ResponseBuilder builder = Response.ok( new TransferStreamingOutput( inputStream ) );
logger.debug( "Download path: {} from historical archive.", path );
publishTrackingEvent( path, id );
return Uni.createFrom().item( builder.build() );
}
else
{
Expand Down
118 changes: 0 additions & 118 deletions src/main/java/org/commonjava/util/sidecar/services/ProxyService.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@
*/
package org.commonjava.util.sidecar.services;

import io.smallrye.mutiny.Multi;
import io.smallrye.mutiny.Uni;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.http.HttpServerRequest;
import kotlin.Pair;
import org.commonjava.util.sidecar.config.ProxyConfiguration;
import org.commonjava.util.sidecar.interceptor.ExceptionHandler;
import org.commonjava.util.sidecar.model.dto.HistoricalEntryDTO;
import org.commonjava.util.sidecar.util.OtelAdapter;
import org.commonjava.util.sidecar.util.ProxyStreamingOutput;
import org.commonjava.util.sidecar.util.UrlUtils;
Expand All @@ -33,12 +31,7 @@
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.ws.rs.core.Response;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import static io.vertx.core.http.HttpMethod.HEAD;
import static javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR;
Expand Down Expand Up @@ -135,76 +128,6 @@ public Uni<Response> wrapAsyncCall( WebClientAdapter.CallAdapter asyncCall, Http
return ret.onFailure().recoverWithItem( this::handleProxyException );
}

public Uni<Boolean> validateChecksum( String trackingId, String packageType, String type, String name, String path,
HttpServerRequest request )
{
Map<String, String> localChecksums = getChecksums( path );
Multi<Boolean> multiResults = Multi.createFrom().iterable( localChecksums.keySet() ).flatMap( checksumType -> {
String localChecksum = localChecksums.get( checksumType );
if ( localChecksum == null )
{
return Multi.createFrom().item( false );
}
String checksumUrl = path + "." + checksumType;
try
{
return downloadAndCompareChecksum( trackingId, packageType, type, name, checksumUrl, localChecksum,
request ).onItem().invoke( result -> {
if ( result != null && result )
{
// This is just used to skip loop to avoid unnecessary checksum download
logger.debug(
"Found the valid checksum compare result, stopping further checks, remote path {}",
checksumUrl );
throw new FoundValidChecksumException();
}
} ).onFailure().recoverWithItem( true ).toMulti();
}
catch ( Exception e )
{
logger.error( "Checksum download compare error for path: {}", checksumUrl, e );
}
return Multi.createFrom().item( false );
} );

Uni<List<Boolean>> collectedResults = multiResults.collect().asList();
return collectedResults.onItem().transform( results -> {
boolean finalResult = results.stream().anyMatch( res -> res );
logger.debug( "FinalResult:{}", finalResult );
return finalResult;
} );
}

private Uni<Boolean> downloadAndCompareChecksum( String trackingId, String packageType, String type, String name,
String checksumUrl, String localChecksum,
HttpServerRequest request )
throws Exception
{
return doGet( trackingId, packageType, type, name, checksumUrl, request ).onItem().transform( response -> {
if ( response.getStatus() == Response.Status.OK.getStatusCode() )
{
ProxyStreamingOutput streamingOutput = (ProxyStreamingOutput) response.getEntity();
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream())
{
streamingOutput.write( outputStream );
String remoteChecksum = outputStream.toString();
return localChecksum.equals( remoteChecksum );
}
catch ( IOException e )
{
logger.error( "Error to read remote checksum, path:{}.", checksumUrl, e );
return null;
}
}
else
{
logger.error( "Failed to download remote checksum for {}: HTTP {}.", checksumUrl,
response.getStatus() );
return null;
}
} );
}

/**
* Send status 500 with error message body.
* @param t error
Expand Down Expand Up @@ -245,45 +168,4 @@ private boolean isHeaderAllowed( Pair<? extends String, ? extends String> header
return !FORBIDDEN_HEADERS.contains( key.toLowerCase() );
}

private Map<String, String> getChecksums( String path )
{
Map<String, String> result = new LinkedHashMap<>();
HistoricalEntryDTO entryDTO = reportService.getHistoricalContentMap().get( path );
if ( entryDTO != null )
{
result.put( ChecksumType.SHA1.getValue(), entryDTO.getSha1() );
result.put( ChecksumType.SHA256.getValue(), entryDTO.getSha256() );
result.put( ChecksumType.MD5.getValue(), entryDTO.getMd5() );
}

return result;
}

enum ChecksumType
{
SHA1( "sha1" ),
SHA256( "sha256" ),
MD5( "md5" );

private final String value;

ChecksumType( String value )
{
this.value = value;
}

public String getValue()
{
return value;
}
}

class FoundValidChecksumException
extends RuntimeException
{
public FoundValidChecksumException()
{
super( "Found a valid checksum, stopping further checks." );
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,6 @@ public void storeTrackedDownload( JsonObject message )
}
}

public HashMap<String, HistoricalEntryDTO> getHistoricalContentMap()
{
return historicalContentMap;
}

public boolean checkValidHistoricalEntryMeta( String trackingPath )
{
HistoricalEntryDTO entryDTO = historicalContentMap.get( trackingPath );
Expand Down