Skip to content

Commit

Permalink
Refactor network API
Browse files Browse the repository at this point in the history
  • Loading branch information
charphi committed Jul 6, 2023
1 parent 7545650 commit 1ae986f
Show file tree
Hide file tree
Showing 25 changed files with 350 additions and 205 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
### Changed

- ![API] Refactor cache API [#500](https://github.com/nbbrd/sdmx-dl/issues/500)
- ![API] Refactor network API [#503](https://github.com/nbbrd/sdmx-dl/issues/503)
- ![SOURCE] Update ECB endpoint [#495](https://github.com/nbbrd/sdmx-dl/issues/495)
- ![FORMAT] Refactor file format API [#502](https://github.com/nbbrd/sdmx-dl/issues/502)
- ![PROVIDER] Move curl backend to an external project
Expand Down
13 changes: 3 additions & 10 deletions sdmx-dl-api/src/main/java/internal/sdmxdl/web/DefaultNetwork.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

import lombok.NonNull;
import sdmxdl.web.Network;
import sdmxdl.web.SSLFactory;
import sdmxdl.web.URLConnectionFactory;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSocketFactory;
import java.net.ProxySelector;

public enum DefaultNetwork implements Network {
Expand All @@ -19,13 +17,8 @@ public enum DefaultNetwork implements Network {
}

@Override
public @NonNull SSLSocketFactory getSSLSocketFactory() {
return HttpsURLConnection.getDefaultSSLSocketFactory();
}

@Override
public @NonNull HostnameVerifier getHostnameVerifier() {
return HttpsURLConnection.getDefaultHostnameVerifier();
public @NonNull SSLFactory getSSLFactory() {
return SSLFactory.getDefault();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package internal.sdmxdl.web;

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

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSocketFactory;

public enum DefaultSSLFactory implements SSLFactory {

INSTANCE;

@Override
public @NonNull SSLSocketFactory getSSLSocketFactory() {
return HttpsURLConnection.getDefaultSSLSocketFactory();
}

@Override
public @NonNull HostnameVerifier getHostnameVerifier() {
return HttpsURLConnection.getDefaultHostnameVerifier();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package internal.sdmxdl.web.spi;

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

import java.util.Collection;
import java.util.Collections;

public enum DefaultNetworking implements Networking {

INSTANCE;

@Override
public @NonNull String getNetworkingId() {
return "DEFAULT";
}

@Override
public int getNetworkingRank() {
return UNKNOWN_NETWORKING_RANK;
}

@Override
public boolean isNetworkingAvailable() {
return true;
}

@Override
public @NonNull Collection<String> getNetworkingProperties() {
return Collections.emptyList();
}

@Override
public @NonNull Network getNetwork(@NonNull SdmxWebSource source) {
return Network.getDefault();
}
}
1 change: 1 addition & 0 deletions sdmx-dl-api/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
uses sdmxdl.ext.spi.CacheProvider;
uses sdmxdl.ext.spi.Dialect;
uses sdmxdl.file.spi.FileReader;
uses sdmxdl.web.spi.Networking;
uses sdmxdl.web.spi.WebAuthenticator;
uses sdmxdl.web.spi.WebDriver;
uses sdmxdl.web.spi.WebMonitoring;
Expand Down
2 changes: 2 additions & 0 deletions sdmx-dl-api/src/main/java/sdmxdl/ext/spi/CacheProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import internal.sdmxdl.NoOpCacheProvider;
import lombok.NonNull;
import nbbrd.design.StaticFactoryMethod;
import nbbrd.service.Quantifier;
import nbbrd.service.ServiceDefinition;
import nbbrd.service.ServiceId;
Expand Down Expand Up @@ -36,6 +37,7 @@ public interface CacheProvider {

int UNKNOWN_RANK = -1;

@StaticFactoryMethod
static @NonNull CacheProvider noOp() {
return NoOpCacheProvider.INSTANCE;
}
Expand Down
10 changes: 5 additions & 5 deletions sdmx-dl-api/src/main/java/sdmxdl/web/Network.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@

import internal.sdmxdl.web.DefaultNetwork;
import lombok.NonNull;
import nbbrd.design.NotThreadSafe;
import nbbrd.design.StaticFactoryMethod;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSocketFactory;
import java.net.ProxySelector;

@NotThreadSafe
public interface Network {

@NonNull ProxySelector getProxySelector();

@NonNull SSLSocketFactory getSSLSocketFactory();

@NonNull HostnameVerifier getHostnameVerifier();
@NonNull SSLFactory getSSLFactory();

@NonNull URLConnectionFactory getURLConnectionFactory();

@StaticFactoryMethod
static @NonNull Network getDefault() {
return DefaultNetwork.INSTANCE;
}
Expand Down
22 changes: 22 additions & 0 deletions sdmx-dl-api/src/main/java/sdmxdl/web/SSLFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package sdmxdl.web;

import internal.sdmxdl.web.DefaultSSLFactory;
import lombok.NonNull;
import nbbrd.design.NotThreadSafe;
import nbbrd.design.StaticFactoryMethod;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSocketFactory;

@NotThreadSafe
public interface SSLFactory {

@NonNull SSLSocketFactory getSSLSocketFactory();

@NonNull HostnameVerifier getHostnameVerifier();

@StaticFactoryMethod
static @NonNull SSLFactory getDefault() {
return DefaultSSLFactory.INSTANCE;
}
}
15 changes: 5 additions & 10 deletions sdmx-dl-api/src/main/java/sdmxdl/web/SdmxWebManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@
*/
package sdmxdl.web;

import internal.util.CacheProviderLoader;
import internal.util.WebAuthenticatorLoader;
import internal.util.WebDriverLoader;
import internal.util.WebMonitoringLoader;
import internal.util.*;
import lombok.AccessLevel;
import lombok.NonNull;
import nbbrd.design.StaticFactoryMethod;
Expand All @@ -28,10 +25,7 @@
import sdmxdl.SdmxManager;
import sdmxdl.ext.SdmxSourceConsumer;
import sdmxdl.ext.spi.CacheProvider;
import sdmxdl.web.spi.WebAuthenticator;
import sdmxdl.web.spi.WebContext;
import sdmxdl.web.spi.WebDriver;
import sdmxdl.web.spi.WebMonitoring;
import sdmxdl.web.spi.*;

import java.io.IOException;
import java.net.URI;
Expand All @@ -57,6 +51,7 @@ public class SdmxWebManager extends SdmxManager<SdmxWebSource> {
.builder()
.drivers(WebDriverLoader.load())
.monitorings(WebMonitoringLoader.load())
.networking(NetworkingLoader.load())
.cacheProvider(CacheProviderLoader.load())
.authenticators(WebAuthenticatorLoader.load())
.build();
Expand All @@ -81,7 +76,7 @@ public class SdmxWebManager extends SdmxManager<SdmxWebSource> {

@lombok.NonNull
@lombok.Builder.Default
Network network = Network.getDefault();
Networking networking = Networking.getDefault();

@lombok.NonNull
@lombok.Builder.Default
Expand Down Expand Up @@ -191,7 +186,7 @@ private WebContext initContext() {
.builder()
.cacheProvider(cacheProvider)
.languages(languages)
.network(network)
.networking(networking)
.eventListener(eventListener)
.authenticators(authenticators)
.build();
Expand Down
40 changes: 40 additions & 0 deletions sdmx-dl-api/src/main/java/sdmxdl/web/spi/Networking.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package sdmxdl.web.spi;

import internal.sdmxdl.web.spi.DefaultNetworking;
import lombok.NonNull;
import nbbrd.design.StaticFactoryMethod;
import nbbrd.design.ThreadSafe;
import nbbrd.service.*;
import sdmxdl.web.Network;
import sdmxdl.web.SdmxWebSource;

import java.util.Collection;

@ServiceDefinition(
quantifier = Quantifier.SINGLE,
loaderName = "internal.util.NetworkingLoader",
fallback = DefaultNetworking.class
)
@ThreadSafe
public interface Networking {

@ServiceId
@NonNull String getNetworkingId();

@ServiceSorter(reverse = true)
int getNetworkingRank();

@ServiceFilter
boolean isNetworkingAvailable();

@NonNull Collection<String> getNetworkingProperties();

@NonNull Network getNetwork(@NonNull SdmxWebSource source);

int UNKNOWN_NETWORKING_RANK = -1;

@StaticFactoryMethod
static @NonNull Networking getDefault() {
return DefaultNetworking.INSTANCE;
}
}
6 changes: 5 additions & 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 @@ -52,9 +52,13 @@ public class WebContext {

@lombok.NonNull
@lombok.Builder.Default
Network network = Network.getDefault();
Networking networking = Networking.getDefault();

public @NonNull Cache getCache(@NonNull SdmxWebSource source) {
return getCacheProvider().getWebCache(source, getEventListener());
}

public @NonNull Network getNetwork(@NonNull SdmxWebSource source) {
return getNetworking().getNetwork(source);
}
}
7 changes: 4 additions & 3 deletions sdmx-dl-api/src/test/java/sdmxdl/web/SdmxWebManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import sdmxdl.*;
import sdmxdl.ext.spi.CacheProvider;
import sdmxdl.ext.spi.Dialect;
import sdmxdl.web.spi.Networking;
import sdmxdl.web.spi.WebDriver;
import tests.sdmxdl.api.SdmxManagerAssert;
import tests.sdmxdl.ext.MockedDialect;
Expand Down Expand Up @@ -60,7 +61,7 @@ public void testFactories() {
assertThat(o.getDrivers()).isEmpty();
assertThat(o.getMonitorings()).isEmpty();
assertThat(o.getLanguages()).isEqualTo(LanguagePriorityList.ANY);
assertThat(o.getNetwork()).isEqualTo(Network.getDefault());
assertThat(o.getNetworking()).isEqualTo(Networking.getDefault());
assertThat(o.getCacheProvider()).isEqualTo(CacheProvider.noOp());
assertThat(o.getEventListener()).isEqualTo(SdmxManager.NO_OP_EVENT_LISTENER);
assertThat(o.getAuthenticators()).isEmpty();
Expand All @@ -73,7 +74,7 @@ public void testFactories() {
assertThat(o.getDrivers()).isEmpty();
assertThat(o.getMonitorings()).isEmpty();
assertThat(o.getLanguages()).isEqualTo(LanguagePriorityList.ANY);
assertThat(o.getNetwork()).isEqualTo(Network.getDefault());
assertThat(o.getNetworking()).isEqualTo(Networking.getDefault());
assertThat(o.getCacheProvider()).isEqualTo(CacheProvider.noOp());
assertThat(o.getEventListener()).isEqualTo(SdmxManager.NO_OP_EVENT_LISTENER);
assertThat(o.getAuthenticators()).isEmpty();
Expand All @@ -85,7 +86,7 @@ public void testFactories() {
assertThat(o.getDrivers()).containsExactly(sampleDriver);
assertThat(o.getMonitorings()).isEmpty();
assertThat(o.getLanguages()).isEqualTo(LanguagePriorityList.ANY);
assertThat(o.getNetwork()).isEqualTo(Network.getDefault());
assertThat(o.getNetworking()).isEqualTo(Networking.getDefault());
assertThat(o.getCacheProvider()).isEqualTo(CacheProvider.noOp());
assertThat(o.getEventListener()).isEqualTo(SdmxManager.NO_OP_EVENT_LISTENER);
assertThat(o.getAuthenticators()).isEmpty();
Expand Down
4 changes: 2 additions & 2 deletions sdmx-dl-api/src/test/java/tests/sdmxdl/ext/NetworkAssert.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ public void assertCompliance(Network network) {
}

public void assertCompliance(SoftAssertions s, Network network) {
s.assertThat(network.getHostnameVerifier()).isNotNull();
s.assertThat(network.getSSLSocketFactory()).isNotNull();
s.assertThat(network.getProxySelector()).isNotNull();
s.assertThat(network.getSSLFactory()).isNotNull();
s.assertThat(network.getURLConnectionFactory()).isNotNull();
}
}
Loading

0 comments on commit 1ae986f

Please sign in to comment.