Java Code Examples for org.apache.camel.ProducerTemplate#requestBodyAndHeader()

The following examples show how to use org.apache.camel.ProducerTemplate#requestBodyAndHeader() . 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: XChangeMetadataIntegrationTest.java    From wildfly-camel with Apache License 2.0 6 votes vote down vote up
@Test
public void testCurrencyMetaData() throws Exception {

    try (CamelContext camelctx = new DefaultCamelContext()) {

        Assume.assumeTrue(checkAPIConnection());

        camelctx.addRoutes(createRouteBuilder());
        camelctx.start();

        ProducerTemplate template = camelctx.createProducerTemplate();
        CurrencyMetaData metadata = template.requestBody("direct:currencyMetaData", Currency.ETH, CurrencyMetaData.class);
        Assert.assertNotNull("CurrencyMetaData not null", metadata);

        metadata = template.requestBodyAndHeader("direct:currencyMetaData", null, HEADER_CURRENCY, Currency.ETH, CurrencyMetaData.class);
        Assert.assertNotNull("CurrencyMetaData not null", metadata);
    }
}
 
Example 2
Source File: XChangeMetadataIntegrationTest.java    From wildfly-camel with Apache License 2.0 6 votes vote down vote up
@Test
public void testCurrencyPairMetaData() throws Exception {

    try (CamelContext camelctx = new DefaultCamelContext()) {

        Assume.assumeTrue(checkAPIConnection());

        camelctx.addRoutes(createRouteBuilder());
        camelctx.start();

        ProducerTemplate template = camelctx.createProducerTemplate();
        CurrencyPairMetaData metadata = template.requestBody("direct:currencyPairMetaData", CurrencyPair.EOS_ETH, CurrencyPairMetaData.class);
        Assert.assertNotNull("CurrencyPairMetaData not null", metadata);

        metadata = template.requestBodyAndHeader("direct:currencyPairMetaData", null, HEADER_CURRENCY_PAIR, CurrencyPair.EOS_ETH, CurrencyPairMetaData.class);
        Assert.assertNotNull("CurrencyPairMetaData not null", metadata);
    }
}
 
Example 3
Source File: XChangeMarketIntegrationTest.java    From wildfly-camel with Apache License 2.0 6 votes vote down vote up
@Test
public void testTicker() throws Exception {

    try (CamelContext camelctx = new DefaultCamelContext()) {

        Assume.assumeTrue(checkAPIConnection());

        camelctx.addRoutes(createRouteBuilder());
        camelctx.start();

        ProducerTemplate template = camelctx.createProducerTemplate();
        Ticker ticker = template.requestBody("direct:ticker", CurrencyPair.EOS_ETH, Ticker.class);
        Assert.assertNotNull("Ticker not null", ticker);
        System.out.println(ticker);

        ticker = template.requestBodyAndHeader("direct:ticker", null, HEADER_CURRENCY_PAIR, CurrencyPair.EOS_ETH, Ticker.class);
        Assert.assertNotNull("Ticker not null", ticker);
        System.out.println(ticker);
    }
}
 
Example 4
Source File: CXFRSProducerIntegrationTest.java    From wildfly-camel with Apache License 2.0 6 votes vote down vote up
@Test
public void testCxfRsProducer() throws Exception {
    deployer.deploy(SIMPLE_WAR);
    try {
        CamelContext camelctx = new DefaultCamelContext();
        camelctx.addRoutes(new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                from("direct:start")
                .setHeader(Exchange.HTTP_METHOD, constant("GET")).
                to("cxfrs://" + getEndpointAddress("/simple-rs-endpoint") + "?resourceClasses=" + GreetingService.class.getName());
            }
        });

        camelctx.start();
        try {
            ProducerTemplate producer = camelctx.createProducerTemplate();
            String result = producer.requestBodyAndHeader("direct:start", "mybody", "name", "Kermit", String.class);
            Assert.assertEquals("Hello Kermit", result);
        } finally {
            camelctx.close();
        }
    } finally {
        deployer.undeploy(SIMPLE_WAR);
    }
}
 
Example 5
Source File: Http4IntegrationTest.java    From wildfly-camel with Apache License 2.0 6 votes vote down vote up
@Test
public void testHttpGetRequest() throws Exception {
    HttpClientConfigurer configurer = new Http4ClientConfigurer();
    initialContext.bind("httpClientConfigurer", configurer);

    CamelContext camelctx = new DefaultCamelContext(new JndiBeanRepository());
    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("direct:start")
            .to("http://localhost:8080/simple/myservlet?httpClientConfigurer=#httpClientConfigurer");
        }
    });

    camelctx.start();
    try {
        ProducerTemplate producer = camelctx.createProducerTemplate();
        String result = producer.requestBodyAndHeader("direct:start", null, Exchange.HTTP_QUERY, "name=Kermit", String.class);
        Assert.assertEquals("Hello Kermit", result);
    } finally {
        camelctx.close();
        initialContext.unbind("httpClientConfigurer");
    }
}
 
Example 6
Source File: SecuredRouteTestCase.java    From wildfly-camel with Apache License 2.0 6 votes vote down vote up
@Test
public void testRoleBasedAccess() throws Exception {
    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("direct:start")
            .policy(new DomainAuthorizationPolicy().roles("Role2"))
            .transform(body().prepend("Hello "));
        }
    });

    camelctx.start();
    try {
        ProducerTemplate producer = camelctx.createProducerTemplate();
        Subject subject = getAuthenticationToken("user-domain", AnnotatedSLSB.USERNAME, AnnotatedSLSB.PASSWORD);
        String result = producer.requestBodyAndHeader("direct:start", "Kermit", Exchange.AUTHENTICATION, subject, String.class);
        Assert.assertEquals("Hello Kermit", result);
    } finally {
        camelctx.close();
    }
}
 
Example 7
Source File: SecuredRouteTestCase.java    From wildfly-camel with Apache License 2.0 5 votes vote down vote up
@Test
public void testInvalidCredentials() throws Exception {
    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("direct:start")
            .policy(new DomainAuthorizationPolicy())
            .transform(body().prepend("Hello "));
        }
    });

    camelctx.start();
    try {
        ProducerTemplate producer = camelctx.createProducerTemplate();
        try {
            Subject subject = getAuthenticationToken("user-domain", AnnotatedSLSB.USERNAME, "bogus");
            producer.requestBodyAndHeader("direct:start", "Kermit", Exchange.AUTHENTICATION, subject, String.class);
            Assert.fail("CamelExecutionException expected");
        } catch (CamelExecutionException ex) {
            Throwable cause = ex.getCause();
            Assert.assertEquals(FailedLoginException.class, cause.getClass());
            Assert.assertTrue(cause.getMessage(), cause.getMessage().contains("Password invalid/Password required"));
        }
    } finally {
        camelctx.close();
    }
}
 
Example 8
Source File: JPAIdempotentConsumerIntegrationTest.java    From wildfly-camel with Apache License 2.0 5 votes vote down vote up
@Test
public void testJPAIdempotentConsumer() throws Exception {
    final TransactionTemplate transactionTemplate = new TransactionTemplate();
    transactionTemplate.setTransactionManager(new JtaTransactionManager(userTransaction));
    transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);

    final JpaMessageIdRepository messageIdRepository = new JpaMessageIdRepository(entityManagerFactory, transactionTemplate, "myProcessorName");

    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("direct:start")
            .idempotentConsumer(simple("${header.messageId}"), messageIdRepository)
            .to("mock:result");
        }
    });

    camelctx.start();
    try {
        MockEndpoint mockEndpoint = camelctx.getEndpoint("mock:result", MockEndpoint.class);
        mockEndpoint.expectedMessageCount(1);

        // Send 5 messages with the same messageId header. Only 1 should be forwarded to the mock:result endpoint
        ProducerTemplate template = camelctx.createProducerTemplate();
        for (int i = 0; i < 5; i++) {
            template.requestBodyAndHeader("direct:start", null, "messageId", "12345");
        }

        mockEndpoint.assertIsSatisfied();
    } finally {
        camelctx.close();
    }
}
 
Example 9
Source File: SAPNetweaverIntegrationTest.java    From wildfly-camel with Apache License 2.0 5 votes vote down vote up
@Test
public void testSAPNetweaverEndpointXmlResponse() throws Exception {
    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("direct:start")
                .to("sap-netweaver:http://localhost:8080/sap/api?json=false");

            from("undertow:http://localhost:8080/sap/api?matchOnUriPrefix=true")
                .process(new Processor() {
                    @Override
                    public void process(Exchange exchange) throws Exception {
                        String data = TestUtils.getResourceValue(SAPNetweaverIntegrationTest.class, "/flight-data.xml");
                        exchange.getMessage().setBody(data);
                    }
                });
        }
    });

    camelctx.start();
    try {
        ProducerTemplate producer = camelctx.createProducerTemplate();
        String result = producer.requestBodyAndHeader("direct:start", null, NetWeaverConstants.COMMAND, SAP_COMMAND, String.class);
        Assert.assertTrue(result.contains("<d:PRICE>422.94</d:PRICE>"));
        Assert.assertTrue(result.contains("<d:CURRENCY>USD</d:CURRENCY>"));
    } finally {
        camelctx.close();
    }
}
 
Example 10
Source File: TwilioIntegrationTest.java    From wildfly-camel with Apache License 2.0 5 votes vote down vote up
@Test
public void testTwilioProducer() throws Exception {
    Assume.assumeNotNull("TWILIO_ACCOUNT_SID is null", TWILIO_ACCOUNT_SID);
    Assume.assumeNotNull("TWILIO_PASSWORD is null", TWILIO_PASSWORD);

    CamelContext camelctx = new DefaultCamelContext();

    TwilioComponent component = camelctx.getComponent("twilio", TwilioComponent.class);
    component.setUsername(TWILIO_ACCOUNT_SID);
    component.setPassword(TWILIO_PASSWORD);
    component.setAccountSid(TWILIO_ACCOUNT_SID);

    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("direct://start").to("twilio://account/fetch");
        }
    });

    camelctx.start();
    try {
        ProducerTemplate template = camelctx.createProducerTemplate();
        Account account = template.requestBodyAndHeader("direct:start", null,
                "CamelTwilioPathSid", TWILIO_ACCOUNT_SID,
                Account.class);

        Assert.assertNotNull("Twilio fetcher result was null", account);
        Assert.assertEquals("Account SID did not match", TWILIO_ACCOUNT_SID, account.getSid());
    } finally {
        camelctx.close();
    }
}
 
Example 11
Source File: SecuredRouteTestCase.java    From wildfly-camel with Apache License 2.0 5 votes vote down vote up
@Test
public void testInsufficientRoles() throws Exception {
    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("direct:start")
            .policy(new DomainAuthorizationPolicy().roles("Role3"))
            .transform(body().prepend("Hello "));
        }
    });

    camelctx.start();
    try {
        ProducerTemplate producer = camelctx.createProducerTemplate();
        try {
            Subject subject = getAuthenticationToken("user-domain", AnnotatedSLSB.USERNAME, AnnotatedSLSB.PASSWORD);
            producer.requestBodyAndHeader("direct:start", "Kermit", Exchange.AUTHENTICATION, subject, String.class);
            Assert.fail("CamelExecutionException expected");
        } catch (CamelExecutionException ex) {
            Throwable cause = ex.getCause();
            Assert.assertEquals(LoginException.class, cause.getClass());
            Assert.assertTrue(cause.getMessage(), cause.getMessage().contains("User does not have required roles: [Role3]"));
        }
    } finally {
        camelctx.close();
    }
}
 
Example 12
Source File: RestSwaggerIntegrationTest.java    From wildfly-camel with Apache License 2.0 5 votes vote down vote up
@Test
public void testRestSwaggerJSON() throws Exception {
    JacksonDataFormat jacksonDataFormat = new JacksonDataFormat();
    jacksonDataFormat.setUnmarshalType(Customer.class);

    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("direct:getCustomerById")
            .to("customer:getCustomerById")
            .convertBodyTo(String.class)
            .unmarshal(jacksonDataFormat);
        }
    });

    RestSwaggerComponent restSwaggerComponent = new RestSwaggerComponent();
    restSwaggerComponent.setSpecificationUri(new URI("http://localhost:8080/api/swagger"));
    restSwaggerComponent.setComponentName("undertow");
    restSwaggerComponent.setConsumes(MediaType.APPLICATION_JSON);
    restSwaggerComponent.setProduces(MediaType.APPLICATION_JSON);

    camelctx.addComponent("customer", restSwaggerComponent);

    camelctx.start();
    try {
        ProducerTemplate template = camelctx.createProducerTemplate();
        Customer customer = template.requestBodyAndHeader("direct:getCustomerById", null, "id", 1, Customer.class);
        Assert.assertNotNull(customer);
        Assert.assertEquals(1, customer.getId());
    } finally {
        camelctx.close();
    }
}
 
Example 13
Source File: ParameterBindingTest.java    From wildfly-camel with Apache License 2.0 5 votes vote down vote up
@Test
public void testParameterBinding() throws Exception {

    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("direct:start").bean(OrderBean.class);
        }
    });

    camelctx.start();
    try {
        ProducerTemplate producer = camelctx.createProducerTemplate();
        String result = producer.requestBodyAndHeader("direct:start", 100, "customerId", 200, String.class);
        Assert.assertEquals("Order 100 from customer 200", result);
    } finally {
        camelctx.close();
    }
}
 
Example 14
Source File: SQLIntegrationTest.java    From wildfly-camel with Apache License 2.0 5 votes vote down vote up
@Test
public void testSqlIdempotentConsumer() throws Exception {
    Assert.assertNotNull("DataSource not null", dataSource);

    final JdbcMessageIdRepository jdbcMessageIdRepository = new JdbcMessageIdRepository(dataSource, "myProcessorName");

    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("direct:start")
            .idempotentConsumer(simple("${header.messageId}"), jdbcMessageIdRepository)
            .to("mock:result");
        }
    });

    camelctx.start();
    try {
        MockEndpoint mockEndpoint = camelctx.getEndpoint("mock:result", MockEndpoint.class);
        mockEndpoint.expectedMessageCount(1);

        // Send 5 messages with the same messageId header. Only 1 should be forwarded to the mock:result endpoint
        ProducerTemplate template = camelctx.createProducerTemplate();
        for (int i = 0; i < 5; i++) {
            template.requestBodyAndHeader("direct:start", null, "messageId", "12345");
        }

        mockEndpoint.assertIsSatisfied();
    } finally {
        camelctx.close();
    }
}
 
Example 15
Source File: AuthorizationPolicyTestCase.java    From wildfly-camel with Apache License 2.0 5 votes vote down vote up
@Test
public void testInsufficientRoles() throws Exception {
    CamelContext camelctx = contextRegistry.getCamelContext("contextC");
    ProducerTemplate producer = camelctx.createProducerTemplate();
    try {
        Subject subject = getAuthenticationToken("user-domain", AnnotatedSLSB.USERNAME, AnnotatedSLSB.PASSWORD);
        producer.requestBodyAndHeader("direct:start", "Kermit", Exchange.AUTHENTICATION, subject, String.class);
        Assert.fail("CamelExecutionException expected");
    } catch (CamelExecutionException ex) {
        Throwable cause = ex.getCause();
        Assert.assertEquals(CamelAuthorizationException.class, cause.getClass());
        Assert.assertTrue(cause.getMessage(), cause.getMessage().contains("User does not have required roles: [Role3]"));
    }
}
 
Example 16
Source File: EhCacheIntegrationTest.java    From wildfly-camel with Apache License 2.0 5 votes vote down vote up
@Test
public void testEhCacheIdempotentRepository() throws Exception {
    EhcacheIdempotentRepository repository = new EhcacheIdempotentRepository(cacheManager, "idempotent");

    CamelContext camelctx = new DefaultCamelContext(new JndiBeanRepository());
    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("direct:start")
            .idempotentConsumer(header("messageId"), repository)
            .to("mock:result");
        }
    });

    camelctx.start();
    try {
        MockEndpoint mockEndpoint = camelctx.getEndpoint("mock:result", MockEndpoint.class);
        mockEndpoint.expectedMessageCount(1);

        // Send 5 messages with the same messageId header. Only 1 should be forwarded to the mock:result endpoint
        ProducerTemplate template = camelctx.createProducerTemplate();
        for (int i = 0; i < 5; i++) {
            template.requestBodyAndHeader("direct:start", null, "messageId", "12345");
        }

        mockEndpoint.assertIsSatisfied();
    } finally {
        camelctx.close();
    }
}
 
Example 17
Source File: AuthorizationPolicyTestCase.java    From wildfly-camel with Apache License 2.0 5 votes vote down vote up
@Test
public void testAuthenticatedAccess() throws Exception {
    CamelContext camelctx = contextRegistry.getCamelContext("contextA");
    ProducerTemplate producer = camelctx.createProducerTemplate();
    Subject subject = getAuthenticationToken("user-domain", AnnotatedSLSB.USERNAME, AnnotatedSLSB.PASSWORD);
    String result = producer.requestBodyAndHeader("direct:start", "Kermit", Exchange.AUTHENTICATION, subject, String.class);
    Assert.assertEquals("Hello Kermit", result);
}
 
Example 18
Source File: DigitalOceanIntegrationTest.java    From wildfly-camel with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateDroplet() throws Exception {
    CamelContext camelctx = createCamelContext(oauthToken);
    camelctx.addRoutes(createRouteBuilder());

    camelctx.start();
    try {
        MockEndpoint mockResult = camelctx.getEndpoint("mock:result", MockEndpoint.class);
        mockResult.expectedMinimumMessageCount(1);

        ProducerTemplate producer = camelctx.createProducerTemplate();
        Droplet droplet = producer.requestBody("direct:createDroplet", null, Droplet.class);

        mockResult.assertIsSatisfied();
        Assert.assertNotNull(droplet.getId());
        Assert.assertEquals(droplet.getRegion().getSlug(), "fra1");
        Assert.assertEquals(2, droplet.getTags().size());

        mockResult.reset();
        mockResult.expectedMinimumMessageCount(1);

        Droplet resDroplet = producer.requestBodyAndHeader("direct:getDroplet", null, DigitalOceanHeaders.ID, droplet.getId(), Droplet.class);
        mockResult.assertIsSatisfied();
        Assert.assertEquals(droplet.getId(), resDroplet.getId());

        Delete delres = producer.requestBodyAndHeader("direct:deleteDroplet", null, DigitalOceanHeaders.ID, droplet.getId(), Delete.class);
        Assert.assertTrue("Droplet deleted", delres.getIsRequestSuccess());
    } finally {
        camelctx.close();
    }
}
 
Example 19
Source File: AnnotatedSLSB.java    From wildfly-camel with Apache License 2.0 5 votes vote down vote up
@RolesAllowed({ "Role2" })
public String secureRouteAccess(String msg) {

    // [TODO #725] Add support for security context propagation
    Subject subject = new Subject();
    String username = ejbctx.getCallerPrincipal().getName();
    subject.getPrincipals().add(new DomainPrincipal("user-domain"));
    subject.getPrincipals().add(new EncodedUsernamePasswordPrincipal(username, PASSWORD.toCharArray()));

    ProducerTemplate producer = camelctx.createProducerTemplate();
    return producer.requestBodyAndHeader("direct:start", msg, Exchange.AUTHENTICATION, subject, String.class);
}
 
Example 20
Source File: GroovyShellFactoryTest.java    From wildfly-camel with Apache License 2.0 4 votes vote down vote up
@Test
public void testGroovyWithHeader() throws Exception {
    ProducerTemplate producer = camelctx.createProducerTemplate();
    String result = producer.requestBodyAndHeader("direct:start", "Kermit", "locale", "es", String.class);
    Assert.assertEquals("Hola Kermit", result);
}