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.shelly.internal.manager;
15 import static org.openhab.binding.shelly.internal.manager.ShellyManagerConstants.IMAGE_PATH;
16 import static org.openhab.binding.shelly.internal.util.ShellyUtils.substringAfter;
18 import java.io.IOException;
19 import java.io.InputStream;
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;
34 * {@link ShellyManagerImageLoader} implements the Shelly Manager's download proxy for images (load them from bundle)
36 * @author Markus Michels - Initial contribution
39 public class ShellyManagerImageLoader extends ShellyManagerPage {
40 private final Logger logger = LoggerFactory.getLogger(ShellyManagerImageLoader.class);
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);
49 public ShellyMgrResponse generateContent(String path, Map<String, String[]> parameters) throws ShellyApiException {
50 return loadImage(substringAfter(path, ShellyManagerConstants.SHELLY_MGR_IMAGES_URI + "/"));
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();
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");
64 } catch (IOException | RuntimeException e) {
65 logger.debug("ShellyManager: Unable to read {} from bundle resources!", image, e);
68 return new ShellyMgrResponse("Unable to read " + image + " from bundle resources!", HttpStatus.NOT_FOUND_404);