BrightSide Workbench Full Report + Source Code
PrintServlet.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2022 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.elephant.print;
20 
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;
45 
50 @WebServlet(name = "Print", urlPatterns = {"/print_/*"})
51 public class PrintServlet extends HttpServlet {
52 
53  private static String PDF = "/pdf_";
54 
55  private void processPrint(Application application, String path, KeyValueMap map) throws IOException {
56  try {
57  if(path.startsWith(PDF + "/")) {
58  HttpServletResponse response = application.getHttpServletResponse();
59  response.setContentType("application/pdf");
60  path = "/print_" + path.substring(PDF.length());
61  HtmlToPdf.of(ElephantContext.toUri(path).toURL(), ElephantContext.toUri(path))
62  .removals("a#toTopButton,style,script,form")
63  .pdf(response.getOutputStream());
64  } else if(map.containsKey("read")) {
65  IConstructor constructor = application.getConstructor();
66  ElContext context = ElContextMap.getContext(path);
67  constructor.forceIsAContext(context);
68  constructor.getResponse().setContentType("text/html");
69  constructor.getResponse().setCharacterEncoding(ElephantContext.getEncoding());
70  constructor.setStopper(map.get(Boolean.class, "stopper", Boolean.TRUE) ? "<end/>" : null);
71  printContext(constructor, context, 1, false);
72  constructor.setRenderingContext(null);
73  } else if(map.containsKey("rss")) {
74  IConstructor constructor = application.getConstructor();
75  ElContext context = ElContextMap.getContext(path);
76  processRss(constructor, context);
77  constructor.setRenderingContext(null);
78  } else {
79  IConstructor constructor = application.getConstructor();
80  ElContext context = ElContextMap.getContext(path);
81  constructor.forceIsAContext(context);
82  constructor.startBody();
83  printContext(constructor, context, map.get(Integer.class, "level", 10), map.get(Boolean.class, "title", Boolean.TRUE));
84  constructor.setRenderingContext(null);
85  constructor.endBody();
86  }
87  } catch (URISyntaxException | ServletException ex) {
88  WebLoggers.severe(this).exception(ex).log();
89  }
90  }
91 
92  private void printContext(IConstructor constructor, ElContext context, int level, boolean title) throws ServletException, IOException {
93  if(level <= 0) return;
94  if(context.canPrint() && context.getWebContext().isWikiPage()) {
95  constructor.setRenderingContext(context);
96  if(title) constructor.getOut().print("<div class='context-title'>" + context.getName() + "</div>");
97  context.getElement().startConstruction();
98  for(ElContext ctx : context.getChildren()) {
99  printContext(constructor, ctx, level-1, title);
100  }
101  }
102  }
103 
104  private void processRss(IConstructor constructor, ElContext context) throws IOException {
105  constructor.getResponse().setContentType("application/rss+xml");
106  constructor.setRenderingContext(context);
107  IElement el = context.getElement();
108  if(el instanceof IRSSable) {
109  SyndFeed feed = new SyndFeedImpl();
110  feed.setFeedType("rss_1.0");
111  feed.setEncoding(ElephantContext.getEncoding());
112  feed.setTitle(ElephantContext.getSiteName() + " - " + context.getName());
113  feed.setLink(ElephantContext.getServerUrl("http"));
114  feed.setDescription(context.getName());
115  feed.setEntries(((IRSSable) el).getSyndEntries());
116  SyndFeedOutput output = new SyndFeedOutput();
117  try {
118  output.output(feed, constructor.getOut());
119  } catch (FeedException ex) {
120  WebLoggers.severe(this).exception(ex).log();
121  }
122  }
123  }
124 
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));
129  }
130  processPrint(new ElephantApplication(request, response), request.getPathInfo(), map);
131  }
132 
133  @Override
134  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
135  processRequest(request, response);
136  }
137 
138  @Override
139  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
140  processRequest(request, response);
141  }
142 
143  @Override
144  public String getServletInfo() {
145  return "Print servlet";
146  }
147 
148 }
149 
abstract HttpServletResponse getHttpServletResponse()
static String getServerUrl(String scheme)
void processRequest(HttpServletRequest request, HttpServletResponse response)
void doPost(HttpServletRequest request, HttpServletResponse response)
void doGet(HttpServletRequest request, HttpServletResponse response)
static ElContext getContext(IConstructor constructor)
TreeSet< ElContext > getChildren()
Definition: ElContext.java:196
static WebLoggers severe(Object entity)
Definition: WebLoggers.java:51
WebLoggers exception(Throwable throwable)
Definition: WebLoggers.java:29
void setRenderingContext(ElContext context)
void forceIsAContext(ElContext context)