Skip to content

Commit

Permalink
Refactor the connection coordinates that were repeated over and over …
Browse files Browse the repository at this point in the history
…into a class
  • Loading branch information
sfuhrm committed Jan 11, 2024
1 parent 5f337b5 commit a1f746f
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 53 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package de.sfuhrm.radiobrowser4j;

import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;

/** A tuple of parameters for creating API connections to the
* radio browser API.
* */
@Builder
@Getter(AccessLevel.PACKAGE)
class ConnectionParams {
/** The URL of the radio browser API.
* Must not be {@code null}.
* */
private final String apiUrl;

/** The timeout for connect and read requests in milliseconds.
* Must be greater than zero. */
private final int timeout;

/** The user agent to use for identifying with the API.
* Must not be {@code null}.
* */
private final String userAgent;

/** The proxy URI to use. May be {@code null}.
* */
private final String proxyUri;

/** The proxy user to use. May be {@code null}.
* */
private final String proxyUser;

/** The proxy password to use. May be {@code null}.
* */
private final String proxyPassword;

/** Checks the parameters.
* @throws IllegalArgumentException if the parameters are invalid.
* */
void check() {
if (apiUrl == null) {
throw new IllegalArgumentException(
"apiUrl must not be null");
}
if (timeout <= 0) {
throw new IllegalArgumentException(
"timeout must be > 0, but is "
+ getTimeout());
}
if (userAgent == null) {
throw new IllegalArgumentException(
"userAgent must not be null");
}
if (proxyUri != null) {
if (proxyUser != null && proxyPassword == null) {
throw new IllegalArgumentException(
"proxyUser was given, but not a proxyPassword");
}
if (proxyUser == null && proxyPassword != null) {
throw new IllegalArgumentException(
"proxyUser was not given, but a proxyPassword");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import lombok.extern.slf4j.Slf4j;

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -50,9 +49,6 @@ public class RadioBrowser {
/** REST implementation. */
private final RestDelegate rest;

/** The user agent name. */
private final String userAgent;

/**
* Creates a new API client using a specific api endpoint.
* @param apiUrl the base URL of the API.
Expand All @@ -69,7 +65,11 @@ public class RadioBrowser {
public RadioBrowser(@NonNull final String apiUrl,
final int timeout,
@NonNull final String myUserAgent) {
this(apiUrl, timeout, myUserAgent, null, null, null);
this(new ConnectionParams.ConnectionParamsBuilder()
.apiUrl(apiUrl)
.timeout(timeout)
.userAgent(myUserAgent)
.build());
}

/**
Expand All @@ -96,19 +96,25 @@ public RadioBrowser(@NonNull final String apiUrl,
final String proxyUri,
final String proxyUser,
final String proxyPassword) {
if (timeout <= 0) {
throw new IllegalArgumentException(
"timeout must be > 0, but is "
+ timeout);
}
this.userAgent = myUserAgent;
rest = new RestDelegateJaxRsImpl(
URI.create(apiUrl),
timeout,
proxyUri,
proxyUser,
proxyPassword,
myUserAgent);

this(new ConnectionParams.ConnectionParamsBuilder()
.apiUrl(apiUrl)
.timeout(timeout)
.userAgent(myUserAgent)
.proxyUri(proxyUri)
.proxyUser(proxyUser)
.proxyPassword(proxyPassword)
.build());
}

/**
* Creates a new API client using a proxy.
* @param connectionParams the parameters for creating an API connection.
* @see ConnectionParams.ConnectionParamsBuilder
* */
RadioBrowser(@NonNull final ConnectionParams connectionParams) {
connectionParams.check();
rest = new RestDelegateJaxRsImpl(connectionParams);
}

/**
Expand All @@ -127,7 +133,11 @@ public RadioBrowser(@NonNull final String apiUrl,
@Deprecated
public RadioBrowser(final int timeout,
final String myUserAgent) {
this(DEFAULT_API_URL, timeout, myUserAgent);
this(new ConnectionParams.ConnectionParamsBuilder()
.apiUrl(DEFAULT_API_URL)
.timeout(timeout)
.userAgent(myUserAgent)
.build());
}

/** Composes URI path components with '/' separators.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,54 +37,38 @@ class RestDelegateJaxRsImpl implements RestDelegate {
private final String userAgent;

/** Create a new instance.
* @param inEndpoint the API endpoint URI address.
* @param inTimeout the timeout in millis.
* @param inProxyUri the optional proxy URI.
* @param inProxyUser the optional proxy user.
* @param inProxyPassword the optional proxy password.
* @param inUserAgent the mandatory user agent string to send.
* @param connectionParams the connection parameters to use.
* */
RestDelegateJaxRsImpl(final URI inEndpoint,
final int inTimeout,
final String inProxyUri,
final String inProxyUser,
final String inProxyPassword,
final String inUserAgent) {
this.endpoint = inEndpoint;
client = newClient(inTimeout,
inProxyUri,
inProxyUser,
inProxyPassword);
this.userAgent = inUserAgent;
RestDelegateJaxRsImpl(final ConnectionParams connectionParams) {
this.endpoint = URI.create(connectionParams.getApiUrl());
client = newClient(connectionParams);
this.userAgent = connectionParams.getUserAgent();
}

/** Create a new JAX-RS client.
* @param timeout connect / read timeout in milliseconds.
* @param proxyUri optional proxy URI.
* @param proxyUser optional proxy user.
* @param proxyPassword optional proxy password.
* @param connectionParams the connection parameters to use.
* @return the client instance that has been created.
* */
private static Client newClient(final int timeout,
final String proxyUri,
final String proxyUser,
final String proxyPassword) {
private static Client newClient(final ConnectionParams connectionParams) {
Client client = ClientBuilder.newBuilder()
.register(ObjectMapperResolver.class)
.register(JacksonFeature.class)
.register(GZipEncoder.class)
.build();
client.property(ClientProperties.CONNECT_TIMEOUT, timeout);
client.property(ClientProperties.READ_TIMEOUT, timeout);
if (proxyUri != null) {
client.property(ClientProperties.PROXY_URI, proxyUri);
if (proxyUser != null) {
client.property(ClientProperties.CONNECT_TIMEOUT,
connectionParams.getTimeout());
client.property(ClientProperties.READ_TIMEOUT,
connectionParams.getTimeout());
if (connectionParams.getProxyUri() != null) {
client.property(ClientProperties.PROXY_URI,
connectionParams.getProxyUri());
if (connectionParams.getProxyUser() != null) {
client.property(ClientProperties.PROXY_USERNAME,
proxyUser);
connectionParams.getProxyUser());
}
if (proxyPassword != null) {
if (connectionParams.getProxyPassword() != null) {
client.property(ClientProperties.PROXY_PASSWORD,
proxyPassword);
connectionParams.getProxyPassword());
}
}
return client;
Expand Down

0 comments on commit a1f746f

Please sign in to comment.