Search in sources :

Example 81 with Predicate

use of org.apache.camel.Predicate in project camel by apache.

the class JmsMessageHeaderContentBasedRouterTest method createRouteBuilder.

@Override
protected RouteBuilder createRouteBuilder() throws Exception {
    return new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            Predicate isA = header("route").isEqualTo("a");
            Predicate isB = header("route").isEqualTo("b");
            from("activemq:queue:start").bean(MyPreProcessorBean.class, "determineRouting").choice().when(isA).to("mock:a").when(isB).to("mock:b").end();
        }
    };
}
Also used : RouteBuilder(org.apache.camel.builder.RouteBuilder) Predicate(org.apache.camel.Predicate)

Example 82 with Predicate

use of org.apache.camel.Predicate in project camel by apache.

the class WidgetGadgetRoute method configure.

@Override
public void configure() throws Exception {
    // you can define the endpoints and predicates here
    // it is more common to inline the endpoints and predicates in the route
    // as shown in the CreateOrderRoute
    Endpoint newOrder = endpoint("activemq:queue:newOrder");
    Predicate isWidget = xpath("/order/product = 'widget'");
    Endpoint widget = endpoint("activemq:queue:widget");
    Endpoint gadget = endpoint("activemq:queue:gadget");
    from(newOrder).choice().when(isWidget).to(// add a log so we can see this happening in the shell
    "log:widget").to(widget).otherwise().to(// add a log so we can see this happening in the shell
    "log:gadget").to(gadget);
}
Also used : Endpoint(org.apache.camel.Endpoint) Predicate(org.apache.camel.Predicate)

Example 83 with Predicate

use of org.apache.camel.Predicate in project camel by apache.

the class TikaParseTest method testDocumentParseWithEncoding.

@Test
public void testDocumentParseWithEncoding() throws Exception {
    File document = new File("src/test/resources/testOpenOffice2.odt");
    template.sendBody("direct:start4", document);
    resultEndpoint.setExpectedMessageCount(1);
    resultEndpoint.expectedMessagesMatches(new Predicate() {

        @Override
        public boolean matches(Exchange exchange) {
            Object body = exchange.getIn().getBody(String.class);
            Map<String, Object> headerMap = exchange.getIn().getHeaders();
            assertThat(body, instanceOf(String.class));
            Charset detectedCharset = null;
            try {
                InputStream bodyIs = new ByteArrayInputStream(((String) body).getBytes(StandardCharsets.UTF_16));
                UniversalEncodingDetector encodingDetector = new UniversalEncodingDetector();
                detectedCharset = encodingDetector.detect(bodyIs, new Metadata());
            } catch (IOException e1) {
                fail();
            }
            assertThat(detectedCharset.name(), startsWith(StandardCharsets.UTF_16.name()));
            assertThat(headerMap.get(Exchange.CONTENT_TYPE), equalTo("application/vnd.oasis.opendocument.text"));
            return true;
        }
    });
    resultEndpoint.assertIsSatisfied();
}
Also used : UniversalEncodingDetector(org.apache.tika.parser.txt.UniversalEncodingDetector) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Metadata(org.apache.tika.metadata.Metadata) Charset(java.nio.charset.Charset) IOException(java.io.IOException) Predicate(org.apache.camel.Predicate) Exchange(org.apache.camel.Exchange) ByteArrayInputStream(java.io.ByteArrayInputStream) File(java.io.File) Map(java.util.Map) Test(org.junit.Test)

Example 84 with Predicate

use of org.apache.camel.Predicate in project camel by apache.

the class JmsToHttpWithOnExceptionAndNoTransactionErrorHandlerConfiguredRoute method configure.

public void configure() throws Exception {
    port = AvailablePortFinder.getNextAvailable(8000);
    // if its a 404 then regard it as handled
    onException(HttpOperationFailedException.class).onWhen(new Predicate() {

        public boolean matches(Exchange exchange) {
            HttpOperationFailedException e = exchange.getException(HttpOperationFailedException.class);
            return e != null && e.getStatusCode() == 404;
        }
    }).handled(true).to("mock:404").transform(constant(noAccess));
    from("activemq:queue:data").policy(required).to("http://localhost:" + port + "/sender").convertBodyTo(String.class).choice().when().xpath("/reply/status != 'ok'").to("mock:rollback").rollback().otherwise().end();
    // this is our http router
    from("jetty:http://localhost:" + port + "/sender").process(new Processor() {

        public void process(Exchange exchange) throws Exception {
            // first hit is always a error code 500 to force the caller to retry
            if (counter++ < 1) {
                // simulate http error 500
                exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, 500);
                exchange.getOut().setBody("Damn some internal server error");
                return;
            }
            String user = exchange.getIn().getHeader("user", String.class);
            if ("unknown".equals(user)) {
                // no page for a unknown user
                exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, 404);
                exchange.getOut().setBody("Page does not exists");
                return;
            } else if ("guest".equals(user)) {
                // not okay for guest user
                exchange.getOut().setBody(nok);
                return;
            }
            exchange.getOut().setBody(ok);
        }
    });
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) HttpOperationFailedException(org.apache.camel.http.common.HttpOperationFailedException) HttpOperationFailedException(org.apache.camel.http.common.HttpOperationFailedException) Predicate(org.apache.camel.Predicate)

Example 85 with Predicate

use of org.apache.camel.Predicate in project camel by apache.

the class PdfCreationTest method testPdfCreationWithEncryption.

@Test
public void testPdfCreationWithEncryption() throws Exception {
    final String ownerPass = "ownerPass";
    final String userPass = "userPass";
    final String expectedText = "expectedText";
    AccessPermission accessPermission = new AccessPermission();
    accessPermission.setCanPrint(false);
    StandardProtectionPolicy protectionPolicy = new StandardProtectionPolicy(ownerPass, userPass, accessPermission);
    protectionPolicy.setEncryptionKeyLength(128);
    template.sendBodyAndHeader("direct:start", expectedText, PdfHeaderConstants.PROTECTION_POLICY_HEADER_NAME, protectionPolicy);
    resultEndpoint.setExpectedMessageCount(1);
    resultEndpoint.expectedMessagesMatches(new Predicate() {

        @Override
        public boolean matches(Exchange exchange) {
            Object body = exchange.getIn().getBody();
            assertThat(body, instanceOf(ByteArrayOutputStream.class));
            try {
                PDDocument doc = PDDocument.load(new ByteArrayInputStream(((ByteArrayOutputStream) body).toByteArray()));
                assertTrue("Expected encrypted document", doc.isEncrypted());
                doc.decrypt(userPass);
                assertFalse("Printing should not be permitted", doc.getCurrentAccessPermission().canPrint());
                PDFTextStripper pdfTextStripper = new PDFTextStripper();
                String text = pdfTextStripper.getText(doc);
                assertEquals(1, doc.getNumberOfPages());
                assertThat(text, containsString(expectedText));
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            return true;
        }
    });
    resultEndpoint.assertIsSatisfied();
}
Also used : Exchange(org.apache.camel.Exchange) ByteArrayInputStream(java.io.ByteArrayInputStream) StandardProtectionPolicy(org.apache.pdfbox.pdmodel.encryption.StandardProtectionPolicy) PDDocument(org.apache.pdfbox.pdmodel.PDDocument) AccessPermission(org.apache.pdfbox.pdmodel.encryption.AccessPermission) Matchers.containsString(org.hamcrest.Matchers.containsString) IOException(java.io.IOException) Predicate(org.apache.camel.Predicate) PDFTextStripper(org.apache.pdfbox.util.PDFTextStripper) Test(org.junit.Test)

Aggregations

Predicate (org.apache.camel.Predicate)124 Exchange (org.apache.camel.Exchange)70 Test (org.junit.Test)40 HashMap (java.util.HashMap)25 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)23 Expression (org.apache.camel.Expression)22 Processor (org.apache.camel.Processor)19 DefaultExchange (org.apache.camel.impl.DefaultExchange)10 RouteBuilder (org.apache.camel.builder.RouteBuilder)9 File (java.io.File)8 Matchers.containsString (org.hamcrest.Matchers.containsString)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 Map (java.util.Map)7 Ignore (org.junit.Ignore)7 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)6 AggregateProcessor (org.apache.camel.processor.aggregate.AggregateProcessor)6 AggregationStrategy (org.apache.camel.processor.aggregate.AggregationStrategy)6 PDDocument (org.apache.pdfbox.pdmodel.PDDocument)6 BodyInAggregatingStrategy (org.apache.camel.processor.BodyInAggregatingStrategy)5