2 * Copyright (c) 2010-2022 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.upnpcontrol.internal.queue;
15 import static org.openhab.binding.upnpcontrol.internal.UpnpControlBindingConstants.FAVORITE_FILE_EXTENSION;
18 import java.io.IOException;
19 import java.nio.charset.StandardCharsets;
20 import java.nio.file.Files;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
27 import com.google.gson.Gson;
28 import com.google.gson.JsonParseException;
31 * Class used to model favorites, with and without full meta data. If metadata exists, it will be in UpnpEntry.
33 * @author Mark Herwege - Initial contribution
36 public class UpnpFavorite {
38 private final Logger logger = LoggerFactory.getLogger(UpnpFavorite.class);
41 * Inner class used for streaming a favorite to disk as a json object.
44 private class Favorite {
50 Favorite(String name, String uri, @Nullable UpnpEntry entry) {
57 private volatile Favorite favorite;
59 private final Gson gson = new Gson();
62 * Create a new favorite from provide URI and details. If {@link UpnpEntry} entry is null, no metadata will be
63 * available with the favorite.
69 public UpnpFavorite(String name, String uri, @Nullable UpnpEntry entry) {
70 favorite = new Favorite(name, uri, entry);
74 * Create a new favorite from a file copy stored on disk. If the favorite cannot be read from disk, an empty
75 * favorite is created.
80 public UpnpFavorite(String name, @Nullable String path) {
81 String fileName = path + name + FAVORITE_FILE_EXTENSION;
82 File file = new File(fileName);
86 if ((path != null) && file.exists()) {
88 logger.debug("Reading contents of {}", file.getAbsolutePath());
89 final byte[] contents = Files.readAllBytes(file.toPath());
90 final String json = new String(contents, StandardCharsets.UTF_8);
92 fav = gson.fromJson(json, Favorite.class);
93 } catch (JsonParseException | UnsupportedOperationException e) {
94 logger.debug("JsonParseException reading {}: {}", file.toPath(), e.getMessage(), e);
95 } catch (IOException e) {
96 logger.debug("IOException reading favorite {} from {}", name, file.toPath());
100 favorite = (fav != null) ? fav : new Favorite(name, "", null);
104 * @return name of favorite
106 public String getName() {
107 return favorite.name;
111 * @return URI of favorite
113 public String getUri() {
118 * @return {@link UpnpEntry} known details of favorite
121 public UpnpEntry getUpnpEntry() {
122 return favorite.entry;
126 * Save the favorite to disk.
131 public void saveFavorite(String name, @Nullable String path) {
136 String fileName = path + name + FAVORITE_FILE_EXTENSION;
137 File file = new File(fileName);
140 // ensure full path exists
141 file.getParentFile().mkdirs();
143 String json = gson.toJson(favorite);
144 final byte[] contents = json.getBytes(StandardCharsets.UTF_8);
145 Files.write(file.toPath(), contents);
146 } catch (IOException e) {
147 logger.debug("IOException writing favorite {} to {}", name, file.toPath());