1 package org.readium.r2_streamer.server.handler;
3 import org.readium.r2_streamer.fetcher.EpubFetcher;
4 import org.readium.r2_streamer.server.ResponseStatus;
5 import com.fasterxml.jackson.core.JsonGenerationException;
6 import com.fasterxml.jackson.databind.JsonMappingException;
7 import com.fasterxml.jackson.databind.ObjectMapper;
9 import java.io.IOException;
12 import fi.iki.elonen.NanoHTTPD;
13 import fi.iki.elonen.router.RouterNanoHTTPD;
16 * Created by mahavir on 3/2/17.
19 public class ManifestHandler extends RouterNanoHTTPD.DefaultHandler {
20 private static final String TAG = "ManifestHandler";
23 public String getMimeType() {
24 return "application/webpub+json";
28 public String getText() {
29 return ResponseStatus.FAILURE_RESPONSE;
33 public NanoHTTPD.Response.IStatus getStatus() {
34 return NanoHTTPD.Response.Status.OK;
38 public NanoHTTPD.Response get(RouterNanoHTTPD.UriResource uriResource, Map<String, String> urlParams, NanoHTTPD.IHTTPSession session) {
41 EpubFetcher fetcher = uriResource.initParameter(EpubFetcher.class);
43 ObjectMapper objectMapper = new ObjectMapper();
44 String json = objectMapper.writeValueAsString(fetcher.publication);
46 return NanoHTTPD.newFixedLengthResponse(getStatus(), getMimeType(), json);
48 } catch (JsonGenerationException | JsonMappingException e) {
49 System.out.println(TAG + " JsonGenerationException | JsonMappingException " + e.toString());
50 return NanoHTTPD.newFixedLengthResponse(NanoHTTPD.Response.Status.INTERNAL_ERROR, getMimeType(), ResponseStatus.FAILURE_RESPONSE);
51 } catch (IOException e) {
52 System.out.println(TAG + " IOException " + e.toString());
53 return NanoHTTPD.newFixedLengthResponse(NanoHTTPD.Response.Status.INTERNAL_ERROR, getMimeType(), ResponseStatus.FAILURE_RESPONSE);