Skip to content

Commit 736a10a

Browse files
committed
Remove the dedicated API for discovery result
It is now combined with the API for one thing. Signed-off-by: Laurent Garnier <[email protected]>
1 parent aa71770 commit 736a10a

File tree

1 file changed

+16
-40
lines changed
  • bundles/org.openhab.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/internal/fileformat

1 file changed

+16
-40
lines changed

bundles/org.openhab.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/internal/fileformat/FileFormatResource.java

+16-40
Original file line numberDiff line numberDiff line change
@@ -239,10 +239,10 @@ public Response createFileFormatForAllThings(final @Context HttpHeaders httpHead
239239
@RolesAllowed({ Role.ADMIN })
240240
@Path("/things/{thingUID}")
241241
@Produces("text/vnd.openhab.dsl.thing")
242-
@Operation(operationId = "createFileFormatForThing", summary = "Create file format for an existing thing in registry.", security = {
242+
@Operation(operationId = "createFileFormatForThing", summary = "Create file format for an existing thing in things or discovery registry.", security = {
243243
@SecurityRequirement(name = "oauth2", scopes = { "admin" }) }, responses = {
244244
@ApiResponse(responseCode = "200", description = "OK", content = @Content(mediaType = "text/vnd.openhab.dsl.thing", schema = @Schema(example = "Thing binding:type:idBridge:id \"Label\" @ \"Location\" (binding:typeBridge:idBridge) [stringParam=\"my value\", booleanParam=true, decimalParam=2.5]"))),
245-
@ApiResponse(responseCode = "404", description = "Thing not found in registry."),
245+
@ApiResponse(responseCode = "404", description = "Thing not found in things or discovery registry or thing type not found."),
246246
@ApiResponse(responseCode = "415", description = "Unsupported media type.") })
247247
public Response createFileFormatForThing(final @Context HttpHeaders httpHeaders,
248248
@DefaultValue("true") @QueryParam("hideDefaultParameters") @Parameter(description = "hide the configuration parameters having the default value") boolean hideDefaultParameters,
@@ -257,50 +257,26 @@ public Response createFileFormatForThing(final @Context HttpHeaders httpHeaders,
257257
ThingUID aThingUID = new ThingUID(thingUID);
258258
Thing thing = thingRegistry.get(aThingUID);
259259
if (thing == null) {
260-
return Response.status(Response.Status.NOT_FOUND)
261-
.entity("Thing with UID '" + thingUID + "' not found in the things registry!").build();
260+
List<DiscoveryResult> results = inbox.getAll().stream().filter(forThingUID(new ThingUID(thingUID)))
261+
.toList();
262+
if (results.isEmpty()) {
263+
return Response.status(Response.Status.NOT_FOUND)
264+
.entity("Thing with UID '" + thingUID + "' not found in the things or discovery registry!")
265+
.build();
266+
}
267+
DiscoveryResult result = results.get(0);
268+
ThingType thingType = thingTypeRegistry.getThingType(result.getThingTypeUID());
269+
if (thingType == null) {
270+
return Response.status(Response.Status.NOT_FOUND)
271+
.entity("Thing type with UID '" + result.getThingTypeUID() + "' does not exist!").build();
272+
}
273+
thing = simulateThing(result, thingType);
262274
}
263275
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
264276
generator.generateFileFormat(outputStream, List.of(thing), hideDefaultParameters);
265277
return Response.ok(new String(outputStream.toByteArray())).build();
266278
}
267279

268-
@GET
269-
@Path("/things/inbox/{thingUID}")
270-
@Produces("text/vnd.openhab.dsl.thing")
271-
@Operation(operationId = "createFileFormatForDiscoveryResult", summary = "Create file format for an existing thing in discovey registry.", security = {
272-
@SecurityRequirement(name = "oauth2", scopes = { "admin" }) }, responses = {
273-
@ApiResponse(responseCode = "200", description = "OK", content = @Content(mediaType = "text/vnd.openhab.dsl.thing", schema = @Schema(example = "Thing binding:type:idBridge:id \"Label\" (binding:typeBridge:idBridge) [stringParam=\"my value\", booleanParam=true, decimalParam=2.5]"))),
274-
@ApiResponse(responseCode = "404", description = "Discovery result not found in the inbox or thing type not found."),
275-
@ApiResponse(responseCode = "415", description = "Unsupported media type.") })
276-
public Response createFileFormatForDiscoveryResult(final @Context HttpHeaders httpHeaders,
277-
@DefaultValue("true") @QueryParam("hideDefaultParameters") @Parameter(description = "hide the configuration parameters having the default value") boolean hideDefaultParameters,
278-
@PathParam("thingUID") @Parameter(description = "thingUID") String thingUID) {
279-
String acceptHeader = httpHeaders.getHeaderString(HttpHeaders.ACCEPT);
280-
String format = "text/vnd.openhab.dsl.thing".equals(acceptHeader) ? "DSL" : null;
281-
ThingFileGenerator generator = format == null ? null : thingFileGenerators.get(format);
282-
if (generator == null) {
283-
return Response.status(Response.Status.UNSUPPORTED_MEDIA_TYPE)
284-
.entity("Unsupported media type '" + acceptHeader + "'!").build();
285-
}
286-
287-
List<DiscoveryResult> results = inbox.getAll().stream().filter(forThingUID(new ThingUID(thingUID))).toList();
288-
if (results.isEmpty()) {
289-
return Response.status(Response.Status.NOT_FOUND)
290-
.entity("Discovery result for thing with UID '" + thingUID + "' not found in the inbox!").build();
291-
}
292-
DiscoveryResult result = results.get(0);
293-
ThingType thingType = thingTypeRegistry.getThingType(result.getThingTypeUID());
294-
if (thingType == null) {
295-
return Response.status(Response.Status.NOT_FOUND)
296-
.entity("Thing type with UID '" + result.getThingTypeUID() + "' does not exist!").build();
297-
}
298-
299-
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
300-
generator.generateFileFormat(outputStream, List.of(simulateThing(result, thingType)), hideDefaultParameters);
301-
return Response.ok(new String(outputStream.toByteArray())).build();
302-
}
303-
304280
/*
305281
* Get all the metadata for a list of items including channel links mapped to metadata in the namespace "channel"
306282
*/

0 commit comments

Comments
 (0)