]> git.basschouten.com Git - openhab-addons.git/blob
6fcde300b8786f9f78ef4d5b662635e24d893939
[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.shelly.internal.manager;
14
15 import static org.openhab.binding.shelly.internal.manager.ShellyManagerConstants.IMAGE_PATH;
16 import static org.openhab.binding.shelly.internal.util.ShellyUtils.substringAfter;
17
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.util.Map;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jetty.client.HttpClient;
24 import org.eclipse.jetty.http.HttpStatus;
25 import org.openhab.binding.shelly.internal.ShellyHandlerFactory;
26 import org.openhab.binding.shelly.internal.api.ShellyApiException;
27 import org.openhab.binding.shelly.internal.handler.ShellyManagerInterface;
28 import org.openhab.binding.shelly.internal.provider.ShellyTranslationProvider;
29 import org.osgi.service.cm.ConfigurationAdmin;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * {@link ShellyManagerImageLoader} implements the Shelly Manager's download proxy for images (load them from bundle)
35  *
36  * @author Markus Michels - Initial contribution
37  */
38 @NonNullByDefault
39 public class ShellyManagerImageLoader extends ShellyManagerPage {
40     private final Logger logger = LoggerFactory.getLogger(ShellyManagerImageLoader.class);
41
42     public ShellyManagerImageLoader(ConfigurationAdmin configurationAdmin,
43             ShellyTranslationProvider translationProvider, HttpClient httpClient, String localIp, int localPort,
44             ShellyHandlerFactory handlerFactory) {
45         super(configurationAdmin, translationProvider, httpClient, localIp, localPort, handlerFactory);
46     }
47
48     @Override
49     public ShellyMgrResponse generateContent(String path, Map<String, String[]> parameters) throws ShellyApiException {
50         return loadImage(substringAfter(path, ShellyManagerConstants.SHELLY_MGR_IMAGES_URI + "/"));
51     }
52
53     protected ShellyMgrResponse loadImage(String image) throws ShellyApiException {
54         String file = IMAGE_PATH + image;
55         logger.trace("Read Image from {}", file);
56         ClassLoader cl = ShellyManagerInterface.class.getClassLoader();
57         if (cl != null) {
58             try (InputStream inputStream = cl.getResourceAsStream(file)) {
59                 if (inputStream != null) {
60                     byte[] buf = new byte[inputStream.available()];
61                     inputStream.read(buf);
62                     return new ShellyMgrResponse(buf, HttpStatus.OK_200, "image/png");
63                 }
64             } catch (IOException | RuntimeException e) {
65                 logger.debug("ShellyManager: Unable to read {} from bundle resources!", image, e);
66             }
67         }
68         return new ShellyMgrResponse("Unable to read " + image + " from bundle resources!", HttpStatus.NOT_FOUND_404);
69     }
70 }