Skip to content

Commit 16b12f1

Browse files
authored
[MRESOLVER-241] Resolver checksum calculation should be driven by layout (#154)
Commit contains two fixes: * MRESOLVER-241 * MRESOLVER-242 High level explanations (as fix of two issues is overlapping) MRESOLVER-241 First part: Refactor checksum calculator and validator to rely on layout used checksums (layout always had these), instead of presence (or absence) or remote checksum locations. Checksum locations should drive only the checksum provision from remote, and nothing else. This change not only fixes the problem with signarure (they have no remote checksums), but also makes new "provided" and "included" checksum still work as expected (fun fact: central DOES provide checksum of checksum in header, even if layout does not). MRESOLVER-242 Second part: Change checksum validator to validate against remote checksum location ONLY if they are expected to exist. This fixes the issue that in some cases, layout returns empty collection for remote checksum locations (as was Maven2RepositoryLayoutEx for signatures). Moreover, introduce checksum filter to resolver to be able to configure ignored extensions instead to have "wired in" `.asc` extension only. The implementation supports "old way" forSignatures=true as well by having empty string for new configuration property (as then no extension will be omitted for checksums, so even signatures will have them created/deployed and fetched/validated). --- https://issues.apache.org/jira/browse/MRESOLVER-241 https://issues.apache.org/jira/browse/MRESOLVER-242
1 parent 374109f commit 16b12f1

File tree

10 files changed

+172
-77
lines changed

10 files changed

+172
-77
lines changed

maven-resolver-connector-basic/src/main/java/org/eclipse/aether/connector/basic/BasicRepositoryConnector.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ public void get( Collection<? extends ArtifactDownload> artifactDownloads,
227227

228228
Executor executor = getExecutor( artifactDownloads, metadataDownloads );
229229
RunnableErrorForwarder errorForwarder = new RunnableErrorForwarder();
230+
List<ChecksumAlgorithmFactory> checksumAlgorithmFactories = layout.getChecksumAlgorithmFactories();
230231

231232
for ( MetadataDownload transfer : safe( metadataDownloads ) )
232233
{
@@ -244,7 +245,7 @@ public void get( Collection<? extends ArtifactDownload> artifactDownloads,
244245
}
245246

246247
Runnable task = new GetTaskRunner( location, transfer.getFile(), checksumPolicy,
247-
checksumLocations, null, listener );
248+
checksumAlgorithmFactories, checksumLocations, null, listener );
248249
executor.execute( errorForwarder.wrap( task ) );
249250
}
250251

@@ -254,7 +255,7 @@ public void get( Collection<? extends ArtifactDownload> artifactDownloads,
254255
for ( ProvidedChecksumsSource providedChecksumsSource : providedChecksumsSources.values() )
255256
{
256257
Map<String, String> provided = providedChecksumsSource.getProvidedArtifactChecksums(
257-
session, transfer, layout.getChecksumAlgorithmFactories() );
258+
session, transfer, checksumAlgorithmFactories );
258259

259260
if ( provided != null )
260261
{
@@ -284,7 +285,7 @@ public void get( Collection<? extends ArtifactDownload> artifactDownloads,
284285
}
285286

286287
task = new GetTaskRunner( location, transfer.getFile(), checksumPolicy,
287-
checksumLocations, providedChecksums, listener );
288+
checksumAlgorithmFactories, checksumLocations, providedChecksums, listener );
288289
}
289290
executor.execute( errorForwarder.wrap( task ) );
290291
}
@@ -434,13 +435,14 @@ class GetTaskRunner
434435
private final ChecksumValidator checksumValidator;
435436

436437
GetTaskRunner( URI path, File file, ChecksumPolicy checksumPolicy,
438+
List<ChecksumAlgorithmFactory> checksumAlgorithmFactories,
437439
List<RepositoryLayout.ChecksumLocation> checksumLocations,
438440
Map<String, String> providedChecksums,
439441
TransferTransportListener<?> listener )
440442
{
441443
super( path, listener );
442444
this.file = requireNonNull( file, "destination file cannot be null" );
443-
checksumValidator = new ChecksumValidator( file, fileProcessor, this,
445+
checksumValidator = new ChecksumValidator( file, checksumAlgorithmFactories, fileProcessor, this,
444446
checksumPolicy, providedChecksums, safe( checksumLocations ) );
445447
}
446448

maven-resolver-connector-basic/src/main/java/org/eclipse/aether/connector/basic/ChecksumCalculator.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636

3737
import org.eclipse.aether.spi.connector.checksum.ChecksumAlgorithm;
3838
import org.eclipse.aether.spi.connector.checksum.ChecksumAlgorithmFactory;
39-
import org.eclipse.aether.spi.connector.layout.RepositoryLayout;
4039

4140
import static java.util.Objects.requireNonNull;
4241

@@ -92,25 +91,25 @@ public Object get()
9291
private final File targetFile;
9392

9493
public static ChecksumCalculator newInstance( File targetFile,
95-
Collection<RepositoryLayout.ChecksumLocation> checksumLocations )
94+
Collection<ChecksumAlgorithmFactory> checksumAlgorithmFactories )
9695
{
97-
if ( checksumLocations == null || checksumLocations.isEmpty() )
96+
if ( checksumAlgorithmFactories == null || checksumAlgorithmFactories.isEmpty() )
9897
{
9998
return null;
10099
}
101-
return new ChecksumCalculator( targetFile, checksumLocations );
100+
return new ChecksumCalculator( targetFile, checksumAlgorithmFactories );
102101
}
103102

104103
private ChecksumCalculator( File targetFile,
105-
Collection<RepositoryLayout.ChecksumLocation> checksumLocations )
104+
Collection<ChecksumAlgorithmFactory> checksumAlgorithmFactories )
106105
{
107106
this.checksums = new ArrayList<>();
108107
Set<String> algos = new HashSet<>();
109-
for ( RepositoryLayout.ChecksumLocation checksumLocation : checksumLocations )
108+
for ( ChecksumAlgorithmFactory checksumAlgorithmFactory : checksumAlgorithmFactories )
110109
{
111-
if ( algos.add( checksumLocation.getChecksumAlgorithmFactory().getName() ) )
110+
if ( algos.add( checksumAlgorithmFactory.getName() ) )
112111
{
113-
this.checksums.add( new Checksum( checksumLocation.getChecksumAlgorithmFactory() ) );
112+
this.checksums.add( new Checksum( checksumAlgorithmFactory ) );
114113
}
115114
}
116115
this.targetFile = targetFile;

maven-resolver-connector-basic/src/main/java/org/eclipse/aether/connector/basic/ChecksumValidator.java

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ boolean fetchChecksum( URI remote, File local )
5959

6060
private final File dataFile;
6161

62+
private final Collection<ChecksumAlgorithmFactory> checksumAlgorithmFactories;
63+
6264
private final Collection<File> tempFiles;
6365

6466
private final FileProcessor fileProcessor;
@@ -74,13 +76,15 @@ boolean fetchChecksum( URI remote, File local )
7476
private final Map<File, Object> checksumFiles;
7577

7678
ChecksumValidator( File dataFile,
79+
Collection<ChecksumAlgorithmFactory> checksumAlgorithmFactories,
7780
FileProcessor fileProcessor,
7881
ChecksumFetcher checksumFetcher,
7982
ChecksumPolicy checksumPolicy,
8083
Map<String, String> providedChecksums,
8184
Collection<ChecksumLocation> checksumLocations )
8285
{
8386
this.dataFile = dataFile;
87+
this.checksumAlgorithmFactories = checksumAlgorithmFactories;
8488
this.tempFiles = new HashSet<>();
8589
this.fileProcessor = fileProcessor;
8690
this.checksumFetcher = checksumFetcher;
@@ -94,7 +98,7 @@ public ChecksumCalculator newChecksumCalculator( File targetFile )
9498
{
9599
if ( checksumPolicy != null )
96100
{
97-
return ChecksumCalculator.newInstance( targetFile, checksumLocations );
101+
return ChecksumCalculator.newInstance( targetFile, checksumAlgorithmFactories );
98102
}
99103
return null;
100104
}
@@ -116,11 +120,14 @@ && validateChecksums( actualChecksums, ChecksumKind.REMOTE_INCLUDED, includedChe
116120
{
117121
return;
118122
}
119-
if ( validateExternalChecksums( actualChecksums ) )
123+
if ( !checksumLocations.isEmpty() )
120124
{
121-
return;
125+
if ( validateExternalChecksums( actualChecksums ) )
126+
{
127+
return;
128+
}
129+
checksumPolicy.onNoMoreChecksums();
122130
}
123-
checksumPolicy.onNoMoreChecksums();
124131
}
125132

126133
private boolean validateChecksums( Map<String, ?> actualChecksums, ChecksumKind kind, Map<String, ?> checksums )
@@ -134,27 +141,26 @@ private boolean validateChecksums( Map<String, ?> actualChecksums, ChecksumKind
134141
{
135142
continue;
136143
}
137-
ChecksumLocation checksumLocation = checksumLocations.stream()
138-
.filter( a -> a.getChecksumAlgorithmFactory().getName().equals( algo ) )
144+
ChecksumAlgorithmFactory checksumAlgorithmFactory = checksumAlgorithmFactories.stream()
145+
.filter( a -> a.getName().equals( algo ) )
139146
.findFirst()
140147
.orElse( null );
141-
if ( checksumLocation == null )
148+
if ( checksumAlgorithmFactory == null )
142149
{
143150
continue;
144151
}
145152

146153
String actual = String.valueOf( calculated );
147154
String expected = entry.getValue().toString();
148-
ChecksumAlgorithmFactory factory = checksumLocation.getChecksumAlgorithmFactory();
149-
checksumFiles.put( getChecksumFile( factory ), expected );
155+
checksumFiles.put( getChecksumFile( checksumAlgorithmFactory ), expected );
150156

151157
if ( !isEqualChecksum( expected, actual ) )
152158
{
153-
checksumPolicy.onChecksumMismatch( factory.getName(), kind,
159+
checksumPolicy.onChecksumMismatch( checksumAlgorithmFactory.getName(), kind,
154160
new ChecksumFailureException( expected, kind.name(), actual )
155161
);
156162
}
157-
else if ( checksumPolicy.onChecksumMatch( factory.getName(), kind ) )
163+
else if ( checksumPolicy.onChecksumMatch( checksumAlgorithmFactory.getName(), kind ) )
158164
{
159165
return true;
160166
}

maven-resolver-connector-basic/src/test/java/org/eclipse/aether/connector/basic/ChecksumCalculatorTest.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,14 @@
2727

2828
import java.io.File;
2929
import java.io.IOException;
30-
import java.net.URI;
3130
import java.nio.ByteBuffer;
3231
import java.nio.charset.StandardCharsets;
3332
import java.util.ArrayList;
3433
import java.util.List;
3534
import java.util.Map;
3635

3736
import org.eclipse.aether.internal.test.util.TestFileUtils;
38-
import org.eclipse.aether.spi.connector.layout.RepositoryLayout;
37+
import org.eclipse.aether.spi.connector.checksum.ChecksumAlgorithmFactory;
3938
import org.junit.Before;
4039
import org.junit.Test;
4140

@@ -48,12 +47,12 @@ public class ChecksumCalculatorTest
4847

4948
private ChecksumCalculator newCalculator( String... algos )
5049
{
51-
List<RepositoryLayout.ChecksumLocation> checksumLocations = new ArrayList<>();
50+
List<ChecksumAlgorithmFactory> checksumAlgorithmFactories = new ArrayList<>();
5251
for ( String algo : algos )
5352
{
54-
checksumLocations.add( new RepositoryLayout.ChecksumLocation( URI.create( "irrelevant" ), selector.select( algo ) ) );
53+
checksumAlgorithmFactories.add( selector.select( algo ) );
5554
}
56-
return ChecksumCalculator.newInstance( file, checksumLocations );
55+
return ChecksumCalculator.newInstance( file, checksumAlgorithmFactories );
5756
}
5857

5958
private ByteBuffer toBuffer( String data )

maven-resolver-connector-basic/src/test/java/org/eclipse/aether/connector/basic/ChecksumValidatorTest.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535

3636
import org.eclipse.aether.internal.test.util.TestFileProcessor;
3737
import org.eclipse.aether.internal.test.util.TestFileUtils;
38+
import org.eclipse.aether.spi.connector.checksum.ChecksumAlgorithmFactory;
3839
import org.eclipse.aether.spi.connector.checksum.ChecksumPolicy;
3940
import org.eclipse.aether.spi.connector.checksum.ChecksumPolicy.ChecksumKind;
4041
import org.eclipse.aether.spi.connector.layout.RepositoryLayout;
@@ -185,17 +186,27 @@ private static URI toUri( String algo )
185186

186187
private static final TestChecksumAlgorithmSelector selector = new TestChecksumAlgorithmSelector();
187188

189+
private List<ChecksumAlgorithmFactory> newChecksumAlgorithmFactories( String... factories )
190+
{
191+
List<ChecksumAlgorithmFactory> checksums = new ArrayList<>();
192+
for ( String factory : factories )
193+
{
194+
checksums.add( selector.select( factory ) );
195+
}
196+
return checksums;
197+
}
198+
188199
private static RepositoryLayout.ChecksumLocation newChecksum( String factory )
189200
{
190201
return RepositoryLayout.ChecksumLocation.forLocation( URI.create( "file" ), selector.select( factory ) );
191202
}
192203

193-
private List<RepositoryLayout.ChecksumLocation> newChecksums( String... factories )
204+
private List<RepositoryLayout.ChecksumLocation> newChecksums( List<ChecksumAlgorithmFactory> checksumAlgorithmFactories )
194205
{
195206
List<RepositoryLayout.ChecksumLocation> checksums = new ArrayList<>();
196-
for ( String factory : factories )
207+
for ( ChecksumAlgorithmFactory factory : checksumAlgorithmFactories )
197208
{
198-
checksums.add( newChecksum( factory ) );
209+
checksums.add( RepositoryLayout.ChecksumLocation.forLocation( URI.create( "file" ), factory ) );
199210
}
200211
return checksums;
201212
}
@@ -207,7 +218,8 @@ private ChecksumValidator newValidator( String... factories )
207218

208219
private ChecksumValidator newValidator( Map<String, String> providedChecksums, String... factories )
209220
{
210-
return new ChecksumValidator( dataFile, new TestFileProcessor(), fetcher, policy, providedChecksums, newChecksums( factories ) );
221+
List<ChecksumAlgorithmFactory> checksumAlgorithmFactories = newChecksumAlgorithmFactories( factories );
222+
return new ChecksumValidator( dataFile, checksumAlgorithmFactories, new TestFileProcessor(), fetcher, policy, providedChecksums, newChecksums( checksumAlgorithmFactories ) );
211223
}
212224

213225
private Map<String, ?> checksums( String... algoDigestPairs )

0 commit comments

Comments
 (0)