From 463cafa30b0699379947a0f0a9888aea72ee60ba Mon Sep 17 00:00:00 2001 From: Spencer Jorgensen Date: Wed, 29 May 2024 10:02:03 -0600 Subject: [PATCH 1/3] Added X-Forwarded-For --- .../com/smartystreets/api/ClientBuilder.java | 17 ++++++++--- .../api/CustomHeaderSenderTest.java | 4 +-- .../smartystreets/api/XForwardedForTest.java | 30 +++++++++++++++++++ 3 files changed, 45 insertions(+), 6 deletions(-) create mode 100644 src/test/java/com/smartystreets/api/XForwardedForTest.java diff --git a/src/main/java/com/smartystreets/api/ClientBuilder.java b/src/main/java/com/smartystreets/api/ClientBuilder.java index 949187f..273b7e9 100644 --- a/src/main/java/com/smartystreets/api/ClientBuilder.java +++ b/src/main/java/com/smartystreets/api/ClientBuilder.java @@ -30,6 +30,7 @@ public class ClientBuilder { private Proxy proxy; private Map customHeaders; private List licenses = new ArrayList<>(); + private String ip; private ClientBuilder() { this.serializer = new SmartySerializer(); @@ -83,7 +84,7 @@ public ClientBuilder withSerializer(Serializer serializer) { this.serializer = serializer; return this; } - + /** * This may be useful when using a local installation of the SmartyStreets APIs. * @param baseUrl Defaults to the URL for the API corresponding to the Client object being built. @@ -99,7 +100,7 @@ public ClientBuilder withCustomBaseUrl(String baseUrl) { * @param customHeaders A String to Object Map of header name/value pairs. * @return Returns this to accommodate method chaining. */ - public ClientBuilder withCustomHeaders(Map customHeaders) { + public ClientBuilder withCustomHeaders(Map customHeaders) { this.customHeaders = customHeaders; return this; } @@ -115,6 +116,11 @@ public ClientBuilder withProxy(Proxy.Type proxyType, String proxyHost, int proxy this.proxy = new Proxy(proxyType, new InetSocketAddress(proxyHost, proxyPort)); return this; } + + public ClientBuilder withXForwardedFor(String ip){ + this.ip = ip; + return this; + } /** * Enables debug mode, which will print information about the HTTP request and response to the console. @@ -190,7 +196,8 @@ private Sender buildSender() { sender = new SmartySender(this.maxTimeout); sender = new StatusCodeSender(sender); - + if (this.ip != null) + customHeaders.put("X-Forwarded-For", this.ip); if (this.customHeaders != null) sender = new CustomHeaderSender(this.customHeaders, sender); @@ -201,9 +208,11 @@ private Sender buildSender() { if (this.maxRetries > 0) sender = new RetrySender(this.maxRetries, new MySleeper(), new MyLogger(), sender); - + sender = new LicenseSender(this.licenses, sender); + + return sender; } diff --git a/src/test/java/com/smartystreets/api/CustomHeaderSenderTest.java b/src/test/java/com/smartystreets/api/CustomHeaderSenderTest.java index 902c3df..f463c30 100644 --- a/src/test/java/com/smartystreets/api/CustomHeaderSenderTest.java +++ b/src/test/java/com/smartystreets/api/CustomHeaderSenderTest.java @@ -19,12 +19,12 @@ public void testAllCustomHeadersAreAddedToTheRequest() throws Exception { RequestCapturingSender inner = new RequestCapturingSender(); CustomHeaderSender sender = new CustomHeaderSender(headers, inner); Request request = new Request(); - + sender.send(request); Map requestHeaders = inner.getRequest().getHeaders(); assertNotNull("There should be headers here.", requestHeaders); assertEquals(headers.get("A"), inner.getRequest().getHeaders().get("A")); - + } } diff --git a/src/test/java/com/smartystreets/api/XForwardedForTest.java b/src/test/java/com/smartystreets/api/XForwardedForTest.java new file mode 100644 index 0000000..002fff9 --- /dev/null +++ b/src/test/java/com/smartystreets/api/XForwardedForTest.java @@ -0,0 +1,30 @@ +package com.smartystreets.api; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Test; + +import com.smartystreets.api.mocks.RequestCapturingSender; + +public class XForwardedForTest { + //In order to create a functioning + @Test + public void testAllCustomHeadersAreAddedToTheRequest() throws Exception { + HashMap headers = new HashMap<>(); + headers.put("X-Forwarded-For", "ip"); + RequestCapturingSender inner = new RequestCapturingSender(); + CustomHeaderSender sender = new CustomHeaderSender(headers, inner); + Request request = new Request(); + + sender.send(request); + + Map requestHeaders = inner.getRequest().getHeaders(); + assertNotNull("Headers here.", requestHeaders); + assertEquals(headers.get("X-Forwarded-For"), inner.getRequest().getHeaders().get("X-Forwarded-For")); + + } +} From e443c23e39dc8cc5818d18b21d0df1575db0e545 Mon Sep 17 00:00:00 2001 From: Spencer Jorgensen Date: Fri, 31 May 2024 17:20:04 +0000 Subject: [PATCH 2/3] Fixed the spacing errors. --- image.jpg | 46 ++++++++++++++ sample.jpg | 46 ++++++++++++++ .../com/smartystreets/api/ClientBuilder.java | 61 ++++++++++++------- .../api/CustomHeaderSenderTest.java | 2 - .../smartystreets/api/XForwardedForTest.java | 2 +- 5 files changed, 131 insertions(+), 26 deletions(-) create mode 100644 image.jpg create mode 100644 sample.jpg diff --git a/image.jpg b/image.jpg new file mode 100644 index 0000000..5a9b52f --- /dev/null +++ b/image.jpg @@ -0,0 +1,46 @@ + + + + Example Domain + + + + + + + + +
+

Example Domain

+

This domain is for use in illustrative examples in documents. You may use this + domain in literature without prior coordination or asking for permission.

+

More information...

+
+ + diff --git a/sample.jpg b/sample.jpg new file mode 100644 index 0000000..5a9b52f --- /dev/null +++ b/sample.jpg @@ -0,0 +1,46 @@ + + + + Example Domain + + + + + + + + +
+

Example Domain

+

This domain is for use in illustrative examples in documents. You may use this + domain in literature without prior coordination or asking for permission.

+

More information...

+
+ + diff --git a/src/main/java/com/smartystreets/api/ClientBuilder.java b/src/main/java/com/smartystreets/api/ClientBuilder.java index 273b7e9..f1d8032 100644 --- a/src/main/java/com/smartystreets/api/ClientBuilder.java +++ b/src/main/java/com/smartystreets/api/ClientBuilder.java @@ -7,8 +7,10 @@ import java.util.Map; /** - * The ClientBuilder class helps you build a client object for one of the supported SmartyStreets APIs.
- * You can use ClientBuilder's methods to customize settings like maximum retries or timeout duration. These methods
+ * The ClientBuilder class helps you build a client object for one of the + * supported SmartyStreets APIs.
+ * You can use ClientBuilder's methods to customize settings like maximum + * retries or timeout duration. These methods
* are chainable, so you can usually get set up with one line of code. */ public class ClientBuilder { @@ -48,7 +50,8 @@ public ClientBuilder(String authId, String authToken) { } /** - * @param maxRetries The maximum number of times to retry sending the request to the API. (Default is 5) + * @param maxRetries The maximum number of times to retry sending the request to + * the API. (Default is 5) * @return Returns this to accommodate method chaining. */ public ClientBuilder retryAtMost(int maxRetries) { @@ -57,7 +60,8 @@ public ClientBuilder retryAtMost(int maxRetries) { } /** - * @param maxTimeout The maximum time (in milliseconds) to wait for a connection, and also to wait for
+ * @param maxTimeout The maximum time (in milliseconds) to wait for a + * connection, and also to wait for
* the response to be read. (Default is 10000) * @return Returns this to accommodate method chaining. */ @@ -67,7 +71,8 @@ public ClientBuilder withMaxTimeout(int maxTimeout) { } /** - * @param sender Default is a series of nested senders. See buildSender(). + * @param sender Default is a series of nested senders. See + * buildSender(). * @return Returns this to accommodate method chaining. */ public ClientBuilder withSender(Sender sender) { @@ -77,6 +82,7 @@ public ClientBuilder withSender(Sender sender) { /** * Changes the Serializer from the default SmartySerializer. + * * @param serializer An object that implements the Serializer interface. * @return Returns this to accommodate method chaining. */ @@ -84,10 +90,12 @@ public ClientBuilder withSerializer(Serializer serializer) { this.serializer = serializer; return this; } - + /** * This may be useful when using a local installation of the SmartyStreets APIs. - * @param baseUrl Defaults to the URL for the API corresponding to the Client object being built. + * + * @param baseUrl Defaults to the URL for the API corresponding to the + * Client object being built. * @return Returns this to accommodate method chaining. */ public ClientBuilder withCustomBaseUrl(String baseUrl) { @@ -97,16 +105,19 @@ public ClientBuilder withCustomBaseUrl(String baseUrl) { /** * Use this to add any additional headers you need. - * @param customHeaders A String to Object Map of header name/value pairs. + * + * @param customHeaders A String to Object Map of header name/value + * pairs. * @return Returns this to accommodate method chaining. */ - public ClientBuilder withCustomHeaders(Map customHeaders) { + public ClientBuilder withCustomHeaders(Map customHeaders) { this.customHeaders = customHeaders; return this; } /** * Use this to specify a proxy through which to send all lookups. + * * @param proxyType Choose a java.net.Proxy.Type. * @param proxyHost The host of the proxy server (do not include the port). * @param proxyPort The port on the proxy server to which you wish to connect. @@ -116,14 +127,16 @@ public ClientBuilder withProxy(Proxy.Type proxyType, String proxyHost, int proxy this.proxy = new Proxy(proxyType, new InetSocketAddress(proxyHost, proxyPort)); return this; } - - public ClientBuilder withXForwardedFor(String ip){ + + public ClientBuilder withXForwardedFor(String ip) { this.ip = ip; return this; } /** - * Enables debug mode, which will print information about the HTTP request and response to the console. + * Enables debug mode, which will print information about the HTTP request and + * response to the console. + * * @return Returns this to accommodate method chaining. */ public ClientBuilder withDebug() { @@ -133,6 +146,7 @@ public ClientBuilder withDebug() { /** * Allows caller to specify licenses (aka "tracks") they wish to use. + * * @return Returns this to accommodate method chaining. */ public ClientBuilder withLicenses(List licenses) { @@ -190,28 +204,29 @@ private Sender buildSender() { return this.httpSender; Sender sender; - if (this.proxy != null) + if (this.proxy != null) { sender = new SmartySender(this.maxTimeout, this.proxy); - else + } else { sender = new SmartySender(this.maxTimeout); + } sender = new StatusCodeSender(sender); - if (this.ip != null) + if (this.ip != null) { customHeaders.put("X-Forwarded-For", this.ip); - if (this.customHeaders != null) + } + if (this.customHeaders != null) { sender = new CustomHeaderSender(this.customHeaders, sender); - - if (this.signer != null) + } + if (this.signer != null) { sender = new SigningSender(this.signer, sender); - + } sender = new URLPrefixSender(this.urlPrefix, sender); - if (this.maxRetries > 0) + if (this.maxRetries > 0) { sender = new RetrySender(this.maxRetries, new MySleeper(), new MyLogger(), sender); - - sender = new LicenseSender(this.licenses, sender); + } - + sender = new LicenseSender(this.licenses, sender); return sender; } diff --git a/src/test/java/com/smartystreets/api/CustomHeaderSenderTest.java b/src/test/java/com/smartystreets/api/CustomHeaderSenderTest.java index f463c30..40381c2 100644 --- a/src/test/java/com/smartystreets/api/CustomHeaderSenderTest.java +++ b/src/test/java/com/smartystreets/api/CustomHeaderSenderTest.java @@ -19,12 +19,10 @@ public void testAllCustomHeadersAreAddedToTheRequest() throws Exception { RequestCapturingSender inner = new RequestCapturingSender(); CustomHeaderSender sender = new CustomHeaderSender(headers, inner); Request request = new Request(); - sender.send(request); Map requestHeaders = inner.getRequest().getHeaders(); assertNotNull("There should be headers here.", requestHeaders); assertEquals(headers.get("A"), inner.getRequest().getHeaders().get("A")); - } } diff --git a/src/test/java/com/smartystreets/api/XForwardedForTest.java b/src/test/java/com/smartystreets/api/XForwardedForTest.java index 002fff9..b7f5715 100644 --- a/src/test/java/com/smartystreets/api/XForwardedForTest.java +++ b/src/test/java/com/smartystreets/api/XForwardedForTest.java @@ -25,6 +25,6 @@ public void testAllCustomHeadersAreAddedToTheRequest() throws Exception { Map requestHeaders = inner.getRequest().getHeaders(); assertNotNull("Headers here.", requestHeaders); assertEquals(headers.get("X-Forwarded-For"), inner.getRequest().getHeaders().get("X-Forwarded-For")); - + } } From 92604b2f004e031095c8af76997b91799fa9537b Mon Sep 17 00:00:00 2001 From: Spencer Jorgensen Date: Fri, 31 May 2024 13:14:04 -0600 Subject: [PATCH 3/3] Removed the comment in the test and some other small things. --- image.jpg | 46 ------------------- sample.jpg | 46 ------------------- .../smartystreets/api/XForwardedForTest.java | 1 - 3 files changed, 93 deletions(-) delete mode 100644 image.jpg delete mode 100644 sample.jpg diff --git a/image.jpg b/image.jpg deleted file mode 100644 index 5a9b52f..0000000 --- a/image.jpg +++ /dev/null @@ -1,46 +0,0 @@ - - - - Example Domain - - - - - - - - -
-

Example Domain

-

This domain is for use in illustrative examples in documents. You may use this - domain in literature without prior coordination or asking for permission.

-

More information...

-
- - diff --git a/sample.jpg b/sample.jpg deleted file mode 100644 index 5a9b52f..0000000 --- a/sample.jpg +++ /dev/null @@ -1,46 +0,0 @@ - - - - Example Domain - - - - - - - - -
-

Example Domain

-

This domain is for use in illustrative examples in documents. You may use this - domain in literature without prior coordination or asking for permission.

-

More information...

-
- - diff --git a/src/test/java/com/smartystreets/api/XForwardedForTest.java b/src/test/java/com/smartystreets/api/XForwardedForTest.java index b7f5715..d8fc715 100644 --- a/src/test/java/com/smartystreets/api/XForwardedForTest.java +++ b/src/test/java/com/smartystreets/api/XForwardedForTest.java @@ -11,7 +11,6 @@ import com.smartystreets.api.mocks.RequestCapturingSender; public class XForwardedForTest { - //In order to create a functioning @Test public void testAllCustomHeadersAreAddedToTheRequest() throws Exception { HashMap headers = new HashMap<>();