Skip to content

Commit 8bf9501

Browse files
author
Chris Wilson
committed
Fixing issue #57 extra slash in URL with HTTP Client >=4.5.3
1 parent b7fcbf4 commit 8bf9501

File tree

5 files changed

+46
-17
lines changed

5 files changed

+46
-17
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ Use this library in Java applications to easily access the SparkPost Email API i
1010

1111
## Version Compatibility Note
1212

13+
### Version 0.6.2 -> 0.6.3
14+
15+
Due to [issue 57](https://github.com/SparkPost/java-sparkpost/issues/57) and to maintain compatibility with old and new version of Apache HTTP Client `SPARKPOST_BASE_URL` must not end with a `/` slash.
16+
1317
### Version 0.12 -> 0.13
1418

1519
Although we try to maintain library backward compatibility this migration may require some minor changes to your code. Substitution data was changed from `Map<String, String>` to `Map<String, Object>`. Most client code will just need to change their map to this new signature.

libs/sparkpost-lib/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
<dependency>
2626
<groupId>org.apache.httpcomponents</groupId>
2727
<artifactId>httpclient</artifactId>
28-
<version>4.5</version>
28+
<version>4.5.3</version>
2929
</dependency>
3030

3131
<!-- the following two dependencies are required for correct jmockit operation

libs/sparkpost-lib/src/main/java/com/sparkpost/resources/Endpoint.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,17 @@ private void addString(String name, String value) {
2121
this.uriBuilder.addParameter(name, value);
2222
}
2323

24-
public Endpoint addCommonParams(String from, String to, String domains, String campaigns, String templates, String metrics,
25-
String timezone, String limit, String orderBy) {
24+
public Endpoint addCommonParams(
25+
String from,
26+
String to,
27+
String domains,
28+
String campaigns,
29+
String templates,
30+
String metrics,
31+
String timezone,
32+
String limit,
33+
String orderBy) {
34+
2635
addParam("from", from);
2736
addParam("to", to);
2837
addParam("domains", domains);
@@ -57,7 +66,14 @@ public Endpoint addParam(String name, Boolean value) {
5766

5867
@Override
5968
public String toString() {
60-
return this.uriBuilder.toString();
69+
String result = this.uriBuilder.toString();
70+
71+
if (result.startsWith("/")) {
72+
return result;
73+
} else {
74+
75+
return "/" + this.uriBuilder.toString();
76+
}
6177
}
6278

6379
}

libs/sparkpost-lib/src/main/java/com/sparkpost/transport/RestConnection.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ public class RestConnection {
4242

4343
/**
4444
* Default endpoint to use for connections :
45-
* https://api.sparkpost.com/api/v1/
45+
* https://api.sparkpost.com/api/v1
4646
*/
47-
public final static String defaultApiEndpoint = "https://api.sparkpost.com/api/v1/";
47+
public final static String defaultApiEndpoint = "https://api.sparkpost.com/api/v1";
4848

4949
private final Client client;
5050

@@ -70,7 +70,7 @@ public enum Method {
7070
* @throws SparkPostException
7171
*/
7272
public RestConnection(Client client) throws SparkPostException {
73-
this(client, null /* means:set to default endpoint */);
73+
this(client, "" /* means:set to default endpoint */);
7474
}
7575

7676
/**
@@ -84,15 +84,17 @@ public RestConnection(Client client) throws SparkPostException {
8484
* @throws SparkPostException
8585
*/
8686
public RestConnection(Client client, String endpoint) throws SparkPostException {
87+
8788
this.client = client;
8889
if (StringUtils.isAnyEmpty(endpoint)) {
8990
this.endpoint = defaultApiEndpoint;
9091
} else {
91-
if (endpoint.endsWith("/")) {
92-
this.endpoint = endpoint;
93-
} else {
94-
this.endpoint = endpoint + '/';
95-
}
92+
93+
this.endpoint = endpoint;
94+
}
95+
96+
if (endpoint.endsWith("/")) {
97+
throw new IllegalStateException("SPARKPOST_BASE_URL should not end with a '/', SPARKPOST_BASE_URL=" + endpoint + "");
9698
}
9799
}
98100

@@ -110,7 +112,11 @@ private HttpURLConnection createConnectionObject(String path, Method method) thr
110112
HttpURLConnection conn;
111113
try {
112114
URL url;
113-
url = new URL(this.endpoint + path);
115+
if (path.startsWith("/")) {
116+
url = new URL(this.endpoint + path);
117+
} else {
118+
url = new URL(this.endpoint + "/" + path);
119+
}
114120

115121
// Retrieve the URLConnection object (but doesn't actually connect):
116122
// (HttpUrlConnection doesn't connect to the server until we've
@@ -160,10 +166,13 @@ private HttpURLConnection createConnectionObject(String path, Method method) thr
160166
throw new SparkPostException("Invalid Method");
161167
}
162168
} catch (MalformedURLException ex) {
169+
163170
throw new SparkPostException("Invalid path: " + path + ex.toString());
164171
} catch (ProtocolException ex) {
172+
165173
throw new SparkPostException("Invalid method:" + ex.toString());
166174
} catch (IOException ex) {
175+
167176
throw new SparkPostException("Error with connection to " + path + ex.toString());
168177
}
169178
return conn;

libs/sparkpost-lib/src/test/java/com/sparkpost/EndPointTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public void testSimpleEndPoint() {
4343
endPoint.addParam("num_rcpt_errors", 3);
4444

4545
String result = endPoint.toString();
46-
Assert.assertEquals("transmissions?num_rcpt_errors=3", result);
46+
Assert.assertEquals("/transmissions?num_rcpt_errors=3", result);
4747
}
4848

4949
/**
@@ -55,7 +55,7 @@ public void testEndPointWithBoolean() {
5555
endPoint.addParam("myBool", new Boolean(true));
5656

5757
String result = endPoint.toString();
58-
Assert.assertEquals("transmissions?myBool=true", result);
58+
Assert.assertEquals("/transmissions?myBool=true", result);
5959
}
6060

6161
/**
@@ -67,7 +67,7 @@ public void testEndPointWithInteger() {
6767
endPoint.addParam("myInteger", new Integer(100));
6868

6969
String result = endPoint.toString();
70-
Assert.assertEquals("transmissions?myInteger=100", result);
70+
Assert.assertEquals("/transmissions?myInteger=100", result);
7171
}
7272

7373
/**
@@ -81,7 +81,7 @@ public void testEndPointWithMultipleParameters() {
8181
endPoint.addParam("MyInteger", new Integer(0));
8282

8383
String result = endPoint.toString();
84-
Assert.assertEquals("transmissions?num_rcpt_errors=3&myBool=false&MyInteger=0", result);
84+
Assert.assertEquals("/transmissions?num_rcpt_errors=3&myBool=false&MyInteger=0", result);
8585
}
8686

8787
}

0 commit comments

Comments
 (0)