/* * This Java source file was generated by the Gradle 'init' task. */ package com.literalice.docrdr; import jakarta.servlet.RequestDispatcher; import jakarta.servlet.ServletContext; import jakarta.servlet.ServletException; import jakarta.servlet.annotation.WebServlet; import jakarta.servlet.http.*; import java.io.IOException; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.select.Elements; @WebServlet(urlPatterns = { "/documents/drafts/*" }) public class DocumentDraftsServlet extends HttpServlet { @Override public void init() throws ServletException { super.init(); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException { ServletContext ctx = getServletContext(); String documentURL = req.getParameter("document-url"); Document document = Jsoup.connect(documentURL).get(); String targetLang = ctx.getInitParameter("TARGET_LANGUAGE"); HTMLDocument htmlDocument = new HTMLDocument(document.title(), documentURL, loadArticleBody(document), targetLang); ctx.setAttribute("document", htmlDocument); HttpSession session = req.getSession(); session.setAttribute("document", htmlDocument); RequestDispatcher dispatcher = req.getRequestDispatcher("/WEB-INF/jsp/documents/draft.jsp"); dispatcher.forward(req, res); } private String loadArticleBody(Document document) { Elements articleBody = document.body().select("[property=articleBody]"); articleBody.select("li, h1, h2, h3, h4, h5, h6").append("\n"); return articleBody.html(); } }