Package ch.entwine.weblounge.common.content

Examples of ch.entwine.weblounge.common.content.PreviewGenerator


      DispatchUtils.sendNotFound(request, response);
      return true;
    }

    // Find a resource preview generator
    PreviewGenerator previewGenerator = null;
    synchronized (previewGenerators) {
      for (PreviewGenerator generator : previewGenerators) {
        if (generator.supports(resource)) {
          previewGenerator = generator;
          break;
View Full Code Here


        logger.warn("Unable to index resources of type '{}': no resource serializer found", resourceType);
        return;
      }

      // Does the serializer come with a preview generator?
      PreviewGenerator previewGenerator = serializer.getPreviewGenerator(resource);
      if (previewGenerator == null) {
        logger.debug("Resource type '{}' does not support previews", resourceType);
        return;
      }
View Full Code Here

    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
View Full Code Here

    ResourceSerializer<?, ?> serializer = serializerService.getSerializerByType(resource.getURI().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 input stream from the scaled image
    File scaledResourceFile = null;
View Full Code Here

TOP

Related Classes of ch.entwine.weblounge.common.content.PreviewGenerator

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.