]> git.basschouten.com Git - openhab-addons.git/blob
2fd5b2d57e199df75b99e6ece427b4f04bbee750
[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.mielecloud.internal.config.servlet;
14
15 import java.io.FileNotFoundException;
16 import java.io.IOException;
17 import java.io.InputStream;
18 import java.net.URL;
19 import java.nio.charset.StandardCharsets;
20 import java.util.Scanner;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.osgi.framework.BundleContext;
24
25 /**
26  * Provides access to resource files for servlets.
27  *
28  * @author Björn Lange - Initial Contribution
29  */
30 @NonNullByDefault
31 public final class ResourceLoader {
32     private static final String BEGINNING_OF_INPUT = "\\A";
33
34     private final String basePath;
35     private final BundleContext bundleContext;
36
37     /**
38      * Creates a new {@link ResourceLoader}.
39      *
40      * @param basePath The base path to use for loading. A trailing {@code "/"} is removed.
41      * @param bundleContext {@link BundleContext} to load from.
42      */
43     public ResourceLoader(String basePath, BundleContext bundleContext) {
44         this.basePath = removeTrailingSlashes(basePath);
45         this.bundleContext = bundleContext;
46     }
47
48     private String removeTrailingSlashes(String value) {
49         String ret = value;
50         while (ret.endsWith("/")) {
51             ret = ret.substring(0, ret.length() - 1);
52         }
53         return ret;
54     }
55
56     /**
57      * Opens a resource relative to the base path.
58      *
59      * @param filename The filename of the resource to load.
60      * @return A stream reading from the resource file.
61      * @throws FileNotFoundException If the requested resource file cannot be found.
62      * @throws IOException If an error occurs while opening a stream to the resource.
63      */
64     public InputStream openResource(String filename) throws IOException {
65         URL url = bundleContext.getBundle().getEntry(basePath + "/" + filename);
66         if (url == null) {
67             throw new FileNotFoundException("Cannot find '" + filename + "' relative to '" + basePath + "'");
68         }
69
70         return url.openStream();
71     }
72
73     /**
74      * Loads the contents of a resource file as UTF-8 encoded {@link String}.
75      *
76      * @param filename The filename of the resource to load.
77      * @return The contents of the file.
78      * @throws FileNotFoundException If the requested resource file cannot be found.
79      * @throws IOException If an error occurs while opening a stream to the resource or reading from it.
80      */
81     public String loadResourceAsString(String filename) throws IOException {
82         try (Scanner scanner = new Scanner(openResource(filename), StandardCharsets.UTF_8.name())) {
83             return scanner.useDelimiter(BEGINNING_OF_INPUT).next();
84         }
85     }
86 }