2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.freeboxos.internal.api;
15 import java.io.ByteArrayInputStream;
16 import java.io.InputStream;
18 import java.util.Locale;
20 import java.util.concurrent.ExecutionException;
21 import java.util.concurrent.TimeoutException;
23 import javax.ws.rs.core.UriBuilder;
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.eclipse.jdt.annotation.Nullable;
27 import org.eclipse.jetty.client.HttpClient;
28 import org.eclipse.jetty.client.api.ContentResponse;
29 import org.eclipse.jetty.client.api.Request;
30 import org.eclipse.jetty.http.HttpMethod;
31 import org.eclipse.jetty.http.HttpStatus;
32 import org.eclipse.jetty.http.HttpStatus.Code;
33 import org.openhab.core.i18n.TranslationProvider;
34 import org.openhab.core.io.net.http.HttpClientFactory;
35 import org.openhab.core.ui.icon.AbstractResourceIconProvider;
36 import org.openhab.core.ui.icon.IconProvider;
37 import org.openhab.core.ui.icon.IconSet;
38 import org.osgi.service.component.annotations.Activate;
39 import org.osgi.service.component.annotations.Component;
40 import org.osgi.service.component.annotations.Reference;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
45 * The {@FreeboxOsIconProvider} delivers icons provided by FreeboxOS
47 * @author Gaƫl L'hopital - Initial contribution
50 @Component(immediate = true, service = { IconProvider.class })
51 public class FreeboxOsIconProvider extends AbstractResourceIconProvider {
53 private final Logger logger = LoggerFactory.getLogger(FreeboxOsIconProvider.class);
55 private final HttpClient httpClient;
56 private final UriBuilder uriBuilder;
59 public FreeboxOsIconProvider(final @Reference TranslationProvider i18nProvider,
60 final @Reference HttpClientFactory httpClientFactory) {
62 this.httpClient = httpClientFactory.getCommonHttpClient();
63 this.uriBuilder = UriBuilder.fromPath("/").scheme("http").host(FreeboxTlsCertificateProvider.DEFAULT_NAME)
64 .path("resources/images/home/pictos");
68 public Set<IconSet> getIconSets(@Nullable Locale locale) {
73 protected Integer getPriority() {
78 protected @Nullable InputStream getResource(String iconSetId, String resourceName) {
79 URI uri = uriBuilder.clone().path(resourceName).build();
80 Request request = httpClient.newRequest(uri).method(HttpMethod.GET);
83 ContentResponse response = request.send();
84 if (HttpStatus.getCode(response.getStatus()) == Code.OK) {
85 return new ByteArrayInputStream(response.getContent());
87 } catch (InterruptedException | TimeoutException | ExecutionException e) {
88 logger.warn("Error getting icon {}: {}", resourceName, e.getMessage());
94 protected boolean hasResource(String iconSetId, String resourceName) {
95 return resourceName.contains(".png") && getResource(iconSetId, resourceName) != null;