]> git.basschouten.com Git - openhab-addons.git/blob
cbea1ab6ae80d7e9b62b6aee041859fc0dee0aed
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.upnpcontrol.internal.queue;
14
15 import static org.openhab.binding.upnpcontrol.internal.UpnpControlBindingConstants.FAVORITE_FILE_EXTENSION;
16
17 import java.io.File;
18 import java.io.IOException;
19 import java.nio.charset.StandardCharsets;
20 import java.nio.file.Files;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 import com.google.gson.Gson;
28 import com.google.gson.JsonParseException;
29
30 /**
31  * Class used to model favorites, with and without full meta data. If metadata exists, it will be in UpnpEntry.
32  *
33  * @author Mark Herwege - Initial contribution
34  */
35 @NonNullByDefault
36 public class UpnpFavorite {
37
38     private final Logger logger = LoggerFactory.getLogger(UpnpFavorite.class);
39
40     /**
41      * Inner class used for streaming a favorite to disk as a json object.
42      *
43      */
44     private class Favorite {
45         String name;
46         String uri;
47         @Nullable
48         UpnpEntry entry;
49
50         Favorite(String name, String uri, @Nullable UpnpEntry entry) {
51             this.name = name;
52             this.uri = uri;
53             this.entry = entry;
54         }
55     }
56
57     private volatile Favorite favorite;
58
59     private final Gson gson = new Gson();
60
61     /**
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.
64      *
65      * @param name
66      * @param uri
67      * @param entry
68      */
69     public UpnpFavorite(String name, String uri, @Nullable UpnpEntry entry) {
70         favorite = new Favorite(name, uri, entry);
71     }
72
73     /**
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.
76      *
77      * @param name
78      * @param path
79      */
80     public UpnpFavorite(String name, @Nullable String path) {
81         String fileName = path + name + FAVORITE_FILE_EXTENSION;
82         File file = new File(fileName);
83
84         Favorite fav = null;
85
86         if ((path != null) && file.exists()) {
87             try {
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);
91
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());
97             }
98         }
99
100         favorite = (fav != null) ? fav : new Favorite(name, "", null);
101     }
102
103     /**
104      * @return name of favorite
105      */
106     public String getName() {
107         return favorite.name;
108     }
109
110     /**
111      * @return URI of favorite
112      */
113     public String getUri() {
114         return favorite.uri;
115     }
116
117     /**
118      * @return {@link UpnpEntry} known details of favorite
119      */
120     @Nullable
121     public UpnpEntry getUpnpEntry() {
122         return favorite.entry;
123     }
124
125     /**
126      * Save the favorite to disk.
127      *
128      * @param name
129      * @param path
130      */
131     public void saveFavorite(String name, @Nullable String path) {
132         if (path == null) {
133             return;
134         }
135
136         String fileName = path + name + FAVORITE_FILE_EXTENSION;
137         File file = new File(fileName);
138
139         try {
140             // ensure full path exists
141             file.getParentFile().mkdirs();
142
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());
148         }
149     }
150 }