From ea68ae0163c9876ee510cdb53f6671ad9ec9f75f Mon Sep 17 00:00:00 2001 From: J-N-K Date: Sun, 13 Mar 2022 16:27:26 +0100 Subject: [PATCH] [rest] Allow binary MediaType for RawType item states (#2825) Signed-off-by: Jan N. Klug --- .../rest/core/internal/item/ItemResource.java | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/bundles/org.openhab.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/internal/item/ItemResource.java b/bundles/org.openhab.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/internal/item/ItemResource.java index 5c7ea1c4150..9b6e9eb2505 100644 --- a/bundles/org.openhab.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/internal/item/ItemResource.java +++ b/bundles/org.openhab.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/internal/item/ItemResource.java @@ -13,14 +13,17 @@ package org.openhab.core.io.rest.core.internal.item; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.Objects; import java.util.Optional; import java.util.Set; import java.util.function.Predicate; +import java.util.stream.Collectors; import java.util.stream.Stream; import javax.annotation.security.RolesAllowed; @@ -75,6 +78,7 @@ import org.openhab.core.library.items.RollershutterItem; import org.openhab.core.library.items.SwitchItem; import org.openhab.core.library.types.OnOffType; +import org.openhab.core.library.types.RawType; import org.openhab.core.library.types.UpDownType; import org.openhab.core.types.Command; import org.openhab.core.types.State; @@ -279,6 +283,47 @@ public Response getPlainItemState(@PathParam("itemname") @Parameter(description } } + /** + * + * @param itemname + * @return + */ + @GET + @RolesAllowed({ Role.USER, Role.ADMIN }) + @Path("/{itemname: [a-zA-Z_0-9]+}/state") + @Operation(operationId = "getItemState", summary = "Gets the state of an item.", responses = { + @ApiResponse(responseCode = "200", description = "OK"), + @ApiResponse(responseCode = "400", description = "Item state is not RawType"), + @ApiResponse(responseCode = "404", description = "Item not found"), + @ApiResponse(responseCode = "415", description = "MediaType not supported by item state") }) + public Response getBinaryItemState(@HeaderParam("Accept") @Nullable String mediaType, + @PathParam("itemname") @Parameter(description = "item name") String itemname) { + List acceptedMediaTypes = Arrays.stream(Objects.requireNonNullElse(mediaType, "").split(",")) + .peek(String::trim).collect(Collectors.toList()); + // get item + Item item = getItem(itemname); + + // if it exists + if (item != null) { + State state = item.getState(); + if (state instanceof RawType) { + String mimeType = ((RawType) state).getMimeType(); + byte[] data = ((RawType) state).getBytes(); + if ((acceptedMediaTypes.contains("image/*") && mimeType.startsWith("image/")) + || acceptedMediaTypes.contains(mimeType)) { + return Response.ok(data).type(mimeType).build(); + } else if (acceptedMediaTypes.contains(MediaType.APPLICATION_OCTET_STREAM)) { + return Response.ok(data).type(MediaType.APPLICATION_OCTET_STREAM).build(); + } else { + return Response.status(Status.UNSUPPORTED_MEDIA_TYPE).build(); + } + } + return Response.status(Status.BAD_REQUEST).build(); + } else { + return Response.status(Status.NOT_FOUND).build(); + } + } + @PUT @RolesAllowed({ Role.USER, Role.ADMIN }) @Path("/{itemname: [a-zA-Z_0-9]+}/state")