if (pageID == null || pageID.length() < 1) {
resp.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
Logger.debug(this, "Page to pdf is " + pageID);
Identifier ident = null;
Host host;
try {
host = hostWebAPI.getCurrentHost(req);
} catch (PortalException e3) {
Logger.error(HTMLPDFServlet.class, e3.getMessage(), e3);
throw new ServletException(e3.getMessage(), e3);
} catch (SystemException e3) {
Logger.error(HTMLPDFServlet.class, e3.getMessage(), e3);
throw new ServletException(e3.getMessage(), e3);
} catch (DotDataException e3) {
Logger.error(HTMLPDFServlet.class, e3.getMessage(), e3);
throw new ServletException(e3.getMessage(), e3);
} catch (DotSecurityException e3) {
Logger.error(HTMLPDFServlet.class, e3.getMessage(), e3);
throw new ServletException(e3.getMessage(), e3);
}
try {
//Long id = Long.valueOf(pageID);
ident = APILocator.getIdentifierAPI().find(pageID);
} catch (NumberFormatException e2) {
boolean external = false;
String uri = pageID;
String pointer = null;
// Is it a virtual link?
if (uri.endsWith("/"))
uri = uri.substring(0, uri.length() - 1);
pointer = VirtualLinksCache.getPathFromCache(host.getHostname() + ":" + uri);
if (!UtilMethods.isSet(pointer)) {
pointer = VirtualLinksCache.getPathFromCache(uri);
}
if (UtilMethods.isSet(pointer)) { // is it a virtual link?
Logger.debug(this, "CMS found virtual link pointer = " + uri + ":" + pointer);
String auxPointer = pointer;
if (auxPointer.indexOf("http://") != -1 || auxPointer.indexOf("https://") != -1) {
auxPointer = pointer.replace("https://", "");
auxPointer = pointer.replace("http://", "");
int startIndex = 0;
int endIndex = auxPointer.indexOf("/");
if (startIndex < endIndex) {
String localHostName = auxPointer.substring(startIndex, endIndex);
Host localHost;
try {
localHost = hostWebAPI.findByName(localHostName, APILocator.getUserAPI().getSystemUser(), false);
} catch (DotDataException e) {
Logger.error(HTMLPDFServlet.class, e.getMessage(), e);
throw new ServletException(e.getMessage(), e);
} catch (DotSecurityException e) {
Logger.error(HTMLPDFServlet.class, e.getMessage(), e);
throw new ServletException(e.getMessage(), e);
}
external = (localHost == null || !InodeUtils.isSet(localHost.getInode() ) ? true : false);
} else {
external = true;
pageID = pointer;
uri = pointer;
}
}
if (!external) {
String ext = Config.getStringProperty("VELOCITY_PAGE_EXTENSION");
if (!pointer.endsWith(ext)) {
if (!pointer.endsWith("/"))
pointer += "/";
pointer += "index." + ext;
}
pageID = pointer;
uri = pointer;
}
}
// Is it external?
if (pageID.startsWith("http://") || pageID.startsWith("https://")) {
if (!external) {
// Didn't come form a virtual link, so we have to make sure
// that we should PDF it according to web.xml
URL url = new URL(pageID);
if (!hostList.contains(url.getHost())) {
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();