You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ni...@apache.org on 2013/10/14 10:38:36 UTC
[1/2] git commit: CAMEL-6860 supports parameterize encryption and
hash algorithm in PGP Data Formater with thanks to Franz
Updated Branches:
refs/heads/master 32f32b94a -> 47e76f104
CAMEL-6860 supports parameterize encryption and hash algorithm in PGP Data Formater with thanks to Franz
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/47e76f10
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/47e76f10
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/47e76f10
Branch: refs/heads/master
Commit: 47e76f1048063308504fbbae38105766c1ba1306
Parents: 9d7d54e
Author: Willem Jiang <ni...@apache.org>
Authored: Mon Oct 14 16:27:10 2013 +0800
Committer: Willem Jiang <ni...@apache.org>
Committed: Mon Oct 14 16:35:33 2013 +0800
----------------------------------------------------------------------
.../camel/converter/crypto/PGPDataFormat.java | 45 +++++++++++++++++++-
.../crypto/PGPDataFormatDynamicTest.java | 20 +++++++++
.../converter/crypto/PGPDataFormatTest.java | 17 ++++++++
.../crypto/SpringPGPDataFormatTest.xml | 1 +
4 files changed, 81 insertions(+), 2 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/47e76f10/components/camel-crypto/src/main/java/org/apache/camel/converter/crypto/PGPDataFormat.java
----------------------------------------------------------------------
diff --git a/components/camel-crypto/src/main/java/org/apache/camel/converter/crypto/PGPDataFormat.java b/components/camel-crypto/src/main/java/org/apache/camel/converter/crypto/PGPDataFormat.java
index eab0e71..e6d8921 100644
--- a/components/camel-crypto/src/main/java/org/apache/camel/converter/crypto/PGPDataFormat.java
+++ b/components/camel-crypto/src/main/java/org/apache/camel/converter/crypto/PGPDataFormat.java
@@ -84,6 +84,8 @@ public class PGPDataFormat extends ServiceSupport implements DataFormat {
public static final String SIGNATURE_KEY_RING = "CamelPGPDataFormatSignatureKeyRing";
public static final String SIGNATURE_KEY_USERID = "CamelPGPDataFormatSignatureKeyUserid";
public static final String SIGNATURE_KEY_PASSWORD = "CamelPGPDataFormatSignatureKeyPassword";
+ public static final String ENCRYPTION_ALGORITHM = "CamelPGPDataFormatEncryptionAlgorithm";
+ public static final String SIGNATURE_HASH_ALGORITHM = "CamelPGPDataFormatSignatureHashAlgorithm";
private static final Logger LOG = LoggerFactory.getLogger(PGPDataFormat.class);
@@ -109,6 +111,19 @@ public class PGPDataFormat extends ServiceSupport implements DataFormat {
private boolean armored;
private boolean integrity = true;
+
+ /** Digest algorithm for signing (marshal).
+ * Possible values are defined in {@link HashAlgorithmTags}.
+ * Default value is SHA1.
+ */
+ private int hashAlgorithm = HashAlgorithmTags.SHA1;
+
+ /**
+ * Symmetric key algorithm for encryption (marschal).
+ * Possible values are defined in {@link SymmetricKeyAlgorithmTags}.
+ * Default value is CAST5.
+ */
+ private int algorithm = SymmetricKeyAlgorithmTags.CAST5;
public PGPDataFormat() {
}
@@ -144,6 +159,14 @@ public class PGPDataFormat extends ServiceSupport implements DataFormat {
protected String findSignatureKeyPassword(Exchange exchange) {
return exchange.getIn().getHeader(SIGNATURE_KEY_PASSWORD, getSignaturePassword(), String.class);
}
+
+ protected int findAlgorithm(Exchange exchange) {
+ return exchange.getIn().getHeader(ENCRYPTION_ALGORITHM, getAlgorithm(), Integer.class);
+ }
+
+ protected int findHashAlgorithm(Exchange exchange) {
+ return exchange.getIn().getHeader(SIGNATURE_HASH_ALGORITHM, getHashAlgorithm(), Integer.class);
+ }
public void marshal(Exchange exchange, Object graph, OutputStream outputStream) throws Exception {
PGPPublicKey key = PGPDataFormatUtil.findPublicKey(exchange.getContext(), findKeyFileName(exchange),
@@ -158,7 +181,7 @@ public class PGPDataFormat extends ServiceSupport implements DataFormat {
outputStream = new ArmoredOutputStream(outputStream);
}
- PGPEncryptedDataGenerator encGen = new PGPEncryptedDataGenerator(new JcePGPDataEncryptorBuilder(SymmetricKeyAlgorithmTags.CAST5)
+ PGPEncryptedDataGenerator encGen = new PGPEncryptedDataGenerator(new JcePGPDataEncryptorBuilder(findAlgorithm(exchange))
.setWithIntegrityPacket(integrity).setSecureRandom(new SecureRandom()).setProvider(getProvider()));
encGen.addMethod(new JcePublicKeyKeyEncryptionMethodGenerator(key));
OutputStream encOut = encGen.open(outputStream, new byte[BUFFER_SIZE]);
@@ -224,7 +247,7 @@ public class PGPDataFormat extends ServiceSupport implements DataFormat {
int algorithm = sigSecretKey.getPublicKey().getAlgorithm();
PGPSignatureGenerator sigGen = new PGPSignatureGenerator(
- new JcaPGPContentSignerBuilder(algorithm, HashAlgorithmTags.SHA1).setProvider(getProvider()));
+ new JcaPGPContentSignerBuilder(algorithm, findHashAlgorithm(exchange)).setProvider(getProvider()));
sigGen.init(PGPSignature.BINARY_DOCUMENT, sigPrivateKey);
sigGen.setHashedSubpackets(spGen.generate());
sigGen.generateOnePassVersion(false).encode(out);
@@ -425,6 +448,24 @@ public class PGPDataFormat extends ServiceSupport implements DataFormat {
public void setProvider(String provider) {
this.provider = provider;
}
+
+
+
+ public int getHashAlgorithm() {
+ return hashAlgorithm;
+ }
+
+ public void setHashAlgorithm(int hashAlgorithm) {
+ this.hashAlgorithm = hashAlgorithm;
+ }
+
+ public int getAlgorithm() {
+ return algorithm;
+ }
+
+ public void setAlgorithm(int algorithm) {
+ this.algorithm = algorithm;
+ }
@Override
protected void doStart() throws Exception {
http://git-wip-us.apache.org/repos/asf/camel/blob/47e76f10/components/camel-crypto/src/test/java/org/apache/camel/converter/crypto/PGPDataFormatDynamicTest.java
----------------------------------------------------------------------
diff --git a/components/camel-crypto/src/test/java/org/apache/camel/converter/crypto/PGPDataFormatDynamicTest.java b/components/camel-crypto/src/test/java/org/apache/camel/converter/crypto/PGPDataFormatDynamicTest.java
index d28316c..af8d8c5 100644
--- a/components/camel-crypto/src/test/java/org/apache/camel/converter/crypto/PGPDataFormatDynamicTest.java
+++ b/components/camel-crypto/src/test/java/org/apache/camel/converter/crypto/PGPDataFormatDynamicTest.java
@@ -19,16 +19,34 @@ package org.apache.camel.converter.crypto;
import java.util.HashMap;
import java.util.Map;
+import org.bouncycastle.bcpg.HashAlgorithmTags;
+import org.bouncycastle.bcpg.SymmetricKeyAlgorithmTags;
+
public class PGPDataFormatDynamicTest extends PGPDataFormatTest {
// setup a wrong userid
+ @Override
protected String getKeyUserId() {
return "wrong";
}
// setup a wrong password
+ @Override
protected String getKeyPassword() {
return "wrong";
}
+
+
+ //setup wrong algorithm
+ @Override
+ protected int getAlgorithm() {
+ return -5;
+ }
+
+ //setup wrong hash algorithm
+ protected int getHashAlgorithm() {
+ return -5;
+ }
+
// override wrong userid and password with correct userid and password in the headers
protected Map<String, Object> getHeaders() {
@@ -37,6 +55,8 @@ public class PGPDataFormatDynamicTest extends PGPDataFormatTest {
headers.put(PGPDataFormat.SIGNATURE_KEY_USERID, "sdude@nowhere.net");
headers.put(PGPDataFormat.KEY_PASSWORD, "sdude");
headers.put(PGPDataFormat.SIGNATURE_KEY_PASSWORD, "sdude");
+ headers.put(PGPDataFormat.ENCRYPTION_ALGORITHM, SymmetricKeyAlgorithmTags.AES_128);
+ headers.put(PGPDataFormat.SIGNATURE_HASH_ALGORITHM, HashAlgorithmTags.SHA512);
return headers;
}
}
http://git-wip-us.apache.org/repos/asf/camel/blob/47e76f10/components/camel-crypto/src/test/java/org/apache/camel/converter/crypto/PGPDataFormatTest.java
----------------------------------------------------------------------
diff --git a/components/camel-crypto/src/test/java/org/apache/camel/converter/crypto/PGPDataFormatTest.java b/components/camel-crypto/src/test/java/org/apache/camel/converter/crypto/PGPDataFormatTest.java
index 8424ac4..e890d53 100644
--- a/components/camel-crypto/src/test/java/org/apache/camel/converter/crypto/PGPDataFormatTest.java
+++ b/components/camel-crypto/src/test/java/org/apache/camel/converter/crypto/PGPDataFormatTest.java
@@ -22,6 +22,8 @@ import java.io.InputStream;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.util.IOHelper;
+import org.bouncycastle.bcpg.HashAlgorithmTags;
+import org.bouncycastle.bcpg.SymmetricKeyAlgorithmTags;
import org.junit.Test;
public class PGPDataFormatTest extends AbstractPGPDataFormatTest {
@@ -49,6 +51,14 @@ public class PGPDataFormatTest extends AbstractPGPDataFormatTest {
protected String getProvider() {
return "BC";
}
+
+ protected int getAlgorithm() {
+ return SymmetricKeyAlgorithmTags.TRIPLE_DES;
+ }
+
+ protected int getHashAlgorithm() {
+ return HashAlgorithmTags.SHA256;
+ }
@Test
public void testEncryption() throws Exception {
@@ -105,6 +115,7 @@ public class PGPDataFormatTest extends AbstractPGPDataFormatTest {
pgpEncrypt.setKeyFileName(keyFileName);
pgpEncrypt.setKeyUserid(keyUserid);
pgpEncrypt.setProvider(getProvider());
+ pgpEncrypt.setAlgorithm(getAlgorithm());
PGPDataFormat pgpDecrypt = new PGPDataFormat();
pgpDecrypt.setKeyFileName(keyFileNameSec);
@@ -133,6 +144,9 @@ public class PGPDataFormatTest extends AbstractPGPDataFormatTest {
pgpSignAndEncrypt.setSignatureKeyUserid(keyUserid);
pgpSignAndEncrypt.setSignaturePassword(keyPassword);
pgpSignAndEncrypt.setProvider(getProvider());
+ pgpSignAndEncrypt.setAlgorithm(getAlgorithm());
+ pgpSignAndEncrypt.setHashAlgorithm(getHashAlgorithm());
+
PGPDataFormat pgpVerifyAndDecrypt = new PGPDataFormat();
pgpVerifyAndDecrypt.setKeyFileName(keyFileNameSec);
@@ -154,6 +168,7 @@ public class PGPDataFormatTest extends AbstractPGPDataFormatTest {
pgpEncryptByteArray.setEncryptionKeyRing(getPublicKeyRing());
pgpEncryptByteArray.setKeyUserid(keyUserid);
pgpEncryptByteArray.setProvider(getProvider());
+ pgpEncryptByteArray.setAlgorithm(SymmetricKeyAlgorithmTags.DES);
PGPDataFormat pgpDecryptByteArray = new PGPDataFormat();
pgpDecryptByteArray.setEncryptionKeyRing(getSecKeyRing());
@@ -172,6 +187,8 @@ public class PGPDataFormatTest extends AbstractPGPDataFormatTest {
pgpSignAndEncryptByteArray.setSignatureKeyUserid(keyUserid);
pgpSignAndEncryptByteArray.setSignaturePassword(keyPassword);
pgpSignAndEncryptByteArray.setProvider(getProvider());
+ pgpSignAndEncryptByteArray.setAlgorithm(SymmetricKeyAlgorithmTags.BLOWFISH);
+ pgpSignAndEncryptByteArray.setHashAlgorithm(HashAlgorithmTags.RIPEMD160);
PGPDataFormat pgpVerifyAndDecryptByteArray = new PGPDataFormat();
pgpVerifyAndDecryptByteArray.setKeyUserid(keyUserid);
http://git-wip-us.apache.org/repos/asf/camel/blob/47e76f10/components/camel-crypto/src/test/resources/org/apache/camel/component/crypto/SpringPGPDataFormatTest.xml
----------------------------------------------------------------------
diff --git a/components/camel-crypto/src/test/resources/org/apache/camel/component/crypto/SpringPGPDataFormatTest.xml b/components/camel-crypto/src/test/resources/org/apache/camel/component/crypto/SpringPGPDataFormatTest.xml
index e511126..38d8c39 100644
--- a/components/camel-crypto/src/test/resources/org/apache/camel/component/crypto/SpringPGPDataFormatTest.xml
+++ b/components/camel-crypto/src/test/resources/org/apache/camel/component/crypto/SpringPGPDataFormatTest.xml
@@ -53,6 +53,7 @@
<bean id="encryptBean" class="org.apache.camel.converter.crypto.PGPDataFormat">
<property name="keyUserid" value="sdude@nowhere.net"/>
<property name="encryptionKeyRing" ref="pubKeyRing"/>
+ <property name="algorithm" value="7"/> <!-- AES128 algorithm -->
</bean>
<bean id="decryptBean" class="org.apache.camel.converter.crypto.PGPDataFormat">
[2/2] git commit: CAMEL-6861 fixed the camel-netty-http route cannot
proxy the response which is chunked
Posted by ni...@apache.org.
CAMEL-6861 fixed the camel-netty-http route cannot proxy the response which is chunked
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/9d7d54e7
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/9d7d54e7
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/9d7d54e7
Branch: refs/heads/master
Commit: 9d7d54e770a6ea1019ee8a7a99da6c9abb726fce
Parents: 32f32b9
Author: Willem Jiang <ni...@apache.org>
Authored: Mon Oct 14 16:20:53 2013 +0800
Committer: Willem Jiang <ni...@apache.org>
Committed: Mon Oct 14 16:35:33 2013 +0800
----------------------------------------------------------------------
.../netty/http/DefaultNettyHttpBinding.java | 5 ++++
.../http/handlers/HttpClientChannelHandler.java | 2 +-
.../NettyHttpClientChunkedResponseTest.java | 30 ++++++++++++++++----
3 files changed, 30 insertions(+), 7 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/9d7d54e7/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/DefaultNettyHttpBinding.java
----------------------------------------------------------------------
diff --git a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/DefaultNettyHttpBinding.java b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/DefaultNettyHttpBinding.java
index e64b800..84d315b 100644
--- a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/DefaultNettyHttpBinding.java
+++ b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/DefaultNettyHttpBinding.java
@@ -346,6 +346,11 @@ public class DefaultNettyHttpBinding implements NettyHttpBinding {
}
if (buffer != null) {
response.setContent(buffer);
+ // We just need to reset the readerIndex this time
+ if (buffer.readerIndex() == buffer.writerIndex()) {
+ buffer.setIndex(0, buffer.writerIndex());
+ }
+ // TODO How to enable the chunk transport
int len = buffer.readableBytes();
// set content-length
response.setHeader(HttpHeaders.Names.CONTENT_LENGTH, len);
http://git-wip-us.apache.org/repos/asf/camel/blob/9d7d54e7/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/handlers/HttpClientChannelHandler.java
----------------------------------------------------------------------
diff --git a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/handlers/HttpClientChannelHandler.java b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/handlers/HttpClientChannelHandler.java
index fc508e2..497ed45 100644
--- a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/handlers/HttpClientChannelHandler.java
+++ b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/handlers/HttpClientChannelHandler.java
@@ -85,7 +85,7 @@ public class HttpClientChannelHandler extends ClientChannelHandler {
// the copy must not be readable when the content was chunked, so set the index to the end
copy.setIndex(end, end);
response.setContent(copy);
- // we the all the content now, so call super to process the received message
+ // we get the all the content now, so call super to process the received message
super.messageReceived(ctx, messageEvent);
}
} else if (msg instanceof HttpResponse) {
http://git-wip-us.apache.org/repos/asf/camel/blob/9d7d54e7/tests/camel-itest/src/test/java/org/apache/camel/itest/nettyhttp/NettyHttpClientChunkedResponseTest.java
----------------------------------------------------------------------
diff --git a/tests/camel-itest/src/test/java/org/apache/camel/itest/nettyhttp/NettyHttpClientChunkedResponseTest.java b/tests/camel-itest/src/test/java/org/apache/camel/itest/nettyhttp/NettyHttpClientChunkedResponseTest.java
index 88cd3be..f9a6c4a 100644
--- a/tests/camel-itest/src/test/java/org/apache/camel/itest/nettyhttp/NettyHttpClientChunkedResponseTest.java
+++ b/tests/camel-itest/src/test/java/org/apache/camel/itest/nettyhttp/NettyHttpClientChunkedResponseTest.java
@@ -25,12 +25,22 @@ import org.junit.Test;
public class NettyHttpClientChunkedResponseTest extends CamelTestSupport {
- private int port;
+ private int port1;
+ private int port2;
@Test
public void testNettyHttpClientChunked() throws Exception {
+ invokeService(port1, true);
+ }
+
+ @Test
+ public void testNettyHttpRouteClientChunked() throws Exception {
+ invokeService(port2, false);
+ }
+
+ private void invokeService(int port, boolean checkChunkedHeader) {
Exchange out = template.request("netty-http:http://localhost:" + port + "/test", new Processor() {
-// Exchange out = template.request("jetty:http://localhost:" + port + "/test", new Processor() {
+
@Override
public void process(Exchange exchange) throws Exception {
exchange.getIn().setBody("Camel in chunks.");
@@ -38,22 +48,30 @@ public class NettyHttpClientChunkedResponseTest extends CamelTestSupport {
});
assertNotNull(out);
-
assertEquals("Bye Camel in chunks.", out.getOut().getBody(String.class));
- assertEquals("chunked", out.getOut().getHeader("Transfer-Encoding"));
+ if (checkChunkedHeader) {
+ assertEquals("chunked", out.getOut().getHeader("Transfer-Encoding"));
+ }
}
+
@Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
- port = AvailablePortFinder.getNextAvailable(8000);
+ port1 = AvailablePortFinder.getNextAvailable(8000);
+ port2 = AvailablePortFinder.getNextAvailable(9000);
// use jetty as server as it supports sending response as chunked encoding
- from("jetty:http://localhost:" + port + "/test")
+ from("jetty:http://localhost:" + port1 + "/test")
.setHeader("Transfer-Encoding", constant("chunked"))
.transform().simple("Bye ${body}");
+
+ // set up a netty http proxy
+ from("netty-http:http://localhost:" + port2 + "/test")
+ .to("netty-http:http://localhost:" + port1 + "/test?bridgeEndpoint=true&throwExceptionOnFailure=false");
+
}
};
}