Search in sources :

Example 1 with ActiveItem

use of org.eclipse.smarthome.core.items.ActiveItem in project smarthome by eclipse.

the class ItemResource method createOrUpdateItem.

/**
 * Create or Update an item by supplying an item bean.
 *
 * @param itemname
 * @param item the item bean.
 * @return
 */
@PUT
@RolesAllowed({ Role.ADMIN })
@Path("/{itemname: [a-zA-Z_0-9]*}")
@Consumes(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Adds a new item to the registry or updates the existing item.")
@ApiResponses(value = { @ApiResponse(code = 200, message = "OK", response = String.class), @ApiResponse(code = 201, message = "Item created."), @ApiResponse(code = 400, message = "Item null."), @ApiResponse(code = 404, message = "Item not found."), @ApiResponse(code = 405, message = "Item not editable.") })
public Response createOrUpdateItem(@HeaderParam(HttpHeaders.ACCEPT_LANGUAGE) @ApiParam(value = "language") String language, @PathParam("itemname") @ApiParam(value = "item name", required = true) String itemname, @ApiParam(value = "item data", required = true) GroupItemDTO item) {
    final Locale locale = LocaleUtil.getLocale(language);
    // If we didn't get an item bean, then return!
    if (item == null) {
        return Response.status(Status.BAD_REQUEST).build();
    }
    ActiveItem newItem = createActiveItem(item);
    if (newItem == null) {
        logger.warn("Received HTTP PUT request at '{}' with an invalid item type '{}'.", uriInfo.getPath(), item.type);
        return Response.status(Status.BAD_REQUEST).build();
    }
    // Save the item
    if (getItem(itemname) == null) {
        // item does not yet exist, create it
        managedItemProvider.add(newItem);
        return getItemResponse(Status.CREATED, newItem, locale, null);
    } else if (managedItemProvider.get(itemname) != null) {
        // item already exists as a managed item, update it
        managedItemProvider.update(newItem);
        return getItemResponse(Status.OK, newItem, locale, null);
    } else {
        // Item exists but cannot be updated
        logger.warn("Cannot update existing item '{}', because is not managed.", itemname);
        return JSONResponse.createErrorResponse(Status.METHOD_NOT_ALLOWED, "Cannot update non-managed Item " + itemname);
    }
}
Also used : Locale(java.util.Locale) ActiveItem(org.eclipse.smarthome.core.items.ActiveItem) Path(javax.ws.rs.Path) RolesAllowed(javax.annotation.security.RolesAllowed) Consumes(javax.ws.rs.Consumes) ApiOperation(io.swagger.annotations.ApiOperation) PUT(javax.ws.rs.PUT) ApiResponses(io.swagger.annotations.ApiResponses)

Example 2 with ActiveItem

use of org.eclipse.smarthome.core.items.ActiveItem in project smarthome by eclipse.

the class ItemResource method addTag.

@PUT
@RolesAllowed({ Role.ADMIN })
@Path("/{itemname: [a-zA-Z_0-9]*}/tags/{tag}")
@ApiOperation(value = "Adds a tag to an item.")
@ApiResponses(value = { @ApiResponse(code = 200, message = "OK"), @ApiResponse(code = 404, message = "Item not found."), @ApiResponse(code = 405, message = "Item not editable.") })
public Response addTag(@PathParam("itemname") @ApiParam(value = "item name", required = true) String itemname, @PathParam("tag") @ApiParam(value = "tag", required = true) String tag) {
    Item item = getItem(itemname);
    if (item == null) {
        logger.info("Received HTTP PUT request at '{}' for the unknown item '{}'.", uriInfo.getPath(), itemname);
        return Response.status(Status.NOT_FOUND).build();
    }
    if (managedItemProvider.get(itemname) == null) {
        return Response.status(Status.METHOD_NOT_ALLOWED).build();
    }
    ((ActiveItem) item).addTag(tag);
    managedItemProvider.update(item);
    return Response.ok(null, MediaType.TEXT_PLAIN).build();
}
Also used : ActiveItem(org.eclipse.smarthome.core.items.ActiveItem) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) RollershutterItem(org.eclipse.smarthome.core.library.items.RollershutterItem) GroupItem(org.eclipse.smarthome.core.items.GroupItem) GenericItem(org.eclipse.smarthome.core.items.GenericItem) Item(org.eclipse.smarthome.core.items.Item) ActiveItem(org.eclipse.smarthome.core.items.ActiveItem) Path(javax.ws.rs.Path) RolesAllowed(javax.annotation.security.RolesAllowed) ApiOperation(io.swagger.annotations.ApiOperation) PUT(javax.ws.rs.PUT) ApiResponses(io.swagger.annotations.ApiResponses)

Example 3 with ActiveItem

use of org.eclipse.smarthome.core.items.ActiveItem in project smarthome by eclipse.

the class ItemResource method removeTag.

@DELETE
@RolesAllowed({ Role.ADMIN })
@Path("/{itemname: [a-zA-Z_0-9]*}/tags/{tag}")
@ApiOperation(value = "Removes a tag from an item.")
@ApiResponses(value = { @ApiResponse(code = 200, message = "OK"), @ApiResponse(code = 404, message = "Item not found."), @ApiResponse(code = 405, message = "Item not editable.") })
public Response removeTag(@PathParam("itemname") @ApiParam(value = "item name", required = true) String itemname, @PathParam("tag") @ApiParam(value = "tag", required = true) String tag) {
    Item item = getItem(itemname);
    if (item == null) {
        logger.info("Received HTTP DELETE request at '{}' for the unknown item '{}'.", uriInfo.getPath(), itemname);
        return Response.status(Status.NOT_FOUND).build();
    }
    if (managedItemProvider.get(itemname) == null) {
        return Response.status(Status.METHOD_NOT_ALLOWED).build();
    }
    ((ActiveItem) item).removeTag(tag);
    managedItemProvider.update(item);
    return Response.ok(null, MediaType.TEXT_PLAIN).build();
}
Also used : ActiveItem(org.eclipse.smarthome.core.items.ActiveItem) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) RollershutterItem(org.eclipse.smarthome.core.library.items.RollershutterItem) GroupItem(org.eclipse.smarthome.core.items.GroupItem) GenericItem(org.eclipse.smarthome.core.items.GenericItem) Item(org.eclipse.smarthome.core.items.Item) ActiveItem(org.eclipse.smarthome.core.items.ActiveItem) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) RolesAllowed(javax.annotation.security.RolesAllowed) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 4 with ActiveItem

use of org.eclipse.smarthome.core.items.ActiveItem in project smarthome by eclipse.

the class ItemResource method createOrUpdateItems.

/**
 * Create or Update a list of items by supplying a list of item beans.
 *
 * @param items the list of item beans.
 * @return array of status information for each item bean
 */
@PUT
@RolesAllowed({ Role.ADMIN })
@Consumes(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Adds a list of items to the registry or updates the existing items.")
@ApiResponses(value = { @ApiResponse(code = 200, message = "OK", response = String.class), @ApiResponse(code = 400, message = "Item list is null.") })
public Response createOrUpdateItems(@ApiParam(value = "array of item data", required = true) GroupItemDTO[] items) {
    // If we didn't get an item list bean, then return!
    if (items == null) {
        return Response.status(Status.BAD_REQUEST).build();
    }
    List<GroupItemDTO> wrongTypes = new ArrayList<>();
    List<ActiveItem> activeItems = new ArrayList<>();
    for (GroupItemDTO item : items) {
        ActiveItem newItem = createActiveItem(item);
        if (newItem == null) {
            wrongTypes.add(item);
        } else {
            activeItems.add(newItem);
        }
    }
    List<ActiveItem> createdItems = new ArrayList<>();
    List<ActiveItem> updatedItems = new ArrayList<>();
    List<ActiveItem> failedItems = new ArrayList<>();
    for (ActiveItem activeItem : activeItems) {
        String itemName = activeItem.getName();
        if (getItem(itemName) == null) {
            // item does not yet exist, create it
            managedItemProvider.add(activeItem);
            createdItems.add(activeItem);
        } else if (managedItemProvider.get(itemName) != null) {
            // item already exists as a managed item, update it
            managedItemProvider.update(activeItem);
            updatedItems.add(activeItem);
        } else {
            // Item exists but cannot be updated
            logger.warn("Cannot update existing item '{}', because it is not managed.", itemName);
            failedItems.add(activeItem);
        }
    }
    // build response
    List<JsonObject> responseList = new ArrayList<>();
    for (GroupItemDTO item : wrongTypes) {
        responseList.add(buildStatusObject(item.name, "error", "Received HTTP PUT request at '" + uriInfo.getPath() + "' with an invalid item type '" + item.type + "'."));
    }
    for (ActiveItem item : failedItems) {
        responseList.add(buildStatusObject(item.getName(), "error", "Cannot update non-managed item"));
    }
    for (ActiveItem item : createdItems) {
        responseList.add(buildStatusObject(item.getName(), "created", null));
    }
    for (ActiveItem item : updatedItems) {
        responseList.add(buildStatusObject(item.getName(), "updated", null));
    }
    return JSONResponse.createResponse(Status.OK, responseList, null);
}
Also used : GroupItemDTO(org.eclipse.smarthome.core.items.dto.GroupItemDTO) ArrayList(java.util.ArrayList) ActiveItem(org.eclipse.smarthome.core.items.ActiveItem) JsonObject(com.google.gson.JsonObject) RolesAllowed(javax.annotation.security.RolesAllowed) Consumes(javax.ws.rs.Consumes) ApiOperation(io.swagger.annotations.ApiOperation) PUT(javax.ws.rs.PUT) ApiResponses(io.swagger.annotations.ApiResponses)

Aggregations

ApiOperation (io.swagger.annotations.ApiOperation)4 ApiResponses (io.swagger.annotations.ApiResponses)4 RolesAllowed (javax.annotation.security.RolesAllowed)4 ActiveItem (org.eclipse.smarthome.core.items.ActiveItem)4 PUT (javax.ws.rs.PUT)3 Path (javax.ws.rs.Path)3 Consumes (javax.ws.rs.Consumes)2 GenericItem (org.eclipse.smarthome.core.items.GenericItem)2 GroupItem (org.eclipse.smarthome.core.items.GroupItem)2 Item (org.eclipse.smarthome.core.items.Item)2 RollershutterItem (org.eclipse.smarthome.core.library.items.RollershutterItem)2 SwitchItem (org.eclipse.smarthome.core.library.items.SwitchItem)2 JsonObject (com.google.gson.JsonObject)1 ArrayList (java.util.ArrayList)1 Locale (java.util.Locale)1 DELETE (javax.ws.rs.DELETE)1 GroupItemDTO (org.eclipse.smarthome.core.items.dto.GroupItemDTO)1