resp.sendError(HttpServletResponse.SC_FORBIDDEN);
return;
}
}
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(pageID);
Logger.debug(this, "Calling iText render");
renderer.layout();
try {
Logger.debug(this, "Using iText to Create PDF");
renderer.createPDF(resp.getOutputStream());
} catch (DocumentException e) {
Logger.error(this, e.getMessage(), e);
resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
return;
}
// Is it a wiki?
String[] path = pageID.split("/");
if (path.length > 2) {
String wiki = map.get("/" + path[1]);
String wikiName = pageID.substring(1); // String of first /
// Get everything after the second /
wikiName = wikiName.substring(wikiName.indexOf("/")+1); // Get
// everything
// after
// the
// second
// /
String title = WikiUtils.normalizeTitle(wikiName);
if (wiki != null) {
String struct = wiki.split("\\|")[0];
String field = wiki.split("\\|")[1];
ContentletAPI capi = APILocator.getContentletAPI();
String query = "+structureInode:" + struct + " +" + field
+ ":\"" + title
+ "\" +languageId:1* +deleted:false +live:true";
List<com.dotmarketing.portlets.contentlet.model.Contentlet> cons = null;
try {
cons = capi.search(query, 1, 0, "text1", user, true);
} catch (DotDataException e) {
Logger.debug(this, "DotDataException: " + e.getMessage(), e);
} catch (DotSecurityException e) {
Logger.debug(this, "DotSecurityException: " + e.getMessage(), e);
} catch (Exception e) {
Logger.debug(this, "ParseException: " + e.getMessage(), e);
}
if (cons != null && cons.size() > 0) {
com.dotmarketing.portlets.contentlet.model.Contentlet c = cons.get(0);
req.setAttribute(WebKeys.CLICKSTREAM_IDENTIFIER_OVERRIDE, c.getIdentifier());
} else {
resp.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
pageID = "/"
+ path[1]
+ "/index."
+ Config
.getStringProperty("VELOCITY_PAGE_EXTENSION");
uri=pageID;
}
}
if (pageID.endsWith("/")) {
uri = pageID + "index."
+ Config.getStringProperty("VELOCITY_PAGE_EXTENSION");
} else {
if (!pageID.endsWith("."
+ Config.getStringProperty("VELOCITY_PAGE_EXTENSION"))) {
uri = pageID
+ "/index."
+ Config
.getStringProperty("VELOCITY_PAGE_EXTENSION");
}
}
try {
ident = APILocator.getIdentifierAPI().find(host, uri);
} catch (Exception e) {
Logger.debug(this, "Exception: " + e.getMessage(), e); }
} catch (Exception e1) {
Logger.debug(this, e1.getMessage(), e1);
}
if (ident == null) {
resp.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
if (!InodeUtils.isSet(ident.getInode() )) {
resp.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
String url = ident.getURI();
url = url.substring(0, url.lastIndexOf("/")) + "/";
try {
if (!perAPI.doesUserHavePermission(ident,PermissionAPI.PERMISSION_READ, user, true)) {
resp.sendError(HttpServletResponse.SC_FORBIDDEN);
}
} catch (DotDataException e1) {
Logger.error(HTMLPDFServlet.class,e1.getMessage(),e1);
resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e1.getMessage());
}
StringWriter sw = new StringWriter();
Context context;
context = VelocityUtil.getWebContext(req, resp);
context.put("pdfExport", true);
String pageIdent = ident.getInode() + "";
try {
VelocityEngine ve = VelocityUtil.getEngine();
ve.getTemplate("/" + location + "/" + pageIdent + "." + VELOCITY_HTMLPAGE_EXTENSION).merge(context, sw);
ITextRenderer renderer = new ITextRenderer();
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setValidating(false);
DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder();
builder.setEntityResolver(new DTDResolver());
String s = sw.toString();
s = escapeEspecialCharacter(s);
s = processCSSPath(s, host, "css", "\\(", "\\)", ")", url);
s = processCSSPath(s, host, "css", "\\\"", "\\\"", "\"", url);
Tidy tidy = new Tidy();
tidy.setXHTML(true);
ByteArrayInputStream is = new ByteArrayInputStream(s.getBytes());
ByteArrayOutputStream os = new ByteArrayOutputStream();
tidy.parse(is, os);
s = os.toString();
is = new ByteArrayInputStream(s.getBytes());
Document doc = builder.parse(is);
NodeList nl = doc.getElementsByTagName("img");
for (int i = 0; i < nl.getLength(); i++) {
Node n = nl.item(i);
Node srcNode = n.getAttributes().getNamedItem("src");
String srcText = srcNode.getNodeValue();
String newText = getRealPath(srcText, host, url);
String cleanText = cleanPath(newText);
srcNode.setNodeValue(cleanText);
}
renderer.setDocument(doc, null);
// renderer.setDocument("http://www.w3.org/");
renderer.layout();
renderer.createPDF(resp.getOutputStream());
} catch (ParseErrorException e) {
Logger.error(this, "ParseErrorException: "
+ e.getMessage(), e);
} catch (ResourceNotFoundException e) {