-
Notifications
You must be signed in to change notification settings - Fork 214
/
UriForLocationHeaderSupplier.java
92 lines (76 loc) · 3.22 KB
/
UriForLocationHeaderSupplier.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
* Copyright (c) 2020 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.gateway.service.endpoints.actors;
import java.util.function.Supplier;
import org.eclipse.ditto.base.model.entity.id.EntityId;
import org.eclipse.ditto.json.JsonPointer;
import org.apache.pekko.http.javadsl.model.HttpMethod;
import org.apache.pekko.http.javadsl.model.HttpRequest;
import org.apache.pekko.http.javadsl.model.Query;
import org.apache.pekko.http.javadsl.model.Uri;
/**
* Returns the URI for the location header for HTTP responses when a resource was created.
*/
final class UriForLocationHeaderSupplier implements Supplier<Uri> {
private final HttpRequest httpRequest;
private final EntityId entityId;
private final JsonPointer resourcePath;
UriForLocationHeaderSupplier(final HttpRequest httpRequest, final EntityId entityId,
final JsonPointer resourcePath) {
this.httpRequest = httpRequest;
this.entityId = entityId;
this.resourcePath = resourcePath;
}
@Override
public Uri get() {
final Uri requestUri = httpRequest.getUri().query(Query.EMPTY); // strip query params
if (isRequestIdempotent()) {
return requestUri;
}
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) {
return removeTrailingSlash(requestUri.substring(0, uriIdIndex));
}
return requestUri;
}
private int getIndexOfEntityId(final String requestUri) {
return requestUri.indexOf(entityId.toString());
}
private static String removeTrailingSlash(final String createdLocationUri) {
if (createdLocationUri.endsWith("/")) {
return createdLocationUri.substring(0, createdLocationUri.length() - 1);
}
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;
}
}