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();
}
};
}
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);
}
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();
}
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);
}
});
}
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();
}
Aggregations