Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MINDEXER-222] RR backend in case of GAVCE search should reuse LastModified HTTP header #364

Merged
merged 2 commits into from
May 16, 2024
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 @@ -41,15 +41,21 @@ 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<Field, Object> result = new HashMap<>();
mayPut(result, MAVEN.GROUP_ID, groupId);
mayPut(result, MAVEN.ARTIFACT_ID, artifactId);
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<Field, Object> result, Field fieldName, Object value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,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) {
Expand All @@ -52,7 +52,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));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,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));
}
}
}
Expand Down Expand Up @@ -104,7 +104,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));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,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;
Expand Down Expand Up @@ -189,12 +193,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;
}
}
Expand All @@ -203,6 +214,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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.notNullValue;

/**
* UT for 2 backends: Maven Central and RAO releases. This tests make use of the fact that RAO is used as "staging"
Expand Down Expand Up @@ -172,6 +173,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);
assertThat(searchResponse.getTotalHits(), equalTo(1));
assertThat(searchResponse.getPage().get(0).getLastUpdated(), notNullValue());
System.out.println("TOTAL HITS: " + searchResponse.getTotalHits());
dumpPage(searchResponse);
}

@Test
public void gavWithTarGz() throws IOException {
// LIST GAVCEs
Expand Down