return;
}
imagePreviewGenerator = previewGenerators.get(0);
}
ResourceURI uri = resource.getURI();
Site site = uri.getSite();
String html = null;
try {
URL pageURL = new URL(UrlUtils.concat(site.getHostname(environment).toExternalForm(), PAGE_HANDLER_PREFIX, uri.getIdentifier()));
html = render(pageURL, site, environment, language, resource.getVersion());
if (StringUtils.isBlank(html)) {
logger.warn("Error rendering preview of page " + uri.getPath());
return;
}
html = HTMLUtils.escapeHtml(HTMLUtils.unescape(html));
} catch (ServletException e) {
logger.warn("Error rendering page " + uri.getPath(), e);
throw new IOException(e);
}
// Try to convert html to xhtml
HtmlCleaner cleaner = new HtmlCleaner();
CleanerProperties xhtmlProperties = cleaner.getProperties();
TagNode xhtmlNode = cleaner.clean(html);
if (xhtmlNode == null) {
logger.warn("Error creating well-formed document from page {}", resource);
return;
}
File xhtmlFile = null;
is = new ByteArrayInputStream(html.getBytes("UTF-8"));
// Write the resource content to disk. This step is needed, as the preview
// generator can only handle files.
try {
xhtmlFile = File.createTempFile("xhtml", ".xml");
Serializer xhtmlSerializer = new SimpleXmlSerializer(xhtmlProperties);
xhtmlSerializer.writeToFile(xhtmlNode, xhtmlFile.getAbsolutePath(), "UTF-8");
} catch (IOException e) {
logger.error("Error creating temporary copy of file content at " + xhtmlFile, e);
FileUtils.deleteQuietly(xhtmlFile);
throw e;
} finally {
IOUtils.closeQuietly(is);
}
File imageFile = File.createTempFile("xhtml-preview", "." + PREVIEW_FORMAT);
FileOutputStream imageFos = null;
// Render the page and write back to client
try {
int screenshotWidth = DEFAULT_SCREENSHOT_WIDTH;
int screenshotHeight = DEFAULT_SCREENSHOT_HEIGHT;
if (style != null && style.getWidth() > 0 && style.getHeight() > 0) {
screenshotHeight = (int) ((float) screenshotWidth / (float) style.getWidth() * style.getHeight());
}
// Create the renderer. Due to a synchronization bug in the software,
// this needs to be synchronized
Java2DRenderer renderer = null;
try {
synchronized (this) {
renderer = new Java2DRenderer(xhtmlFile, screenshotWidth, screenshotHeight);
}
} catch (Throwable t) {
if (isRenderingEnvironmentSane) {
logger.warn("Error creating Java 2D renderer for previews: {}" + t.getMessage());
logger.warn("Page preview rendering will be switched off");
isRenderingEnvironmentSane = false;
}
logger.debug("Error creating Java 2D renderer for preview of page {}: {}" + uri.getPath(), t.getMessage());
return;
}
// Configure the renderer
renderer.getSharedContext().setBaseURL(site.getHostname().toExternalForm());