You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by cs...@apache.org on 2023/07/24 12:51:03 UTC
[maven-indexer] branch master updated: [MINDEXER-195] RAO support for RR backend (#326)
This is an automated email from the ASF dual-hosted git repository.
cstamas pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-indexer.git
The following commit(s) were added to refs/heads/master by this push:
new 87f7f27 [MINDEXER-195] RAO support for RR backend (#326)
87f7f27 is described below
commit 87f7f27c91840f16f6f0c665f02a332c9b9bd021
Author: Tamas Cservenak <ta...@cservenak.net>
AuthorDate: Mon Jul 24 14:50:57 2023 +0200
[MINDEXER-195] RAO support for RR backend (#326)
RAO is powered by Nx2 that emits DIFFERENT structured HTML than Maven Central, hence HTML parsing is slightly different.
"exploded" bits to be able to provide different extractors and now RR supports Maven Central and Nx2 remote repositories.
---
https://issues.apache.org/jira/browse/MINDEXER-195
---
.../remoterepository/{internal => }/Context.java | 2 +-
.../backend/remoterepository/RecordFactory.java | 64 +++++++
.../RemoteRepositorySearchBackendFactory.java | 43 +++--
.../remoterepository/ResponseExtractor.java | 51 ++++++
.../extractor/MavenCentralResponseExtractor.java | 79 +++++++++
.../extractor/Nx2ResponseExtractor.java | 75 +++++++++
.../extractor/ResponseExtractorSupport.java | 112 +++++++++++++
.../RemoteRepositorySearchBackendImpl.java | 185 ++-------------------
.../RemoteRepositorySearchBackendImplTest.java | 26 ++-
9 files changed, 456 insertions(+), 181 deletions(-)
diff --git a/search-backend-remoterepository/src/main/java/org/apache/maven/search/backend/remoterepository/internal/Context.java b/search-backend-remoterepository/src/main/java/org/apache/maven/search/backend/remoterepository/Context.java
similarity index 97%
rename from search-backend-remoterepository/src/main/java/org/apache/maven/search/backend/remoterepository/internal/Context.java
rename to search-backend-remoterepository/src/main/java/org/apache/maven/search/backend/remoterepository/Context.java
index b1531f6..beedc2e 100644
--- a/search-backend-remoterepository/src/main/java/org/apache/maven/search/backend/remoterepository/internal/Context.java
+++ b/search-backend-remoterepository/src/main/java/org/apache/maven/search/backend/remoterepository/Context.java
@@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
-package org.apache.maven.search.backend.remoterepository.internal;
+package org.apache.maven.search.backend.remoterepository;
import java.util.HashMap;
import java.util.Map;
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
new file mode 100644
index 0000000..4949930
--- /dev/null
+++ b/search-backend-remoterepository/src/main/java/org/apache/maven/search/backend/remoterepository/RecordFactory.java
@@ -0,0 +1,64 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.search.backend.remoterepository;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.maven.search.MAVEN;
+import org.apache.maven.search.Record;
+import org.apache.maven.search.request.Field;
+
+import static java.util.Objects.requireNonNull;
+
+/**
+ * Helper class that creates record instances for provided backend.
+ */
+public final class RecordFactory {
+
+ private final RemoteRepositorySearchBackend backend;
+
+ public RecordFactory(RemoteRepositorySearchBackend backend) {
+ this.backend = requireNonNull(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) {
+ 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);
+ }
+
+ private static void mayPut(Map<Field, Object> result, Field fieldName, Object value) {
+ if (value == null) {
+ return;
+ }
+ if (value instanceof String && ((String) value).isBlank()) {
+ return;
+ }
+ result.put(fieldName, value);
+ }
+}
diff --git a/search-backend-remoterepository/src/main/java/org/apache/maven/search/backend/remoterepository/RemoteRepositorySearchBackendFactory.java b/search-backend-remoterepository/src/main/java/org/apache/maven/search/backend/remoterepository/RemoteRepositorySearchBackendFactory.java
index 7e8aec3..4acb628 100644
--- a/search-backend-remoterepository/src/main/java/org/apache/maven/search/backend/remoterepository/RemoteRepositorySearchBackendFactory.java
+++ b/search-backend-remoterepository/src/main/java/org/apache/maven/search/backend/remoterepository/RemoteRepositorySearchBackendFactory.java
@@ -18,6 +18,8 @@
*/
package org.apache.maven.search.backend.remoterepository;
+import org.apache.maven.search.backend.remoterepository.extractor.MavenCentralResponseExtractor;
+import org.apache.maven.search.backend.remoterepository.extractor.Nx2ResponseExtractor;
import org.apache.maven.search.backend.remoterepository.internal.Java11HttpClientRemoteRepositorySearchTransport;
import org.apache.maven.search.backend.remoterepository.internal.RemoteRepositorySearchBackendImpl;
@@ -25,28 +27,49 @@ import org.apache.maven.search.backend.remoterepository.internal.RemoteRepositor
* The remote repository search backend factory.
*/
public class RemoteRepositorySearchBackendFactory {
- public static final String DEFAULT_BACKEND_ID = "central-rr";
+ public static final String BACKEND_ID = "search-rr";
- public static final String DEFAULT_REPOSITORY_ID = "central";
+ public static final String CENTRAL_REPOSITORY_ID = "central";
- public static final String DEFAULT_URI = "https://repo.maven.apache.org/maven2/";
+ public static final String CENTRAL_URI = "https://repo.maven.apache.org/maven2/";
+
+ public static final String RAO_RELEASES_REPOSITORY_ID = "apache.releases.https";
+
+ public static final String RAO_RELEASES_URI = "https://repository.apache.org/content/repositories/releases/";
/**
* Creates "default" RR search backend against Maven Central suitable for most use cases.
*/
- public static RemoteRepositorySearchBackend createDefault() {
+ public static RemoteRepositorySearchBackend createDefaultMavenCentral() {
+ return create(
+ BACKEND_ID,
+ CENTRAL_REPOSITORY_ID,
+ CENTRAL_URI,
+ new Java11HttpClientRemoteRepositorySearchTransport(),
+ new MavenCentralResponseExtractor());
+ }
+
+ /**
+ * Creates "default" RR search backend against repository.apache.org releases repository suitable for most use cases.
+ */
+ public static RemoteRepositorySearchBackend createDefaultRAOReleases() {
return create(
- DEFAULT_BACKEND_ID,
- DEFAULT_REPOSITORY_ID,
- DEFAULT_URI,
- new Java11HttpClientRemoteRepositorySearchTransport());
+ BACKEND_ID,
+ RAO_RELEASES_REPOSITORY_ID,
+ RAO_RELEASES_URI,
+ new Java11HttpClientRemoteRepositorySearchTransport(),
+ new Nx2ResponseExtractor());
}
/**
* Creates RR search backend using provided parameters.
*/
public static RemoteRepositorySearchBackend create(
- String backendId, String repositoryId, String baseUri, RemoteRepositorySearchTransport transport) {
- return new RemoteRepositorySearchBackendImpl(backendId, repositoryId, baseUri, transport);
+ String backendId,
+ String repositoryId,
+ String baseUri,
+ RemoteRepositorySearchTransport transport,
+ ResponseExtractor responseExtractor) {
+ return new RemoteRepositorySearchBackendImpl(backendId, repositoryId, baseUri, transport, responseExtractor);
}
}
diff --git a/search-backend-remoterepository/src/main/java/org/apache/maven/search/backend/remoterepository/ResponseExtractor.java b/search-backend-remoterepository/src/main/java/org/apache/maven/search/backend/remoterepository/ResponseExtractor.java
new file mode 100644
index 0000000..21d3990
--- /dev/null
+++ b/search-backend-remoterepository/src/main/java/org/apache/maven/search/backend/remoterepository/ResponseExtractor.java
@@ -0,0 +1,51 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.search.backend.remoterepository;
+
+import java.util.List;
+
+import org.apache.maven.search.Record;
+import org.jsoup.nodes.Document;
+
+/**
+ * A component extracting data from response, that is aware of any remote specifics (like HTML structure).
+ */
+public interface ResponseExtractor {
+ /**
+ * Method parsing document out of HTML page like this one:
+ * <a href="https://repo.maven.apache.org/maven2/org/apache/maven/indexer/">https://repo.maven.apache.org/maven2/org/apache/maven/indexer/</a>
+ * <p>
+ * Note: this method is "best effort" and may enlist non-existent As (think nested Gs).
+ */
+ int populateG(Context context, Document document, RecordFactory recordFactory, List<Record> page);
+
+ /**
+ * Method parsing document out of XML Maven Metadata like this one:
+ * <a href="https://repo.maven.apache.org/maven2/org/apache/maven/indexer/search-api/maven-metadata.xml">https://repo.maven.apache.org/maven2/org/apache/maven/indexer/search-api/maven-metadata.xml</a>
+ */
+ int populateGA(Context context, Document document, RecordFactory recordFactory, List<Record> page);
+
+ /**
+ * Method parsing document out of HTML page like this one:
+ * <a href="https://repo.maven.apache.org/maven2/org/apache/maven/indexer/search-api/7.0.3/">https://repo.maven.apache.org/maven2/org/apache/maven/indexer/search-api/7.0.3/</a>
+ * <p>
+ * Note: this method is "best effort" and may enlist fake artifacts.
+ */
+ int populateGAV(Context context, Document document, RecordFactory recordFactory, List<Record> page);
+}
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
new file mode 100644
index 0000000..cc481ad
--- /dev/null
+++ b/search-backend-remoterepository/src/main/java/org/apache/maven/search/backend/remoterepository/extractor/MavenCentralResponseExtractor.java
@@ -0,0 +1,79 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.search.backend.remoterepository.extractor;
+
+import java.util.List;
+
+import org.apache.maven.search.Record;
+import org.apache.maven.search.backend.remoterepository.Context;
+import org.apache.maven.search.backend.remoterepository.RecordFactory;
+import org.jsoup.nodes.Document;
+import org.jsoup.nodes.Element;
+
+/**
+ * Extractor for Maven Central.
+ */
+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
+ * a directory. The trailing slash is removed by this method, if any.
+ */
+ private String nameInHref(Element element) {
+ String name = element.attr("href");
+ if (name.endsWith("/")) {
+ name = name.substring(0, name.length() - 1);
+ }
+ return name;
+ }
+
+ @Override
+ public int populateG(Context context, Document document, RecordFactory recordFactory, List<Record> page) {
+ // Index HTML page like this one:
+ // https://repo.maven.apache.org/maven2/org/apache/maven/indexer/
+ Element contents = document.getElementById("contents");
+ if (contents != null) {
+ for (Element element : contents.getElementsByTag("a")) {
+ String name = nameInHref(element);
+ if (accept(name)) {
+ page.add(recordFactory.create(context.getGroupId(), name, null, null, null));
+ }
+ }
+ }
+ return page.size();
+ }
+
+ @Override
+ public int populateGAV(Context context, Document document, RecordFactory recordFactory, List<Record> page) {
+ // Index HTML page like this one:
+ // https://repo.maven.apache.org/maven2/org/apache/maven/indexer/search-api/7.0.3/
+ Element contents = document.getElementById("contents");
+ if (contents != null) {
+ for (Element element : contents.getElementsByTag("a")) {
+ // skip possible subdirectories and files without extensions
+ String name = element.attr("href");
+ if (name.endsWith("/") || !name.contains(".")) {
+ continue;
+ }
+ populateGAVName(context, nameInHref(element), recordFactory, page);
+ }
+ }
+ return page.size();
+ }
+}
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
new file mode 100644
index 0000000..1f89989
--- /dev/null
+++ b/search-backend-remoterepository/src/main/java/org/apache/maven/search/backend/remoterepository/extractor/Nx2ResponseExtractor.java
@@ -0,0 +1,75 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.search.backend.remoterepository.extractor;
+
+import java.util.List;
+
+import org.apache.maven.search.Record;
+import org.apache.maven.search.backend.remoterepository.Context;
+import org.apache.maven.search.backend.remoterepository.RecordFactory;
+import org.jsoup.nodes.Document;
+import org.jsoup.nodes.Element;
+import org.jsoup.select.Elements;
+
+/**
+ * Extractor for Sonatype Nexus2.
+ */
+public class Nx2ResponseExtractor extends ResponseExtractorSupport {
+ protected boolean accept(String name) {
+ return !"Parent Directory".equals(name) && super.accept(name);
+ }
+
+ private String name(Element element) {
+ String name = element.text();
+ if (name.endsWith("/")) {
+ name = name.substring(0, name.length() - 1);
+ }
+ return name;
+ }
+
+ @Override
+ public int populateG(Context context, Document document, RecordFactory recordFactory, List<Record> page) {
+ // Index HTML page like this one:
+ // https://repo.maven.apache.org/maven2/org/apache/maven/indexer/
+ Elements elements = document.getElementsByTag("a");
+ for (Element element : elements) {
+ String name = name(element);
+ if (accept(name)) {
+ page.add(recordFactory.create(context.getGroupId(), name, null, null, null));
+ }
+ }
+ return page.size();
+ }
+
+ @Override
+ public int populateGAV(Context context, Document document, RecordFactory recordFactory, List<Record> page) {
+ // Index HTML page like this one:
+ // https://repo.maven.apache.org/maven2/org/apache/maven/indexer/search-api/7.0.3/
+ Elements elements = document.getElementsByTag("a");
+ for (Element element : elements) {
+ // skip possible subdirectories and files without extensions
+ String name = element.attr("href");
+ if (name.endsWith("/") || !name.contains(".")) {
+ continue;
+ }
+ populateGAVName(context, name(element), recordFactory, page);
+ }
+ 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
new file mode 100644
index 0000000..ef5f5fd
--- /dev/null
+++ b/search-backend-remoterepository/src/main/java/org/apache/maven/search/backend/remoterepository/extractor/ResponseExtractorSupport.java
@@ -0,0 +1,112 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.search.backend.remoterepository.extractor;
+
+import java.util.List;
+
+import org.apache.maven.search.Record;
+import org.apache.maven.search.backend.remoterepository.Context;
+import org.apache.maven.search.backend.remoterepository.RecordFactory;
+import org.apache.maven.search.backend.remoterepository.ResponseExtractor;
+import org.jsoup.nodes.Document;
+import org.jsoup.nodes.Element;
+
+/**
+ * A support class for extractors.
+ */
+public abstract class ResponseExtractorSupport implements ResponseExtractor {
+ protected boolean isChecksum(String name) {
+ return name.endsWith(".sha1") || name.endsWith(".md5") || name.endsWith(".sha256") || name.endsWith(".sha512");
+ }
+
+ protected boolean isSignature(String name) {
+ return name.endsWith(".asc") || name.endsWith(".sigstore");
+ }
+
+ protected boolean isMetadata(String name) {
+ return name.equals("maven-metadata.xml");
+ }
+
+ /**
+ * Returns {@code true} if the name is not empty, not directory special (".."), is not metadata
+ * is not signature and is not checksum. Hence, it should be a name of interest.
+ */
+ protected boolean accept(String name) {
+ return name != null
+ && !name.isEmpty()
+ && !name.contains("..")
+ && !isMetadata(name)
+ && !isSignature(name)
+ && !isChecksum(name);
+ }
+
+ /**
+ * This is Maven metadata parsing, is NOT remote end specific, unlike HTML parsing.
+ */
+ @Override
+ public int populateGA(Context context, Document document, RecordFactory recordFactory, List<Record> page) {
+ // Maven Metadata XML like this one:
+ // https://repo.maven.apache.org/maven2/org/apache/maven/indexer/search-api/maven-metadata.xml
+ Element metadata = document.getElementsByTag("metadata").first();
+ if (metadata != null) {
+ Element versioning = metadata.getElementsByTag("versioning").first();
+ if (versioning != null) {
+ Element versions = versioning.getElementsByTag("versions").first();
+ if (versions != null) {
+ for (Element version : versions.getElementsByTag("version")) {
+ page.add(recordFactory.create(
+ context.getGroupId(), context.getArtifactId(), version.text(), null, null));
+ }
+ }
+ }
+ }
+ return page.size();
+ }
+
+ /**
+ * Processes extracted "name" extracted by {@link #populateGAV(Context, Document, RecordFactory, List)} method.
+ */
+ protected void populateGAVName(Context context, String name, RecordFactory recordFactory, List<Record> page) {
+ if (accept(name)) {
+ if (name.startsWith(context.getArtifactId())) {
+ name = name.substring(context.getArtifactId().length() + 1);
+ if (name.startsWith(context.getVersion())) {
+ name = name.substring(context.getVersion().length() + 1);
+ String ext = null;
+ String classifier = null;
+ if (name.contains(".")) {
+ while (name.contains(".")) {
+ if (ext == null) {
+ ext = name.substring(name.lastIndexOf('.') + 1);
+ } else {
+ ext = name.substring(name.lastIndexOf('.') + 1) + "." + ext;
+ }
+ name = name.substring(0, name.lastIndexOf('.'));
+ }
+ classifier = name.isEmpty() ? null : name;
+ } else {
+ ext = name;
+ }
+ page.add(recordFactory.create(
+ context.getGroupId(), context.getArtifactId(), context.getVersion(), classifier, ext));
+ }
+ }
+ }
+ }
+}
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 15e4a28..aeba160 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
@@ -24,23 +24,22 @@ import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
-import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
-import org.apache.maven.search.MAVEN;
import org.apache.maven.search.Record;
import org.apache.maven.search.SearchRequest;
+import org.apache.maven.search.backend.remoterepository.Context;
+import org.apache.maven.search.backend.remoterepository.RecordFactory;
import org.apache.maven.search.backend.remoterepository.RemoteRepositorySearchBackend;
import org.apache.maven.search.backend.remoterepository.RemoteRepositorySearchResponse;
import org.apache.maven.search.backend.remoterepository.RemoteRepositorySearchTransport;
-import org.apache.maven.search.request.Field;
+import org.apache.maven.search.backend.remoterepository.ResponseExtractor;
import org.apache.maven.search.support.SearchBackendSupport;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
-import org.jsoup.nodes.Element;
import org.jsoup.parser.Parser;
import static java.util.Objects.requireNonNull;
@@ -56,6 +55,8 @@ public class RemoteRepositorySearchBackendImpl extends SearchBackendSupport impl
private final RemoteRepositorySearchTransport transport;
+ private final ResponseExtractor responseExtractor;
+
private final Map<String, String> commonHeaders;
protected enum State {
@@ -70,10 +71,15 @@ public class RemoteRepositorySearchBackendImpl extends SearchBackendSupport impl
* Creates a customized instance of SMO backend, like an in-house instances of SMO or different IDs.
*/
public RemoteRepositorySearchBackendImpl(
- String backendId, String repositoryId, String baseUri, RemoteRepositorySearchTransport transport) {
+ String backendId,
+ String repositoryId,
+ String baseUri,
+ RemoteRepositorySearchTransport transport,
+ ResponseExtractor responseExtractor) {
super(backendId, repositoryId);
this.baseUri = requireNonNull(baseUri);
this.transport = requireNonNull(transport);
+ this.responseExtractor = requireNonNull(responseExtractor);
this.commonHeaders = Map.of(
"User-Agent",
@@ -139,6 +145,7 @@ public class RemoteRepositorySearchBackendImpl extends SearchBackendSupport impl
int totalHits = 0;
List<Record> page = new ArrayList<>(searchRequest.getPaging().getPageSize());
+ RecordFactory recordFactory = new RecordFactory(this);
Document document = null;
if (state.ordinal() < State.GAVCE.ordinal()) {
Parser parser = state == State.GA ? Parser.xmlParser() : Parser.htmlParser();
@@ -154,13 +161,13 @@ public class RemoteRepositorySearchBackendImpl extends SearchBackendSupport impl
switch (state) {
case G:
- totalHits = populateG(context, document, page);
+ totalHits = responseExtractor.populateG(context, document, recordFactory, page);
break;
case GA:
- totalHits = populateGA(context, document, page);
+ totalHits = responseExtractor.populateGA(context, document, recordFactory, page);
break;
case GAV:
- totalHits = populateGAV(context, document, page);
+ totalHits = responseExtractor.populateGAV(context, document, recordFactory, page);
break;
default:
throw new IllegalStateException("State" + state); // checkstyle
@@ -181,7 +188,7 @@ public class RemoteRepositorySearchBackendImpl extends SearchBackendSupport impl
}
}
if (matches) {
- page.add(create(
+ page.add(recordFactory.create(
context.getGroupId(),
context.getArtifactId(),
context.getVersion(),
@@ -195,156 +202,6 @@ public class RemoteRepositorySearchBackendImpl extends SearchBackendSupport impl
return new RemoteRepositorySearchResponseImpl(searchRequest, totalHits, page, uri, document);
}
- protected boolean isChecksum(String name) {
- return name.endsWith(".sha1") || name.endsWith(".md5") || name.endsWith(".sha256") || name.endsWith(".sha512");
- }
-
- protected boolean isSignature(String name) {
- return name.endsWith(".asc") || name.endsWith(".sigstore");
- }
-
- protected boolean isMetadata(String name) {
- return name.equals("maven-metadata.xml");
- }
-
- /**
- * Returns {@code true} if the name is not empty, not directory special (".."), is not metadata
- * is not signature and is not checksum. Hence, it should be a name of interest.
- */
- protected boolean accept(String name) {
- return !name.isEmpty() && !name.contains("..") && !isMetadata(name) && !isSignature(name) && !isChecksum(name);
- }
-
- /**
- * Extracts the "name" from {@code href} attribute. In case of Maven Central, the href
- * attribute contains name in form of {@code "name/"} (followed by slash), if name denotes
- * a directory. The trailing slash is removed by this method, if any.
- * <p>
- * Override this method if needed (parsing different HTML output than Maven Central).
- */
- protected String nameInHref(Element element) {
- String name = element.attr("href");
- if (name.endsWith("/")) {
- name = name.substring(0, name.length() - 1);
- }
- return name;
- }
-
- /**
- * Method parsing document out of HTML page like this one:
- * <a href="https://repo.maven.apache.org/maven2/org/apache/maven/indexer/">https://repo.maven.apache.org/maven2/org/apache/maven/indexer/</a>
- * <p>
- * Note: this method is "best effort" and may enlist non-existent As (think nested Gs).
- * <p>
- * Override this method if needed (parsing different HTML output than Maven Central).
- */
- protected int populateG(Context context, Document document, List<Record> page) {
- // Index HTML page like this one:
- // https://repo.maven.apache.org/maven2/org/apache/maven/indexer/
- Element contents = document.getElementById("contents");
- if (contents != null) {
- for (Element element : contents.getElementsByTag("a")) {
- String name = nameInHref(element);
- if (accept(name)) {
- page.add(create(context.getGroupId(), name, null, null, null));
- }
- }
- }
- return page.size();
- }
-
- /**
- * Method parsing document out of XML Maven Metadata like this one:
- * <a href="https://repo.maven.apache.org/maven2/org/apache/maven/indexer/search-api/maven-metadata.xml">https://repo.maven.apache.org/maven2/org/apache/maven/indexer/search-api/maven-metadata.xml</a>
- */
- protected int populateGA(Context context, Document document, List<Record> page) {
- // Maven Metadata XML like this one:
- // https://repo.maven.apache.org/maven2/org/apache/maven/indexer/search-api/maven-metadata.xml
- Element metadata = document.getElementsByTag("metadata").first();
- if (metadata != null) {
- Element versioning = metadata.getElementsByTag("versioning").first();
- if (versioning != null) {
- Element versions = versioning.getElementsByTag("versions").first();
- if (versions != null) {
- for (Element version : versions.getElementsByTag("version")) {
- page.add(create(context.getGroupId(), context.getArtifactId(), version.text(), null, null));
- }
- }
- }
- }
- return page.size();
- }
-
- /**
- * Method parsing document out of HTML page like this one:
- * <a href="https://repo.maven.apache.org/maven2/org/apache/maven/indexer/search-api/7.0.3/">https://repo.maven.apache.org/maven2/org/apache/maven/indexer/search-api/7.0.3/</a>
- * <p>
- * Note: this method is "best effort" and may enlist fake artifacts.
- * <p>
- * Override this method if needed (parsing different HTML output than Maven Central).
- */
- protected int populateGAV(Context context, Document document, List<Record> page) {
- // Index HTML page like this one:
- // https://repo.maven.apache.org/maven2/org/apache/maven/indexer/search-api/7.0.3/
- Element contents = document.getElementById("contents");
- if (contents != null) {
- for (Element element : contents.getElementsByTag("a")) {
- // skip possible subdirectories and files without extensions
- String name = element.attr("href");
- if (name.endsWith("/") || !name.contains(".")) {
- continue;
- }
- name = nameInHref(element);
- if (accept(name)) {
- if (name.startsWith(context.getArtifactId())) {
- name = name.substring(context.getArtifactId().length() + 1);
- if (name.startsWith(context.getVersion())) {
- name = name.substring(context.getVersion().length() + 1);
- String ext = null;
- String classifier = null;
- if (name.contains(".")) {
- while (name.contains(".")) {
- if (ext == null) {
- ext = name.substring(name.lastIndexOf('.') + 1);
- } else {
- ext = name.substring(name.lastIndexOf('.') + 1) + "." + ext;
- }
- name = name.substring(0, name.lastIndexOf('.'));
- }
- classifier = name.isEmpty() ? null : name;
- } else {
- ext = name;
- }
- page.add(create(
- context.getGroupId(),
- context.getArtifactId(),
- context.getVersion(),
- classifier,
- ext));
- }
- }
- }
- }
- }
- return page.size();
- }
-
- /**
- * Creates a {@link Record} instance using passed in field values. All field values except
- * {@code groupId} are optional (nullable).
- */
- protected Record create(
- String groupId, String artifactId, String version, String classifier, String fileExtension) {
- 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(getBackendId(), getRepositoryId(), null, null, result);
- }
-
private static String readChecksum(InputStream inputStream) throws IOException {
String checksum = "";
try (BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8), 512)) {
@@ -374,14 +231,4 @@ public class RemoteRepositorySearchBackendImpl extends SearchBackendSupport impl
return checksum;
}
-
- private static void mayPut(Map<Field, Object> result, Field fieldName, Object value) {
- if (value == null) {
- return;
- }
- if (value instanceof String && ((String) value).isBlank()) {
- return;
- }
- result.put(fieldName, value);
- }
}
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 a84920b..8e19c33 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
@@ -21,6 +21,8 @@ package org.apache.maven.search.backend.remoterepository.internal;
import java.io.IOException;
import java.time.Instant;
import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
@@ -34,12 +36,32 @@ import org.apache.maven.search.request.BooleanQuery;
import org.apache.maven.search.request.FieldQuery;
import org.apache.maven.search.request.Query;
import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.greaterThan;
+/**
+ * UT for 2 backends: Maven Central and RAO releases. This tests make use of the fact that RAO is used as "staging"
+ * area for Maven Central, hence RAO releases contains everything that was staged and synced to Maven Central.
+ */
+@RunWith(Parameterized.class)
public class RemoteRepositorySearchBackendImplTest {
- private RemoteRepositorySearchBackend backend = RemoteRepositorySearchBackendFactory.createDefault();
+
+ @Parameterized.Parameters
+ public static Collection<Object> data() {
+ return Arrays.asList(
+ RemoteRepositorySearchBackendFactory.createDefaultMavenCentral(),
+ RemoteRepositorySearchBackendFactory.createDefaultRAOReleases());
+ }
+
+ private final RemoteRepositorySearchBackend backend;
+
+ public RemoteRepositorySearchBackendImplTest(RemoteRepositorySearchBackend backend) {
+ this.backend = backend;
+ }
private void dumpSingle(AtomicInteger counter, List<Record> page) {
for (Record record : page) {
@@ -110,6 +132,7 @@ public class RemoteRepositorySearchBackendImplTest {
SearchRequest searchRequest =
new SearchRequest(FieldQuery.fieldQuery(MAVEN.GROUP_ID, "org.apache.maven.plugins"));
RemoteRepositorySearchResponse searchResponse = backend.search(searchRequest);
+ assertThat(searchResponse.getTotalHits(), greaterThan(0));
System.out.println("TOTAL HITS: " + searchResponse.getTotalHits());
dumpPage(searchResponse);
}
@@ -121,6 +144,7 @@ public class RemoteRepositorySearchBackendImplTest {
FieldQuery.fieldQuery(MAVEN.GROUP_ID, "org.apache.maven.plugins"),
FieldQuery.fieldQuery(MAVEN.ARTIFACT_ID, "maven-clean-plugin")));
RemoteRepositorySearchResponse searchResponse = backend.search(searchRequest);
+ assertThat(searchResponse.getTotalHits(), greaterThan(0));
System.out.println("TOTAL HITS: " + searchResponse.getTotalHits());
dumpPage(searchResponse);
}