Skip to content

Commit

Permalink
Fix double slashes in location header
Browse files Browse the repository at this point in the history
Currently requests ending with slash lead to duplicate slashes in location header (i.e. POST /things/ leads to /things//entityId).
Fixing by removing trailing slashes from base URI if present.

Signed-off-by: David Schwilk <david.schwilk@bosch.io>
  • Loading branch information
DerSchwilk committed Sep 16, 2021
1 parent b00c611 commit 535f6b8
Showing 1 changed file with 18 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

import java.util.function.Supplier;

import org.eclipse.ditto.json.JsonPointer;
import org.eclipse.ditto.base.model.entity.id.EntityId;
import org.eclipse.ditto.json.JsonPointer;

import akka.http.javadsl.model.HttpMethod;
import akka.http.javadsl.model.HttpRequest;
Expand All @@ -31,7 +31,9 @@ final class UriForLocationHeaderSupplier implements Supplier<Uri> {
private final EntityId entityId;
private final JsonPointer resourcePath;

UriForLocationHeaderSupplier(final HttpRequest httpRequest, final EntityId entityId, final JsonPointer resourcePath) {
UriForLocationHeaderSupplier(final HttpRequest httpRequest, final EntityId entityId,
final JsonPointer resourcePath) {

this.httpRequest = httpRequest;
this.entityId = entityId;
this.resourcePath = resourcePath;
Expand All @@ -43,15 +45,22 @@ public Uri get() {
if (isRequestIdempotent()) {
return requestUri;
}
return Uri.create(removeTrailingSlash(getLocationUriString(removeEntityId(requestUri.toString()))))
.query(Query.EMPTY);
final var uriWithoutEntityIdString = prepareUriForLocationHeaderConcat(requestUri);
final var locationHeaderString = concatLocationHeader(uriWithoutEntityIdString);
return Uri.create(locationHeaderString).query(Query.EMPTY);
}

private boolean isRequestIdempotent() {
final HttpMethod requestMethod = httpRequest.method();
return requestMethod.isIdempotent();
}

private String prepareUriForLocationHeaderConcat(final Uri requestUri) {
final var uriWithoutEntityId = removeEntityId(requestUri.toString());
// handles requests without entityId with trailing slash (i.e. POST api/things/)
return removeTrailingSlash(uriWithoutEntityId);
}

private String removeEntityId(final String requestUri) {
final int uriIdIndex = getIndexOfEntityId(requestUri);
if (0 < uriIdIndex) {
Expand All @@ -71,6 +80,11 @@ private static String removeTrailingSlash(final String createdLocationUri) {
return createdLocationUri;
}

private String concatLocationHeader(final String requestUri) {
final var locationUriString = getLocationUriString(requestUri);
return removeTrailingSlash(locationUriString);
}

private String getLocationUriString(final String requestUri) {
return requestUri + "/" + entityId + resourcePath;
}
Expand Down

0 comments on commit 535f6b8

Please sign in to comment.