From 6694f1bd281fc7d6a2d9494111d5f5460c125b34 Mon Sep 17 00:00:00 2001 From: Francesco Borg Bonaci Date: Wed, 25 Oct 2017 23:05:54 +0200 Subject: [PATCH 1/3] Added missing javadocs documentation for global request attribute related methods. --- src/main/java/rocks/bastion/Bastion.java | 6 ++ .../GlobalRequestAttributes.java | 87 ++++++++++++++++++- 2 files changed, 89 insertions(+), 4 deletions(-) diff --git a/src/main/java/rocks/bastion/Bastion.java b/src/main/java/rocks/bastion/Bastion.java index f695be2..33fb7eb 100644 --- a/src/main/java/rocks/bastion/Bastion.java +++ b/src/main/java/rocks/bastion/Bastion.java @@ -204,6 +204,12 @@ public static Configuration loadConfiguration(String resourceLocation) { *

* Starts building or modifying the configuration of the {@link GlobalRequestAttributes} for Bastion. *

+ *

+ * Global attributes could be used in cases where multiple subsequent requests require the same attributes, such as + * authentication headers or route parameters. Once {@link GlobalRequestAttributes} are set, they are applied to + * every single subsequent Bastion API call. The global attributes can be reset to their default values if needed + * using {@link GlobalRequestAttributes#clear()}. + *

* * @return The configured global request attributes. */ diff --git a/src/main/java/rocks/bastion/core/configuration/GlobalRequestAttributes.java b/src/main/java/rocks/bastion/core/configuration/GlobalRequestAttributes.java index 2b53429..1502372 100644 --- a/src/main/java/rocks/bastion/core/configuration/GlobalRequestAttributes.java +++ b/src/main/java/rocks/bastion/core/configuration/GlobalRequestAttributes.java @@ -1,12 +1,13 @@ package rocks.bastion.core.configuration; -import java.util.ArrayList; -import java.util.Collection; - import rocks.bastion.core.ApiHeader; import rocks.bastion.core.ApiQueryParam; +import rocks.bastion.core.HttpRequest; import rocks.bastion.core.RouteParam; +import java.util.ArrayList; +import java.util.Collection; + import static java.util.Objects.requireNonNull; /** @@ -33,54 +34,106 @@ public void clear() { globalRequestTimeout = 0; } + /** + * Returns the configured global headers. The default value for this configuration is an empty list. + * + * @see HttpRequest#headers() + */ public Collection getGlobalHeaders() { return globalHeaders; } + /** + * Sets the request headers for all Bastion requests. The supplied collection will replace any previously + * configured global headers. + * + * @see HttpRequest#headers() + */ public GlobalRequestAttributes setGlobalHeaders(Collection globalHeaders) { requireNonNull(globalHeaders, "globalHeaders should not be null."); this.globalHeaders = globalHeaders; return this; } + /** + * Returns the configured global query parameters. The default value for this configuration is an empty list. + * + * @see HttpRequest#queryParams() + */ public Collection getGlobalQueryParams() { return globalQueryParams; } + /** + * Sets the request query parameters for all Bastion requests. The supplied collection will replace any previously + * configured global query parameters. + * + * @see HttpRequest#queryParams() + */ public GlobalRequestAttributes setGlobalQueryParams(Collection globalQueryParams) { requireNonNull(globalQueryParams, "globalQueryParams should not be null."); this.globalQueryParams = globalQueryParams; return this; } + /** + * Returns the configured global route parameters. The default value for this configuration is an empty list. + * + * @see HttpRequest#routeParams() + */ public Collection getGlobalRouteParams() { return globalRouteParams; } + /** + * Sets the request route parameters for all Bastion requests. The supplied collection will replace any previously + * configured global route parameters. + * + * @see HttpRequest#routeParams() + */ public GlobalRequestAttributes setGlobalRouteParams(Collection globalRouteParams) { requireNonNull(globalRouteParams, "globalRouteParams should not be null."); this.globalRouteParams = globalRouteParams; return this; } + /** + * Returns the configured global timeout. The default value for this is 0, indicating no timeout. + * + * @see HttpRequest#timeout() + */ public long getGlobalRequestTimeout() { return globalRequestTimeout; } + /** + * Sets the timeout (in milliseconds) for all Bastion requests. A value of 0 indicates no timeout. + * + * @see HttpRequest#timeout() + */ public GlobalRequestAttributes setGlobalRequestTimeout(long globalRequestTimeout) { requireNonNull(globalRequestTimeout, "globalRequestTimeout should not be null."); this.globalRequestTimeout = globalRequestTimeout; return this; } + /** + * Adds a header for all Bastion requests. + * + * @see HttpRequest#headers() + */ public GlobalRequestAttributes addHeader(String name, String value) { requireNonNull(name, "Header name should not be null."); requireNonNull(value, "Header value should not be null."); globalHeaders.add(new ApiHeader(name, value)); return this; - } + /** + * Removes the header from all Bastion requests if it was already present. + * + * @see HttpRequest#headers() + */ public GlobalRequestAttributes removeHeader(String name, String value) { requireNonNull(name, "Header name should not be null."); requireNonNull(value, "Header value should not be null."); @@ -88,6 +141,11 @@ public GlobalRequestAttributes removeHeader(String name, String value) { return this; } + /** + * Adds a query parameter for all Bastion requests. + * + * @see HttpRequest#queryParams() + */ public GlobalRequestAttributes addQueryParam(String name, String value) { requireNonNull(name, "Query parameter name should not be null."); requireNonNull(value, "Query parameter value should not be null."); @@ -95,6 +153,11 @@ public GlobalRequestAttributes addQueryParam(String name, String value) { return this; } + /** + * Removes the query parameter from all Bastion requests if it was already present. + * + * @see HttpRequest#queryParams() + */ public GlobalRequestAttributes removeQueryParam(String name, String value) { requireNonNull(name, "Query parameter name should not be null."); requireNonNull(value, "Query parameter value should not be null."); @@ -102,6 +165,11 @@ public GlobalRequestAttributes removeQueryParam(String name, String value) { return this; } + /** + * Adds a route parameter for all Bastion requests. + * + * @see HttpRequest#routeParams() () + */ public GlobalRequestAttributes addRouteParam(String name, String value) { requireNonNull(name, "Route parameter name should not be null."); requireNonNull(value, "Route parameter value should not be null."); @@ -109,6 +177,11 @@ public GlobalRequestAttributes addRouteParam(String name, String value) { return this; } + /** + * Removes the route parameter from all Bastion requests if it was already present. + * + * @see HttpRequest#routeParams() + */ public GlobalRequestAttributes removeRouteParam(String name, String value) { requireNonNull(name, "Route parameter name should not be null."); requireNonNull(value, "Route parameter value should not be null."); @@ -116,7 +189,13 @@ public GlobalRequestAttributes removeRouteParam(String name, String value) { return this; } + /** + * Sets the timeout (in milliseconds) for all Bastion requests. A value of 0 indicates no timeout. + * + * @see HttpRequest#timeout() + */ public GlobalRequestAttributes timeout(long timeout) { + requireNonNull(timeout, "timeout should not be null."); globalRequestTimeout = timeout; return this; } From e507016023c2da3c15a8594efd7cfad7e42e2236 Mon Sep 17 00:00:00 2001 From: Francesco Borg Bonaci Date: Sun, 29 Oct 2017 17:21:58 +0100 Subject: [PATCH 2/3] replaced unnecessary null check with a more relevant check --- .../core/configuration/GlobalRequestAttributes.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/main/java/rocks/bastion/core/configuration/GlobalRequestAttributes.java b/src/main/java/rocks/bastion/core/configuration/GlobalRequestAttributes.java index 1502372..1f4655c 100644 --- a/src/main/java/rocks/bastion/core/configuration/GlobalRequestAttributes.java +++ b/src/main/java/rocks/bastion/core/configuration/GlobalRequestAttributes.java @@ -112,7 +112,9 @@ public long getGlobalRequestTimeout() { * @see HttpRequest#timeout() */ public GlobalRequestAttributes setGlobalRequestTimeout(long globalRequestTimeout) { - requireNonNull(globalRequestTimeout, "globalRequestTimeout should not be null."); + if (globalRequestTimeout < 0) { + throw new IllegalArgumentException("globalRequestTimeout should be equal to or greater than 0, with 0 indicating no timeout."); + } this.globalRequestTimeout = globalRequestTimeout; return this; } @@ -195,7 +197,9 @@ public GlobalRequestAttributes removeRouteParam(String name, String value) { * @see HttpRequest#timeout() */ public GlobalRequestAttributes timeout(long timeout) { - requireNonNull(timeout, "timeout should not be null."); + if (globalRequestTimeout < 0) { + throw new IllegalArgumentException("timeout should be equal to or greater than 0, with 0 indicating no timeout."); + } globalRequestTimeout = timeout; return this; } From b617bf216eabc7158546ac5051791d5af3a140ad Mon Sep 17 00:00:00 2001 From: Francesco Borg Bonaci Date: Mon, 30 Oct 2017 11:45:42 +0100 Subject: [PATCH 3/3] fixed validating incorrect value --- .../bastion/core/configuration/GlobalRequestAttributes.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/rocks/bastion/core/configuration/GlobalRequestAttributes.java b/src/main/java/rocks/bastion/core/configuration/GlobalRequestAttributes.java index 1f4655c..2590ce7 100644 --- a/src/main/java/rocks/bastion/core/configuration/GlobalRequestAttributes.java +++ b/src/main/java/rocks/bastion/core/configuration/GlobalRequestAttributes.java @@ -197,7 +197,7 @@ public GlobalRequestAttributes removeRouteParam(String name, String value) { * @see HttpRequest#timeout() */ public GlobalRequestAttributes timeout(long timeout) { - if (globalRequestTimeout < 0) { + if (timeout < 0) { throw new IllegalArgumentException("timeout should be equal to or greater than 0, with 0 indicating no timeout."); } globalRequestTimeout = timeout;