Skip to content

Commit 80b89f3

Browse files
committed
#1959: fix base and href of generated WoT ThingDescriptions
Signed-off-by: Thomas Jäckle <[email protected]>
1 parent c572154 commit 80b89f3

File tree

1 file changed

+31
-28
lines changed

1 file changed

+31
-28
lines changed

wot/integration/src/main/java/org/eclipse/ditto/wot/integration/generator/DefaultWotThingDescriptionGenerator.java

+31-28
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import javax.annotation.Nullable;
3636
import javax.annotation.concurrent.Immutable;
3737

38+
import org.apache.pekko.actor.ActorSystem;
3839
import org.eclipse.ditto.base.model.exceptions.DittoRuntimeException;
3940
import org.eclipse.ditto.base.model.headers.DittoHeaderDefinition;
4041
import org.eclipse.ditto.base.model.headers.DittoHeaders;
@@ -98,8 +99,6 @@
9899
import org.eclipse.ditto.wot.model.WotThingModelInvalidException;
99100
import org.eclipse.ditto.wot.model.WotThingModelPlaceholderUnresolvedException;
100101

101-
import org.apache.pekko.actor.ActorSystem;
102-
103102
/**
104103
* Default Ditto specific implementation of {@link WotThingDescriptionGenerator}.
105104
*/
@@ -311,9 +310,9 @@ private void removeTmRequired(final ThingModel.Builder builder) {
311310
private void addBase(final ThingDescription.Builder thingDescriptionBuilder,
312311
final ThingId thingId, @Nullable final String featureId) {
313312
if (null != featureId) {
314-
final String featurePath = "/features/" + featureId;
315-
thingDescriptionBuilder.setId(IRI.of("urn:" + thingId + featurePath))
316-
.setBase(IRI.of(buildThingIdBasePath(thingId) + featurePath));
313+
final String featurePath = "features/" + featureId;
314+
thingDescriptionBuilder.setId(IRI.of("urn:" + thingId + "/" + featurePath))
315+
.setBase(IRI.of(buildThingIdBasePath(thingId) + featurePath + "/"));
317316
} else {
318317
thingDescriptionBuilder.setId(IRI.of("urn:" + thingId))
319318
.setBase(IRI.of(buildThingIdBasePath(thingId)));
@@ -331,7 +330,7 @@ private void addInstanceVersion(final ThingDescription.Builder thingDescriptionB
331330
}
332331

333332
private String buildThingIdBasePath(final ThingId thingId) {
334-
return toThingDescriptionConfig.getBasePrefix() + "/api/2/things/" + thingId;
333+
return toThingDescriptionConfig.getBasePrefix() + "/api/2/things/" + thingId + "/";
335334
}
336335

337336
private void addThingDescriptionLinks(final ThingDescription.Builder tdBuilder, final URL tmUrl,
@@ -372,7 +371,7 @@ private void convertThingDescriptionTmSubmodelLinksToItems(final ThingDescriptio
372371
return Link.newBuilder()
373372
.setRel("item")
374373
.setType(ContentType.APPLICATION_TD_JSON.getValue())
375-
.setHref(IRI.of("/features/" + link.getValue(TM_SUBMODEL_INSTANCE_NAME)
374+
.setHref(IRI.of("features/" + link.getValue(TM_SUBMODEL_INSTANCE_NAME)
376375
.filter(JsonValue::isString)
377376
.map(JsonValue::asString)
378377
.orElseThrow(() -> WotThingModelInvalidException
@@ -436,11 +435,12 @@ private void generateRootForms(final ThingModel thingModel,
436435
DITTO_FIELDS_URI_VARIABLE,
437436
DittoHeaderDefinition.CHANNEL.getKey(),
438437
DittoHeaderDefinition.TIMEOUT.getKey());
438+
final String hrefWithoutLeadingSlash = propertiesPath.toString().substring(1);
439439
if (thingModelForms.isPresent()) {
440440
tdBuilder.setForms(thingModelForms.get()
441441
.stream()
442442
.map(rfe -> RootFormElement.fromJson(rfe.toBuilder()
443-
.setHref(IRI.of(propertiesPath))
443+
.setHref(IRI.of(hrefWithoutLeadingSlash))
444444
.setAdditionalResponses(provideAdditionalResponses())
445445
.build()
446446
))
@@ -449,27 +449,27 @@ private void generateRootForms(final ThingModel thingModel,
449449
} else {
450450
tdBuilder.setForms(List.of(
451451
buildRootFormElement(SingleRootFormElementOp.READALLPROPERTIES,
452-
propertiesPath + readUriVariablesParams,
452+
hrefWithoutLeadingSlash + readUriVariablesParams,
453453
"GET"
454454
),
455455
buildRootFormElement(SingleRootFormElementOp.READMULTIPLEPROPERTIES,
456-
propertiesPath + readMultiplePropertiesUriVariablesParams,
456+
hrefWithoutLeadingSlash + readMultiplePropertiesUriVariablesParams,
457457
"GET"
458458
),
459459
buildRootFormElement(SingleRootFormElementOp.WRITEALLPROPERTIES,
460-
propertiesPath + writeUriVariablesParams,
460+
hrefWithoutLeadingSlash + writeUriVariablesParams,
461461
"PUT"
462462
),
463463
buildRootFormElement(SingleRootFormElementOp.WRITEMULTIPLEPROPERTIES,
464-
propertiesPath + writeUriVariablesParams,
464+
hrefWithoutLeadingSlash + writeUriVariablesParams,
465465
"PATCH",
466466
builder -> builder.setContentType(ContentType.APPLICATION_MERGE_PATCH_JSON.getValue())
467467
),
468468
buildRootFormElement(List.of(
469469
SingleRootFormElementOp.OBSERVEALLPROPERTIES,
470470
SingleRootFormElementOp.UNOBSERVEALLPROPERTIES
471471
),
472-
propertiesPath,
472+
hrefWithoutLeadingSlash,
473473
"GET",
474474
builder -> builder
475475
.setSubprotocol(SUBPROTOCOL_SSE)
@@ -479,7 +479,7 @@ private void generateRootForms(final ThingModel thingModel,
479479
SingleRootFormElementOp.SUBSCRIBEALLEVENTS,
480480
SingleRootFormElementOp.UNSUBSCRIBEALLEVENTS
481481
),
482-
JsonPointer.of("/outbox/messages"),
482+
"outbox/messages",
483483
"GET",
484484
builder -> builder
485485
.setSubprotocol(SUBPROTOCOL_SSE)
@@ -512,7 +512,7 @@ private RootFormElement buildRootFormElement(final SingleRootFormElementOp op,
512512
}
513513

514514
private RootFormElement buildRootFormElement(final Collection<SingleRootFormElementOp> ops,
515-
final JsonPointer hrefPointer,
515+
final CharSequence hrefPointer,
516516
final String htvMethodName,
517517
final Consumer<RootFormElement.Builder> builderConsumer
518518
) {
@@ -557,11 +557,12 @@ private void generatePropertiesForms(final ThingModel thingModel,
557557
final String readUriVariablesParams = provideUriVariablesBag(
558558
DittoHeaderDefinition.CHANNEL.getKey(),
559559
DittoHeaderDefinition.TIMEOUT.getKey());
560+
final String hrefWithoutLeadingSlash = propertyHref.toString().substring(1);
560561
return property.getForms()
561562
.map(propertyFormElements -> property.toBuilder()
562563
.setForms(PropertyForms.of(propertyFormElements.stream()
563564
.map(pfe -> pfe.toBuilder()
564-
.setHref(IRI.of(propertyHref))
565+
.setHref(IRI.of(hrefWithoutLeadingSlash))
565566
.setAdditionalResponses(provideAdditionalResponses())
566567
.build()
567568
)
@@ -573,18 +574,18 @@ private void generatePropertiesForms(final ThingModel thingModel,
573574
final List<PropertyFormElement> formElements = new ArrayList<>();
574575
if (!property.isWriteOnly()) {
575576
formElements.add(buildPropertyFormElement(SinglePropertyFormElementOp.READPROPERTY,
576-
propertyHref + readUriVariablesParams,
577+
hrefWithoutLeadingSlash + readUriVariablesParams,
577578
"GET"
578579
));
579580
}
580581

581582
if (!property.isReadOnly()) {
582583
formElements.add(buildPropertyFormElement(SinglePropertyFormElementOp.WRITEPROPERTY,
583-
propertyHref + writeUriVariablesParams,
584+
hrefWithoutLeadingSlash + writeUriVariablesParams,
584585
"PUT"
585586
));
586587
formElements.add(buildPropertyFormElement(SinglePropertyFormElementOp.WRITEPROPERTY,
587-
propertyHref + writeUriVariablesParams,
588+
hrefWithoutLeadingSlash + writeUriVariablesParams,
588589
"PATCH",
589590
builder -> builder
590591
.setContentType(
@@ -599,7 +600,7 @@ private void generatePropertiesForms(final ThingModel thingModel,
599600
SinglePropertyFormElementOp.OBSERVEPROPERTY,
600601
SinglePropertyFormElementOp.UNOBSERVEPROPERTY
601602
),
602-
propertyHref,
603+
hrefWithoutLeadingSlash,
603604
"GET",
604605
builder -> builder
605606
.setSubprotocol(SUBPROTOCOL_SSE)
@@ -725,7 +726,7 @@ private void generateActionsForms(final ThingModel thingModel,
725726
.map(actions -> actions.map(actionEntry -> {
726727
final String actionName = actionEntry.getKey();
727728
final Action action = actionEntry.getValue();
728-
final JsonPointer actionHref = JsonPointer.of("/inbox/messages/" + actionName);
729+
final String actionHrefWithoutLeadingSlash = "inbox/messages/" + actionName;
729730
final String uriVariablesParams = provideUriVariablesBag(
730731
DittoHeaderDefinition.TIMEOUT.getKey(),
731732
DittoHeaderDefinition.RESPONSE_REQUIRED.getKey());
@@ -734,7 +735,7 @@ private void generateActionsForms(final ThingModel thingModel,
734735
.setSynchronous(true)
735736
.setForms(ActionForms.of(actionFormElements.stream()
736737
.map(afe -> afe.toBuilder()
737-
.setHref(IRI.of(actionHref))
738+
.setHref(IRI.of(actionHrefWithoutLeadingSlash))
738739
.setAdditionalResponses(provideAdditionalResponses())
739740
.build()
740741
)
@@ -746,7 +747,7 @@ private void generateActionsForms(final ThingModel thingModel,
746747
.setSynchronous(true)
747748
.setForms(ActionForms.of(List.of(
748749
buildActionFormElement(SingleActionFormElementOp.INVOKEACTION,
749-
actionHref + uriVariablesParams)
750+
actionHrefWithoutLeadingSlash + uriVariablesParams)
750751
)))
751752
.build()
752753
);
@@ -788,12 +789,12 @@ private void generateEventsForms(final ThingModel thingModel, final ThingDescrip
788789
.map(events -> events.map(eventEntry -> {
789790
final String eventName = eventEntry.getKey();
790791
final Event event = eventEntry.getValue();
791-
final JsonPointer eventHref = JsonPointer.of("/outbox/messages/" + eventName);
792+
final String eventHrefWithoutLeadingSlash = "outbox/messages/" + eventName;
792793
return event.getForms()
793794
.map(eventFormElements -> event.toBuilder()
794795
.setForms(EventForms.of(eventFormElements.stream()
795796
.map(efe -> efe.toBuilder()
796-
.setHref(IRI.of(eventHref))
797+
.setHref(IRI.of(eventHrefWithoutLeadingSlash))
797798
.setAdditionalResponses(provideAdditionalResponses())
798799
.build()
799800
)
@@ -803,7 +804,8 @@ private void generateEventsForms(final ThingModel thingModel, final ThingDescrip
803804
)
804805
.orElseGet(() -> event.toBuilder()
805806
.setForms(EventForms.of(List.of(
806-
buildEventFormElement(SingleEventFormElementOp.SUBSCRIBEEVENT, eventHref)
807+
buildEventFormElement(SingleEventFormElementOp.SUBSCRIBEEVENT,
808+
eventHrefWithoutLeadingSlash)
807809
)))
808810
.build()
809811
);
@@ -814,7 +816,7 @@ private void generateEventsForms(final ThingModel thingModel, final ThingDescrip
814816
}
815817

816818
private EventFormElement buildEventFormElement(final SingleEventFormElementOp op,
817-
final JsonPointer hrefPointer
819+
final CharSequence hrefPointer
818820
) {
819821
return EventFormElement.newBuilder()
820822
.setOp(op)
@@ -887,7 +889,8 @@ private Optional<JsonValue> resolvePlaceholder(final String value, @Nullable fin
887889
final String placeholderToResolve = matcher.group(TM_PLACEHOLDER_PL_GROUP).trim();
888890
if (null != modelPlaceholders) {
889891
return modelPlaceholders.getValue(placeholderToResolve)
890-
.or(() -> Optional.ofNullable(toThingDescriptionConfig.getPlaceholders().get(placeholderToResolve)));
892+
.or(() -> Optional.ofNullable(
893+
toThingDescriptionConfig.getPlaceholders().get(placeholderToResolve)));
891894
} else {
892895
return Optional.ofNullable(toThingDescriptionConfig.getPlaceholders().get(placeholderToResolve));
893896
}

0 commit comments

Comments
 (0)