org.apache.camel.converter.crypto.CryptoDataFormat Java Examples

The following examples show how to use org.apache.camel.converter.crypto.CryptoDataFormat. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: EncryptionDynamicRoute.java    From camel-cookbook-examples with Apache License 2.0 6 votes vote down vote up
@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");
}
 
Example #2
Source File: EncryptionRoute.java    From camel-cookbook-examples with Apache License 2.0 6 votes vote down vote up
@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");
}
 
Example #3
Source File: CryptoDataFormatIntegrationTest.java    From wildfly-camel with Apache License 2.0 6 votes vote down vote up
@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.close();
    }
}
 
Example #4
Source File: MessageEncryptionTest.java    From camelinaction2 with Apache License 2.0 5 votes vote down vote up
@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");
        }
    };
}
 
Example #5
Source File: CryptoDataFormatAutoConfiguration.java    From camel-spring-boot with Apache License 2.0 4 votes vote down vote up
@Bean(name = "crypto-dataformat-factory")
@ConditionalOnMissingBean(CryptoDataFormat.class)
public DataFormatFactory configureCryptoDataFormatFactory() throws Exception {
    return new DataFormatFactory() {
        @Override
        public DataFormat newInstance() {
            CryptoDataFormat dataformat = new CryptoDataFormat();
            if (CamelContextAware.class
                    .isAssignableFrom(CryptoDataFormat.class)) {
                CamelContextAware contextAware = CamelContextAware.class
                        .cast(dataformat);
                if (contextAware != null) {
                    contextAware.setCamelContext(camelContext);
                }
            }
            try {
                Map<String, Object> parameters = new HashMap<>();
                IntrospectionSupport.getProperties(configuration,
                        parameters, null, false);
                CamelPropertiesHelper.setCamelProperties(camelContext,
                        dataformat, parameters, false);
            } catch (Exception e) {
                throw new RuntimeCamelException(e);
            }
            if (ObjectHelper.isNotEmpty(customizers)) {
                for (DataFormatCustomizer<CryptoDataFormat> customizer : customizers) {
                    boolean useCustomizer = (customizer instanceof HasId)
                            ? HierarchicalPropertiesEvaluator.evaluate(
                                    applicationContext.getEnvironment(),
                                    "camel.dataformat.customizer",
                                    "camel.dataformat.crypto.customizer",
                                    ((HasId) customizer).getId())
                            : HierarchicalPropertiesEvaluator.evaluate(
                                    applicationContext.getEnvironment(),
                                    "camel.dataformat.customizer",
                                    "camel.dataformat.crypto.customizer");
                    if (useCustomizer) {
                        LOGGER.debug(
                                "Configure dataformat {}, with customizer {}",
                                dataformat, customizer);
                        customizer.customize(dataformat);
                    }
                }
            }
            return dataformat;
        }
    };
}