private Format getMobiFormat(Book book) { for (Format format : book.getFormats()) { if ("application/x-mobipocket-ebook".equals(format.getMediaType())) { return format; } } return null; } }
private Format parseFormat(Element file) throws XPathExpressionException { Format format = new Format(); String about = xpath.evaluate("@rdf:about", file); try { format.setFile(new URI(about)); } catch (URISyntaxException e) { throw new IllegalArgumentException(e); } format.setExtent(new Integer(xpath.evaluate("dcterms:extent", file))); format.setMediaType(xpath.evaluate("dcterms:format/rdf:Description/rdf:value", file)); format.setModified(xpath.evaluate("dcterms:modified", file)); return format; }
public Book parse(Document doc) throws XPathExpressionException, URISyntaxException { Book book = new Book(); String id = xpath.evaluate("//pgterms:ebook/@rdf:about", doc); book.setId(id); book.setTitle(xpath.evaluate("//dcterms:title/text()", doc)); book.setCreator(xpath.evaluate("//dcterms:creator//pgterms:name/text()", doc)); NodeList files = (NodeList)xpath.evaluate("//pgterms:file", doc, XPathConstants.NODESET); //NodeList subjects = (NodeList)xpath.evaluate("//dcterms:subject/rdf:Description/rdf:value", doc, XPathConstants.NODESET); for (int c = 0; c < files.getLength(); c++) { Format format = parseFormat((Element)files.item(c)); if (format.getMediaType().startsWith("image")) { book.setCover(format.getFile()); } book.getFormats().add(format); } /* for (int c = 0; c < subjects.getLength(); c++) { Element subjectEl = (Element)subjects.item(c); book.getSubjects().add(new Subject(subjectEl.getTextContent())); } */ return book; }
@POST @Path("{id}") public Response sendBook(@PathParam("id") String id) { String recipient = "root@localhost"; LOG.info("Sending book {} to {}.", id, recipient ); Book book = bookRepo.getBook(id); Format format = getMobiFormat(book); if (format == null) { return Response.status(Status.NOT_FOUND).build(); } URI uri = format.getFile(); new MailSender().send(recipient, uri); LOG.info("Mail sent successfully"); return Response.ok().build(); }