Search in sources :

Example 6 with Metadata

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

the class ItemResource method getItems.

@GET
@RolesAllowed({ Role.USER, Role.ADMIN })
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Get all available items.", response = EnrichedItemDTO.class, responseContainer = "List")
@ApiResponses(value = { @ApiResponse(code = 200, message = "OK", response = EnrichedItemDTO.class, responseContainer = "List") })
public Response getItems(@HeaderParam(HttpHeaders.ACCEPT_LANGUAGE) @ApiParam(value = "language") @Nullable String language, @QueryParam("type") @ApiParam(value = "item type filter", required = false) @Nullable String type, @QueryParam("tags") @ApiParam(value = "item tag filter", required = false) @Nullable String tags, @QueryParam("metadata") @ApiParam(value = "metadata selector", required = false) @Nullable String namespaceSelector, @DefaultValue("false") @QueryParam("recursive") @ApiParam(value = "get member items recursively", required = false) boolean recursive, @QueryParam("fields") @ApiParam(value = "limit output to the given fields (comma separated)", required = false) @Nullable String fields) {
    final Locale locale = localeService.getLocale(language);
    final Set<String> namespaces = splitAndFilterNamespaces(namespaceSelector, locale);
    logger.debug("Received HTTP GET request at '{}'", uriInfo.getPath());
    Stream<EnrichedItemDTO> itemStream = // 
    getItems(type, tags).stream().map(// 
    item -> EnrichedItemDTOMapper.map(item, recursive, null, uriInfo.getBaseUri(), locale)).peek(// 
    dto -> addMetadata(dto, namespaces, null)).peek(dto -> dto.editable = isEditable(dto.name));
    itemStream = dtoMapper.limitToFields(itemStream, fields);
    return Response.ok(new Stream2JSONInputStream(itemStream)).build();
}
Also used : Locale(java.util.Locale) JsonObject(com.google.gson.JsonObject) LocaleService(org.eclipse.smarthome.io.rest.LocaleService) RolesAllowed(javax.annotation.security.RolesAllowed) Produces(javax.ws.rs.Produces) DTOMapper(org.eclipse.smarthome.io.rest.DTOMapper) Path(javax.ws.rs.Path) LoggerFactory(org.slf4j.LoggerFactory) OnOffType(org.eclipse.smarthome.core.library.types.OnOffType) ApiParam(io.swagger.annotations.ApiParam) Command(org.eclipse.smarthome.core.types.Command) Metadata(org.eclipse.smarthome.core.items.Metadata) ApiOperation(io.swagger.annotations.ApiOperation) MediaType(javax.ws.rs.core.MediaType) QueryParam(javax.ws.rs.QueryParam) Consumes(javax.ws.rs.Consumes) Nullable(org.eclipse.jdt.annotation.Nullable) Locale(java.util.Locale) UpDownType(org.eclipse.smarthome.core.library.types.UpDownType) EnrichedItemDTO(org.eclipse.smarthome.io.rest.core.item.EnrichedItemDTO) Map(java.util.Map) DefaultValue(javax.ws.rs.DefaultValue) ManagedItemProvider(org.eclipse.smarthome.core.items.ManagedItemProvider) HeaderParam(javax.ws.rs.HeaderParam) State(org.eclipse.smarthome.core.types.State) DELETE(javax.ws.rs.DELETE) RESTResource(org.eclipse.smarthome.io.rest.RESTResource) Context(javax.ws.rs.core.Context) NonNullByDefault(org.eclipse.jdt.annotation.NonNullByDefault) MetadataKey(org.eclipse.smarthome.core.items.MetadataKey) GroupItemDTO(org.eclipse.smarthome.core.items.dto.GroupItemDTO) Predicate(java.util.function.Predicate) Collection(java.util.Collection) Set(java.util.Set) EventPublisher(org.eclipse.smarthome.core.events.EventPublisher) ReferencePolicy(org.osgi.service.component.annotations.ReferencePolicy) Role(org.eclipse.smarthome.core.auth.Role) EnrichedItemDTOMapper(org.eclipse.smarthome.io.rest.core.item.EnrichedItemDTOMapper) List(java.util.List) ItemBuilderFactory(org.eclipse.smarthome.core.items.ItemBuilderFactory) Stream(java.util.stream.Stream) HttpHeaders(javax.ws.rs.core.HttpHeaders) Response(javax.ws.rs.core.Response) MetadataDTO(org.eclipse.smarthome.core.items.dto.MetadataDTO) JSONResponse(org.eclipse.smarthome.io.rest.JSONResponse) WebApplicationException(javax.ws.rs.WebApplicationException) UriInfo(javax.ws.rs.core.UriInfo) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) PathParam(javax.ws.rs.PathParam) RollershutterItem(org.eclipse.smarthome.core.library.items.RollershutterItem) GET(javax.ws.rs.GET) TypeParser(org.eclipse.smarthome.core.types.TypeParser) HashMap(java.util.HashMap) ApiResponses(io.swagger.annotations.ApiResponses) GroupItem(org.eclipse.smarthome.core.items.GroupItem) ArrayList(java.util.ArrayList) Component(org.osgi.service.component.annotations.Component) Api(io.swagger.annotations.Api) Status(javax.ws.rs.core.Response.Status) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) POST(javax.ws.rs.POST) Logger(org.slf4j.Logger) GenericItem(org.eclipse.smarthome.core.items.GenericItem) ItemEventFactory(org.eclipse.smarthome.core.items.events.ItemEventFactory) EnrichedGroupItemDTO(org.eclipse.smarthome.io.rest.core.item.EnrichedGroupItemDTO) Stream2JSONInputStream(org.eclipse.smarthome.io.rest.Stream2JSONInputStream) ItemNotFoundException(org.eclipse.smarthome.core.items.ItemNotFoundException) Item(org.eclipse.smarthome.core.items.Item) ItemRegistry(org.eclipse.smarthome.core.items.ItemRegistry) MetadataRegistry(org.eclipse.smarthome.core.items.MetadataRegistry) ReferenceCardinality(org.osgi.service.component.annotations.ReferenceCardinality) ItemDTOMapper(org.eclipse.smarthome.core.items.dto.ItemDTOMapper) ApiResponse(io.swagger.annotations.ApiResponse) PUT(javax.ws.rs.PUT) Reference(org.osgi.service.component.annotations.Reference) EnrichedItemDTO(org.eclipse.smarthome.io.rest.core.item.EnrichedItemDTO) Stream2JSONInputStream(org.eclipse.smarthome.io.rest.Stream2JSONInputStream) RolesAllowed(javax.annotation.security.RolesAllowed) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 7 with Metadata

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

the class ItemResourceOSGiTest method testRemoveMetadata_unmanagedMetadata.

@Test
public void testRemoveMetadata_unmanagedMetadata() {
    MetadataProvider provider = mock(MetadataProvider.class);
    when(provider.getAll()).thenReturn(Collections.singleton(new Metadata(new MetadataKey("namespace", ITEM_NAME1), "some value", null)));
    registerService(provider);
    Response response = itemResource.removeMetadata(ITEM_NAME1, "namespace");
    assertEquals(409, response.getStatus());
}
Also used : Response(javax.ws.rs.core.Response) MetadataProvider(org.eclipse.smarthome.core.items.MetadataProvider) Metadata(org.eclipse.smarthome.core.items.Metadata) MetadataKey(org.eclipse.smarthome.core.items.MetadataKey) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 8 with Metadata

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

the class MetadataRegistryImplTest method testGet.

@Test
public void testGet() throws Exception {
    MetadataKey key = new MetadataKey("namespace", "itemName");
    registry.added(managedProvider, new Metadata(key, "value", Collections.emptyMap()));
    registry.added(managedProvider, new Metadata(new MetadataKey("other", "itemName"), "other", Collections.emptyMap()));
    registry.added(managedProvider, new Metadata(new MetadataKey("namespace", "other"), "other", Collections.emptyMap()));
    Metadata res = registry.get(key);
    assertNotNull(res);
    assertEquals("value", res.getValue());
    assertEquals("namespace", res.getUID().getNamespace());
    assertEquals("itemName", res.getUID().getItemName());
}
Also used : Metadata(org.eclipse.smarthome.core.items.Metadata) MetadataKey(org.eclipse.smarthome.core.items.MetadataKey) Test(org.junit.Test)

Example 9 with Metadata

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

the class AbstractItemEventSubscriberOSGiTest method setup.

@Before
public void setup() {
    initMocks(this);
    eventPublisher = getService(EventPublisher.class);
    assertNotNull(eventPublisher);
    when(itemProvider.getAll()).thenReturn(Collections.singletonList(new SwitchItem(ITEM_NAME)));
    registerService(itemProvider);
    EventSubscriber itemEventSubscriber = new AbstractItemEventSubscriber() {

        @Override
        protected void receiveCommand(ItemCommandEvent event) {
            commandEvent = event;
        }

        @Override
        protected void receiveUpdate(ItemStateEvent event) {
            updateEvent = event;
        }
    };
    registerService(itemEventSubscriber, EventSubscriber.class.getName());
    when(mockMetadataProvider.getAll()).thenReturn(Collections.singletonList(new Metadata(new MetadataKey("autoupdate", ITEM_NAME), Boolean.toString(false), null)));
    registerService(mockMetadataProvider);
}
Also used : EventSubscriber(org.eclipse.smarthome.core.events.EventSubscriber) EventPublisher(org.eclipse.smarthome.core.events.EventPublisher) Metadata(org.eclipse.smarthome.core.items.Metadata) MetadataKey(org.eclipse.smarthome.core.items.MetadataKey) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) Before(org.junit.Before)

Example 10 with Metadata

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

the class GenericMetadataProvider method addMetadata.

/**
 * Adds metadata to this provider
 *
 * @param bindingType
 * @param itemName
 * @param configuration
 */
public void addMetadata(String bindingType, String itemName, String value, @Nullable Map<String, Object> configuration) {
    MetadataKey key = new MetadataKey(bindingType, itemName);
    Metadata md = new Metadata(key, value, configuration);
    try {
        lock.writeLock().lock();
        metadata.add(md);
    } finally {
        lock.writeLock().unlock();
    }
    notifyListenersAboutAddedElement(md);
}
Also used : Metadata(org.eclipse.smarthome.core.items.Metadata) MetadataKey(org.eclipse.smarthome.core.items.MetadataKey)

Aggregations

Metadata (org.eclipse.smarthome.core.items.Metadata)15 MetadataKey (org.eclipse.smarthome.core.items.MetadataKey)14 Test (org.junit.Test)6 SwitchItem (org.eclipse.smarthome.core.library.items.SwitchItem)5 GenericItem (org.eclipse.smarthome.core.items.GenericItem)4 GroupItem (org.eclipse.smarthome.core.items.GroupItem)4 Item (org.eclipse.smarthome.core.items.Item)4 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 HashMap (java.util.HashMap)3 JsonObject (com.google.gson.JsonObject)2 ApiOperation (io.swagger.annotations.ApiOperation)2 ApiResponses (io.swagger.annotations.ApiResponses)2 RolesAllowed (javax.annotation.security.RolesAllowed)2 Consumes (javax.ws.rs.Consumes)2 PUT (javax.ws.rs.PUT)2 Path (javax.ws.rs.Path)2 Response (javax.ws.rs.core.Response)2 EventPublisher (org.eclipse.smarthome.core.events.EventPublisher)2 MetadataDTO (org.eclipse.smarthome.core.items.dto.MetadataDTO)2