Skip to content

Commit

Permalink
[MINDEXER-199] Unify Search API backend transports (#331)
Browse files Browse the repository at this point in the history
And provide simple Java11 implementation.

---

https://issues.apache.org/jira/browse/MINDEXER-199
  • Loading branch information
cstamas authored Oct 25, 2023
1 parent 2d096ab commit 34e3b46
Show file tree
Hide file tree
Showing 14 changed files with 67 additions and 287 deletions.
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ under the License.
<maven.compiler.source>${javaVersion}</maven.compiler.source>
<maven.compiler.target>${javaVersion}</maven.compiler.target>
<minimalMavenBuildVersion>[3.8.7,)</minimalMavenBuildVersion>
<minimalJavaBuildVersion>[17.0.6,18)</minimalJavaBuildVersion>
<!-- spotless on Java21: move upper once new parent POM used -->
<minimalJavaBuildVersion>[17.0.6,21)</minimalJavaBuildVersion>

<surefire.redirectTestOutputToFile>true</surefire.redirectTestOutputToFile>
<failsafe.redirectTestOutputToFile>true</failsafe.redirectTestOutputToFile>
Expand Down
4 changes: 0 additions & 4 deletions search-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ under the License.
<name>Maven :: Search API</name>
<description>Indexer Search API.</description>

<properties>
<javaVersion>8</javaVersion>
</properties>

<dependencies>
<!-- Test -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.transport;

import java.io.IOException;
import java.io.InputStream;
Expand All @@ -29,14 +29,12 @@
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.maven.search.backend.remoterepository.RemoteRepositorySearchTransport;

import static java.util.Objects.requireNonNull;

/**
* Java 11 {@link HttpClient} backed transport.
*/
public class Java11HttpClientRemoteRepositorySearchTransport implements RemoteRepositorySearchTransport {
public class Java11HttpClientTransport implements Transport {
private static class ResponseImpl implements Response {

private final HttpResponse<?> response;
Expand Down Expand Up @@ -78,11 +76,11 @@ public void close() throws IOException {

private final HttpClient client;

public Java11HttpClientRemoteRepositorySearchTransport() {
public Java11HttpClientTransport() {
this(Duration.ofSeconds(10L));
}

public Java11HttpClientRemoteRepositorySearchTransport(Duration timeout) {
public Java11HttpClientTransport(Duration timeout) {
this(
timeout,
HttpClient.newBuilder()
Expand All @@ -91,7 +89,7 @@ public Java11HttpClientRemoteRepositorySearchTransport(Duration timeout) {
.build());
}

public Java11HttpClientRemoteRepositorySearchTransport(Duration timeout, HttpClient client) {
public Java11HttpClientTransport(Duration timeout, HttpClient client) {
this.timeout = requireNonNull(timeout);
this.client = requireNonNull(client);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,20 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.search.backend.remoterepository;
package org.apache.maven.search.transport;

import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

/**
* A trivial "transport abstraction" to make possible pluggable implementations.
* A trivial "transport abstraction" to make possible pluggable implementations. Most obviously leans toward HTTP,
* but is not limited to.
*
* @since TBD
*/
public interface RemoteRepositorySearchTransport {
public interface Transport {
/**
* Trivial response.
*/
Expand All @@ -39,12 +42,12 @@ interface Response extends Closeable {
}

/**
* This method should issue a HTTP GET requests using {@code serviceUri} and return response.
* This method should issue HTTP GET requests using {@code serviceUri} and return response.
*/
Response get(String serviceUri, Map<String, String> headers) throws IOException;

/**
* This method should issue a HTTP HEAD requests using {@code serviceUri} and return response.
* This method should issue HTTP HEAD requests using {@code serviceUri} and return response.
*/
Response head(String serviceUri, Map<String, String> headers) throws IOException;
}
4 changes: 0 additions & 4 deletions search-backend-remoterepository/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ under the License.
<name>Maven :: Search API Remote Repository Backend</name>
<description>Indexer Search Backend implemented by remote repository (limited).</description>

<properties>
<javaVersion>11</javaVersion>
</properties>

<dependencies>
<dependency>
<groupId>org.apache.maven.indexer</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@

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;
import org.apache.maven.search.transport.Java11HttpClientTransport;
import org.apache.maven.search.transport.Transport;

/**
* The remote repository search backend factory.
Expand All @@ -45,7 +46,7 @@ public static RemoteRepositorySearchBackend createDefaultMavenCentral() {
BACKEND_ID,
CENTRAL_REPOSITORY_ID,
CENTRAL_URI,
new Java11HttpClientRemoteRepositorySearchTransport(),
new Java11HttpClientTransport(),
new MavenCentralResponseExtractor());
}

Expand All @@ -57,7 +58,7 @@ public static RemoteRepositorySearchBackend createDefaultRAOReleases() {
BACKEND_ID,
RAO_RELEASES_REPOSITORY_ID,
RAO_RELEASES_URI,
new Java11HttpClientRemoteRepositorySearchTransport(),
new Java11HttpClientTransport(),
new Nx2ResponseExtractor());
}

Expand All @@ -68,7 +69,7 @@ public static RemoteRepositorySearchBackend create(
String backendId,
String repositoryId,
String baseUri,
RemoteRepositorySearchTransport transport,
Transport transport,
ResponseExtractor responseExtractor) {
return new RemoteRepositorySearchBackendImpl(backendId, repositoryId, baseUri, transport, responseExtractor);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@
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.backend.remoterepository.ResponseExtractor;
import org.apache.maven.search.support.SearchBackendSupport;
import org.apache.maven.search.transport.Transport;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.parser.Parser;
Expand All @@ -53,7 +53,7 @@
public class RemoteRepositorySearchBackendImpl extends SearchBackendSupport implements RemoteRepositorySearchBackend {
private final String baseUri;

private final RemoteRepositorySearchTransport transport;
private final Transport transport;

private final ResponseExtractor responseExtractor;

Expand All @@ -74,7 +74,7 @@ public RemoteRepositorySearchBackendImpl(
String backendId,
String repositoryId,
String baseUri,
RemoteRepositorySearchTransport transport,
Transport transport,
ResponseExtractor responseExtractor) {
super(backendId, repositoryId);
this.baseUri = requireNonNull(baseUri);
Expand Down Expand Up @@ -149,7 +149,7 @@ public RemoteRepositorySearchResponse search(SearchRequest searchRequest) throws
Document document = null;
if (state.ordinal() < State.GAVCE.ordinal()) {
Parser parser = state == State.GA ? Parser.xmlParser() : Parser.htmlParser();
try (RemoteRepositorySearchTransport.Response response = transport.get(uri, commonHeaders)) {
try (Transport.Response response = transport.get(uri, commonHeaders)) {
if (response.getCode() == 200) {
document = Jsoup.parse(response.getBody(), StandardCharsets.UTF_8.name(), uri, parser);
}
Expand All @@ -173,12 +173,11 @@ public RemoteRepositorySearchResponse search(SearchRequest searchRequest) throws
throw new IllegalStateException("State" + state); // checkstyle
}
} else {
try (RemoteRepositorySearchTransport.Response response = transport.head(uri, commonHeaders)) {
try (Transport.Response response = transport.head(uri, commonHeaders)) {
if (response.getCode() == 200) {
boolean matches = context.getSha1() == null;
if (context.getSha1() != null) {
try (RemoteRepositorySearchTransport.Response sha1Response =
transport.get(uri + ".sha1", commonHeaders)) {
try (Transport.Response sha1Response = transport.get(uri + ".sha1", commonHeaders)) {
if (response.getCode() == 200) {
try (InputStream body = sha1Response.getBody()) {
String remoteSha1 = readChecksum(body);
Expand Down Expand Up @@ -211,7 +210,7 @@ private static String readChecksum(InputStream inputStream) throws IOException {
break;
}
line = line.trim();
if (line.length() > 0) {
if (!line.isEmpty()) {
checksum = line;
break;
}
Expand Down
37 changes: 0 additions & 37 deletions search-backend-smo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ under the License.
<name>Maven :: Search API SMO Backend</name>
<description>Indexer Search Backend implemented by SMO.</description>

<properties>
<javaVersion>8</javaVersion>
</properties>

<dependencies>
<dependency>
<groupId>org.apache.maven.indexer</groupId>
Expand Down Expand Up @@ -73,39 +69,6 @@ under the License.
<directory>src/main/filtered-resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>compile-java-11</id>
<goals>
<goal>compile</goal>
</goals>
<phase>compile</phase>
<configuration>
<release>11</release>
<compileSourceRoots>
<compileSourceRoot>${project.basedir}/src/main/java11</compileSourceRoot>
</compileSourceRoots>
<multiReleaseOutput>true</multiReleaseOutput>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifestEntries>
<Multi-Release>true</Multi-Release>
</manifestEntries>
</archive>
</configuration>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
package org.apache.maven.search.backend.smo;

import org.apache.maven.search.backend.smo.internal.SmoSearchBackendImpl;
import org.apache.maven.search.backend.smo.internal.SmoSearchTransportSupplier;
import org.apache.maven.search.transport.Java11HttpClientTransport;
import org.apache.maven.search.transport.Transport;

/**
* The SMO search backend factory.
Expand All @@ -35,15 +36,13 @@ public class SmoSearchBackendFactory {
* Creates "default" SMO search backend suitable for most use cases.
*/
public SmoSearchBackend createDefault() {
return create(
DEFAULT_BACKEND_ID, DEFAULT_REPOSITORY_ID, DEFAULT_SMO_URI, new SmoSearchTransportSupplier().get());
return create(DEFAULT_BACKEND_ID, DEFAULT_REPOSITORY_ID, DEFAULT_SMO_URI, new Java11HttpClientTransport());
}

/**
* Creates SMO search backend using provided parameters.
*/
public SmoSearchBackend create(
String backendId, String repositoryId, String smoUri, SmoSearchTransport transportSupport) {
return new SmoSearchBackendImpl(backendId, repositoryId, smoUri, transportSupport);
public SmoSearchBackend create(String backendId, String repositoryId, String smoUri, Transport transport) {
return new SmoSearchBackendImpl(backendId, repositoryId, smoUri, transport);
}
}
Loading

0 comments on commit 34e3b46

Please sign in to comment.