Skip to content

Commit

Permalink
[REST] Prevent internal server error on invalid link requests on REST…
Browse files Browse the repository at this point in the history
… API (#2179)

Closes openhab/openhab-webui#878

Signed-off-by: Kai Kreuzer <kai@openhab.org>
  • Loading branch information
kaikreuzer authored Feb 6, 2021
1 parent b99aa44 commit 87211d2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import org.osgi.service.jaxrs.whiteboard.JaxrsWhiteboardConstants;
import org.osgi.service.jaxrs.whiteboard.propertytypes.JaxrsApplicationSelect;
import org.osgi.service.jaxrs.whiteboard.propertytypes.JaxrsExtension;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Trap exceptions.
Expand All @@ -35,10 +37,12 @@
@NonNullByDefault
public class JSONResponseExceptionMapper implements ExceptionMapper<@NonNull Exception> {

private final Logger logger = LoggerFactory.getLogger(JSONResponseExceptionMapper.class);
private final ExceptionMapper<Exception> delegate = new JSONResponse.ExceptionMapper();

@Override
public Response toResponse(Exception e) {
logger.error("Unexpected exception occurred while processing REST request.", e);
return delegate.toResponse(e);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -141,17 +141,23 @@ public Response getLink(@PathParam("itemName") @Parameter(description = "itemNam
public Response link(@PathParam("itemName") @Parameter(description = "itemName") String itemName,
@PathParam("channelUID") @Parameter(description = "channelUID") String channelUid,
@Parameter(description = "link data") @Nullable ItemChannelLinkDTO bean) {
ChannelUID uid;
try {
uid = new ChannelUID(channelUid);
} catch (IllegalArgumentException e) {
return Response.status(Status.BAD_REQUEST).build();
}
ItemChannelLink link;
if (bean == null) {
link = new ItemChannelLink(itemName, new ChannelUID(channelUid), new Configuration());
link = new ItemChannelLink(itemName, uid, new Configuration());
} else {
if (bean.channelUID != null && !bean.channelUID.equals(channelUid)) {
return Response.status(Status.BAD_REQUEST).build();
}
if (bean.itemName != null && !bean.itemName.equals(itemName)) {
return Response.status(Status.BAD_REQUEST).build();
}
link = new ItemChannelLink(itemName, new ChannelUID(channelUid), new Configuration(bean.configuration));
link = new ItemChannelLink(itemName, uid, new Configuration(bean.configuration));
}
if (itemChannelLinkRegistry.get(link.getUID()) == null) {
itemChannelLinkRegistry.add(link);
Expand All @@ -174,7 +180,13 @@ public Response link(@PathParam("itemName") @Parameter(description = "itemName")
@ApiResponse(responseCode = "405", description = "Link not editable.") })
public Response unlink(@PathParam("itemName") @Parameter(description = "itemName") String itemName,
@PathParam("channelUID") @Parameter(description = "channelUID") String channelUid) {
String linkId = AbstractLink.getIDFor(itemName, new ChannelUID(channelUid));
ChannelUID uid;
try {
uid = new ChannelUID(channelUid);
} catch (IllegalArgumentException e) {
return Response.status(Status.BAD_REQUEST).build();
}
String linkId = AbstractLink.getIDFor(itemName, uid);
if (itemChannelLinkRegistry.get(linkId) == null) {
String message = "Link " + linkId + " does not exist!";
return JSONResponse.createResponse(Status.NOT_FOUND, null, message);
Expand Down

0 comments on commit 87211d2

Please sign in to comment.