summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTobias Kellner <tobias.kellner@iaik.tugraz.at>2016-10-10 18:35:33 +0200
committerTobias Kellner <tobias.kellner@iaik.tugraz.at>2016-10-25 17:00:13 +0200
commit5962867b411bc53bf14938b0312414f040986b54 (patch)
tree91c25af9dd61249f047f0567e0f72b87abda3091
parent1f614332bcd103229f15f51157a52824005fba96 (diff)
downloadmocca-5962867b411bc53bf14938b0312414f040986b54.tar.gz
mocca-5962867b411bc53bf14938b0312414f040986b54.tar.bz2
mocca-5962867b411bc53bf14938b0312414f040986b54.zip
Cache (and scale) custom emblem images
-rw-r--r--pdf-over-gui/src/main/java/at/asit/pdfover/gui/composites/SimpleConfigurationComposite.java4
-rw-r--r--pdf-over-gui/src/main/java/at/asit/pdfover/gui/utils/SignaturePlaceholderCache.java12
-rw-r--r--pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/PositioningState.java4
-rw-r--r--pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/PrepareSigningState.java6
-rw-r--r--pdf-over-signator/src/main/java/at/asit/pdfover/signator/CachedFileNameEmblem.java150
-rw-r--r--pdf-over-signator/src/main/java/at/asit/pdfover/signator/SignatureParameter.java4
6 files changed, 170 insertions, 10 deletions
diff --git a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/composites/SimpleConfigurationComposite.java b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/composites/SimpleConfigurationComposite.java
index a124c86d..4c02c044 100644
--- a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/composites/SimpleConfigurationComposite.java
+++ b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/composites/SimpleConfigurationComposite.java
@@ -70,7 +70,7 @@ import at.asit.pdfover.gui.workflow.config.ConfigManipulator;
import at.asit.pdfover.gui.workflow.config.ConfigurationContainer;
import at.asit.pdfover.gui.workflow.config.PersistentConfigProvider;
import at.asit.pdfover.gui.workflow.states.State;
-import at.asit.pdfover.signator.FileNameEmblem;
+import at.asit.pdfover.signator.CachedFileNameEmblem;
import at.asit.pdfover.signator.SignatureParameter;
/**
@@ -619,7 +619,7 @@ public class SimpleConfigurationComposite extends BaseConfigurationComposite {
param.setSignaturePdfACompat(this.configurationContainer.getSignaturePdfACompat());
if (image != null && !image.trim().isEmpty()) {
logo = new ImageData(image);
- param.setEmblem(new FileNameEmblem(image));
+ param.setEmblem(new CachedFileNameEmblem(image));
}
img = SignaturePlaceholderCache.getSWTPlaceholder(param);
diff --git a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/utils/SignaturePlaceholderCache.java b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/utils/SignaturePlaceholderCache.java
index 87ce24ee..306f74b7 100644
--- a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/utils/SignaturePlaceholderCache.java
+++ b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/utils/SignaturePlaceholderCache.java
@@ -33,6 +33,8 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import at.asit.pdfover.gui.Constants;
+import at.asit.pdfover.signator.CachedFileNameEmblem;
+import at.asit.pdfover.signator.Emblem;
import at.asit.pdfover.signator.SignatureParameter;
/**
@@ -71,7 +73,15 @@ public class SignaturePlaceholderCache {
final String sigNoteProp = "NOTE"; //$NON-NLS-1$
String sigLang = param.getSignatureLanguage();
- String sigEmbl = (param.getEmblem() == null ? "" : param.getEmblem().getFileName()); //$NON-NLS-1$
+ String sigEmbl = ""; //$NON-NLS-1$
+ if (param.getEmblem() != null) {
+ Emblem embl = param.getEmblem();
+ if (embl instanceof CachedFileNameEmblem) {
+ sigEmbl = ((CachedFileNameEmblem) embl).getOriginalFileName();
+ } else {
+ sigEmbl = embl.getFileName();
+ }
+ }
String sigPdfA = param.getSignaturePdfACompat() ? Constants.TRUE : Constants.FALSE;
String sigNote = param.getProperty("SIG_NOTE"); //$NON-NLS-1$
if (sigNote == null)
diff --git a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/PositioningState.java b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/PositioningState.java
index ae1303c8..32b335a7 100644
--- a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/PositioningState.java
+++ b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/PositioningState.java
@@ -36,8 +36,8 @@ import at.asit.pdfover.gui.utils.SignaturePlaceholderCache;
import at.asit.pdfover.gui.workflow.StateMachine;
import at.asit.pdfover.gui.workflow.Status;
import at.asit.pdfover.gui.workflow.config.ConfigProvider;
+import at.asit.pdfover.signator.CachedFileNameEmblem;
import at.asit.pdfover.signator.Emblem;
-import at.asit.pdfover.signator.FileNameEmblem;
import at.asit.pdfover.signator.SignatureParameter;
import at.asit.pdfover.signator.SignaturePosition;
@@ -104,7 +104,7 @@ public class PositioningState extends State {
// Update possibly changed values
ConfigProvider config = stateMachine.getConfigProvider();
SignatureParameter param = stateMachine.getPDFSigner().getPDFSigner().newParameter();
- Emblem emblem = new FileNameEmblem(config.getDefaultEmblem());
+ Emblem emblem = new CachedFileNameEmblem(config.getDefaultEmblem());
param.setEmblem(emblem);
if(config.getSignatureNote() != null && !config.getSignatureNote().isEmpty()) {
param.setProperty("SIG_NOTE", config.getSignatureNote()); //$NON-NLS-1$
diff --git a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/PrepareSigningState.java b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/PrepareSigningState.java
index 45636568..155073d6 100644
--- a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/PrepareSigningState.java
+++ b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/PrepareSigningState.java
@@ -26,14 +26,14 @@ import org.slf4j.LoggerFactory;
import at.asit.pdfover.gui.MainWindow.Buttons;
import at.asit.pdfover.gui.MainWindowBehavior;
import at.asit.pdfover.gui.composites.WaitingComposite;
-import at.asit.pdfover.gui.controls.ErrorDialog;
import at.asit.pdfover.gui.controls.Dialog.BUTTONS;
+import at.asit.pdfover.gui.controls.ErrorDialog;
import at.asit.pdfover.gui.utils.Messages;
import at.asit.pdfover.gui.workflow.StateMachine;
import at.asit.pdfover.gui.workflow.Status;
import at.asit.pdfover.gui.workflow.config.ConfigProvider;
import at.asit.pdfover.signator.BKUs;
-import at.asit.pdfover.signator.FileNameEmblem;
+import at.asit.pdfover.signator.CachedFileNameEmblem;
import at.asit.pdfover.signator.PDFFileDocumentSource;
import at.asit.pdfover.signator.SignatureParameter;
import at.asit.pdfover.signator.Signer;
@@ -144,7 +144,7 @@ public class PrepareSigningState extends State {
if (configuration.getDefaultEmblem() != null
&& !configuration.getDefaultEmblem().isEmpty()) {
- this.state.signatureParameter.setEmblem(new FileNameEmblem(
+ this.state.signatureParameter.setEmblem(new CachedFileNameEmblem(
configuration.getDefaultEmblem()));
}
diff --git a/pdf-over-signator/src/main/java/at/asit/pdfover/signator/CachedFileNameEmblem.java b/pdf-over-signator/src/main/java/at/asit/pdfover/signator/CachedFileNameEmblem.java
new file mode 100644
index 00000000..dc54d84a
--- /dev/null
+++ b/pdf-over-signator/src/main/java/at/asit/pdfover/signator/CachedFileNameEmblem.java
@@ -0,0 +1,150 @@
+/*
+ * Copyright 2012 by A-SIT, Secure Information Technology Center Austria
+ *
+ * Licensed under the EUPL, Version 1.1 or - as soon they will be approved by
+ * the European Commission - subsequent versions of the EUPL (the "Licence");
+ * You may not use this work except in compliance with the Licence.
+ * You may obtain a copy of the Licence at:
+ * http://joinup.ec.europa.eu/software/page/eupl
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the Licence is distributed on an "AS IS" basis,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the Licence for the specific language governing permissions and
+ * limitations under the Licence.
+ */
+package at.asit.pdfover.signator;
+
+// Imports
+import java.awt.Graphics2D;
+import java.awt.image.BufferedImage;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.util.Properties;
+
+import javax.imageio.ImageIO;
+
+import org.apache.commons.codec.digest.DigestUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ *
+ */
+public class CachedFileNameEmblem implements Emblem {
+ /**
+ * SLF4J Logger instance
+ **/
+ private static final Logger log = LoggerFactory
+ .getLogger(CachedFileNameEmblem.class);
+
+ private String fileName = null;
+
+ /**
+ * Constructor
+ * @param filename
+ */
+ public CachedFileNameEmblem(String filename) {
+ this.fileName = filename;
+ }
+
+ private String getFileHash(String filename) throws IOException {
+ InputStream is = Files.newInputStream(Paths.get(this.fileName));
+ return DigestUtils.md5Hex(is);
+ }
+
+ /* (non-Javadoc)
+ * @see at.asit.pdfover.signator.Emblem#getFileName()
+ */
+ @Override
+ public String getFileName() {
+ final String fileDir = System.getProperty("user.home") + File.separator + ".pdf-over"; //$NON-NLS-1$ //$NON-NLS-2$
+ final String imgFileName = ".emblem"; //$NON-NLS-1$
+ final String imgFileExt = "png"; //$NON-NLS-1$
+ final String propFileName = ".emblem.properties"; //$NON-NLS-1$
+
+ final String imgProp = "IMG"; //$NON-NLS-1$
+ final String hshProp = "HSH"; //$NON-NLS-1$
+ final int maxWidth = 480;
+ final int maxHeight = 600;
+
+ String emblemImg = this.fileName;
+ String emblemHsh = null;
+ String cachedEmblemFileName = fileDir + File.separator + imgFileName + "." + imgFileExt; //$NON-NLS-1$
+
+ if (emblemImg == null || !(new File(emblemImg).exists()))
+ return null;
+
+ Properties emblemProps = new Properties();
+ // compare cache, try to load if match
+ try {
+ InputStream in = new FileInputStream(new File(fileDir, propFileName));
+ emblemProps.load(in);
+ if (emblemImg.equals(emblemProps.getProperty(imgProp))) {
+ emblemHsh = getFileHash(emblemImg);
+ if (emblemHsh.equals(emblemProps.getProperty(hshProp))) {
+ log.debug("Emblem cache hit: " + cachedEmblemFileName); //$NON-NLS-1$
+ return cachedEmblemFileName; //$NON-NLS-1$
+ }
+ }
+ log.debug("Emblem cache miss");
+ } catch (Exception e) {
+ log.warn("Can't load emblem cache", e); //$NON-NLS-1$
+ }
+
+ try {
+ // create new cache
+ if (emblemHsh == null)
+ emblemHsh = getFileHash(emblemImg);
+ emblemProps.setProperty(imgProp, emblemImg);
+ emblemProps.setProperty(hshProp, emblemHsh);
+ BufferedImage img = ImageIO.read(new File(emblemImg));
+ int oheight = img.getHeight();
+ int owidth = img.getWidth();
+ double ratio = (double)owidth/(double)oheight;
+
+ int height = oheight;
+ int width = owidth;
+ if (height > maxHeight) {
+ height = maxHeight;
+ width = (int) (maxHeight * ratio);
+ }
+ if (width > maxWidth) {
+ width = maxWidth;
+ height = (int) (maxWidth / ratio);
+ }
+ BufferedImage cacheImg = img;
+ if (width != owidth || height == oheight) {
+ //scale image
+ log.debug("Scaling emblem: " + owidth + "x" + oheight + " to " + width + "x" + height); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
+ cacheImg = new BufferedImage(width, height, img.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : img.getType());
+ Graphics2D g = cacheImg.createGraphics();
+ g.drawImage(img, 0, 0, width, height, null);
+ g.dispose();
+ }
+
+ File file = new File(fileDir, imgFileName + "." + imgFileExt); //$NON-NLS-1$
+ ImageIO.write(cacheImg, imgFileExt, file); // ignore returned boolean
+ OutputStream out = new FileOutputStream(new File(fileDir, propFileName));
+ emblemProps.store(out, null);
+ } catch (IOException e) {
+ log.error("Can't save emblem cache", e); //$NON-NLS-1$
+ return this.fileName;
+ }
+ return cachedEmblemFileName;
+ }
+
+ /**
+ * Return the original filename
+ * @return the original filename
+ */
+ public String getOriginalFileName() {
+ return this.fileName;
+ }
+}
diff --git a/pdf-over-signator/src/main/java/at/asit/pdfover/signator/SignatureParameter.java b/pdf-over-signator/src/main/java/at/asit/pdfover/signator/SignatureParameter.java
index 8b08e3dd..8e1b965e 100644
--- a/pdf-over-signator/src/main/java/at/asit/pdfover/signator/SignatureParameter.java
+++ b/pdf-over-signator/src/main/java/at/asit/pdfover/signator/SignatureParameter.java
@@ -162,9 +162,9 @@ public abstract class SignatureParameter {
public abstract SignatureDimension getPlaceholderDimension();
/**
- * Gets the Dimension to display the Placeholder
+ * Gets the Placeholder image
*
- * @return the placeholder dimensions
+ * @return the placeholder image
*/
public abstract Image getPlaceholder();