/**
 * 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 camelinaction;

import java.io.InputStream;
import java.security.Key;
import java.security.KeyStore;

import javax.crypto.SecretKey;

import org.apache.camel.Exchange;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.converter.crypto.CryptoDataFormat;
import org.apache.camel.impl.JndiRegistry;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Test;

public class MessageEncryptionTest extends CamelTestSupport {

    private KeyStore keystore;
    private Key secretKey;

    @Override
    protected JndiRegistry createRegistry() throws Exception {
        JndiRegistry registry = super.createRegistry();
        keystore = loadKeystore("/cia_secrets.jceks", "supersecret");
        secretKey = keystore.getKey("ciasecrets", "secret".toCharArray());
        registry.bind("keystore", keystore);
        return registry;
    }

    public static KeyStore loadKeystore(String file, String password) throws Exception {
        KeyStore keystore = KeyStore.getInstance("JCEKS");
        InputStream in = MessageEncryptionTest.class.getResourceAsStream(file);
        keystore.load(in, password.toCharArray());
        return keystore;
    }
        
    @Test
    public void testEncryptAndDecryptMessage() throws Exception {
        getMockEndpoint("mock:unencrypted").expectedBodiesReceived("Hello World");

        template.sendBody("direct:start", "Hello World");

        assertMockEndpointsSatisfied();
        
        Exchange exchange = getMockEndpoint("mock:encrypted").getReceivedExchanges().get(0);
        assertNotEquals("Hello World", exchange.getIn().getBody());
    }

    @Override
    protected RouteBuilder createRouteBuilder() throws Exception {
        return new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                CryptoDataFormat crypto = new CryptoDataFormat("DES", secretKey);
                
                from("direct:start")
                .marshal(crypto)
                .to("mock:encrypted")
                .unmarshal(crypto)
                .to("mock:unencrypted");
            }
        };
    }
}

@@ -825,36 +825,8 @@
 n;%0A%0A
-import java.io.InputStream;%0A
 impo
@@ -883,40 +883,8 @@
 e;%0A%0A
-import javax.crypto.SecretKey;%0A%0A
 impo
@@ -1115,16 +1115,70 @@
 upport;%0A
+import org.apache.camel.util.jsse.KeyStoreParameters;%0A
 import o
@@ -1260,39 +1260,8 @@
  %7B%0A%0A
-    private KeyStore keystore;%0A
     
@@ -1417,32 +1417,60 @@
 stry();%0A        
+%0A        KeyStoreParameters 
 keystore = loadK
@@ -1468,43 +1468,63 @@
 e = 
-loadKeys
+new KeyS
 tore
-(%22/cia_secrets.jceks%22, 
+Parameters();%0A        keystore.setPassword(
 %22sup
@@ -1546,20 +1546,8 @@
     
- secretKey =
  key
@@ -1556,407 +1556,257 @@
 ore.
-getKey(%22
+setResource(%22./
 cia
+_
 secrets
-%22, %22secret%22.toCharArray());%0A        registry.bind(%22keystore%22, keystore);%0A        return registry;
+.jceks%22);%0A        keystore.setType(%22JCEKS%22);%0A        
 %0A    
-%7D%0A%0A
     
-public static KeyStore loadKeystore(String file, String password) throws Exception %7B%0A        KeyStore keystore = KeyStore.getInstance(%22JCEKS%22);%0A        InputStream in = MessageEncryptionTest.class.getResourceAsStream(file);%0A        keystore.load(in, password.toCharArray()
+KeyStore store = keystore.createKeyStore();%0A        secretKey = store.getKey(%22ciasecrets%22, %22secret%22.toCharArray());%0A        registry.bind(%22ciasecrets%22, secretKey
 );%0A 
@@ -1823,24 +1823,24 @@
 urn 
-keystore
+registry
 ;%0A    %7D%0A
     
@@ -1831,24 +1831,25 @@
 stry;%0A    %7D%0A
+%0A
         %0A   

switch to KeyStoreParameters