Author: davsclaus
Date: Sat Nov 5 07:30:32 2011
New Revision: 1197898
URL: http://svn.apache.org/viewvc?rev=1197898&view=rev
Log:
CAMEL-4549: Applied patch. Thanks to Adam.
Modified:
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
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=1197898&r1=1197897&r2=1197898&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
Sat Nov 5 07:30:32 2011
@@ -22,6 +22,7 @@ import java.io.InputStream;
import java.security.NoSuchProviderException;
import java.util.Iterator;
+import org.apache.camel.util.IOHelper;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPrivateKey;
import org.bouncycastle.openpgp.PGPPublicKey;
@@ -33,42 +34,40 @@ import org.bouncycastle.openpgp.PGPSecre
import org.bouncycastle.openpgp.PGPUtil;
public final class PGPDataFormatUtil {
-
+
private PGPDataFormatUtil() {
-
}
-
+
public static PGPPublicKey findPublicKey(String filename, String userid)
throws IOException, PGPException,
- NoSuchProviderException {
+ NoSuchProviderException {
FileInputStream fis = new FileInputStream(filename);
PGPPublicKey privKey;
try {
privKey = findPublicKey(fis, userid);
} finally {
- fis.close();
+ IOHelper.close(fis);
}
return privKey;
}
+ @SuppressWarnings("unchecked")
public static PGPPublicKey findPublicKey(InputStream input, String userid)
throws IOException, PGPException,
- NoSuchProviderException {
+ NoSuchProviderException {
PGPPublicKeyRingCollection pgpSec = new
PGPPublicKeyRingCollection(PGPUtil.getDecoderStream(input));
- @SuppressWarnings("unchecked")
- Iterator<PGPPublicKeyRing> keyRingIter =
(Iterator<PGPPublicKeyRing>)pgpSec.getKeyRings();
+ Iterator<PGPPublicKeyRing> keyRingIter = (Iterator<PGPPublicKeyRing>)
pgpSec.getKeyRings();
while (keyRingIter.hasNext()) {
PGPPublicKeyRing keyRing = keyRingIter.next();
- @SuppressWarnings("unchecked")
- Iterator<PGPPublicKey> keyIter =
(Iterator<PGPPublicKey>)keyRing.getPublicKeys();
+ Iterator<PGPPublicKey> keyIter = (Iterator<PGPPublicKey>)
keyRing.getPublicKeys();
+ String keyUserId = null;
while (keyIter.hasNext()) {
- PGPPublicKey key = (PGPPublicKey)keyIter.next();
- for (@SuppressWarnings("unchecked")
- Iterator<String> iterator =
(Iterator<String>)key.getUserIDs(); iterator.hasNext();) {
- String userId = iterator.next();
- if (key.isEncryptionKey() && userId.contains(userid)) {
- return key;
- }
+ PGPPublicKey key = keyIter.next();
+ for (Iterator<String> iterator = (Iterator<String>)
key.getUserIDs(); iterator.hasNext();) {
+ keyUserId = iterator.next();
+ }
+ if (key.isEncryptionKey() && keyUserId != null &&
keyUserId.contains(userid)) {
+ return key;
}
}
}
@@ -76,33 +75,31 @@ public final class PGPDataFormatUtil {
return null;
}
- public static PGPPrivateKey findPrivateKey(String filename, String userid,
String passphrase)
- throws IOException, PGPException, NoSuchProviderException {
+ public static PGPPrivateKey findPrivateKey(String filename, String userid,
String passphrase) throws IOException,
+ PGPException, NoSuchProviderException {
FileInputStream fis = new FileInputStream(filename);
PGPPrivateKey privKey;
try {
privKey = findPrivateKey(fis, userid, passphrase);
} finally {
- fis.close();
+ IOHelper.close(fis);
}
return privKey;
}
- public static PGPPrivateKey findPrivateKey(InputStream input, String
userid, String passphrase)
- throws IOException, PGPException, NoSuchProviderException {
+ @SuppressWarnings("unchecked")
+ public static PGPPrivateKey findPrivateKey(InputStream input, String
userid, String passphrase) throws IOException,
+ PGPException, NoSuchProviderException {
PGPSecretKeyRingCollection pgpSec = new
PGPSecretKeyRingCollection(PGPUtil.getDecoderStream(input));
- @SuppressWarnings("unchecked")
- Iterator<PGPSecretKeyRing> keyRingIter =
(Iterator<PGPSecretKeyRing>)pgpSec.getKeyRings();
+ Iterator<PGPSecretKeyRing> keyRingIter = (Iterator<PGPSecretKeyRing>)
pgpSec.getKeyRings();
while (keyRingIter.hasNext()) {
PGPSecretKeyRing keyRing = keyRingIter.next();
- @SuppressWarnings("unchecked")
- Iterator<PGPSecretKey> keyIter =
(Iterator<PGPSecretKey>)keyRing.getSecretKeys();
+ Iterator<PGPSecretKey> keyIter = (Iterator<PGPSecretKey>)
keyRing.getSecretKeys();
while (keyIter.hasNext()) {
- PGPSecretKey key = (PGPSecretKey)keyIter.next();
- for (@SuppressWarnings("unchecked")
- Iterator<String> iterator =
(Iterator<String>)key.getUserIDs(); iterator.hasNext();) {
+ PGPSecretKey key = keyIter.next();
+ for (Iterator<String> iterator = (Iterator<String>)
key.getUserIDs(); iterator.hasNext();) {
String userId = iterator.next();
if (key.isSigningKey() && userId.contains(userid)) {
return key.extractPrivateKey(passphrase.toCharArray(),
"BC");
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=1197898&r1=1197897&r2=1197898&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
Sat Nov 5 07:30:32 2011
@@ -22,13 +22,12 @@ import java.util.Map;
import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
-import org.apache.camel.InvalidPayloadException;
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.bouncycastle.openpgp.PGPPublicKey;
import org.junit.Test;
public class PGPDataFormatTest extends CamelTestSupport {
@@ -60,8 +59,7 @@ public class PGPDataFormatTest extends C
assertMocksSatisfied(encrypted, unencrypted, payload);
}
- private void assertMocksSatisfied(MockEndpoint encrypted, MockEndpoint
unencrypted, String payload)
- throws InterruptedException, InvalidPayloadException {
+ private void assertMocksSatisfied(MockEndpoint encrypted, MockEndpoint
unencrypted, String payload) throws Exception {
awaitAndAssert(unencrypted);
awaitAndAssert(encrypted);
for (Exchange e : unencrypted.getReceivedExchanges()) {
@@ -73,26 +71,36 @@ public class PGPDataFormatTest extends C
}
}
- @Override
protected RouteBuilder createRouteBuilder() {
return new RouteBuilder() {
public void configure() throws Exception {
- PGPDataFormat cryptoFormat = new PGPDataFormat();
+ // START SNIPPET: pgp-format
+ PGPDataFormat pgpDataFormat = new PGPDataFormat();
PGPPublicKey pKey =
PGPDataFormatUtil.findPublicKey(keyFileName, keyUserid);
PGPPrivateKey sKey =
PGPDataFormatUtil.findPrivateKey(keyFileNameSec, keyUserid, keyPassword);
- cryptoFormat.setPublicKey(pKey);
- cryptoFormat.setPrivateKey(sKey);
+ pgpDataFormat.setPublicKey(pKey);
+ pgpDataFormat.setPrivateKey(sKey);
-
from("direct:inline").marshal(cryptoFormat).to("mock:encrypted").unmarshal(cryptoFormat)
+ from("direct:inline")
+ .marshal(pgpDataFormat)
+ .to("mock:encrypted")
+ .unmarshal(pgpDataFormat)
.to("mock:unencrypted");
+ // END SNIPPET: pgp-format
- PGPDataFormat cryptoFormatNoKey = new PGPDataFormat();
- cryptoFormat.setPublicKey(pKey);
- cryptoFormat.setPrivateKey(sKey);
-
-
from("direct:inlineHeaders").setHeader(PGPDataFormat.KEY_PUB).constant(pKey)
-
.setHeader(PGPDataFormat.KEY_PRI).constant(sKey).marshal(cryptoFormatNoKey)
-
.to("mock:encrypted").unmarshal(cryptoFormatNoKey).to("mock:unencrypted");
+ // 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");
+ // END SNIPPET: pgp-format-header
}
};
}