]> git.basschouten.com Git - openhab-addons.git/blob
0ad6ffab0d1d45d01a594cbd83fb274f95e5e361
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.freeboxos.internal.api;
14
15 import java.io.ByteArrayInputStream;
16 import java.io.InputStream;
17 import java.net.URI;
18 import java.util.Locale;
19 import java.util.Set;
20 import java.util.concurrent.ExecutionException;
21 import java.util.concurrent.TimeUnit;
22 import java.util.concurrent.TimeoutException;
23
24 import javax.ws.rs.core.UriBuilder;
25
26 import org.eclipse.jdt.annotation.NonNullByDefault;
27 import org.eclipse.jdt.annotation.Nullable;
28 import org.eclipse.jetty.client.HttpClient;
29 import org.eclipse.jetty.client.api.ContentResponse;
30 import org.eclipse.jetty.client.api.Request;
31 import org.eclipse.jetty.http.HttpMethod;
32 import org.eclipse.jetty.http.HttpStatus;
33 import org.eclipse.jetty.http.HttpStatus.Code;
34 import org.openhab.core.i18n.TranslationProvider;
35 import org.openhab.core.io.net.http.HttpClientFactory;
36 import org.openhab.core.ui.icon.AbstractResourceIconProvider;
37 import org.openhab.core.ui.icon.IconProvider;
38 import org.openhab.core.ui.icon.IconSet;
39 import org.osgi.service.component.annotations.Activate;
40 import org.osgi.service.component.annotations.Component;
41 import org.osgi.service.component.annotations.Reference;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 /**
46  * The {@link FreeboxOsIconProvider} delivers icons provided by FreeboxOS
47  *
48  * @author GaĆ«l L'hopital - Initial contribution
49  */
50 @NonNullByDefault
51 @Component(immediate = true, service = { IconProvider.class })
52 public class FreeboxOsIconProvider extends AbstractResourceIconProvider {
53
54     private final Logger logger = LoggerFactory.getLogger(FreeboxOsIconProvider.class);
55     private static final int REQUEST_TIMEOUT_MS = 8000;
56
57     private final HttpClient httpClient;
58     private final UriBuilder uriBuilder;
59
60     @Activate
61     public FreeboxOsIconProvider(final @Reference TranslationProvider i18nProvider,
62             final @Reference HttpClientFactory httpClientFactory) {
63         super(i18nProvider);
64         this.httpClient = httpClientFactory.getCommonHttpClient();
65         this.uriBuilder = UriBuilder.fromPath("/").scheme("http").host(FreeboxTlsCertificateProvider.DEFAULT_NAME)
66                 .path("resources/images/home/pictos");
67     }
68
69     @Override
70     public Set<IconSet> getIconSets(@Nullable Locale locale) {
71         return Set.of();
72     }
73
74     @Override
75     protected Integer getPriority() {
76         return 4;
77     }
78
79     @Override
80     protected @Nullable InputStream getResource(String iconSetId, String resourceName) {
81         URI uri = uriBuilder.clone().path(resourceName).build();
82         Request request = httpClient.newRequest(uri).method(HttpMethod.GET).timeout(REQUEST_TIMEOUT_MS,
83                 TimeUnit.MILLISECONDS);
84
85         try {
86             ContentResponse response = request.send();
87             if (HttpStatus.getCode(response.getStatus()) == Code.OK) {
88                 return new ByteArrayInputStream(response.getContent());
89             }
90         } catch (InterruptedException | TimeoutException | ExecutionException e) {
91             logger.warn("Error getting icon {}: {}", resourceName, e.getMessage());
92         }
93         return null;
94     }
95
96     @Override
97     protected boolean hasResource(String iconSetId, String resourceName) {
98         return resourceName.contains(".png") && getResource(iconSetId, resourceName) != null;
99     }
100 }