break;
case STOP_SERVER:
// Can be given via "--remote XX XX XX"
- if (key == null)
- key = Instance.getInstance().getConfig().getString(Config.SERVER_KEY);
- if (port == null)
+ if (key == null) {
+ key = Instance.getInstance().getConfig()
+ .getString(Config.SERVER_KEY);
+
+ // If a subkey in RW mode exists, use it
+ for (String subkey : Instance.getInstance().getConfig()
+ .getList(Config.SERVER_ALLOWED_SUBKEYS,
+ new ArrayList<String>())) {
+ if ((subkey + "|").contains("|rw|")) {
+ key = key + "|" + subkey;
+ break;
+ }
+ }
+ }
+
+ if (port == null) {
port = Instance.getInstance().getConfig().getInteger(Config.SERVER_PORT);
+ }
+
+ if (host == null) {
+ String mode = Instance.getInstance().getConfig()
+ .getString(Config.SERVER_MODE, "fanfix");
+ if ("http".equals(mode)) {
+ host = "http://localhost";
+ } else if ("https".equals(mode)) {
+ host = "https://localhost";
+ } else if ("fanfix".equals(mode)) {
+ host = "fanfix://localhost";
+ }
+ }
if (port == null) {
System.err.println("No port given nor configured in the config file");
* @param key
* the key to contact the Fanfix server
* @param host
- * the host on which it runs (NULL means localhost)
+ * the host on which it runs
* @param port
* the port on which it runs
*
* @throws SSLException
* when the key was not accepted
*/
- private void stopServer(String key, String host, Integer port)
+ private void stopServer(String key, String host, int port)
throws IOException, SSLException {
if (host.startsWith("http://") || host.startsWith("https://")) {
new WebLibrary(key, host, port).stop();
in = new FileInputStream(cover);
try {
synchronized (lock) {
- sourceCovers.put(source, new Image(in));
+ Image img = new Image(in);
+ if (img.getSize() == 0) {
+ img.close();
+ throw new IOException(
+ "Empty image not accepted");
+ }
+ sourceCovers.put(source, img);
}
} finally {
in.close();
in = new FileInputStream(cover);
try {
synchronized (lock) {
- authorCovers.put(author, new Image(in));
+ Image img = new Image(in);
+ if (img.getSize() == 0) {
+ img.close();
+ throw new IOException(
+ "Empty image not accepted");
+ }
+ authorCovers.put(author, img);
}
} finally {
in.close();
* in case of I/O errors
*/
public void stop() throws IOException {
- post(WebLibraryUrls.EXIT_URL, null).close();
+ try {
+ post(WebLibraryUrls.EXIT_URL, null).close();
+ } catch (Exception e) {
+ try {
+ Thread.sleep(200);
+ } catch (InterruptedException e1) {
+ }
+ if (getStatus() != Status.UNAVAILABLE) {
+ throw new IOException("Cannot exit the library", e);
+ }
+ }
}
@Override
try {
Image img = new Image(in);
if (img.getSize() > 0) {
+ img.close();
return img;
}
try {
Image img = new Image(in);
if (img.getSize() > 0) {
+ img.close();
return img;
}
try {
Image img = new Image(in);
if (img.getSize() > 0) {
+ img.close();
return img;
}
return NanoHTTPD.newFixedLengthResponse(Status.SERVICE_UNAVAILABLE,
NanoHTTPD.MIME_PLAINTEXT, "Server is already exiting...");
}
-
+
exiting = true;
Instance.getInstance().getTraceHandler().trace("Exiting");
doStop();
+ new Thread(new Runnable() {
+ @Override
+ public void run() {
+ try {
+ Thread.sleep(1500);
+ } catch (InterruptedException e) {
+ }
+
+ Instance.getInstance().getTraceHandler()
+ .trace("Exit timeout: force-quit");
+ System.exit(0);
+ }
+ }, "Exit program after timeout of 1500 ms").start();
+
return NanoHTTPD.newFixedLengthResponse(Status.OK,
NanoHTTPD.MIME_PLAINTEXT, "Exited");
}
return NanoHTTPD.newFixedLengthResponse(Status.FORBIDDEN,
NanoHTTPD.MIME_PLAINTEXT, "SET story part not allowed");
}
-
+
if (exiting) {
return NanoHTTPD.newFixedLengthResponse(Status.SERVICE_UNAVAILABLE,
NanoHTTPD.MIME_PLAINTEXT, "Server is exiting...");
return NanoHTTPD.newFixedLengthResponse(Status.FORBIDDEN,
NanoHTTPD.MIME_PLAINTEXT, "Cover request not allowed");
}
-
+
if (exiting) {
return NanoHTTPD.newFixedLengthResponse(Status.SERVICE_UNAVAILABLE,
NanoHTTPD.MIME_PLAINTEXT, "Server is exiting...");
return NanoHTTPD.newFixedLengthResponse(Status.FORBIDDEN,
NanoHTTPD.MIME_PLAINTEXT, "Import not allowed");
}
-
+
if (exiting) {
return NanoHTTPD.newFixedLengthResponse(Status.SERVICE_UNAVAILABLE,
NanoHTTPD.MIME_PLAINTEXT, "Server is exiting...");
return NanoHTTPD.newFixedLengthResponse(Status.FORBIDDEN,
NanoHTTPD.MIME_PLAINTEXT, "Delete not allowed");
}
-
+
if (exiting) {
return NanoHTTPD.newFixedLengthResponse(Status.SERVICE_UNAVAILABLE,
NanoHTTPD.MIME_PLAINTEXT, "Server is exiting...");
}
Response rep = null;
- if (!login.isSuccess() && WebLibraryUrls.isSupportedUrl(uri)) {
+ if (!login.isSuccess()
+ && WebLibraryUrls.isSupportedUrl(uri, true)) {
rep = loginPage(login, uri);
}
if (rep == null) {
try {
- if (WebLibraryUrls.isSupportedUrl(uri)) {
+ if (WebLibraryUrls.isSupportedUrl(uri, false)) {
if (WebLibraryUrls.INDEX_URL.equals(uri)) {
rep = root(session, cookies, login);
} else if (WebLibraryUrls.VERSION_URL.equals(uri)) {
.replace("{luid}", luid);
}
- static public boolean isSupportedUrl(String url) {
- return INDEX_URL.equals(url) || VERSION_URL.equals(url)
- || LOGOUT_URL.equals(url) || EXIT_URL.equals(url)
- || isViewUrl(url) || isStoryUrl(url) || isListUrl(url)
- || isCoverUrl(url) || isImprtUrl(url) || isDeleteUrl(url);
+ static public boolean isSupportedUrl(String url,
+ boolean requiresLoginOnly) {
+ if (requiresLoginOnly) {
+ return INDEX_URL.equals(url) || LOGOUT_URL.equals(url)
+ || EXIT_URL.equals(url) || isViewUrl(url) || isStoryUrl(url)
+ || isListUrl(url) || isCoverUrl(url) || isImprtUrl(url)
+ || isDeleteUrl(url);
+ }
+
+ return isSupportedUrl(url, true) || VERSION_URL.equals(url);
}
static public String getCoverUrlStory(String luid) {
try {
InputStream in = Instance.getInstance().getCache().open(new URL(coverUrl), getSupport(), true);
try {
- meta.setCover(new Image(in));
+ Image img = new Image(in);
+ if (img.getSize() == 0) {
+ img.close();
+ throw new IOException(
+ "Empty image not accepted");
+ }
+ meta.setCover(img);
} finally {
in.close();
}
InputStream in = Instance.getInstance().getCache().open(new URL(coverUrl), getSupport(),
true);
try {
- meta.setCover(new Image(in));
+ Image ii = new Image(in);
+ if (ii.getSize() == 0) {
+ ii.close();
+ throw new IOException(
+ "Empty image not accepted");
+ }
+ meta.setCover(ii);
} finally {
in.close();
}
InputStream in = null;
try {
in = Instance.getInstance().getCache().open(url, support, true);
- return new Image(in);
+ Image img = new Image(in);
+ if (img.getSize() == 0) {
+ img.close();
+ throw new IOException(
+ "Empty image not accepted");
+ }
+ return img;
} catch (IOException e) {
} finally {
if (in != null) {
InputStream in = null;
try {
in = Instance.getInstance().getCache().open(url, support, true);
- return new Image(in);
+ Image img = new Image(in);
+ if (img.getSize() == 0) {
+ img.close();
+ throw new IOException(
+ "Empty image not accepted");
+ }
+ return img;
} catch (IOException e) {
} finally {
if (in != null) {
InputStream in = null;
try {
in = Instance.getInstance().getCache().open(url, getSupport(url), true);
- return new Image(in);
+ Image img = new Image(in);
+ if (img.getSize() == 0) {
+ img.close();
+ throw new IOException(
+ "Empty image not accepted");
+ }
+ return img;
} catch (IOException e) {
} finally {
if (in != null) {
if (imageEntry) {
String uuid = meta.getUuid() + "_" + entry.getName();
try {
- images.put(uuid, new Image(zipIn));
+ Image img = new Image(zipIn);
+ if (img.getSize() == 0) {
+ img.close();
+ throw new IOException(
+ "Empty image not accepted");
+ }
+ images.put(uuid, img);
} catch (Exception e) {
Instance.getInstance().getTraceHandler().error(e);
}
// Cover
if (getCover() && cover == null) {
try {
- cover = new Image(zipIn);
+ Image img = new Image(zipIn);
+ if (img.getSize() == 0) {
+ img.close();
+ throw new IOException(
+ "Empty image not accepted");
+ }
+ cover = img;
} catch (Exception e) {
Instance.getInstance().getTraceHandler()
.error(e);
try {
InputStream in = Instance.getInstance().getCache().open(coverImageUrl, null, true);
try {
- meta.setCover(new Image(in));
+ Image img = new Image(in);
+ if (img.getSize() == 0) {
+ img.close();
+ throw new IOException(
+ "Empty image not accepted");
+ }
+ meta.setCover(img);
} finally {
in.close();
}
if (img != null) {
String coverUrl = img.absUrl("src");
- InputStream coverIn;
try {
- coverIn = Instance.getInstance().getCache().open(new URL(coverUrl), this, true);
+ InputStream coverIn = Instance.getInstance().getCache()
+ .open(new URL(coverUrl), this, true);
try {
- return new Image(coverIn);
+ Image ii = new Image(coverIn);
+ if (ii.getSize() == 0) {
+ ii.close();
+ throw new IOException("Empty image not accepted");
+ }
+
+ return ii;
} finally {
coverIn.close();
}