You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2011/11/07 08:00:37 UTC

svn commit: r1198634 - in /camel/trunk: camel-core/src/main/java/org/apache/camel/builder/ camel-core/src/main/java/org/apache/camel/model/ camel-core/src/main/java/org/apache/camel/model/dataformat/ components/camel-crypto/src/main/java/org/apache/cam...

Author: davsclaus
Date: Mon Nov  7 07:00:36 2011
New Revision: 1198634

URL: http://svn.apache.org/viewvc?rev=1198634&view=rev
Log:
CAMEL-4618: Added pgp data format to DSL. Thanks to Adam for patch.

Added:
    camel/trunk/camel-core/src/main/java/org/apache/camel/model/dataformat/PGPDataFormat.java
    camel/trunk/components/camel-crypto/src/main/resources/META-INF/services/org/apache/camel/dataformat/pgp
    camel/trunk/components/camel-crypto/src/test/java/org/apache/camel/converter/crypto/AbstractPGPDataFormatTest.java
    camel/trunk/components/camel-crypto/src/test/java/org/apache/camel/converter/crypto/SpringPGPDataFormatTest.java
    camel/trunk/components/camel-crypto/src/test/resources/org/apache/camel/component/crypto/SpringPGPDataFormatTest.xml
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/builder/DataFormatClause.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/model/MarshalDefinition.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/model/UnmarshalDefinition.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/model/dataformat/DataFormatsDefinition.java
    camel/trunk/components/camel-crypto/src/main/java/org/apache/camel/converter/crypto/PGPDataFormat.java
    camel/trunk/components/camel-crypto/src/main/java/org/apache/camel/converter/crypto/PGPDataFormatUtil.java
    camel/trunk/components/camel-crypto/src/test/java/org/apache/camel/converter/crypto/PGPDataFormatTest.java
    camel/trunk/components/camel-crypto/src/test/resources/org/apache/camel/component/crypto/SpringCryptoDataFormatTest.xml

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/builder/DataFormatClause.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/DataFormatClause.java?rev=1198634&r1=1198633&r2=1198634&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/builder/DataFormatClause.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/builder/DataFormatClause.java Mon Nov  7 07:00:36 2011
@@ -34,6 +34,7 @@ import org.apache.camel.model.dataformat
 import org.apache.camel.model.dataformat.JibxDataFormat;
 import org.apache.camel.model.dataformat.JsonDataFormat;
 import org.apache.camel.model.dataformat.JsonLibrary;
+import org.apache.camel.model.dataformat.PGPDataFormat;
 import org.apache.camel.model.dataformat.ProtobufDataFormat;
 import org.apache.camel.model.dataformat.RssDataFormat;
 import org.apache.camel.model.dataformat.SerializationDataFormat;
@@ -153,6 +154,40 @@ public class DataFormatClause<T extends 
     }
 
     /**
+     * Uses the PGP data format
+     */
+    public T pgp(String keyFileName, String keyUserid) {
+        PGPDataFormat pgp = new PGPDataFormat();
+        pgp.setKeyFileName(keyFileName);
+        pgp.setKeyUserid(keyUserid);
+        return dataFormat(pgp);
+    }
+
+    /**
+     * Uses the PGP data format
+     */
+    public T pgp(String keyFileName, String keyUserid, String password) {
+        PGPDataFormat pgp = new PGPDataFormat();
+        pgp.setKeyFileName(keyFileName);
+        pgp.setKeyUserid(keyUserid);
+        pgp.setPassword(password);
+        return dataFormat(pgp);
+    }
+
+    /**
+     * Uses the PGP data format
+     */
+    public T pgp(String keyFileName, String keyUserid, String password, boolean armored, boolean integrity) {
+        PGPDataFormat pgp = new PGPDataFormat();
+        pgp.setKeyFileName(keyFileName);
+        pgp.setKeyUserid(keyUserid);
+        pgp.setPassword(password);
+        pgp.setArmored(armored);
+        pgp.setIntegrity(integrity);
+        return dataFormat(pgp);
+    }
+
+    /**
      * Uses the JAXB data format
      */
     public T jaxb() {

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/model/MarshalDefinition.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/model/MarshalDefinition.java?rev=1198634&r1=1198633&r2=1198634&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/model/MarshalDefinition.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/model/MarshalDefinition.java Mon Nov  7 07:00:36 2011
@@ -35,6 +35,7 @@ import org.apache.camel.model.dataformat
 import org.apache.camel.model.dataformat.JaxbDataFormat;
 import org.apache.camel.model.dataformat.JibxDataFormat;
 import org.apache.camel.model.dataformat.JsonDataFormat;
+import org.apache.camel.model.dataformat.PGPDataFormat;
 import org.apache.camel.model.dataformat.ProtobufDataFormat;
 import org.apache.camel.model.dataformat.RssDataFormat;
 import org.apache.camel.model.dataformat.SerializationDataFormat;
@@ -87,6 +88,7 @@ public class MarshalDefinition extends N
     @XmlElement(required = false, name = "tidyMarkup", type = TidyMarkupDataFormat.class),
     @XmlElement(required = false, name = "xmlBeans", type = XMLBeansDataFormat.class),
     @XmlElement(required = false, name = "xstream", type = XStreamDataFormat.class),
+    @XmlElement(required = false, name = "pgp", type = PGPDataFormat.class),
     @XmlElement(required = false, name = "zip", type = ZipDataFormat.class)}
     )
     private DataFormatDefinition dataFormatType;

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/model/UnmarshalDefinition.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/model/UnmarshalDefinition.java?rev=1198634&r1=1198633&r2=1198634&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/model/UnmarshalDefinition.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/model/UnmarshalDefinition.java Mon Nov  7 07:00:36 2011
@@ -35,6 +35,7 @@ import org.apache.camel.model.dataformat
 import org.apache.camel.model.dataformat.JaxbDataFormat;
 import org.apache.camel.model.dataformat.JibxDataFormat;
 import org.apache.camel.model.dataformat.JsonDataFormat;
+import org.apache.camel.model.dataformat.PGPDataFormat;
 import org.apache.camel.model.dataformat.ProtobufDataFormat;
 import org.apache.camel.model.dataformat.RssDataFormat;
 import org.apache.camel.model.dataformat.SerializationDataFormat;
@@ -87,6 +88,7 @@ public class UnmarshalDefinition extends
     @XmlElement(required = false, name = "tidyMarkup", type = TidyMarkupDataFormat.class),
     @XmlElement(required = false, name = "xmlBeans", type = XMLBeansDataFormat.class),
     @XmlElement(required = false, name = "xstream", type = XStreamDataFormat.class),
+    @XmlElement(required = false, name = "pgp", type = PGPDataFormat.class),
     @XmlElement(required = false, name = "zip", type = ZipDataFormat.class)}
     )
     private DataFormatDefinition dataFormatType;

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/model/dataformat/DataFormatsDefinition.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/model/dataformat/DataFormatsDefinition.java?rev=1198634&r1=1198633&r2=1198634&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/model/dataformat/DataFormatsDefinition.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/model/dataformat/DataFormatsDefinition.java Mon Nov  7 07:00:36 2011
@@ -58,6 +58,7 @@ public class DataFormatsDefinition {
         @XmlElement(required = false, name = "tidyMarkup", type = TidyMarkupDataFormat.class),
         @XmlElement(required = false, name = "xmlBeans", type = XMLBeansDataFormat.class),
         @XmlElement(required = false, name = "xstream", type = XStreamDataFormat.class),
+        @XmlElement(required = false, name = "pgp", type = PGPDataFormat.class),
         @XmlElement(required = false, name = "zip", type = ZipDataFormat.class)}
         )
     private List<DataFormatDefinition> dataFormats;

Added: camel/trunk/camel-core/src/main/java/org/apache/camel/model/dataformat/PGPDataFormat.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/model/dataformat/PGPDataFormat.java?rev=1198634&view=auto
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/model/dataformat/PGPDataFormat.java (added)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/model/dataformat/PGPDataFormat.java Mon Nov  7 07:00:36 2011
@@ -0,0 +1,107 @@
+/**
+ * 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.camel.model.dataformat;
+
+import javax.xml.bind.annotation.XmlAccessType;
+import javax.xml.bind.annotation.XmlAccessorType;
+import javax.xml.bind.annotation.XmlAttribute;
+import javax.xml.bind.annotation.XmlRootElement;
+
+import org.apache.camel.model.DataFormatDefinition;
+import org.apache.camel.spi.DataFormat;
+
+/**
+ * Represents a <a href="http://camel.apache.org/crypto.html">pgp</a>
+ * {@link org.apache.camel.spi.DataFormat}.
+ */
+@XmlRootElement(name = "pgp")
+@XmlAccessorType(XmlAccessType.FIELD)
+public class PGPDataFormat extends DataFormatDefinition {
+    @XmlAttribute
+    private String keyUserid;
+    @XmlAttribute
+    private String password;
+    @XmlAttribute
+    private String keyFileName;
+    @XmlAttribute
+    private Boolean armored;
+    @XmlAttribute
+    private Boolean integrity;
+
+    public PGPDataFormat() {
+        super("pgp");
+    }
+
+    @Override
+    protected void configureDataFormat(DataFormat dataFormat) {
+        if (keyUserid != null) {
+            setProperty(dataFormat, "keyUserid", keyUserid);
+        }
+        if (password != null) {
+            setProperty(dataFormat, "password", password);
+        }
+        if (keyFileName != null) {
+            setProperty(dataFormat, "keyFileName", keyFileName);
+        }
+        if (armored != null) {
+            setProperty(dataFormat, "armored", armored);
+        }
+        if (integrity != null) {
+            setProperty(dataFormat, "integrity", integrity);
+        }
+    }
+
+    public Boolean getArmored() {
+        return armored;
+    }
+
+    public void setArmored(Boolean armored) {
+        this.armored = armored;
+    }
+
+    public Boolean getIntegrity() {
+        return integrity;
+    }
+
+    public void setIntegrity(Boolean integrity) {
+        this.integrity = integrity;
+    }
+
+    public String getKeyFileName() {
+        return keyFileName;
+    }
+
+    public void setKeyFileName(String keyFileName) {
+        this.keyFileName = keyFileName;
+    }
+
+    public String getKeyUserid() {
+        return keyUserid;
+    }
+
+    public void setKeyUserid(String keyUserid) {
+        this.keyUserid = keyUserid;
+    }
+
+    public String getPassword() {
+        return password;
+    }
+
+    public void setPassword(String password) {
+        this.password = password;
+    }
+}
\ No newline at end of file

Modified: camel/trunk/components/camel-crypto/src/main/java/org/apache/camel/converter/crypto/PGPDataFormat.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-crypto/src/main/java/org/apache/camel/converter/crypto/PGPDataFormat.java?rev=1198634&r1=1198633&r2=1198634&view=diff
==============================================================================
--- camel/trunk/components/camel-crypto/src/main/java/org/apache/camel/converter/crypto/PGPDataFormat.java (original)
+++ camel/trunk/components/camel-crypto/src/main/java/org/apache/camel/converter/crypto/PGPDataFormat.java Mon Nov  7 07:00:36 2011
@@ -17,13 +17,10 @@
 package org.apache.camel.converter.crypto;
 
 import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.security.SecureRandom;
 import java.security.Security;
-import java.util.Date;
 
 import org.apache.camel.Exchange;
 import org.apache.camel.spi.DataFormat;
@@ -34,12 +31,10 @@ import org.bouncycastle.bcpg.ArmoredOutp
 import org.bouncycastle.bcpg.CompressionAlgorithmTags;
 import org.bouncycastle.jce.provider.BouncyCastleProvider;
 import org.bouncycastle.openpgp.PGPCompressedData;
-import org.bouncycastle.openpgp.PGPCompressedDataGenerator;
 import org.bouncycastle.openpgp.PGPEncryptedData;
 import org.bouncycastle.openpgp.PGPEncryptedDataGenerator;
 import org.bouncycastle.openpgp.PGPEncryptedDataList;
 import org.bouncycastle.openpgp.PGPLiteralData;
-import org.bouncycastle.openpgp.PGPLiteralDataGenerator;
 import org.bouncycastle.openpgp.PGPObjectFactory;
 import org.bouncycastle.openpgp.PGPPrivateKey;
 import org.bouncycastle.openpgp.PGPPublicKey;
@@ -48,22 +43,15 @@ import org.bouncycastle.openpgp.PGPUtil;
 import org.bouncycastle.util.io.Streams;
 
 /**
- * <code>PGPDataFormat</code> uses the bouncy castle libraries to enable
- * encryption and decryption in the PGP format I have also tested decrypting the
- * files produced using GnuPG Linux command line program gpg (GnuPG) 1.4.11
- * <ul>
- *   <li>http://www.bouncycastle.org/java.html</li>
- * <ul>
- * <p/>
+ * <code>PGPDataFormat</code> uses the <a href="http://www.bouncycastle.org/java.htm">bouncy castle</a>
+ * libraries to enable encryption and decryption in the PGP format.
  */
 public class PGPDataFormat implements DataFormat {
 
-    public static final String KEY_PUB = "CamelCryptoKeyPub";
-    public static final String KEY_PRI = "CamelCryptoKeyPri";
-
-    private PGPPublicKey configuredKey;
-    private PGPPrivateKey configuredPrivateKey;
-    private boolean armor;
+    private String keyUserid;
+    private String password;
+    private String keyFileName;
+    private boolean armored;
     private boolean integrity = true;
 
     public PGPDataFormat() {
@@ -72,42 +60,22 @@ public class PGPDataFormat implements Da
         }
     }
 
-    public void setArmored(boolean armor) {
-        this.armor = armor;
-    }
-
-    public void setIntegrity(boolean integrity) {
-        this.integrity = integrity;
-    }
-
-    /**
-     * Set the key that should be used to encrypt or decrypt incoming encrypted exchanges.
-     */
-    public void setPublicKey(PGPPublicKey key) {
-        this.configuredKey = key;
-    }
-
-    public void setPrivateKey(PGPPrivateKey key) {
-        this.configuredPrivateKey = key;
-    }
-
     public void marshal(Exchange exchange, Object graph, OutputStream outputStream) throws Exception {
-        PGPPublicKey key = getPublicKey(exchange);
+        PGPPublicKey key = PGPDataFormatUtil.findPublicKey(exchange.getContext(), this.keyFileName, this.keyUserid);
         if (key == null) {
             throw new IllegalArgumentException("Public key is null, cannot proceed");
         }
 
         InputStream plaintextStream = ExchangeHelper.convertToMandatoryType(exchange, InputStream.class, graph);
 
-        byte[] compressedData = compress(IOUtils.toByteArray(plaintextStream),
+        byte[] compressedData = PGPDataFormatUtil.compress(IOUtils.toByteArray(plaintextStream),
                 PGPLiteralData.CONSOLE, CompressionAlgorithmTags.ZIP);
 
-        if (armor) {
+        if (armored) {
             outputStream = new ArmoredOutputStream(outputStream);
         }
 
-        PGPEncryptedDataGenerator encGen = new PGPEncryptedDataGenerator(
-                PGPEncryptedData.CAST5, integrity, new SecureRandom(), "BC");
+        PGPEncryptedDataGenerator encGen = new PGPEncryptedDataGenerator(PGPEncryptedData.CAST5, integrity, new SecureRandom(), "BC");
         encGen.addMethod(key);
 
         OutputStream encOut = encGen.open(outputStream, compressedData.length);
@@ -115,9 +83,7 @@ public class PGPDataFormat implements Da
             encOut.write(compressedData);
         } finally {
             IOHelper.close(encOut);
-            if (armor) {
-                IOHelper.close(outputStream);
-            }
+            IOHelper.close(outputStream);
         }
     }
 
@@ -126,7 +92,7 @@ public class PGPDataFormat implements Da
             return null;
         }
 
-        PGPPrivateKey key = getPrivateKey(exchange);
+        PGPPrivateKey key = PGPDataFormatUtil.findPrivateKey(exchange.getContext(), keyFileName, keyUserid, password);
         if (key == null) {
             throw new IllegalArgumentException("Private key is null, cannot proceed");
         }
@@ -155,42 +121,58 @@ public class PGPDataFormat implements Da
         return Streams.readAll(ld.getInputStream());
     }
 
-    private static byte[] compress(byte[] clearData, String fileName, int algorithm) throws IOException {
-        ByteArrayOutputStream bOut = new ByteArrayOutputStream();
-        PGPCompressedDataGenerator comData = new PGPCompressedDataGenerator(algorithm);
-        OutputStream cos = comData.open(bOut); // open it with the final destination
-
-        PGPLiteralDataGenerator lData = new PGPLiteralDataGenerator();
-
-        OutputStream pOut = lData.open(cos, // the compressed output stream
-                PGPLiteralData.BINARY, fileName, // "filename" to store
-                clearData.length, // length of clear data
-                new Date() // current time
-        );
+    /**
+     * Sets if the encrypted file should be written in ascii visible text
+     */
+    public void setArmored(boolean armored) {
+        this.armored = armored;
+    }
 
-        try {
-            pOut.write(clearData);
-        } finally {
-            IOHelper.close(pOut);
-            comData.close();
-        }
-        return bOut.toByteArray();
+    public boolean getArmored() {
+        return this.armored;
     }
 
-    private PGPPublicKey getPublicKey(Exchange exchange) {
-        PGPPublicKey key = exchange.getIn().getHeader(KEY_PUB, PGPPublicKey.class);
-        if (key == null) {
-            key = configuredKey;
-        }
-        return key;
+    /**
+     * Whether or not to add a integrity check/sign to the encrypted file
+     */
+    public void setIntegrity(boolean integrity) {
+        this.integrity = integrity;
     }
 
-    private PGPPrivateKey getPrivateKey(Exchange exchange) {
-        PGPPrivateKey key = exchange.getIn().getHeader(KEY_PRI, PGPPrivateKey.class);
-        if (key == null) {
-            key = configuredPrivateKey;
-        }
-        return key;
+    public boolean getIntegrity() {
+        return this.integrity;
+    }
+
+    /**
+     * Userid of the key used to encrypt/decrypt
+     */
+    public void setKeyUserid(String keyUserid) {
+        this.keyUserid = keyUserid;
+    }
+
+    public String getKeyUserid() {
+        return keyUserid;
     }
 
+    /**
+     * filename of the keyring that will be used, classpathResource
+     */
+    public void setKeyFileName(String keyFileName) {
+        this.keyFileName = keyFileName;
+    }
+
+    public String getKeyFileName() {
+        return keyFileName;
+    }
+
+    /**
+     * Password used to open the private keyring
+     */
+    public void setPassword(String password) {
+        this.password = password;
+    }
+
+    public String getPassword() {
+        return password;
+    }
 }

Modified: camel/trunk/components/camel-crypto/src/main/java/org/apache/camel/converter/crypto/PGPDataFormatUtil.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-crypto/src/main/java/org/apache/camel/converter/crypto/PGPDataFormatUtil.java?rev=1198634&r1=1198633&r2=1198634&view=diff
==============================================================================
--- camel/trunk/components/camel-crypto/src/main/java/org/apache/camel/converter/crypto/PGPDataFormatUtil.java (original)
+++ camel/trunk/components/camel-crypto/src/main/java/org/apache/camel/converter/crypto/PGPDataFormatUtil.java Mon Nov  7 07:00:36 2011
@@ -16,14 +16,22 @@
  */
 package org.apache.camel.converter.crypto;
 
+import java.io.ByteArrayOutputStream;
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.OutputStream;
 import java.security.NoSuchProviderException;
+import java.util.Date;
 import java.util.Iterator;
 
+import org.apache.camel.CamelContext;
 import org.apache.camel.util.IOHelper;
+import org.apache.camel.util.ResourceHelper;
+import org.bouncycastle.openpgp.PGPCompressedDataGenerator;
 import org.bouncycastle.openpgp.PGPException;
+import org.bouncycastle.openpgp.PGPLiteralData;
+import org.bouncycastle.openpgp.PGPLiteralDataGenerator;
 import org.bouncycastle.openpgp.PGPPrivateKey;
 import org.bouncycastle.openpgp.PGPPublicKey;
 import org.bouncycastle.openpgp.PGPPublicKeyRing;
@@ -38,20 +46,21 @@ public final class PGPDataFormatUtil {
     private PGPDataFormatUtil() {
     }
 
-    public static PGPPublicKey findPublicKey(String filename, String userid) throws IOException, PGPException,
+    public static PGPPublicKey findPublicKey(CamelContext context, String filename, String userid) throws IOException, PGPException,
             NoSuchProviderException {
-        FileInputStream fis = new FileInputStream(filename);
+
+        InputStream is = ResourceHelper.resolveMandatoryResourceAsInputStream(context.getClassResolver(), filename);
         PGPPublicKey privKey;
         try {
-            privKey = findPublicKey(fis, userid);
+            privKey = findPublicKey(context, is, userid);
         } finally {
-            IOHelper.close(fis);
+            IOHelper.close(is);
         }
         return privKey;
     }
 
     @SuppressWarnings("unchecked")
-    public static PGPPublicKey findPublicKey(InputStream input, String userid) throws IOException, PGPException,
+    public static PGPPublicKey findPublicKey(CamelContext context, InputStream input, String userid) throws IOException, PGPException,
             NoSuchProviderException {
         PGPPublicKeyRingCollection pgpSec = new PGPPublicKeyRingCollection(PGPUtil.getDecoderStream(input));
 
@@ -75,20 +84,22 @@ public final class PGPDataFormatUtil {
         return null;
     }
 
-    public static PGPPrivateKey findPrivateKey(String filename, String userid, String passphrase) throws IOException,
+    public static PGPPrivateKey findPrivateKey(CamelContext context, String filename, String userid, String passphrase) throws IOException,
             PGPException, NoSuchProviderException {
-        FileInputStream fis = new FileInputStream(filename);
+
+        InputStream is = ResourceHelper.resolveMandatoryResourceAsInputStream(context.getClassResolver(), filename);
+
         PGPPrivateKey privKey;
         try {
-            privKey = findPrivateKey(fis, userid, passphrase);
+            privKey = findPrivateKey(context, is, userid, passphrase);
         } finally {
-            IOHelper.close(fis);
+            IOHelper.close(is);
         }
         return privKey;
     }
 
     @SuppressWarnings("unchecked")
-    public static PGPPrivateKey findPrivateKey(InputStream input, String userid, String passphrase) throws IOException,
+    public static PGPPrivateKey findPrivateKey(CamelContext context, InputStream input, String userid, String passphrase) throws IOException,
             PGPException, NoSuchProviderException {
         PGPSecretKeyRingCollection pgpSec = new PGPSecretKeyRingCollection(PGPUtil.getDecoderStream(input));
 
@@ -111,4 +122,26 @@ public final class PGPDataFormatUtil {
         return null;
     }
 
+    public static byte[] compress(byte[] clearData, String fileName, int algorithm) throws IOException {
+        ByteArrayOutputStream bOut = new ByteArrayOutputStream();
+        PGPCompressedDataGenerator comData = new PGPCompressedDataGenerator(algorithm);
+        OutputStream cos = comData.open(bOut); // open it with the final destination
+
+        PGPLiteralDataGenerator lData = new PGPLiteralDataGenerator();
+
+        OutputStream pOut = lData.open(cos, // the compressed output stream
+                PGPLiteralData.BINARY, fileName, // "filename" to store
+                clearData.length, // length of clear data
+                new Date() // current time
+        );
+
+        try {
+            pOut.write(clearData);
+        } finally {
+            IOHelper.close(pOut);
+            comData.close();
+        }
+        return bOut.toByteArray();
+    }
+
 }

Added: camel/trunk/components/camel-crypto/src/main/resources/META-INF/services/org/apache/camel/dataformat/pgp
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-crypto/src/main/resources/META-INF/services/org/apache/camel/dataformat/pgp?rev=1198634&view=auto
==============================================================================
--- camel/trunk/components/camel-crypto/src/main/resources/META-INF/services/org/apache/camel/dataformat/pgp (added)
+++ camel/trunk/components/camel-crypto/src/main/resources/META-INF/services/org/apache/camel/dataformat/pgp Mon Nov  7 07:00:36 2011
@@ -0,0 +1,17 @@
+#
+# 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.
+#
+class=org.apache.camel.converter.crypto.PGPDataFormat

Added: camel/trunk/components/camel-crypto/src/test/java/org/apache/camel/converter/crypto/AbstractPGPDataFormatTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-crypto/src/test/java/org/apache/camel/converter/crypto/AbstractPGPDataFormatTest.java?rev=1198634&view=auto
==============================================================================
--- camel/trunk/components/camel-crypto/src/test/java/org/apache/camel/converter/crypto/AbstractPGPDataFormatTest.java (added)
+++ camel/trunk/components/camel-crypto/src/test/java/org/apache/camel/converter/crypto/AbstractPGPDataFormatTest.java Mon Nov  7 07:00:36 2011
@@ -0,0 +1,65 @@
+/**
+ * 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.camel.converter.crypto;
+
+import java.io.ByteArrayInputStream;
+import java.util.Map;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.Exchange;
+import org.apache.camel.InvalidPayloadException;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.apache.camel.util.ExchangeHelper;
+
+public abstract class AbstractPGPDataFormatTest extends CamelTestSupport {
+    
+    protected void doRoundTripEncryptionTests(String endpoint, Map<String, Object> headers) throws Exception {
+        MockEndpoint encrypted = setupExpectations(context, 3, "mock:encrypted");
+        MockEndpoint unencrypted = setupExpectations(context, 3, "mock:unencrypted");
+
+        String payload = "Hi Alice, Be careful Eve is listening, signed Bob";
+        template.sendBodyAndHeaders(endpoint, payload, headers);
+        template.sendBodyAndHeaders(endpoint, payload.getBytes(), headers);
+        template.sendBodyAndHeaders(endpoint, new ByteArrayInputStream(payload.getBytes()), headers);
+
+        assertMocksSatisfied(encrypted, unencrypted, payload);
+    }
+
+    protected void assertMocksSatisfied(MockEndpoint encrypted, MockEndpoint unencrypted, String payload) throws InterruptedException, InvalidPayloadException {
+        awaitAndAssert(unencrypted);
+        awaitAndAssert(encrypted);
+        for (Exchange e : unencrypted.getReceivedExchanges()) {
+            assertEquals(payload, ExchangeHelper.getMandatoryInBody(e, String.class));
+        }
+        for (Exchange e : encrypted.getReceivedExchanges()) {
+            byte[] ciphertext = ExchangeHelper.getMandatoryInBody(e, byte[].class);
+            assertNotSame(payload, new String(ciphertext));
+        }
+    }
+
+    protected void awaitAndAssert(MockEndpoint mock) throws InterruptedException {
+        mock.assertIsSatisfied();
+    }
+
+    public MockEndpoint setupExpectations(CamelContext context, int expected, String mock) {
+        MockEndpoint mockEp = context.getEndpoint(mock, MockEndpoint.class);
+        mockEp.expectedMessageCount(expected);
+        return mockEp;
+    }
+
+}

Modified: camel/trunk/components/camel-crypto/src/test/java/org/apache/camel/converter/crypto/PGPDataFormatTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-crypto/src/test/java/org/apache/camel/converter/crypto/PGPDataFormatTest.java?rev=1198634&r1=1198633&r2=1198634&view=diff
==============================================================================
--- camel/trunk/components/camel-crypto/src/test/java/org/apache/camel/converter/crypto/PGPDataFormatTest.java (original)
+++ camel/trunk/components/camel-crypto/src/test/java/org/apache/camel/converter/crypto/PGPDataFormatTest.java Mon Nov  7 07:00:36 2011
@@ -16,26 +16,12 @@
  */
 package org.apache.camel.converter.crypto;
 
-import java.io.ByteArrayInputStream;
 import java.util.HashMap;
-import java.util.Map;
 
-import org.apache.camel.CamelContext;
-import org.apache.camel.Exchange;
 import org.apache.camel.builder.RouteBuilder;
-import org.apache.camel.component.mock.MockEndpoint;
-import org.apache.camel.test.junit4.CamelTestSupport;
-import org.apache.camel.util.ExchangeHelper;
-import org.bouncycastle.openpgp.PGPPrivateKey;
-import org.bouncycastle.openpgp.PGPPublicKey;
 import org.junit.Test;
 
-public class PGPDataFormatTest extends CamelTestSupport {
-
-    static String keyFileName = "src/test/resources/org/apache/camel/component/crypto/pubring.gpg";
-    static String keyFileNameSec = "src/test/resources/org/apache/camel/component/crypto/secring.gpg";
-    static String keyUserid = "sdude@nowhere.net";
-    static String keyPassword = "sdude";
+public class PGPDataFormatTest extends AbstractPGPDataFormatTest {
 
     @Test
     public void testEncryption() throws Exception {
@@ -43,76 +29,59 @@ public class PGPDataFormatTest extends C
     }
 
     @Test
-    public void testEncryptionHeaders() throws Exception {
-        doRoundTripEncryptionTests("direct:inlineHeaders", new HashMap<String, Object>());
+    public void testEncryption2() throws Exception {
+        doRoundTripEncryptionTests("direct:inline2", new HashMap<String, Object>());
     }
 
-    private void doRoundTripEncryptionTests(String endpoint, Map<String, Object> headers) throws Exception {
-        MockEndpoint encrypted = setupExpectations(context, 3, "mock:encrypted");
-        MockEndpoint unencrypted = setupExpectations(context, 3, "mock:unencrypted");
-
-        String payload = "Hi Alice, Be careful Eve is listening, signed Bob";
-        template.sendBodyAndHeaders(endpoint, payload, headers);
-        template.sendBodyAndHeaders(endpoint, payload.getBytes(), headers);
-        template.sendBodyAndHeaders(endpoint, new ByteArrayInputStream(payload.getBytes()), headers);
-
-        assertMocksSatisfied(encrypted, unencrypted, payload);
-    }
-
-    private void assertMocksSatisfied(MockEndpoint encrypted, MockEndpoint unencrypted, String payload) throws Exception {
-        awaitAndAssert(unencrypted);
-        awaitAndAssert(encrypted);
-        for (Exchange e : unencrypted.getReceivedExchanges()) {
-            assertEquals(payload, ExchangeHelper.getMandatoryInBody(e, String.class));
-        }
-        for (Exchange e : encrypted.getReceivedExchanges()) {
-            byte[] ciphertext = ExchangeHelper.getMandatoryInBody(e, byte[].class);
-            assertNotSame(payload, new String(ciphertext));
-        }
+    @Test
+    public void testEncryptionArmor() throws Exception {
+        doRoundTripEncryptionTests("direct:inline-armor", new HashMap<String, Object>());
     }
 
     protected RouteBuilder createRouteBuilder() {
         return new RouteBuilder() {
             public void configure() throws Exception {
                 // START SNIPPET: pgp-format
-                PGPDataFormat pgpDataFormat = new PGPDataFormat();
-                PGPPublicKey pKey = PGPDataFormatUtil.findPublicKey(keyFileName, keyUserid);
-                PGPPrivateKey sKey = PGPDataFormatUtil.findPrivateKey(keyFileNameSec, keyUserid, keyPassword);
-                pgpDataFormat.setPublicKey(pKey);
-                pgpDataFormat.setPrivateKey(sKey);
+                // Public Key FileName
+                String keyFileName = "org/apache/camel/component/crypto/pubring.gpg";
+                // Private Key FileName
+                String keyFileNameSec = "org/apache/camel/component/crypto/secring.gpg";
+                // Keyring Userid Used to Encrypt
+                String keyUserid = "sdude@nowhere.net";
+                // Private key password
+                String keyPassword = "sdude";
 
                 from("direct:inline")
-                    .marshal(pgpDataFormat)
-                    .to("mock:encrypted")
-                    .unmarshal(pgpDataFormat)
-                    .to("mock:unencrypted");
+                        .marshal().pgp(keyFileName, keyUserid)
+                        .to("mock:encrypted")
+                        .unmarshal().pgp(keyFileNameSec, keyUserid, keyPassword)
+                        .to("mock:unencrypted");
                 // END SNIPPET: pgp-format
 
                 // START SNIPPET: pgp-format-header
-                PGPDataFormat pgpDataFormatNoKey = new PGPDataFormat();
-                pgpDataFormat.setPublicKey(pKey);
-                pgpDataFormat.setPrivateKey(sKey);
-
-                from("direct:inlineHeaders")
-                    .setHeader(PGPDataFormat.KEY_PUB).constant(pKey)
-                    .setHeader(PGPDataFormat.KEY_PRI).constant(sKey)
-                    .marshal(pgpDataFormatNoKey)
-                    .to("mock:encrypted")
-                    .unmarshal(pgpDataFormatNoKey)
-                    .to("mock:unencrypted");
+                PGPDataFormat pgpEncrypt = new PGPDataFormat();
+                pgpEncrypt.setKeyFileName(keyFileName);
+                pgpEncrypt.setKeyUserid(keyUserid);
+
+                PGPDataFormat pgpDecrypt = new PGPDataFormat();
+                pgpDecrypt.setKeyFileName(keyFileNameSec);
+                pgpDecrypt.setKeyUserid(keyUserid);
+                pgpDecrypt.setPassword(keyPassword);
+
+                from("direct:inline2")
+                        .marshal(pgpEncrypt)
+                        .to("mock:encrypted")
+                        .unmarshal(pgpDecrypt)
+                        .to("mock:unencrypted");
+
+                from("direct:inline-armor")
+                        .marshal().pgp(keyFileName, keyUserid, null, true, true)
+                        .to("mock:encrypted")
+                        .unmarshal().pgp(keyFileNameSec, keyUserid, keyPassword, true, true)
+                        .to("mock:unencrypted");
                 // END SNIPPET: pgp-format-header
             }
         };
     }
 
-    private void awaitAndAssert(MockEndpoint mock) throws InterruptedException {
-        mock.assertIsSatisfied();
-    }
-
-    public MockEndpoint setupExpectations(CamelContext context, int expected, String mock) {
-        MockEndpoint mockEp = context.getEndpoint(mock, MockEndpoint.class);
-        mockEp.expectedMessageCount(expected);
-        return mockEp;
-    }
-
 }

Added: camel/trunk/components/camel-crypto/src/test/java/org/apache/camel/converter/crypto/SpringPGPDataFormatTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-crypto/src/test/java/org/apache/camel/converter/crypto/SpringPGPDataFormatTest.java?rev=1198634&view=auto
==============================================================================
--- camel/trunk/components/camel-crypto/src/test/java/org/apache/camel/converter/crypto/SpringPGPDataFormatTest.java (added)
+++ camel/trunk/components/camel-crypto/src/test/java/org/apache/camel/converter/crypto/SpringPGPDataFormatTest.java Mon Nov  7 07:00:36 2011
@@ -0,0 +1,36 @@
+/**
+ * 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.camel.converter.crypto;
+
+import java.util.HashMap;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.spring.SpringCamelContext;
+import org.junit.Test;
+
+public class SpringPGPDataFormatTest extends AbstractPGPDataFormatTest {
+
+    protected CamelContext createCamelContext() throws Exception {
+        return SpringCamelContext.springCamelContext("/org/apache/camel/component/crypto/SpringPGPDataFormatTest.xml");
+    }
+
+    @Test
+    public void testEncryption() throws Exception {
+        doRoundTripEncryptionTests("direct:inline", new HashMap<String, Object>());
+    }
+
+}

Modified: camel/trunk/components/camel-crypto/src/test/resources/org/apache/camel/component/crypto/SpringCryptoDataFormatTest.xml
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-crypto/src/test/resources/org/apache/camel/component/crypto/SpringCryptoDataFormatTest.xml?rev=1198634&r1=1198633&r2=1198634&view=diff
==============================================================================
--- camel/trunk/components/camel-crypto/src/test/resources/org/apache/camel/component/crypto/SpringCryptoDataFormatTest.xml (original)
+++ camel/trunk/components/camel-crypto/src/test/resources/org/apache/camel/component/crypto/SpringCryptoDataFormatTest.xml Mon Nov  7 07:00:36 2011
@@ -1,13 +1,20 @@
 <?xml version="1.0" encoding="UTF-8"?>
-  <!--
-    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.
-  -->
+<!--
+    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.
+-->
 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

Added: camel/trunk/components/camel-crypto/src/test/resources/org/apache/camel/component/crypto/SpringPGPDataFormatTest.xml
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-crypto/src/test/resources/org/apache/camel/component/crypto/SpringPGPDataFormatTest.xml?rev=1198634&view=auto
==============================================================================
--- camel/trunk/components/camel-crypto/src/test/resources/org/apache/camel/component/crypto/SpringPGPDataFormatTest.xml (added)
+++ camel/trunk/components/camel-crypto/src/test/resources/org/apache/camel/component/crypto/SpringPGPDataFormatTest.xml Mon Nov  7 07:00:36 2011
@@ -0,0 +1,45 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+    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.
+-->
+<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+       xsi:schemaLocation="
+       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
+       http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
+    ">
+
+  <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
+    <!-- START SNIPPET: pgp-xml-basic -->
+    <dataFormats>
+      <!-- will load the file from classpath by default, but you can prefix with file: to load from file system -->
+      <pgp id="encrypt" keyFileName="org/apache/camel/component/crypto/pubring.gpg"
+           keyUserid="sdude@nowhere.net"/>
+      <pgp id="decrypt" keyFileName="org/apache/camel/component/crypto/secring.gpg"
+           keyUserid="sdude@nowhere.net" password="sdude"/>
+    </dataFormats>
+
+    <route>
+      <from uri="direct:inline"/>
+      <marshal ref="encrypt"/>
+      <to uri="mock:encrypted"/>
+      <unmarshal ref="decrypt"/>
+      <to uri="mock:unencrypted"/>
+    </route>
+    <!-- END SNIPPET: pgp-xml-basic -->
+  </camelContext>
+
+
+</beans>
\ No newline at end of file