| 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; |
| 16 | import be.nikiroo.fanfix.data.Chapter; |
| 17 | import be.nikiroo.fanfix.data.JsonIO; |
| 18 | import be.nikiroo.fanfix.data.MetaData; |
| 19 | import be.nikiroo.fanfix.data.Paragraph; |
| 20 | import be.nikiroo.fanfix.data.Paragraph.ParagraphType; |
| 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; |
| 25 | import be.nikiroo.utils.Version; |
| 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 | |
| 108 | /** |
| 109 | * Return the version of the program running server-side. |
| 110 | * <p> |
| 111 | * Never returns NULL. |
| 112 | * |
| 113 | * @return the version or an empty {@link Version} if not known |
| 114 | */ |
| 115 | public Version getVersion() { |
| 116 | try { |
| 117 | InputStream in = post(WebLibraryUrls.VERSION_URL); |
| 118 | try { |
| 119 | return new Version(IOUtils.readSmallStream(in)); |
| 120 | } finally { |
| 121 | in.close(); |
| 122 | } |
| 123 | } catch (IOException e) { |
| 124 | } |
| 125 | |
| 126 | return new Version(); |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public Status getStatus() { |
| 131 | try { |
| 132 | post(WebLibraryUrls.INDEX_URL).close(); |
| 133 | } catch (IOException e) { |
| 134 | try { |
| 135 | post("/style.css").close(); |
| 136 | return Status.UNAUTHORIZED; |
| 137 | } catch (IOException ioe) { |
| 138 | return Status.UNAVAILABLE; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | return rw ? Status.READ_WRITE : Status.READ_ONLY; |
| 143 | } |
| 144 | |
| 145 | @Override |
| 146 | public String getLibraryName() { |
| 147 | return (rw ? "[READ-ONLY] " : "") + host + ":" + port + " (" |
| 148 | + getVersion() + ")"; |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public Image getCover(String luid) throws IOException { |
| 153 | InputStream in = post(WebLibraryUrls.getStoryUrlCover(luid)); |
| 154 | try { |
| 155 | return new Image(in); |
| 156 | } finally { |
| 157 | in.close(); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | @Override |
| 162 | public Image getCustomSourceCover(String source) throws IOException { |
| 163 | InputStream in = post(WebLibraryUrls.getCoverUrlSource(source)); |
| 164 | try { |
| 165 | return new Image(in); |
| 166 | } finally { |
| 167 | in.close(); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | @Override |
| 172 | public Image getCustomAuthorCover(String author) throws IOException { |
| 173 | InputStream in = post(WebLibraryUrls.getCoverUrlAuthor(author)); |
| 174 | try { |
| 175 | return new Image(in); |
| 176 | } finally { |
| 177 | in.close(); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | @Override |
| 182 | public void setSourceCover(String source, String luid) throws IOException { |
| 183 | Map<String, String> post = new HashMap<String, String>(); |
| 184 | post.put("luid", luid); |
| 185 | post(WebLibraryUrls.getCoverUrlSource(source), post).close(); |
| 186 | } |
| 187 | |
| 188 | @Override |
| 189 | public void setAuthorCover(String author, String luid) throws IOException { |
| 190 | Map<String, String> post = new HashMap<String, String>(); |
| 191 | post.put("luid", luid); |
| 192 | post(WebLibraryUrls.getCoverUrlAuthor(author), post).close(); |
| 193 | } |
| 194 | |
| 195 | @Override |
| 196 | public synchronized Story getStory(final String luid, Progress pg) |
| 197 | throws IOException { |
| 198 | if (pg == null) { |
| 199 | pg = new Progress(); |
| 200 | } |
| 201 | |
| 202 | Story story; |
| 203 | InputStream in = post(WebLibraryUrls.getStoryUrlJson(luid)); |
| 204 | try { |
| 205 | JSONObject json = new JSONObject(IOUtils.readSmallStream(in)); |
| 206 | story = JsonIO.toStory(json); |
| 207 | } finally { |
| 208 | in.close(); |
| 209 | } |
| 210 | |
| 211 | int max = 0; |
| 212 | for (Chapter chap : story) { |
| 213 | max += chap.getParagraphs().size(); |
| 214 | } |
| 215 | pg.setMinMax(0, max); |
| 216 | |
| 217 | story.getMeta().setCover(getCover(luid)); |
| 218 | int chapNum = 1; |
| 219 | for (Chapter chap : story) { |
| 220 | int number = 1; |
| 221 | for (Paragraph para : chap) { |
| 222 | if (para.getType() == ParagraphType.IMAGE) { |
| 223 | InputStream subin = post( |
| 224 | WebLibraryUrls.getStoryUrl(luid, chapNum, number)); |
| 225 | try { |
| 226 | para.setContentImage(new Image(subin)); |
| 227 | } finally { |
| 228 | subin.close(); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | pg.add(1); |
| 233 | number++; |
| 234 | } |
| 235 | |
| 236 | chapNum++; |
| 237 | } |
| 238 | |
| 239 | pg.done(); |
| 240 | return story; |
| 241 | } |
| 242 | |
| 243 | @Override |
| 244 | protected List<MetaData> getMetas(Progress pg) throws IOException { |
| 245 | List<MetaData> metas = new ArrayList<MetaData>(); |
| 246 | InputStream in = post(WebLibraryUrls.LIST_URL_METADATA); |
| 247 | JSONArray jsonArr = new JSONArray(IOUtils.readSmallStream(in)); |
| 248 | for (int i = 0; i < jsonArr.length(); i++) { |
| 249 | JSONObject json = jsonArr.getJSONObject(i); |
| 250 | metas.add(JsonIO.toMetaData(json)); |
| 251 | } |
| 252 | |
| 253 | return metas; |
| 254 | } |
| 255 | |
| 256 | @Override |
| 257 | // Could work (more slowly) without it |
| 258 | public MetaData imprt(final URL url, Progress pg) throws IOException { |
| 259 | if (pg == null) { |
| 260 | pg = new Progress(); |
| 261 | } |
| 262 | |
| 263 | // Import the file locally if it is actually a file |
| 264 | |
| 265 | if (url == null || url.getProtocol().equalsIgnoreCase("file")) { |
| 266 | return super.imprt(url, pg); |
| 267 | } |
| 268 | |
| 269 | // Import it remotely if it is an URL |
| 270 | |
| 271 | try { |
| 272 | String luid = null; |
| 273 | |
| 274 | Map<String, String> post = new HashMap<String, String>(); |
| 275 | post.put("url", url.toString()); |
| 276 | InputStream in = post(WebLibraryUrls.IMPRT_URL_IMPORT, post); |
| 277 | try { |
| 278 | luid = IOUtils.readSmallStream(in); |
| 279 | } finally { |
| 280 | in.close(); |
| 281 | } |
| 282 | |
| 283 | Progress subPg = null; |
| 284 | do { |
| 285 | try { |
| 286 | Thread.sleep(2000); |
| 287 | } catch (InterruptedException e) { |
| 288 | } |
| 289 | |
| 290 | in = post(WebLibraryUrls.getImprtProgressUrl(luid)); |
| 291 | try { |
| 292 | subPg = JsonIO.toProgress( |
| 293 | new JSONObject(IOUtils.readSmallStream(in))); |
| 294 | } finally { |
| 295 | in.close(); |
| 296 | } |
| 297 | } while (subPg != null); |
| 298 | |
| 299 | in = post(WebLibraryUrls.getStoryUrlMetadata(luid)); |
| 300 | try { |
| 301 | return JsonIO.toMetaData( |
| 302 | new JSONObject(IOUtils.readSmallStream(in))); |
| 303 | } finally { |
| 304 | in.close(); |
| 305 | } |
| 306 | } finally { |
| 307 | pg.done(); |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | @Override |
| 312 | // Could work (more slowly) without it |
| 313 | protected synchronized void changeSTA(final String luid, |
| 314 | final String newSource, final String newTitle, |
| 315 | final String newAuthor, Progress pg) throws IOException { |
| 316 | MetaData meta = getInfo(luid); |
| 317 | if (meta != null) { |
| 318 | if (!meta.getSource().equals(newSource)) { |
| 319 | Map<String, String> post = new HashMap<String, String>(); |
| 320 | post.put("value", newSource); |
| 321 | post(WebLibraryUrls.getStoryUrlSource(luid), post).close(); |
| 322 | } |
| 323 | if (!meta.getTitle().equals(newTitle)) { |
| 324 | Map<String, String> post = new HashMap<String, String>(); |
| 325 | post.put("value", newTitle); |
| 326 | post(WebLibraryUrls.getStoryUrlTitle(luid), post).close(); |
| 327 | } |
| 328 | if (!meta.getAuthor().equals(newAuthor)) { |
| 329 | Map<String, String> post = new HashMap<String, String>(); |
| 330 | post.put("value", newAuthor); |
| 331 | post(WebLibraryUrls.getStoryUrlAuthor(luid), post).close(); |
| 332 | } |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | @Override |
| 337 | protected void updateInfo(MetaData meta) { |
| 338 | // Will be taken care of directly server side |
| 339 | } |
| 340 | |
| 341 | @Override |
| 342 | protected void invalidateInfo(String luid) { |
| 343 | // Will be taken care of directly server side |
| 344 | } |
| 345 | |
| 346 | // The following methods are only used by Save and Delete in BasicLibrary: |
| 347 | |
| 348 | @Override |
| 349 | protected String getNextId() { |
| 350 | throw new java.lang.InternalError("Should not have been called"); |
| 351 | } |
| 352 | |
| 353 | @Override |
| 354 | protected void doDelete(String luid) throws IOException { |
| 355 | throw new java.lang.InternalError("Should not have been called"); |
| 356 | } |
| 357 | |
| 358 | @Override |
| 359 | protected Story doSave(Story story, Progress pg) throws IOException { |
| 360 | throw new java.lang.InternalError("Should not have been called"); |
| 361 | } |
| 362 | |
| 363 | // |
| 364 | |
| 365 | @Override |
| 366 | public File getFile(final String luid, Progress pg) { |
| 367 | throw new java.lang.InternalError( |
| 368 | "Operation not supportorted on remote Libraries"); |
| 369 | } |
| 370 | |
| 371 | // starts with "/", never NULL |
| 372 | private InputStream post(String path) throws IOException { |
| 373 | return post(path, null); |
| 374 | } |
| 375 | |
| 376 | // starts with "/", never NULL |
| 377 | private InputStream post(String path, Map<String, String> post) |
| 378 | throws IOException { |
| 379 | URL url = new URL(host + ":" + port + path); |
| 380 | |
| 381 | if (post == null) { |
| 382 | post = new HashMap<String, String>(); |
| 383 | } |
| 384 | post.put("login", subkey); |
| 385 | post.put("password", key); |
| 386 | |
| 387 | return Instance.getInstance().getCache().openNoCache(url, null, post, |
| 388 | null, null); |
| 389 | } |
| 390 | } |