]> git.basschouten.com Git - openhab-addons.git/blob
8c5067226dfac89179ffbe38fdd1eb657b70daf2
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.TimeoutException;
22
23 import javax.ws.rs.core.UriBuilder;
24
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;
43
44 /**
45  * The {@link FreeboxOsIconProvider} delivers icons provided by FreeboxOS
46  *
47  * @author GaĆ«l L'hopital - Initial contribution
48  */
49 @NonNullByDefault
50 @Component(immediate = true, service = { IconProvider.class })
51 public class FreeboxOsIconProvider extends AbstractResourceIconProvider {
52
53     private final Logger logger = LoggerFactory.getLogger(FreeboxOsIconProvider.class);
54
55     private final HttpClient httpClient;
56     private final UriBuilder uriBuilder;
57
58     @Activate
59     public FreeboxOsIconProvider(final @Reference TranslationProvider i18nProvider,
60             final @Reference HttpClientFactory httpClientFactory) {
61         super(i18nProvider);
62         this.httpClient = httpClientFactory.getCommonHttpClient();
63         this.uriBuilder = UriBuilder.fromPath("/").scheme("http").host(FreeboxTlsCertificateProvider.DEFAULT_NAME)
64                 .path("resources/images/home/pictos");
65     }
66
67     @Override
68     public Set<IconSet> getIconSets(@Nullable Locale locale) {
69         return Set.of();
70     }
71
72     @Override
73     protected Integer getPriority() {
74         return 4;
75     }
76
77     @Override
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);
81
82         try {
83             ContentResponse response = request.send();
84             if (HttpStatus.getCode(response.getStatus()) == Code.OK) {
85                 return new ByteArrayInputStream(response.getContent());
86             }
87         } catch (InterruptedException | TimeoutException | ExecutionException e) {
88             logger.warn("Error getting icon {}: {}", resourceName, e.getMessage());
89         }
90         return null;
91     }
92
93     @Override
94     protected boolean hasResource(String iconSetId, String resourceName) {
95         return resourceName.contains(".png") && getResource(iconSetId, resourceName) != null;
96     }
97 }