syncope-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ilgro...@apache.org
Subject syncope git commit: [SYNCOPE-751] Implementation provided based on PDFBox
Date Tue, 22 Dec 2015 15:12:12 GMT
Repository: syncope
Updated Branches:
  refs/heads/1_2_X 2a30d5073 -> 5571cc73b


[SYNCOPE-751] Implementation provided based on PDFBox


Project: http://git-wip-us.apache.org/repos/asf/syncope/repo
Commit: http://git-wip-us.apache.org/repos/asf/syncope/commit/5571cc73
Tree: http://git-wip-us.apache.org/repos/asf/syncope/tree/5571cc73
Diff: http://git-wip-us.apache.org/repos/asf/syncope/diff/5571cc73

Branch: refs/heads/1_2_X
Commit: 5571cc73b7c04b1066ae661d60c99c1a4bfb9b73
Parents: 2a30d50
Author: Francesco Chicchiriccò <ilgrosso@apache.org>
Authored: Tue Dec 22 16:12:02 2015 +0100
Committer: Francesco Chicchiriccò <ilgrosso@apache.org>
Committed: Tue Dec 22 16:12:02 2015 +0100

----------------------------------------------------------------------
 console/pom.xml                                 |   7 +-
 .../syncope/console/preview/PreviewUtil.java    |   9 +-
 .../form/preview/AbstractBinaryPreviewer.java   |   3 -
 .../html/form/preview/BinaryImagePreviewer.java |  15 +-
 .../html/form/preview/BinaryPDFPreviewer.java   | 148 +++++++++++++++++++
 .../html/form/preview/BinaryPDFPreviewer.html   |  34 +++++
 pom.xml                                         |   6 +
 7 files changed, 205 insertions(+), 17 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/syncope/blob/5571cc73/console/pom.xml
----------------------------------------------------------------------
diff --git a/console/pom.xml b/console/pom.xml
index 2b2264c..3c76019 100644
--- a/console/pom.xml
+++ b/console/pom.xml
@@ -111,6 +111,11 @@ under the License.
     </dependency>
 
     <dependency>
+      <groupId>org.apache.pdfbox</groupId>
+      <artifactId>pdfbox</artifactId>
+    </dependency>
+      
+    <dependency>
       <groupId>org.springframework</groupId>
       <artifactId>spring-core</artifactId>
     </dependency>
@@ -405,7 +410,7 @@ under the License.
       <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-war-plugin</artifactId>
-	<inherited>true</inherited>
+        <inherited>true</inherited>
         <configuration>
           <webResources>
             <resource>

http://git-wip-us.apache.org/repos/asf/syncope/blob/5571cc73/console/src/main/java/org/apache/syncope/console/preview/PreviewUtil.java
----------------------------------------------------------------------
diff --git a/console/src/main/java/org/apache/syncope/console/preview/PreviewUtil.java b/console/src/main/java/org/apache/syncope/console/preview/PreviewUtil.java
index 44bab84..5440006 100644
--- a/console/src/main/java/org/apache/syncope/console/preview/PreviewUtil.java
+++ b/console/src/main/java/org/apache/syncope/console/preview/PreviewUtil.java
@@ -31,10 +31,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 @org.springframework.stereotype.Component
 public class PreviewUtil {
 
-    /**
-     * Logger.
-     */
-    private static final Logger LOG = LoggerFactory.getLogger(PreviewUtil.class);
+    protected static final Logger LOG = LoggerFactory.getLogger(PreviewUtil.class);
 
     @Autowired
     private PreviewPanelClassInitializer previewPanelClassInitializer;
@@ -50,7 +47,7 @@ public class PreviewUtil {
                 ? null
                 : ((AbstractBinaryPreviewer) Class.forName(previewer.getName()).
                 getConstructor(String.class, String.class, byte[].class).newInstance(
-                        new Object[] { "previewer", mimeType, Base64.decodeBase64(file) })).preview();
+                new Object[] { "previewer", mimeType, Base64.decodeBase64(file) })).preview();
     }
 
     public Component getPreviewer(final String mimeType, final byte[] file) throws ClassNotFoundException,
@@ -62,6 +59,6 @@ public class PreviewUtil {
                 ? null
                 : ((AbstractBinaryPreviewer) Class.forName(previewer.getName()).
                 getConstructor(String.class, String.class, byte[].class).newInstance(
-                        new Object[] { "previewer", mimeType, file })).preview();
+                new Object[] { "previewer", mimeType, file })).preview();
     }
 }

http://git-wip-us.apache.org/repos/asf/syncope/blob/5571cc73/console/src/main/java/org/apache/syncope/console/wicket/markup/html/form/preview/AbstractBinaryPreviewer.java
----------------------------------------------------------------------
diff --git a/console/src/main/java/org/apache/syncope/console/wicket/markup/html/form/preview/AbstractBinaryPreviewer.java
b/console/src/main/java/org/apache/syncope/console/wicket/markup/html/form/preview/AbstractBinaryPreviewer.java
index 2b2ac49..08ca15e 100644
--- a/console/src/main/java/org/apache/syncope/console/wicket/markup/html/form/preview/AbstractBinaryPreviewer.java
+++ b/console/src/main/java/org/apache/syncope/console/wicket/markup/html/form/preview/AbstractBinaryPreviewer.java
@@ -25,9 +25,6 @@ import org.slf4j.LoggerFactory;
 
 public abstract class AbstractBinaryPreviewer extends Panel {
 
-    /**
-     * Logger.
-     */
     protected static final Logger LOG = LoggerFactory.getLogger(AbstractBinaryPreviewer.class);
 
     private static final long serialVersionUID = -2482706463911903025L;

http://git-wip-us.apache.org/repos/asf/syncope/blob/5571cc73/console/src/main/java/org/apache/syncope/console/wicket/markup/html/form/preview/BinaryImagePreviewer.java
----------------------------------------------------------------------
diff --git a/console/src/main/java/org/apache/syncope/console/wicket/markup/html/form/preview/BinaryImagePreviewer.java
b/console/src/main/java/org/apache/syncope/console/wicket/markup/html/form/preview/BinaryImagePreviewer.java
index bb72c61..99453bd 100644
--- a/console/src/main/java/org/apache/syncope/console/wicket/markup/html/form/preview/BinaryImagePreviewer.java
+++ b/console/src/main/java/org/apache/syncope/console/wicket/markup/html/form/preview/BinaryImagePreviewer.java
@@ -38,14 +38,15 @@ public class BinaryImagePreviewer extends AbstractBinaryPreviewer {
 
     @Override
     public Component preview() {
-        return this.add(new NonCachingImage("previewImage", new ThumbnailImageResource(new
DynamicImageResource() {
+        return this.addOrReplace(
+                new NonCachingImage("previewImage", new ThumbnailImageResource(new DynamicImageResource()
{
 
-            private static final long serialVersionUID = 923201517955737928L;
+                    private static final long serialVersionUID = 923201517955737928L;
 
-            @Override
-            protected byte[] getImageData(final IResource.Attributes attributes) {
-                return uploadedBytes;
-            }
-        }, IMG_SIZE)));
+                    @Override
+                    protected byte[] getImageData(final IResource.Attributes attributes)
{
+                        return uploadedBytes;
+                    }
+                }, IMG_SIZE)));
     }
 }

http://git-wip-us.apache.org/repos/asf/syncope/blob/5571cc73/console/src/main/java/org/apache/syncope/console/wicket/markup/html/form/preview/BinaryPDFPreviewer.java
----------------------------------------------------------------------
diff --git a/console/src/main/java/org/apache/syncope/console/wicket/markup/html/form/preview/BinaryPDFPreviewer.java
b/console/src/main/java/org/apache/syncope/console/wicket/markup/html/form/preview/BinaryPDFPreviewer.java
new file mode 100644
index 0000000..96b6c01
--- /dev/null
+++ b/console/src/main/java/org/apache/syncope/console/wicket/markup/html/form/preview/BinaryPDFPreviewer.java
@@ -0,0 +1,148 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.syncope.console.wicket.markup.html.form.preview;
+
+import java.awt.Graphics2D;
+import java.awt.RenderingHints;
+import java.awt.image.BufferedImage;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.Serializable;
+import java.util.List;
+import org.apache.pdfbox.pdmodel.PDDocument;
+import org.apache.pdfbox.pdmodel.PDPage;
+import org.apache.syncope.console.preview.BinaryPreview;
+import org.apache.wicket.Component;
+import org.apache.wicket.markup.html.WebMarkupContainer;
+import org.apache.wicket.markup.html.image.NonCachingImage;
+import org.apache.wicket.markup.html.panel.Fragment;
+import org.apache.wicket.request.resource.DynamicImageResource;
+import org.apache.wicket.request.resource.IResource;
+import org.apache.wicket.util.io.IOUtils;
+import org.apache.wicket.util.time.Time;
+
+@BinaryPreview(mimeTypes = { "application/pdf" })
+public class BinaryPDFPreviewer extends AbstractBinaryPreviewer {
+
+    private static final long serialVersionUID = 7743389062928086373L;
+
+    private static final int IMG_SIZE = 230;
+
+    private static final int RESOLUTION = 96;
+
+    private static final int IMAGE_TYPE = BufferedImage.TYPE_INT_RGB;
+
+    private transient BufferedImage firstPage;
+
+    public BinaryPDFPreviewer(final String id, final String mimeType, final byte[] uploadedBytes)
{
+        super(id, mimeType, uploadedBytes);
+    }
+
+    @Override
+    public Component preview() {
+        firstPage = null;
+
+        PDDocument document = null;
+        try {
+            document = PDDocument.load(new ByteArrayInputStream(uploadedBytes));
+            if (document.isEncrypted()) {
+                LOG.info("Document is encrypted, no preview is possible");
+            } else {
+                @SuppressWarnings("unchecked")
+                List<PDPage> pages = document.getDocumentCatalog().getAllPages();
+                firstPage = pages.get(0).convertToImage(IMAGE_TYPE, RESOLUTION);
+            }
+        } catch (IOException e) {
+            LOG.error("While generating thumbnail from first page", e);
+        } finally {
+            IOUtils.closeQuietly(document);
+        }
+
+        Fragment fragment;
+        if (firstPage == null) {
+            fragment = new Fragment("preview", "noPreviewFragment", this);
+        } else {
+            fragment = new Fragment("preview", "previewFragment", this);
+            fragment.add(new NonCachingImage("previewImage", new ThumbnailImageResource(firstPage)));
+        }
+
+        WebMarkupContainer previewContainer = new WebMarkupContainer("previewContainer");
+        previewContainer.setOutputMarkupId(true);
+        previewContainer.add(fragment);
+        return this.addOrReplace(previewContainer);
+    }
+
+    private static class ThumbnailImageResource extends DynamicImageResource implements Serializable
{
+
+        private static final long serialVersionUID = 923201517955737928L;
+
+        private transient final BufferedImage image;
+
+        private transient byte[] thumbnail;
+
+        public ThumbnailImageResource(final BufferedImage originalImage) {
+            this.image = originalImage;
+        }
+
+        @Override
+        protected byte[] getImageData(final IResource.Attributes attributes) {
+            if (thumbnail == null) {
+                final BufferedImage image = getScaledImageInstance();
+                thumbnail = toImageData(image);
+                setLastModifiedTime(Time.now());
+            }
+            return thumbnail;
+        }
+
+        private BufferedImage getScaledImageInstance() {
+            int originalWidth = image.getWidth();
+            int originalHeight = image.getHeight();
+
+            if ((originalWidth > IMG_SIZE) || (originalHeight > IMG_SIZE)) {
+                final int newWidth;
+                final int newHeight;
+
+                if (originalWidth > originalHeight) {
+                    newWidth = IMG_SIZE;
+                    newHeight = (IMG_SIZE * originalHeight) / originalWidth;
+                } else {
+                    newWidth = (IMG_SIZE * originalWidth) / originalHeight;
+                    newHeight = IMG_SIZE;
+                }
+
+                // http://today.java.net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.html
+                BufferedImage dimg = new BufferedImage(newWidth, newHeight, image.getType());
+                Graphics2D g = dimg.createGraphics();
+                try {
+                    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
+                            RenderingHints.VALUE_INTERPOLATION_BILINEAR);
+                    g.drawImage(image, 0, 0, newWidth, newHeight, 0, 0, originalWidth,
+                            originalHeight, null);
+                } finally {
+                    g.dispose();
+                }
+
+                return dimg;
+            }
+
+            // no need for resizing
+            return image;
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/syncope/blob/5571cc73/console/src/main/resources/org/apache/syncope/console/wicket/markup/html/form/preview/BinaryPDFPreviewer.html
----------------------------------------------------------------------
diff --git a/console/src/main/resources/org/apache/syncope/console/wicket/markup/html/form/preview/BinaryPDFPreviewer.html
b/console/src/main/resources/org/apache/syncope/console/wicket/markup/html/form/preview/BinaryPDFPreviewer.html
new file mode 100644
index 0000000..52e4bc2
--- /dev/null
+++ b/console/src/main/resources/org/apache/syncope/console/wicket/markup/html/form/preview/BinaryPDFPreviewer.html
@@ -0,0 +1,34 @@
+<!--
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements.  See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership.  The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License.  You may obtain a copy of the License at
+
+  http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied.  See the License for the
+specific language governing permissions and limitations
+under the License.
+-->
+<html xmlns="http://www.w3.org/1999/xhtml" xmlns:wicket="http://wicket.apache.org">
+  <wicket:extend>
+    <div wicket:id="previewContainer">
+      <span wicket:id="preview"/>
+    </div>
+
+    <wicket:fragment wicket:id="previewFragment">
+      <div><img wicket:id = "previewImage"/></div>
+    </wicket:fragment>
+    <wicket:fragment wicket:id="noPreviewFragment">
+      <div>
+        <i>Preview not available</i>
+      </div>
+    </wicket:fragment>
+  </wicket:extend>
+</html>

http://git-wip-us.apache.org/repos/asf/syncope/blob/5571cc73/pom.xml
----------------------------------------------------------------------
diff --git a/pom.xml b/pom.xml
index 2e4a78e..7044198 100644
--- a/pom.xml
+++ b/pom.xml
@@ -834,6 +834,12 @@ under the License.
       <!-- /Wicket -->
 
       <dependency>
+        <groupId>org.apache.pdfbox</groupId>
+        <artifactId>pdfbox</artifactId>
+        <version>1.8.10</version>
+      </dependency>
+
+      <dependency>
         <groupId>org.codehaus.groovy</groupId>
         <artifactId>groovy-all</artifactId>
         <version>${groovy.version}</version>


Mime
View raw message