19 package org.turro.elephant.print;
21 import com.sun.syndication.feed.synd.SyndFeed;
22 import com.sun.syndication.feed.synd.SyndFeedImpl;
23 import com.sun.syndication.io.FeedException;
24 import com.sun.syndication.io.SyndFeedOutput;
25 import java.io.IOException;
26 import java.net.URISyntaxException;
27 import java.net.URLDecoder;
28 import java.nio.charset.StandardCharsets;
29 import javax.servlet.ServletException;
30 import javax.servlet.annotation.WebServlet;
31 import javax.servlet.http.HttpServlet;
32 import javax.servlet.http.HttpServletRequest;
33 import javax.servlet.http.HttpServletResponse;
34 import org.turro.collections.KeyValueMap;
35 import org.turro.elephant.context.Application;
36 import org.turro.elephant.context.ElephantApplication;
37 import org.turro.elephant.context.ElephantContext;
38 import org.turro.elephant.context.IConstructor;
39 import org.turro.elephant.context.IElement;
40 import org.turro.elephant.feed.IRSSable;
41 import org.turro.elephant.web.ElContext;
42 import org.turro.elephant.web.ElContextMap;
43 import org.turro.html.HtmlToPdf;
44 import org.turro.log.WebLoggers;
50 @WebServlet(name =
"Print", urlPatterns = {
"/print_/*"})
53 private static String PDF =
"/pdf_";
55 private void processPrint(
Application application, String path, KeyValueMap map)
throws IOException {
57 if(path.startsWith(PDF +
"/")) {
59 response.setContentType(
"application/pdf");
60 path =
"/print_" + path.substring(PDF.length());
62 .removals(
"a#toTopButton,style,script,form")
63 .pdf(response.getOutputStream());
64 }
else if(map.containsKey(
"read")) {
68 constructor.
getResponse().setContentType(
"text/html");
70 constructor.
setStopper(map.get(Boolean.class,
"stopper", Boolean.TRUE) ?
"<end/>" :
null);
71 printContext(constructor, context, 1,
false);
73 }
else if(map.containsKey(
"rss")) {
76 processRss(constructor, context);
83 printContext(constructor, context, map.get(Integer.class,
"level", 10), map.get(Boolean.class,
"title", Boolean.TRUE));
87 }
catch (URISyntaxException | ServletException ex) {
92 private void printContext(
IConstructor constructor,
ElContext context,
int level,
boolean title)
throws ServletException, IOException {
93 if(level <= 0)
return;
96 if(title) constructor.
getOut().print(
"<div class='context-title'>" + context.
getName() +
"</div>");
99 printContext(constructor, ctx, level-1, title);
105 constructor.
getResponse().setContentType(
"application/rss+xml");
109 SyndFeed feed =
new SyndFeedImpl();
110 feed.setFeedType(
"rss_1.0");
114 feed.setDescription(context.
getName());
115 feed.setEntries(((
IRSSable) el).getSyndEntries());
116 SyndFeedOutput output =
new SyndFeedOutput();
118 output.output(feed, constructor.
getOut());
119 }
catch (FeedException ex) {
125 protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
126 KeyValueMap map =
new KeyValueMap();
127 for(String key : request.getParameterMap().keySet()) {
128 map.put(key, URLDecoder.decode(request.getParameter(key), StandardCharsets.UTF_8));
134 protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
135 processRequest(request, response);
139 protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
140 processRequest(request, response);
145 return "Print servlet";
IConstructor getConstructor()
abstract HttpServletResponse getHttpServletResponse()
static String getSiteName()
static String getServerUrl(String scheme)
static String getEncoding()
static URI toUri(String localPath)
void processRequest(HttpServletRequest request, HttpServletResponse response)
void doPost(HttpServletRequest request, HttpServletResponse response)
void doGet(HttpServletRequest request, HttpServletResponse response)
static ElContext getContext(IConstructor constructor)
WebContext getWebContext()
TreeSet< ElContext > getChildren()
static WebLoggers severe(Object entity)
WebLoggers exception(Throwable throwable)
HttpServletResponse getResponse()
void setStopper(String stopper)
void setRenderingContext(ElContext context)
void forceIsAContext(ElContext context)