Skip to content
This repository has been archived by the owner on Jan 26, 2022. It is now read-only.

Commit

Permalink
Merge pull request #77 from cloud-of-things/fix-double-slashes
Browse files Browse the repository at this point in the history
remove double slashes in url because they are leading to 404
  • Loading branch information
Andyck authored Nov 7, 2018
2 parents 6bd19fe + 30258c5 commit 1ebb794
Showing 1 changed file with 16 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public String doRequestWithIdResponse(String json, String api, String contentTyp
.addHeader("Authorization", "Basic " + encodedAuthString)
.addHeader("Content-Type", contentType)
.addHeader("Accept", accept)
.url(host + "/" + api)
.url(host + "/" + trimSlashes(api))
.post(body)
.build();
response = client.newCall(request).execute();
Expand Down Expand Up @@ -132,7 +132,7 @@ public String doPostRequest(String json, String api, String contentType, String
RequestBody body = RequestBody.create(MediaType.parse(contentType), json);
Request.Builder requestBuilder = new Request.Builder()
.addHeader("Authorization", "Basic " + encodedAuthString)
.url(host + "/" + api)
.url(host + "/" + trimSlashes(api))
.post(body);

if (contentType != null) {
Expand Down Expand Up @@ -178,7 +178,7 @@ public String doFormUpload(String file, String name, String api, String contentT
Request request = new Request.Builder()
.addHeader("Authorization", "Basic " + encodedAuthString)
.addHeader("Content-Type", contentType)
.url(host + "/" + api)
.url(host + "/" + trimSlashes(api))
.post(body)
.build();

Expand Down Expand Up @@ -221,7 +221,7 @@ public String doFormUpload(byte[][] files, String[] names, String api) {
Request request = new Request.Builder()
.addHeader("Authorization", "Basic " + encodedAuthString)
.addHeader("Content-Type", "text/foo")
.url(host + "/" + api)
.url(host + "/" + trimSlashes(api))
.post(bodyBuilder.build())
.build();

Expand Down Expand Up @@ -272,7 +272,7 @@ public String doRealTimePollingRequest(String json, String api, String contentTy
.addHeader("Authorization", "Basic " + encodedAuthString)
.addHeader("Content-Type", contentType)
.addHeader("Accept", contentType)
.url(host + "/" + api)
.url(host + "/" + trimSlashes(api))
.post(body)
.build();

Expand Down Expand Up @@ -446,7 +446,7 @@ public String getResponse(String id, String api, String accept) {
public byte[] getResponseInBytes(String id, String api, String accept){
Request.Builder requestBuilder = new Request.Builder()
.addHeader("Authorization", "Basic " + encodedAuthString)
.url(host + "/" + removeTrailingSlash(api) + "/" + id);
.url(host + "/" + trimSlashes(api) + "/" + id);

if (accept != null) {
requestBuilder.addHeader("Accept", accept);
Expand Down Expand Up @@ -478,7 +478,7 @@ public byte[] getResponseInBytes(String id, String api, String accept){
public String getResponse(String api) {
Request request = new Request.Builder()
.addHeader("Authorization", "Basic " + encodedAuthString)
.url(host + "/" + api)
.url(host + "/" + trimSlashes(api))
.build();

Response response = null;
Expand Down Expand Up @@ -530,7 +530,7 @@ public String doPutRequest(String data, String path, String contentType, String
Request.Builder requestBuilder = new Request.Builder()
.addHeader("Authorization", "Basic " + encodedAuthString)
.addHeader("Content-Type", contentType)
.url(host + "/" + path)
.url(host + "/" + trimSlashes(path))
.put(requestBody);

if (accept != null) {
Expand Down Expand Up @@ -567,7 +567,7 @@ public String doPutRequestWithIdResponseInBytes(byte[] data, String path, String
Request.Builder requestBuilder = new Request.Builder()
.addHeader("Authorization", "Basic " + encodedAuthString)
.addHeader("Content-Type", contentType)
.url(host + "/" + path)
.url(host + "/" + trimSlashes(path))
.put(requestBody);

Request request = requestBuilder.build();
Expand Down Expand Up @@ -609,7 +609,7 @@ public String doPutRequestWithIdResponse(String data, String path, String conten
public void delete(String id, String api) {
Request request = new Request.Builder()
.addHeader("Authorization", "Basic " + encodedAuthString)
.url(host + "/" + removeTrailingSlash(api) + "/" + id)
.url(host + "/" + trimSlashes(api) + "/" + id)
.delete()
.build();
Response response = null;
Expand All @@ -631,7 +631,7 @@ public void delete(String id, String api) {
public void deleteBy(final String filter, final String api) {
final Request request = new Request.Builder()
.addHeader("Authorization", "Basic " + encodedAuthString)
.url(host + "/" + removeTrailingSlash(api) + "?" + filter)
.url(host + "/" + trimSlashes(api) + "?" + filter)
.delete()
.build();
Response response = null;
Expand Down Expand Up @@ -700,9 +700,13 @@ private void closeResponseBodyIfResponseAndBodyNotNull(final Response response)
}
}

private String removeTrailingSlash(String path) {
private String trimSlashes(String path) {
Objects.requireNonNull(path);

if(path.startsWith("/")) {
path = path.substring(1);
}

if(path.endsWith("/")) {
path = path.substring(0, path.length()-1);
}
Expand Down

0 comments on commit 1ebb794

Please sign in to comment.