-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[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
- Loading branch information
Showing
9 changed files
with
456 additions
and
181 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
...ository/src/main/java/org/apache/maven/search/backend/remoterepository/RecordFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
...ory/src/main/java/org/apache/maven/search/backend/remoterepository/ResponseExtractor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
79 changes: 79 additions & 0 deletions
79
...apache/maven/search/backend/remoterepository/extractor/MavenCentralResponseExtractor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
...java/org/apache/maven/search/backend/remoterepository/extractor/Nx2ResponseExtractor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
Oops, something went wrong.