Skip to content

Commit b6bcab5

Browse files
committed
Proxies now support an additional path component
1 parent b79be02 commit b6bcab5

File tree

6 files changed

+63
-6
lines changed

6 files changed

+63
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77
## [Unreleased]
88
### Added
99
- Util method `Parameters.isPositiveInteger` returning Predicate<String> used to check for positive numeric integers.
10+
- `Proxy` objects now support an additional path component.
1011

1112
### Changed
1213
- Method `APIUtil.prettyPrint(JsonNode)` now wraps lines with a fix Unix-style (LF) line separator.

src/main/java/com/github/m0nk3y2k4/thetvdb/TheTVDBApiFactory.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,15 +146,37 @@ public static QueryParameters createQueryParameters(@Nonnull Map<String, String>
146146
/**
147147
* Creates a new proxy object based on the given parameters. A proxy may be provided when creating a new TheTVDBApi
148148
* instance in order to forward all communication towards this proxy instead of directly communicating with the
149-
* remote <i>TheTVDB.com</i> API.
149+
* remote <i>TheTVDB.com</i> API. The returned proxy object represents a URI in the following format:
150+
* <em>{@code "protocol://host:port"}</em>.
150151
*
151152
* @param protocol The protocol used by the proxy
152153
* @param host The host name of the proxy
153154
* @param port The port number to be used for communication
154155
*
155156
* @return New immutable proxy object based on the given parameters
157+
*
158+
* @see TheTVDBApiFactory#createProxy(String, String, String, int) createProxy(protocol, host, path, port)
156159
*/
157160
public static Proxy createProxy(@Nonnull String protocol, @Nonnull String host, int port) {
158161
return new RemoteAPI.Builder().protocol(protocol).host(host).port(port).build();
159162
}
163+
164+
/**
165+
* Creates a new proxy object based on the given parameters. A proxy may be provided when creating a new TheTVDBApi
166+
* instance in order to forward all communication towards this proxy instead of directly communicating with the
167+
* remote <i>TheTVDB.com</i> API. The returned proxy object represents a URI in the following format:
168+
* <em>{@code "protocol://host:port/path"}</em>.
169+
*
170+
* @param protocol The protocol used by the proxy
171+
* @param host The host name of the proxy
172+
* @param path The path component with a leading '/'. Will be appended to the authority component.
173+
* @param port The port number to be used for communication
174+
*
175+
* @return New immutable proxy object based on the given parameters
176+
*
177+
* @see TheTVDBApiFactory#createProxy(String, String, int) createProxy(protocol, host, port)
178+
*/
179+
public static Proxy createProxy(@Nonnull String protocol, @Nonnull String host, @Nonnull String path, int port) {
180+
return new RemoteAPI.Builder().protocol(protocol).host(host).port(port).path(path).build();
181+
}
160182
}

src/main/java/com/github/m0nk3y2k4/thetvdb/api/Proxy.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,17 @@
1616

1717
package com.github.m0nk3y2k4.thetvdb.api;
1818

19+
import java.util.Optional;
20+
1921
import com.github.m0nk3y2k4.thetvdb.TheTVDBApiFactory;
2022

2123
/**
2224
* Interface representing a proxy service to be used for communication with the remote API. Such proxies may be provided
2325
* when creating a new API instance in order to forward the all the communication towards this proxy rather than
2426
* directly communicating with the the actual <i>TheTVDB.com</i> remote API. The latter one will be the default behavior
2527
* if no specific proxy is set during the API instantiation. Instances of this interface might be created via the {@link
26-
* TheTVDBApiFactory}.
28+
* TheTVDBApiFactory}. The URI represented by this proxy will be assembled in the following form:
29+
* <em>{@code protocol://host:port[/path]}</em>
2730
*/
2831
public interface Proxy {
2932

@@ -41,6 +44,13 @@ public interface Proxy {
4144
*/
4245
String getHost();
4346

47+
/**
48+
* Returns the optional path component of this proxy.
49+
*
50+
* @return The optional path component of this proxy
51+
*/
52+
Optional<String> getPath();
53+
4454
/**
4555
* Returns the port number used for communication with this proxy.
4656
*

src/main/java/com/github/m0nk3y2k4/thetvdb/internal/connection/RemoteAPI.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public int getPort() {
9696
* @throws MalformedURLException If an unknown protocol is used or the port is a negative number other than -1
9797
*/
9898
public URL forResource(@Nonnull String resource) throws MalformedURLException {
99-
return new URL(getProtocol(), getHost(), getPort(), resource);
99+
return new URL(getProtocol(), getHost(), getPort(), getPath().orElse("") + resource);
100100
}
101101

102102
/**

src/test/java/com/github/m0nk3y2k4/thetvdb/TheTVDBApiFactoryTest.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,27 @@ void createQueryParameters_withValidParameterMap_verifyQueryParameters() {
6363
}
6464

6565
@Test
66-
void createProxy_withValidSettings_verifyProxySettings() {
66+
void createProxy_withoutPath_verifyProxySettings() {
6767
final String protocol = "https";
68-
final String host = "sub.proxy.com";
68+
final String host = "sub1.proxy.com";
6969
final int port = 9584;
7070
Proxy proxy = TheTVDBApiFactory.createProxy(protocol, host, port);
7171
assertThat(proxy.getProtocol()).isEqualTo(protocol);
7272
assertThat(proxy.getHost()).isEqualTo(host);
73+
assertThat(proxy.getPath()).isEmpty();
74+
assertThat(proxy.getPort()).isEqualTo(port);
75+
}
76+
77+
@Test
78+
void createProxy_withPath_verifyProxySettings() {
79+
final String protocol = "https";
80+
final String host = "sub2.proxy.com";
81+
final String path = "/path";
82+
final int port = 6487;
83+
Proxy proxy = TheTVDBApiFactory.createProxy(protocol, host, path, port);
84+
assertThat(proxy.getProtocol()).isEqualTo(protocol);
85+
assertThat(proxy.getHost()).isEqualTo(host);
86+
assertThat(proxy.getPath()).contains(path);
7387
assertThat(proxy.getPort()).isEqualTo(port);
7488
}
7589
}

src/test/java/com/github/m0nk3y2k4/thetvdb/internal/connection/RemoteAPITest.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,20 @@ void buildNewRemoteAPI_withSpecificOrDefaultSettings_verifyAPISettings(RemoteAPI
5151
}
5252

5353
@Test
54-
void forResource() throws Exception {
54+
void forResource_withoutPath_checkUrl() throws Exception {
5555
final URL remote = new URL("http", "localhost", 3654, "/someRemoteResource");
5656
URL url = new RemoteAPI.Builder().protocol(remote.getProtocol()).host(remote.getHost()).port(remote.getPort())
5757
.build().forResource(remote.getFile());
5858
assertThat(url).isEqualTo(remote);
5959
}
60+
61+
@Test
62+
void forResource_withPath_checkUrl() throws Exception {
63+
final String path = "/path";
64+
final String resource = "/someRemoteResource";
65+
final URL remote = new URL("http", "localhost", 3654, path + resource);
66+
URL url = new RemoteAPI.Builder().protocol(remote.getProtocol()).host(remote.getHost()).path(path)
67+
.port(remote.getPort()).build().forResource(resource);
68+
assertThat(url).isEqualTo(remote);
69+
}
6070
}

0 commit comments

Comments
 (0)