@Bean @ConditionalOnClass(CamelContext.class) @ConditionalOnMissingBean(CryptoDataFormat.class) public CryptoDataFormat configureCryptoDataFormat( CamelContext camelContext, CryptoDataFormatConfiguration configuration) throws Exception { CryptoDataFormat dataformat = new CryptoDataFormat(); if (dataformat instanceof CamelContextAware) { ((CamelContextAware) dataformat).setCamelContext(camelContext); } Map<String, Object> parameters = new HashMap<>(); IntrospectionSupport.getProperties(configuration, parameters, null, false); IntrospectionSupport.setProperties(camelContext, camelContext.getTypeConverter(), dataformat, parameters); return dataformat; }
@Override public void configure() throws Exception { final CryptoDataFormat crypto = new CryptoDataFormat("DES", null); from("direct:encrypt") .process(new Processor() { @Override public void process(Exchange exchange) throws Exception { Registry registry = exchange.getContext().getRegistry(); Message in = exchange.getIn(); Key key = registry.lookupByNameAndType("shared_" + in.getHeader("system"), Key.class); in.setHeader(CryptoDataFormat.KEY, key); } }) .log("Encrypting message: ${body} using ${header[CamelCryptoKey]}") .marshal(crypto) .log("Message encrypted: ${body}") .to("direct:decrypt"); from("direct:decrypt") .log("Decrypting message: ${body} using ${header[CamelCryptoKey]}") .unmarshal(crypto) .log("Message decrypted: ${body}") .to("mock:decrypted"); }
@Override public void configure() throws Exception { CryptoDataFormat sharedKeyCrypto = new CryptoDataFormat("DES", sharedKey); from("direct:encrypt") .log("Encrypting message") .marshal(sharedKeyCrypto) .log("Message encrypted: ${body}") .to("direct:decrypt"); from("direct:decrypt") .log("Decrypting message") .unmarshal(sharedKeyCrypto) .log("Message decrypted: ${body}") .to("mock:decrypted"); }
@Test public void testMarshalUnmarshallDes() throws Exception { final KeyGenerator generator = KeyGenerator.getInstance("DES"); final CryptoDataFormat cryptoFormat = new CryptoDataFormat("DES", generator.generateKey()); CamelContext camelctx = new DefaultCamelContext(); camelctx.addRoutes(new RouteBuilder() { @Override public void configure() throws Exception { from("direct:start") .marshal(cryptoFormat) .unmarshal(cryptoFormat); } }); camelctx.start(); try { ProducerTemplate producer = camelctx.createProducerTemplate(); String result = producer.requestBody("direct:start", "password", String.class); Assert.assertEquals("password", result.trim()); } finally { camelctx.stop(); } }
@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"); } }; }