From 1d9313a3385c88f1d95bbcbce14d5b53dc95b269 Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Thu, 16 May 2024 19:57:19 +0200 Subject: [PATCH] Add RR change https://github.com/apache/maven-indexer/pull/364 --- .../backend/remoterepository/RecordFactory.java | 10 ++++++++-- .../MavenCentralResponseExtractor.java | 4 ++-- .../extractor/Nx2ResponseExtractor.java | 2 +- .../extractor/ResponseExtractorSupport.java | 9 +++++++-- .../RemoteRepositorySearchBackendImpl.java | 17 ++++++++++++++++- .../RemoteRepositorySearchBackendImplTest.java | 16 ++++++++++++++++ 6 files changed, 50 insertions(+), 8 deletions(-) diff --git a/search-backend-remoterepository/src/main/java/org/apache/maven/search/backend/remoterepository/RecordFactory.java b/search-backend-remoterepository/src/main/java/org/apache/maven/search/backend/remoterepository/RecordFactory.java index 0881ec7..e4be3a9 100644 --- a/search-backend-remoterepository/src/main/java/org/apache/maven/search/backend/remoterepository/RecordFactory.java +++ b/search-backend-remoterepository/src/main/java/org/apache/maven/search/backend/remoterepository/RecordFactory.java @@ -40,7 +40,13 @@ public RecordFactory(RemoteRepositorySearchBackend backend) { /** * Creates {@link Record} on behalf of backend. Only {@code groupId} is mandatory, all the other values are optional (nullable). */ - public Record create(String groupId, String artifactId, String version, String classifier, String fileExtension) { + public Record create( + String groupId, + String artifactId, + String version, + String classifier, + String fileExtension, + Long lastUpdated) { requireNonNull(groupId); HashMap result = new HashMap<>(); mayPut(result, MAVEN.GROUP_ID, groupId); @@ -48,7 +54,7 @@ public Record create(String groupId, String artifactId, String version, String c mayPut(result, MAVEN.VERSION, version); mayPut(result, MAVEN.CLASSIFIER, classifier); mayPut(result, MAVEN.FILE_EXTENSION, fileExtension); - return new Record(backend.getBackendId(), backend.getRepositoryId(), null, null, result); + return new Record(backend.getBackendId(), backend.getRepositoryId(), null, lastUpdated, result); } private static void mayPut(Map result, Field fieldName, Object value) { diff --git a/search-backend-remoterepository/src/main/java/org/apache/maven/search/backend/remoterepository/extractor/MavenCentralResponseExtractor.java b/search-backend-remoterepository/src/main/java/org/apache/maven/search/backend/remoterepository/extractor/MavenCentralResponseExtractor.java index 4ccbffc..e8a89c9 100644 --- a/search-backend-remoterepository/src/main/java/org/apache/maven/search/backend/remoterepository/extractor/MavenCentralResponseExtractor.java +++ b/search-backend-remoterepository/src/main/java/org/apache/maven/search/backend/remoterepository/extractor/MavenCentralResponseExtractor.java @@ -31,7 +31,7 @@ public class MavenCentralResponseExtractor extends ResponseExtractorSupport { /** * Extracts the "name" from {@code href} attribute. In case of Maven Central, the href - * attribute contains name in realative form as {@code "name/"} (followed by slash), if name denotes + * attribute contains name in relative form as {@code "name/"} (followed by slash), if name denotes * a directory. The trailing slash is removed by this method, if any. */ private String nameInHref(Element element) { @@ -51,7 +51,7 @@ public int populateG(Context context, Document document, RecordFactory recordFac for (Element element : contents.getElementsByTag("a")) { String name = nameInHref(element); if (accept(name)) { - page.add(recordFactory.create(context.getGroupId(), name, null, null, null)); + page.add(recordFactory.create(context.getGroupId(), name, null, null, null, null)); } } } diff --git a/search-backend-remoterepository/src/main/java/org/apache/maven/search/backend/remoterepository/extractor/Nx2ResponseExtractor.java b/search-backend-remoterepository/src/main/java/org/apache/maven/search/backend/remoterepository/extractor/Nx2ResponseExtractor.java index 535dc7d..0e05fa5 100644 --- a/search-backend-remoterepository/src/main/java/org/apache/maven/search/backend/remoterepository/extractor/Nx2ResponseExtractor.java +++ b/search-backend-remoterepository/src/main/java/org/apache/maven/search/backend/remoterepository/extractor/Nx2ResponseExtractor.java @@ -50,7 +50,7 @@ public int populateG(Context context, Document document, RecordFactory recordFac for (Element element : elements) { String name = name(element); if (accept(name)) { - page.add(recordFactory.create(context.getGroupId(), name, null, null, null)); + page.add(recordFactory.create(context.getGroupId(), name, null, null, null, null)); } } return page.size(); diff --git a/search-backend-remoterepository/src/main/java/org/apache/maven/search/backend/remoterepository/extractor/ResponseExtractorSupport.java b/search-backend-remoterepository/src/main/java/org/apache/maven/search/backend/remoterepository/extractor/ResponseExtractorSupport.java index 9727a26..6359590 100644 --- a/search-backend-remoterepository/src/main/java/org/apache/maven/search/backend/remoterepository/extractor/ResponseExtractorSupport.java +++ b/search-backend-remoterepository/src/main/java/org/apache/maven/search/backend/remoterepository/extractor/ResponseExtractorSupport.java @@ -70,7 +70,7 @@ public int populateGA(Context context, Document document, RecordFactory recordFa if (versions != null) { for (Element version : versions.getElementsByTag("version")) { page.add(recordFactory.create( - context.getGroupId(), context.getArtifactId(), version.text(), null, null)); + context.getGroupId(), context.getArtifactId(), version.text(), null, null, null)); } } } @@ -103,7 +103,12 @@ protected void populateGAVName(Context context, String name, RecordFactory recor ext = name; } page.add(recordFactory.create( - context.getGroupId(), context.getArtifactId(), context.getVersion(), classifier, ext)); + context.getGroupId(), + context.getArtifactId(), + context.getVersion(), + classifier, + ext, + null)); } } } diff --git a/search-backend-remoterepository/src/main/java/org/apache/maven/search/backend/remoterepository/internal/RemoteRepositorySearchBackendImpl.java b/search-backend-remoterepository/src/main/java/org/apache/maven/search/backend/remoterepository/internal/RemoteRepositorySearchBackendImpl.java index cf1eeb4..4e403f3 100644 --- a/search-backend-remoterepository/src/main/java/org/apache/maven/search/backend/remoterepository/internal/RemoteRepositorySearchBackendImpl.java +++ b/search-backend-remoterepository/src/main/java/org/apache/maven/search/backend/remoterepository/internal/RemoteRepositorySearchBackendImpl.java @@ -25,8 +25,12 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.nio.charset.StandardCharsets; +import java.time.ZoneId; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.List; +import java.util.Locale; import java.util.Map; import java.util.Objects; import java.util.Properties; @@ -188,12 +192,19 @@ public RemoteRepositorySearchResponse search(SearchRequest searchRequest) throws } } if (matches) { + String lastModifiedHeader = response.getHeaders().get("last-modified"); + Long lastModified = lastModifiedHeader == null + ? null + : ZonedDateTime.parse(lastModifiedHeader, RFC7231) + .toInstant() + .toEpochMilli(); page.add(recordFactory.create( context.getGroupId(), context.getArtifactId(), context.getVersion(), context.getClassifier(), - context.getFileExtension())); + context.getFileExtension(), + lastModified)); totalHits = 1; } } @@ -202,6 +213,10 @@ public RemoteRepositorySearchResponse search(SearchRequest searchRequest) throws return new RemoteRepositorySearchResponseImpl(searchRequest, totalHits, page, uri, document); } + private static final DateTimeFormatter RFC7231 = DateTimeFormatter.ofPattern( + "EEE, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH) + .withZone(ZoneId.of("GMT")); + private static String readChecksum(InputStream inputStream) throws IOException { String checksum = ""; try (BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8), 512)) { diff --git a/search-backend-remoterepository/src/test/java/org/apache/maven/search/backend/remoterepository/internal/RemoteRepositorySearchBackendImplTest.java b/search-backend-remoterepository/src/test/java/org/apache/maven/search/backend/remoterepository/internal/RemoteRepositorySearchBackendImplTest.java index 9ec2584..d4b0ba3 100644 --- a/search-backend-remoterepository/src/test/java/org/apache/maven/search/backend/remoterepository/internal/RemoteRepositorySearchBackendImplTest.java +++ b/search-backend-remoterepository/src/test/java/org/apache/maven/search/backend/remoterepository/internal/RemoteRepositorySearchBackendImplTest.java @@ -20,6 +20,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertThrows; import java.io.IOException; @@ -156,6 +157,21 @@ public void gav() throws IOException { dumpPage(searchResponse); } + @Test + public void gave() throws IOException { + // LIST GAVCEs + SearchRequest searchRequest = new SearchRequest(BooleanQuery.and( + FieldQuery.fieldQuery(MAVEN.GROUP_ID, "org.apache.maven.plugins"), + FieldQuery.fieldQuery(MAVEN.ARTIFACT_ID, "maven-clean-plugin"), + FieldQuery.fieldQuery(MAVEN.VERSION, "3.1.0"), + FieldQuery.fieldQuery(MAVEN.FILE_EXTENSION, "jar"))); + RemoteRepositorySearchResponse searchResponse = backend.search(searchRequest); + assertEquals(1, searchResponse.getTotalHits()); + assertNotNull(searchResponse.getPage().get(0).getLastUpdated()); + System.out.println("TOTAL HITS: " + searchResponse.getTotalHits()); + dumpPage(searchResponse); + } + @Test public void gavWithTarGz() throws IOException { // LIST GAVCEs