Skip to content

Commit

Permalink
Add RI networking
Browse files Browse the repository at this point in the history
  • Loading branch information
charphi committed Jul 25, 2023
1 parent 63c4d6d commit 2c5bca5
Show file tree
Hide file tree
Showing 35 changed files with 703 additions and 213 deletions.
18 changes: 9 additions & 9 deletions docs/content/cli/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ The format is [RFC4180](https://tools.ietf.org/html/rfc4180).

Network-related options used to deal with performance, proxies, security and authentication.

| Name | Shortcut | Parameter | Description |
|----------------------------------------------------------------------|----------|----------------------------------------|-----------------------------------------------------------------|
| <a id="curl" href="#curl">`--curl`</a> | - | - | Use curl backend instead of JDK. |
| <a id="no-cache" href="#no-cache">`--no-cache`</a> | - | - | Disable caching. |
| <a id="auto-proxy" href="#auto-proxy">`--auto-proxy`</a> | - | - | Enable automatic proxy detection. |
| <a id="no-default-ssl" href="#no-default-ssl">`--no-default-ssl`</a> | - | - | Disable default truststore. |
| <a id="no-system-ssl" href="#no-system-ssl">`--no-system-ssl`</a> | - | - | Disable system truststore. |
| <a id="no-system-auth" href="#no-system-auth">`--no-system-auth`</a> | - | - | Disable system authentication. |
| <a id="user" href="#user">`--user`</a> | - | [`<user:password>`](../datatypes#user) | Specify the user and password to use for server authentication. |
| Name | Shortcut | Parameter | Description |
|------------------------------------------------------------------------|----------|----------------------------------------|-----------------------------------------------------------------|
| <a id="auto-proxy" href="#auto-proxy">`--[no-]auto-proxy`</a> | - | - | Enable automatic proxy detection. |
| <a id="curl" href="#curl">`--[no-]curl`</a> | - | - | Use curl backend instead of JDK. |
| <a id="no-default-ssl" href="#no-default-ssl">`--[no-]default-ssl`</a> | - | - | Disable default truststore. |
| <a id="no-system-ssl" href="#no-system-ssl">`--[no-]system-ssl`</a> | - | - | Disable system truststore. |
| <a id="no-cache" href="#no-cache">`--no-cache`</a> | - | - | Disable caching. |
| <a id="no-system-auth" href="#no-system-auth">`--system-auth`</a> | - | - | Disable system authentication. |
| <a id="user" href="#user">`--user`</a> | - | [`<user:password>`](../datatypes#user) | Specify the user and password to use for server authentication. |
4 changes: 4 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,10 @@
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<useModulePath>false</useModulePath>
<argLine>
--add-opens java.base/java.util=ALL-UNNAMED
--add-opens java.base/java.lang=ALL-UNNAMED
</argLine>
</configuration>
</plugin>
</plugins>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package internal.sdmxdl.web.spi;

import lombok.NonNull;
import sdmxdl.web.spi.Network;
import org.checkerframework.checker.nullness.qual.Nullable;
import sdmxdl.ErrorListener;
import sdmxdl.EventListener;
import sdmxdl.web.SdmxWebSource;
import sdmxdl.web.spi.Network;
import sdmxdl.web.spi.Networking;

import java.util.Collection;
Expand Down Expand Up @@ -33,7 +36,10 @@ public boolean isNetworkingAvailable() {
}

@Override
public @NonNull Network getNetwork(@NonNull SdmxWebSource source) {
public @NonNull Network getNetwork(
@NonNull SdmxWebSource source,
@Nullable EventListener<? super SdmxWebSource> onEvent,
@Nullable ErrorListener<? super SdmxWebSource> onError) {
return Network.getDefault();
}
}
10 changes: 9 additions & 1 deletion sdmx-dl-api/src/main/java/sdmxdl/web/spi/Networking.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import nbbrd.design.StaticFactoryMethod;
import nbbrd.design.ThreadSafe;
import nbbrd.service.*;
import org.checkerframework.checker.nullness.qual.Nullable;
import sdmxdl.ErrorListener;
import sdmxdl.EventListener;
import sdmxdl.web.SdmxWebSource;

import java.util.Collection;
Expand All @@ -28,10 +31,15 @@ public interface Networking {

@NonNull Collection<String> getNetworkingProperties();

@NonNull Network getNetwork(@NonNull SdmxWebSource source);
@NonNull Network getNetwork(
@NonNull SdmxWebSource source,
@Nullable EventListener<? super SdmxWebSource> onEvent,
@Nullable ErrorListener<? super SdmxWebSource> onError);

int UNKNOWN_NETWORKING_RANK = -1;

String NETWORKING_PROPERTY_PREFIX = "sdmxdl.networking";

@StaticFactoryMethod
static @NonNull Networking getDefault() {
return DefaultNetworking.INSTANCE;
Expand Down
2 changes: 1 addition & 1 deletion sdmx-dl-api/src/main/java/sdmxdl/web/spi/WebContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,6 @@ public class WebContext {
}

public @NonNull Network getNetwork(@NonNull SdmxWebSource source) {
return networking.getNetwork(source);
return networking.getNetwork(source, onEvent, onError);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package internal.sdmxdl.web;

import org.junit.jupiter.api.Test;
import tests.sdmxdl.web.spi.NetworkAssert;

public class DefaultNetworkTest {

@Test
public void testCompliance() {
NetworkAssert.assertCompliance(DefaultNetwork.INSTANCE);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package internal.sdmxdl.web.spi;

import org.junit.jupiter.api.Test;
import tests.sdmxdl.web.spi.NetworkingAssert;

public class DefaultNetworkingTest {

@Test
public void testCompliance() {
NetworkingAssert.assertCompliance(DefaultNetworking.INSTANCE);
}
}
10 changes: 10 additions & 0 deletions sdmx-dl-api/src/test/java/tests/sdmxdl/api/TckUtil.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
package tests.sdmxdl.api;

import lombok.NonNull;
import org.assertj.core.api.Condition;
import org.assertj.core.api.SoftAssertions;
import org.assertj.core.description.Description;
import org.assertj.core.description.TextDescription;

import java.util.regex.Pattern;

@lombok.experimental.UtilityClass
public class TckUtil {

public static @NonNull Condition<String> startingWith(@NonNull String prefix) {
return new Condition<>(o -> o.startsWith(prefix), "start with " + prefix);
}

@FunctionalInterface
public interface TckTests {
void run(SoftAssertions s) throws Exception;
Expand All @@ -25,4 +33,6 @@ public static void run(TckTests tests) {
public static Description nullDescriptionOf(String method, String parameter) {
return new TextDescription("Expecting '%s' to raise NPE when called with null %s", method, parameter);
}

public static final Pattern SCREAMING_SNAKE_CASE = Pattern.compile("^[A-Z0-9]+(?:_[A-Z0-9]+)*$");
}
22 changes: 22 additions & 0 deletions sdmx-dl-api/src/test/java/tests/sdmxdl/web/spi/NetworkAssert.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package tests.sdmxdl.web.spi;

import lombok.NonNull;
import sdmxdl.web.spi.Network;

import static org.assertj.core.api.Assertions.assertThat;

@lombok.experimental.UtilityClass
public class NetworkAssert {

public static void assertCompliance(@NonNull Network x) {
assertThat(x.getProxySelector())
.isNotNull();

assertThat(x.getSSLFactory())
.isNotNull()
.satisfies(SSLFactoryAssert::assertCompliance);

assertThat(x.getURLConnectionFactory())
.isNotNull();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package tests.sdmxdl.web.spi;

import lombok.NonNull;
import sdmxdl.web.SdmxWebSource;
import sdmxdl.web.spi.Networking;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatNullPointerException;
import static sdmxdl.web.spi.Networking.NETWORKING_PROPERTY_PREFIX;
import static tests.sdmxdl.api.TckUtil.SCREAMING_SNAKE_CASE;
import static tests.sdmxdl.api.TckUtil.startingWith;

@lombok.experimental.UtilityClass
public class NetworkingAssert {

@SuppressWarnings("DataFlowIssue")
public static void assertCompliance(@NonNull Networking x) {
assertThat(x.getNetworkingId())
.containsPattern(SCREAMING_SNAKE_CASE);

assertThat(x.getNetworkingProperties())
.are(startingWith(NETWORKING_PROPERTY_PREFIX))
.doesNotHaveDuplicates();

assertThatNullPointerException()
.isThrownBy(() -> x.getNetwork(null, null, null));

SdmxWebSource validSource = SdmxWebSource
.builder()
.id("valid")
.driver("SDMX21")
.endpointOf("http://localhost")
.build();

assertThat(x.getNetwork(validSource, null, null))
.isNotNull()
.satisfies(NetworkAssert::assertCompliance);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package tests.sdmxdl.web.spi;

import lombok.NonNull;
import sdmxdl.web.spi.SSLFactory;

import static org.assertj.core.api.Assertions.assertThat;

@lombok.experimental.UtilityClass
public class SSLFactoryAssert {

public static void assertCompliance(@NonNull SSLFactory x) {
assertThat(x.getSSLSocketFactory())
.isNotNull();

assertThat(x.getHostnameVerifier())
.isNotNull();
}
}
31 changes: 5 additions & 26 deletions sdmx-dl-cli/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@
<artifactId>sdmx-dl-provider-base</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>sdmx-dl-provider-ri</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>sdmx-dl-format-xml</artifactId>
Expand Down Expand Up @@ -90,39 +95,13 @@
<artifactId>picocli-jansi-graalvm</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>com.github.nbbrd.java-net-proxy</groupId>
<artifactId>java-net-proxy</artifactId>
<version>1.0.1</version>
</dependency>
<dependency>
<groupId>io.github.hakky54</groupId>
<artifactId>sslcontext-kickstart</artifactId>
<version>8.1.2</version>
<exclusions>
<exclusion>
<artifactId>slf4j-api</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.github.nbbrd.java-console-properties</groupId>
<artifactId>java-console-properties</artifactId>
<version>1.4.0</version>
</dependency>
<dependency>
<groupId>com.github.nbbrd.java-io-util</groupId>
<artifactId>java-io-curl</artifactId>
</dependency>

<!-- runtime only -->
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>sdmx-dl-provider-ri</artifactId>
<version>${project.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
Expand Down
46 changes: 27 additions & 19 deletions sdmx-dl-cli/src/main/java/internal/sdmxdl/cli/NetworkOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,51 @@

import internal.sdmxdl.cli.ext.AuthOptions;
import internal.sdmxdl.cli.ext.CacheOptions;
import internal.sdmxdl.cli.ext.ProxyOptions;
import internal.sdmxdl.cli.ext.SslOptions;
import nbbrd.io.curl.CurlHttpURLConnection;
import picocli.CommandLine;
import sdmxdl.web.spi.URLConnectionFactory;

@lombok.Getter
@lombok.Setter
public class NetworkOptions {

@CommandLine.Option(
names = "--dummy-network-option",
hidden = true,
defaultValue = "false"
names = {"--auto-proxy"},
negatable = true,
defaultValue = "${env:SDMXDL_NETWORKING_AUTOPROXY:-false}",
fallbackValue = "true",
descriptionKey = "cli.autoProxy"
)
private boolean dummyNetworkOption;
private boolean autoProxy;

@CommandLine.Option(
names = "--curl",
defaultValue = "false",
negatable = true,
defaultValue = "${env:SDMXDL_NETWORKING_CURLBACKEND:-false}",
fallbackValue = "true",
descriptionKey = "cli.sdmx.curl"
)
private boolean curl;
private boolean curlBackend;

@CommandLine.ArgGroup(validate = false)
private CacheOptions cacheOptions = new CacheOptions();
@CommandLine.Option(
names = {"--no-default-ssl"},
negatable = true,
defaultValue = "${env:SDMXDL_NETWORKING_NODEFAULTSSL:-false}",
fallbackValue = "true",
descriptionKey = "cli.noDefaultSsl"
)
boolean noDefaultSsl;

@CommandLine.ArgGroup(validate = false)
private ProxyOptions proxyOptions = new ProxyOptions();
@CommandLine.Option(
names = {"--no-system-ssl"},
negatable = true,
defaultValue = "${env:SDMXDL_NETWORKING_NOSYSTEMSSL:-false}",
fallbackValue = "true",
descriptionKey = "cli.noSystemSsl"
)
boolean noSystemSsl;

@CommandLine.ArgGroup(validate = false)
private SslOptions sslOptions = new SslOptions();
private CacheOptions cacheOptions = new CacheOptions();

@CommandLine.ArgGroup(validate = false)
private AuthOptions authOptions = new AuthOptions();

public URLConnectionFactory getURLConnectionFactory() {
return isCurl() ? CurlHttpURLConnection::of : URLConnectionFactory.getDefault();
}
}
Loading

0 comments on commit 2c5bca5

Please sign in to comment.