ResourceSerializer<?, ?> serializer = serializerService.getSerializerByType(resourceURI.getType());
if (serializer == null)
throw new WebApplicationException(Status.PRECONDITION_FAILED);
// Does the serializer come with a preview generator?
PreviewGenerator previewGenerator = serializer.getPreviewGenerator(resource);
if (previewGenerator == null)
throw new WebApplicationException(Status.NOT_FOUND);
// Load the resource contents from the repository
InputStream resourceInputStream = null;
long contentLength = -1;
// Load the input stream from the scaled image
InputStream contentRepositoryIs = null;
FileOutputStream fos = null;
try {
long resourceLastModified = ResourceUtils.getModificationDate(resource, language).getTime();
if (!scaledResourceFile.isFile() || scaledResourceFile.lastModified() < resourceLastModified) {
if (!force)
throw new WebApplicationException(Response.Status.NOT_FOUND);
contentRepositoryIs = contentRepository.getContent(resourceURI, language);
scaledResourceFile = ImageStyleUtils.createScaledFile(resource, language, style);
scaledResourceFile.setLastModified(Math.max(new Date().getTime(), resourceLastModified));
fos = new FileOutputStream(scaledResourceFile);
logger.debug("Creating scaled image '{}' at {}", resource, scaledResourceFile);
previewGenerator.createPreview(resource, environment, language, style, DEFAULT_PREVIEW_FORMAT, contentRepositoryIs, fos);
if (scaledResourceFile.length() == 0) {
logger.debug("Error scaling '{}': file size is 0", resourceURI);
IOUtils.closeQuietly(resourceInputStream);
FileUtils.deleteQuietly(scaledResourceFile);
}
}
// Did scaling work? If not, cleanup and tell the user
if (scaledResourceFile.length() == 0)
throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR);
// The scaled resource should now exist
resourceInputStream = new FileInputStream(scaledResourceFile);
contentLength = scaledResourceFile.length();
} catch (WebApplicationException e) {
IOUtils.closeQuietly(resourceInputStream);
FileUtils.deleteQuietly(scaledResourceFile);
if (scaledResourceFile != null)
deleteIfEmpty(scaledResourceFile.getParentFile());
throw e;
} catch (ContentRepositoryException e) {
logger.error("Error loading {} image '{}' from {}: {}", new Object[] {
language,
resource,
contentRepository,
e.getMessage() });
logger.error(e.getMessage(), e);
IOUtils.closeQuietly(resourceInputStream);
FileUtils.deleteQuietly(scaledResourceFile);
if (scaledResourceFile != null)
deleteIfEmpty(scaledResourceFile.getParentFile());
throw new WebApplicationException();
} catch (IOException e) {
logger.error("Error scaling image '{}': {}", resourceURI, e.getMessage());
IOUtils.closeQuietly(resourceInputStream);
FileUtils.deleteQuietly(scaledResourceFile);
if (scaledResourceFile != null)
deleteIfEmpty(scaledResourceFile.getParentFile());
throw new WebApplicationException();
} catch (IllegalArgumentException e) {
logger.error("Image '{}' is of unsupported format: {}", resourceURI, e.getMessage());
IOUtils.closeQuietly(resourceInputStream);
FileUtils.deleteQuietly(scaledResourceFile);
if (scaledResourceFile != null)
deleteIfEmpty(scaledResourceFile.getParentFile());
throw new WebApplicationException();
} catch (Throwable t) {
logger.error("Error scaling image '{}': {}", resourceURI, t.getMessage());
IOUtils.closeQuietly(resourceInputStream);
FileUtils.deleteQuietly(scaledResourceFile);
if (scaledResourceFile != null)
deleteIfEmpty(scaledResourceFile.getParentFile());
throw new WebApplicationException();
} finally {
IOUtils.closeQuietly(contentRepositoryIs);
IOUtils.closeQuietly(fos);
}
// Create the response
final InputStream is = resourceInputStream;
ResponseBuilder response = Response.ok(new StreamingOutput() {
public void write(OutputStream os) throws IOException,
WebApplicationException {
try {
IOUtils.copy(is, os);
os.flush();
} catch (IOException e) {
if (!RequestUtils.isCausedByClient(e))
logger.error("Error writing preview to client", e);
} finally {
IOUtils.closeQuietly(is);
}
}
});
// Add mime type header
String mimetype = previewGenerator.getContentType(resource, language, style);
if (mimetype == null)
mimetype = MediaType.APPLICATION_OCTET_STREAM;
response.type(mimetype);
// Add last modified header