Skip to content

Commit

Permalink
fix: set relative urls correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreaGiulianelli committed Dec 12, 2024
1 parent a1e9c78 commit 59a1f6b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.Optional;
import java.util.stream.Collectors;

import io.github.webbasedwodt.common.UriUtil;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonObjectBuilder;
import io.github.webbasedwodt.DTDManager.api.DTDManager;
Expand Down Expand Up @@ -81,7 +82,7 @@ public WoTDTDManager(
final PlatformManagementInterfaceReader platformManagementInterfaceReader
) {
this.configuration = configuration;
this.dittoBaseUrl = this.configuration.getDittoUrl().resolve("/api/2/things/").toString();
this.dittoBaseUrl = UriUtil.uriRelativeResolve(this.configuration.getDittoUrl(), "/api/2/things/").toString();
this.dittoThingId = configuration.getDittoThing().getEntityId().get().toString();
this.digitalTwinUri = configuration.getDigitalTwinUri();
this.ontology = configuration.getOntology();
Expand Down Expand Up @@ -162,7 +163,7 @@ public ThingDescription getDTD() {
.setType("application/tm+json")
.build());
links.add(Link.newBuilder()
.setHref(IRI.of(this.digitalTwinUri.resolve("/dtkg").toString()))
.setHref(IRI.of(UriUtil.uriRelativeResolve(this.digitalTwinUri, "dtkg").toString()))
.setRel(WoDTVocabulary.DTKG.getUri())
.build());

Expand Down Expand Up @@ -190,9 +191,12 @@ public ThingDescription getDTD() {
.setActions(Actions.from(this.actions.values()))
.setForms(List.of(RootFormElement.newBuilder()
.setHref(IRI.of(
URI.create(this.digitalTwinUri.toString().replaceFirst("([a-zA-Z][a-zA-Z0-9+.-]*):", "ws:"))
.resolve("/dtkg")
.toString()
UriUtil.uriRelativeResolve(
URI.create(
this.digitalTwinUri.toString().replaceFirst("([a-zA-Z][a-zA-Z0-9+.-]*):", "ws:")
),
"dtkg"
).toString()
))
.setSubprotocol("websocket")
.setOp(SingleRootFormElementOp.OBSERVEALLPROPERTIES)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,18 @@
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

import io.github.webbasedwodt.PlatformManagementInterface.api.PlatformManagementInterface;
import io.github.webbasedwodt.common.UriUtil;

/**
* Base implementation of the {@link PlatformManagementInterface}.
*/
public class BasePlatformManagementInterface implements PlatformManagementInterface {
private static final String PATH_TO_PLATFORM_WODT = "/wodt";
private static final String PATH_TO_PLATFORM_WODT = "wodt";
private static final int ACCEPTED_REQUEST_STATUS_CODE = 202;
private final URI digitalTwinUri;
private final Set<URI> platforms;
Expand Down Expand Up @@ -72,21 +71,20 @@ public void signalDigitalTwinDeletion() {
final HttpClient httpClient = HttpClient.newHttpClient();
this.platforms.forEach(platformUrl -> {
final HttpRequest httpRequest = HttpRequest.newBuilder()
.uri(getPlatformWoDT(platformUrl, this.digitalTwinUri.toString()))
.uri(getPlatformCachedDT(platformUrl, this.digitalTwinUri.toString()))
.DELETE()
.build();
httpClient.sendAsync(httpRequest, HttpResponse.BodyHandlers.ofString());
});
this.platforms.clear();
}

private URI getPlatformWoDT(final URI platformUrl, final String... path) {
if (path.length > 0) {
return platformUrl.resolve(
PATH_TO_PLATFORM_WODT
+ Arrays.stream(path).collect(Collectors.joining("/", "/", "")));
}
return platformUrl.resolve(PATH_TO_PLATFORM_WODT);
private URI getPlatformWoDT(final URI platformUrl) {
return UriUtil.uriRelativeResolve(platformUrl, PATH_TO_PLATFORM_WODT);
}

private URI getPlatformCachedDT(final URI platformUrl, final String resource) {
return URI.create(getPlatformWoDT(platformUrl) + "/" + resource);
}

@Override
Expand Down

0 comments on commit 59a1f6b

Please sign in to comment.