update from master
[fanfix.git] / library / WebLibraryServer.java
index 50dc3d942dcf39aeeadeb68d88c58a714e7551cf..6260f6a5c6744d9c9411d0941ad3ad0a54c5e58b 100644 (file)
@@ -110,7 +110,7 @@ public class WebLibraryServer extends WebLibraryServerHtml {
                        return NanoHTTPD.newFixedLengthResponse(Status.SERVICE_UNAVAILABLE,
                                        NanoHTTPD.MIME_PLAINTEXT, "Server is already exiting...");
                }
-               
+
                exiting = true;
                Instance.getInstance().getTraceHandler().trace("Exiting");
 
@@ -131,6 +131,20 @@ public class WebLibraryServer extends WebLibraryServerHtml {
 
                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");
        }
@@ -203,9 +217,8 @@ public class WebLibraryServer extends WebLibraryServerHtml {
                                jsons.add(JsonIO.toJson(meta));
                        }
 
-                       return newInputStreamResponse("application/json",
-                                       new ByteArrayInputStream(
-                                                       new JSONArray(jsons).toString().getBytes()));
+                       return NanoHTTPD.newFixedLengthResponse(Status.OK,
+                                       "application/json", new JSONArray(jsons).toString());
                }
 
                return NanoHTTPD.newFixedLengthResponse(Status.BAD_REQUEST,
@@ -341,7 +354,7 @@ public class WebLibraryServer extends WebLibraryServerHtml {
                        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...");
@@ -374,7 +387,11 @@ public class WebLibraryServer extends WebLibraryServerHtml {
        protected Response getCover(String uri, WLoginResult login)
                        throws IOException {
                String[] uriParts = uri.split("/");
+               int startAt = 0;
                int off = 2; // "" and "cover"
+               for (int i = 0; i < off; i++) {
+                       startAt += uriParts[i].length() + "/".length();
+               }
 
                if (uriParts.length < off + 2) {
                        return NanoHTTPD.newFixedLengthResponse(Status.BAD_REQUEST,
@@ -382,7 +399,7 @@ public class WebLibraryServer extends WebLibraryServerHtml {
                }
 
                String type = uriParts[off + 0];
-               String id = uriParts[off + 1];
+               String id = uri.substring(startAt + type.length() + "/".length());
 
                InputStream in = null;
 
@@ -416,6 +433,10 @@ public class WebLibraryServer extends WebLibraryServerHtml {
                        throws IOException {
                String[] uriParts = uri.split("/");
                int off = 2; // "" and "cover"
+               int startAt = 0;
+               for (int i = 0; i < off; i++) {
+                       startAt += uriParts[i].length() + "/".length();
+               }
 
                if (uriParts.length < off + 2) {
                        return NanoHTTPD.newFixedLengthResponse(Status.BAD_REQUEST,
@@ -426,14 +447,14 @@ public class WebLibraryServer extends WebLibraryServerHtml {
                        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...");
                }
 
                String type = uriParts[off + 0];
-               String id = uriParts[off + 1];
+               String id = uri.substring(startAt + type.length() + "/".length());
 
                if ("source".equals(type)) {
                        sourceCover(id, login, luid);
@@ -457,7 +478,7 @@ public class WebLibraryServer extends WebLibraryServerHtml {
                        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...");
@@ -529,7 +550,7 @@ public class WebLibraryServer extends WebLibraryServerHtml {
                        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...");