Commit | Line | Data |
---|---|---|
f433d153 NR |
1 | package be.nikiroo.fanfix.library; |
2 | ||
3 | import java.io.File; | |
4 | import java.io.IOException; | |
5 | import java.io.InputStream; | |
6 | import java.net.URL; | |
7 | import java.util.ArrayList; | |
8 | import java.util.HashMap; | |
9 | import java.util.List; | |
10 | import java.util.Map; | |
11 | ||
12 | import org.json.JSONArray; | |
13 | import org.json.JSONObject; | |
14 | ||
15 | import be.nikiroo.fanfix.Instance; | |
c5103223 | 16 | import be.nikiroo.fanfix.data.Chapter; |
f433d153 NR |
17 | import be.nikiroo.fanfix.data.JsonIO; |
18 | import be.nikiroo.fanfix.data.MetaData; | |
c5103223 NR |
19 | import be.nikiroo.fanfix.data.Paragraph; |
20 | import be.nikiroo.fanfix.data.Paragraph.ParagraphType; | |
f433d153 NR |
21 | import be.nikiroo.fanfix.data.Story; |
22 | import be.nikiroo.utils.IOUtils; | |
23 | import be.nikiroo.utils.Image; | |
24 | import be.nikiroo.utils.Progress; | |
5ee0fc14 | 25 | import be.nikiroo.utils.Version; |
f433d153 NR |
26 | |
27 | /** | |
28 | * This {@link BasicLibrary} will access a remote server to list the available | |
29 | * stories, and download the ones you try to load to the local directory | |
30 | * specified in the configuration. | |
31 | * <p> | |
32 | * This remote library uses http:// or https://. | |
33 | * | |
34 | * @author niki | |
35 | */ | |
36 | public class WebLibrary extends BasicLibrary { | |
37 | private String host; | |
38 | private int port; | |
39 | private final String key; | |
40 | private final String subkey; | |
41 | ||
42 | // informative only (server will make the actual checks) | |
43 | private boolean rw; | |
44 | ||
45 | /** | |
46 | * Create a {@link RemoteLibrary} linked to the given server. | |
47 | * <p> | |
48 | * Note that the key is structured: | |
49 | * <tt><b><i>xxx</i></b>(|<b><i>yyy</i></b>|<b>wl</b>)(|<b>rw</b>)</tt> | |
50 | * <p> | |
51 | * Note that anything before the first pipe (<tt>|</tt>) character is | |
52 | * considered to be the encryption key, anything after that character is | |
53 | * called the subkey (including the other pipe characters and flags!). | |
54 | * <p> | |
55 | * This is important because the subkey (including the pipe characters and | |
56 | * flags) must be present as-is in the server configuration file to be | |
57 | * allowed. | |
58 | * <ul> | |
59 | * <li><b><i>xxx</i></b>: the encryption key used to communicate with the | |
60 | * server</li> | |
61 | * <li><b><i>yyy</i></b>: the secondary key</li> | |
62 | * <li><b>rw</b>: flag to allow read and write access if it is not the | |
63 | * default on this server</li> | |
64 | * <li><b>wl</b>: flag to allow access to all the stories (bypassing the | |
65 | * whitelist if it exists)</li> | |
66 | * </ul> | |
67 | * <p> | |
68 | * Some examples: | |
69 | * <ul> | |
70 | * <li><b>my_key</b>: normal connection, will take the default server | |
71 | * options</li> | |
72 | * <li><b>my_key|agzyzz|wl</b>: will ask to bypass the white list (if it | |
73 | * exists)</li> | |
74 | * <li><b>my_key|agzyzz|rw</b>: will ask read-write access (if the default | |
75 | * is read-only)</li> | |
76 | * <li><b>my_key|agzyzz|wl|rw</b>: will ask both read-write access and white | |
77 | * list bypass</li> | |
78 | * </ul> | |
79 | * | |
80 | * @param key | |
81 | * the key that will allow us to exchange information with the | |
82 | * server | |
83 | * @param host | |
84 | * the host to contact or NULL for localhost | |
85 | * @param port | |
86 | * the port to contact it on | |
87 | */ | |
88 | public WebLibrary(String key, String host, int port) { | |
89 | int index = -1; | |
90 | if (key != null) { | |
91 | index = key.indexOf('|'); | |
92 | } | |
93 | ||
94 | if (index >= 0) { | |
95 | this.key = key.substring(0, index); | |
96 | this.subkey = key.substring(index + 1); | |
97 | } else { | |
98 | this.key = key; | |
99 | this.subkey = ""; | |
100 | } | |
101 | ||
102 | this.rw = subkey.contains("|rw"); | |
103 | ||
104 | this.host = host; | |
105 | this.port = port; | |
106 | ||
107 | // TODO: not supported yet | |
108 | this.rw = false; | |
109 | } | |
110 | ||
5ee0fc14 NR |
111 | public Version getVersion() { |
112 | try { | |
113 | InputStream in = download(WebLibraryUrls.VERSION_URL); | |
114 | try { | |
115 | return new Version(IOUtils.readSmallStream(in)); | |
116 | } finally { | |
117 | in.close(); | |
118 | } | |
119 | } catch (IOException e) { | |
120 | } | |
121 | ||
a4efa44c | 122 | return new Version(); |
5ee0fc14 NR |
123 | } |
124 | ||
f433d153 NR |
125 | @Override |
126 | public Status getStatus() { | |
127 | try { | |
5ee0fc14 | 128 | download(WebLibraryUrls.INDEX_URL).close(); |
f433d153 NR |
129 | } catch (IOException e) { |
130 | try { | |
8dc26a0a | 131 | download("/style.css").close(); |
f433d153 NR |
132 | return Status.UNAUTHORIZED; |
133 | } catch (IOException ioe) { | |
5ee0fc14 | 134 | return Status.UNAVAILABLE; |
f433d153 NR |
135 | } |
136 | } | |
137 | ||
138 | return rw ? Status.READ_WRITE : Status.READ_ONLY; | |
139 | } | |
140 | ||
141 | @Override | |
142 | public String getLibraryName() { | |
a4efa44c NR |
143 | return (rw ? "[READ-ONLY] " : "") + host + ":" + port + " (" |
144 | + getVersion() + ")"; | |
f433d153 NR |
145 | } |
146 | ||
147 | @Override | |
148 | public Image getCover(String luid) throws IOException { | |
5ee0fc14 NR |
149 | InputStream in = download(WebLibraryUrls.getStoryUrlCover(luid)); |
150 | try { | |
f433d153 | 151 | return new Image(in); |
5ee0fc14 NR |
152 | } finally { |
153 | in.close(); | |
f433d153 | 154 | } |
f433d153 NR |
155 | } |
156 | ||
c5103223 | 157 | @Override |
6673ec59 NR |
158 | public Image getCustomSourceCover(String source) throws IOException { |
159 | InputStream in = download(WebLibraryUrls.getCoverUrlSource(source)); | |
160 | try { | |
161 | return new Image(in); | |
162 | } finally { | |
163 | in.close(); | |
164 | } | |
c5103223 NR |
165 | } |
166 | ||
167 | @Override | |
6673ec59 NR |
168 | public Image getCustomAuthorCover(String author) throws IOException { |
169 | InputStream in = download(WebLibraryUrls.getCoverUrlAuthor(author)); | |
170 | try { | |
171 | return new Image(in); | |
172 | } finally { | |
173 | in.close(); | |
174 | } | |
c5103223 NR |
175 | } |
176 | ||
f433d153 NR |
177 | @Override |
178 | public void setSourceCover(String source, String luid) throws IOException { | |
179 | // TODO Auto-generated method stub | |
180 | throw new IOException("Not implemented yet"); | |
181 | } | |
182 | ||
183 | @Override | |
184 | public void setAuthorCover(String author, String luid) throws IOException { | |
185 | // TODO Auto-generated method stub | |
186 | throw new IOException("Not implemented yet"); | |
187 | } | |
188 | ||
c5103223 NR |
189 | @Override |
190 | public synchronized Story getStory(final String luid, Progress pg) | |
191 | throws IOException { | |
192 | ||
193 | // TODO: pg | |
194 | ||
195 | Story story; | |
5ee0fc14 | 196 | InputStream in = download(WebLibraryUrls.getStoryUrlJson(luid)); |
c5103223 NR |
197 | try { |
198 | JSONObject json = new JSONObject(IOUtils.readSmallStream(in)); | |
199 | story = JsonIO.toStory(json); | |
200 | } finally { | |
201 | in.close(); | |
202 | } | |
203 | ||
204 | story.getMeta().setCover(getCover(luid)); | |
205 | int chapNum = 1; | |
206 | for (Chapter chap : story) { | |
207 | int number = 1; | |
208 | for (Paragraph para : chap) { | |
209 | if (para.getType() == ParagraphType.IMAGE) { | |
210 | InputStream subin = download( | |
5ee0fc14 | 211 | WebLibraryUrls.getStoryUrl(luid, chapNum, number)); |
c5103223 NR |
212 | try { |
213 | para.setContentImage(new Image(subin)); | |
214 | } finally { | |
215 | subin.close(); | |
216 | } | |
217 | } | |
218 | ||
219 | number++; | |
220 | } | |
221 | ||
222 | chapNum++; | |
223 | } | |
224 | ||
225 | return story; | |
226 | } | |
227 | ||
f433d153 NR |
228 | @Override |
229 | protected List<MetaData> getMetas(Progress pg) throws IOException { | |
230 | List<MetaData> metas = new ArrayList<MetaData>(); | |
5ee0fc14 | 231 | InputStream in = download(WebLibraryUrls.LIST_URL_METADATA); |
f433d153 NR |
232 | JSONArray jsonArr = new JSONArray(IOUtils.readSmallStream(in)); |
233 | for (int i = 0; i < jsonArr.length(); i++) { | |
234 | JSONObject json = jsonArr.getJSONObject(i); | |
235 | metas.add(JsonIO.toMetaData(json)); | |
236 | } | |
237 | ||
238 | return metas; | |
239 | } | |
240 | ||
241 | @Override | |
242 | // Could work (more slowly) without it | |
243 | public MetaData imprt(final URL url, Progress pg) throws IOException { | |
244 | if (true) | |
245 | throw new IOException("Not implemented yet"); | |
246 | ||
247 | // Import the file locally if it is actually a file | |
248 | ||
249 | if (url == null || url.getProtocol().equalsIgnoreCase("file")) { | |
250 | return super.imprt(url, pg); | |
251 | } | |
252 | ||
253 | // Import it remotely if it is an URL | |
254 | ||
255 | // TODO | |
256 | return super.imprt(url, pg); | |
257 | } | |
258 | ||
259 | @Override | |
260 | // Could work (more slowly) without it | |
261 | protected synchronized void changeSTA(final String luid, | |
262 | final String newSource, final String newTitle, | |
263 | final String newAuthor, Progress pg) throws IOException { | |
264 | // TODO | |
265 | super.changeSTA(luid, newSource, newTitle, newAuthor, pg); | |
266 | } | |
267 | ||
268 | @Override | |
269 | protected void updateInfo(MetaData meta) { | |
270 | // Will be taken care of directly server side | |
271 | } | |
272 | ||
273 | @Override | |
274 | protected void invalidateInfo(String luid) { | |
275 | // Will be taken care of directly server side | |
276 | } | |
277 | ||
278 | // The following methods are only used by Save and Delete in BasicLibrary: | |
279 | ||
280 | @Override | |
281 | protected int getNextId() { | |
282 | throw new java.lang.InternalError("Should not have been called"); | |
283 | } | |
284 | ||
285 | @Override | |
286 | protected void doDelete(String luid) throws IOException { | |
287 | throw new java.lang.InternalError("Should not have been called"); | |
288 | } | |
289 | ||
290 | @Override | |
291 | protected Story doSave(Story story, Progress pg) throws IOException { | |
292 | throw new java.lang.InternalError("Should not have been called"); | |
293 | } | |
294 | ||
295 | // | |
296 | ||
297 | @Override | |
298 | public File getFile(final String luid, Progress pg) { | |
299 | throw new java.lang.InternalError( | |
300 | "Operation not supportorted on remote Libraries"); | |
301 | } | |
302 | ||
5ee0fc14 | 303 | // starts with "/", never NULL |
f433d153 NR |
304 | private InputStream download(String path) throws IOException { |
305 | URL url = new URL(host + ":" + port + path); | |
306 | ||
307 | Map<String, String> post = new HashMap<String, String>(); | |
308 | post.put("login", subkey); | |
309 | post.put("password", key); | |
310 | ||
311 | return Instance.getInstance().getCache().openNoCache(url, null, post, | |
312 | null, null); | |
313 | } | |
314 | } |