protected RouteBuilder createRouteBuilder() { return new RouteBuilder() { public void configure() { context.setTracing(true); from("direct:start") .doTry() .process(validator) .doCatch(ValidationException.class) .setHeader("xxx", constant("yyy")) .end() .doTry() .process(validator).to("mock:valid") .doCatch(ValidationException.class) .pipeline("seda:a", "mock:invalid"); } }; }
protected RouteBuilder createRouteBuilder() { return new RouteBuilder() { public void configure() { from("direct:start") .doTry() .to("direct:embedded") .doCatch(ValidationException.class) .to("mock:invalid"); from("direct:embedded") .errorHandler(noErrorHandler()) .doTry() .process(validator) .to("mock:valid") .doFinally() .setHeader("valid", constant(false)) .end(); } }; }
protected RouteBuilder createRouteBuilder() { return new RouteBuilder() { public void configure() { errorHandler(deadLetterChannel("mock:error").redeliveryDelay(0).maximumRedeliveries(3)); onException(ValidationException.class).to("mock:outer"); from("direct:start") .doTry() .process(validator) .to("mock:valid") .doCatch(ValidationException.class) .to("mock:invalid") .process(validator); } }; }
protected RouteBuilder createRouteBuilder() { return new RouteBuilder() { public void configure() { from("direct:start") .doTry() .process(validator) .setHeader("valid", constant(true)) .doCatch(ValidationException.class) .setHeader("valid", constant(false)) .doFinally() .setBody(body()) .choice() .when(header("valid").isEqualTo(true)) .to("mock:valid") .otherwise() .to("mock:invalid"); } }; }
protected RouteBuilder createRouteBuilder() { return new RouteBuilder() { public void configure() { from("direct:start") .errorHandler(noErrorHandler()) .doTry() .process(validator) .doCatch(ValidationException.class) .process(validator) .doFinally() .choice() .when(header("foo").isEqualTo("bar")) .to("mock:valid") .otherwise() .to("mock:invalid"); } }; }
protected RouteBuilder createRouteBuilder() { return new RouteBuilder() { public void configure() { from("direct:start").process(new Processor() { public void process(Exchange exchange) throws Exception { try { validator.process(exchange); template.send("mock:valid", exchange); } catch (ValidationException e) { template.send("mock:invalid", exchange); } } }); } }; }
@Override protected RouteBuilder createRouteBuilder() throws Exception { // we have to do it here, because we need the context created first CatalogManager.getStaticManager().setIgnoreMissingProperties(true); CatalogResolver catalogResolver = new CatalogResolver(true); URL catalogUrl = ResourceHelper.resolveMandatoryResourceAsUrl(context.getClassResolver(), "org/apache/camel/component/validator/catalog.cat"); catalogResolver.getCatalog().parseCatalog(catalogUrl); LSResourceResolver resourceResolver = new CatalogLSResourceResolver(catalogResolver); JndiRegistry registry = (JndiRegistry) ((PropertyPlaceholderDelegateRegistry) context.getRegistry()).getRegistry(); registry.bind("resourceResolver", resourceResolver); return new RouteBuilder() { @Override public void configure() throws Exception { from("direct:start") .doTry() .to("validator:org/apache/camel/component/validator/report.xsd?resourceResolver=#resourceResolver") .to("mock:valid") .doCatch(ValidationException.class) .to("mock:invalid") .doFinally() .to("mock:finally") .end(); } }; }
@Override protected RouteBuilder createRouteBuilder() throws Exception { return new RouteBuilder() { @Override public void configure() throws Exception { from("file:target/validator?noop=true") .doTry() .to("validator:org/apache/camel/component/validator/schema.xsd") .to("mock:valid") .doCatch(ValidationException.class) .to("mock:invalid") .doFinally() .to("mock:finally") .end(); } }; }
@Override protected RouteBuilder createRouteBuilder() throws Exception { return new RouteBuilder() { @Override public void configure() throws Exception { from("direct:start") .doTry() .to("validator:org/apache/camel/component/validator/person.xsd") .to("mock:valid") .doCatch(ValidationException.class) .to("mock:invalid") .doFinally() .to("mock:finally") .end(); } }; }
@Override protected RouteBuilder createRouteBuilder() throws Exception { return new RouteBuilder() { @Override public void configure() throws Exception { from("direct:start") .doTry() .to("validator:org/apache/camel/component/validator/xsds/person.xsd") .to("mock:valid") .doCatch(ValidationException.class) .to("mock:invalid") .doFinally() .to("mock:finally") .end(); } }; }
protected RouteBuilder createRouteBuilder() throws Exception { return new RouteBuilder() { public void configure() throws Exception { // make sure mock:error is the dead letter channel // use no delay for fast unit testing errorHandler(deadLetterChannel("mock:error").maximumRedeliveries(2).redeliveryDelay(0).logStackTrace(false)); // special for not handled when we got beer onException(ValidationException.class).onWhen(exceptionMessage().contains("beer")) .handled(false).to("mock:beer"); // special failure handler for ValidationException onException(ValidationException.class).handled(true).to("mock:invalid"); // our route logic to process files from the input folder from("file:target/messages/input/?delete=true"). process(new MyValidatorProcessor()). to("mock:valid"); } }; }
/** * Test for the valid schema location * @throws Exception */ @Test public void testRelativeParentSchemaImport() throws Exception { context.addRoutes(new RouteBuilder() { @Override public void configure() throws Exception { from("direct:start") .doTry() .to("validator:org/apache/camel/component/validator/relativeparent/child/child.xsd") .to("mock:valid") .doCatch(ValidationException.class) .to("mock:invalid") .doFinally() .to("mock:finally") .end(); } }); validEndpoint.expectedMessageCount(1); finallyEndpoint.expectedMessageCount(1); template.sendBody("direct:start", "<childuser xmlns='http://foo.com/bar'><user><id>1</id><username>Test User</username></user></childuser>"); MockEndpoint.assertIsSatisfied(validEndpoint, invalidEndpoint, finallyEndpoint); }
/** * Test for the invalid schema import location. * * @throws Exception */ @Test public void testDotSlashSchemaImport() throws Exception { this.context.addRoutes(new RouteBuilder() { @Override public void configure() throws Exception { from("direct:start").doTry() .to("validator:org/apache/camel/component/validator/dotslash/child.xsd").to("mock:valid") .doCatch(ValidationException.class).to("mock:invalid").doFinally().to("mock:finally") .end(); } }); validEndpoint.expectedMessageCount(1); finallyEndpoint.expectedMessageCount(1); template .sendBody("direct:start", "<childuser xmlns='http://foo.com/bar'><user><id>1</id><username>Test User</username></user></childuser>"); MockEndpoint.assertIsSatisfied(validEndpoint, invalidEndpoint, finallyEndpoint); }
/** * Test for the invalid schema import location. * * @throws Exception */ @Test public void testRelativeDoubleSlashSchemaImport() throws Exception { this.context.addRoutes(new RouteBuilder() { @Override public void configure() throws Exception { from("direct:start").doTry() .to("validator:org/apache/camel/component/validator/doubleslash/child.xsd") .to("mock:valid").doCatch(ValidationException.class).to("mock:invalid").doFinally() .to("mock:finally").end(); } }); validEndpoint.expectedMessageCount(1); finallyEndpoint.expectedMessageCount(1); template .sendBody("direct:start", "<childuser xmlns='http://foo.com/bar'><user><id>1</id><username>Test User</username></user></childuser>"); MockEndpoint.assertIsSatisfied(validEndpoint, invalidEndpoint, finallyEndpoint); }
/** * Test for the valid schema location relative to a path other than the validating schema * @throws Exception */ @Test public void testChildParentUncleSchemaImport() throws Exception { context.addRoutes(new RouteBuilder() { @Override public void configure() throws Exception { from("direct:start") .doTry() .to("validator:org/apache/camel/component/validator/childparentuncle/child/child.xsd") .to("mock:valid") .doCatch(ValidationException.class) .to("mock:invalid") .doFinally() .to("mock:finally") .end(); } }); validEndpoint.expectedMessageCount(1); finallyEndpoint.expectedMessageCount(1); template.sendBody("direct:start", "<childuser xmlns='http://foo.com/bar'><user><id>1</id><username>Test User</username></user></childuser>"); MockEndpoint.assertIsSatisfied(validEndpoint, invalidEndpoint, finallyEndpoint); }
@Override protected RouteBuilder createRouteBuilder() throws Exception { return new RouteBuilder() { @Override public void configure() throws Exception { from("jms:queue:inbox") .convertBodyTo(String.class) .doTry() .to("validator:file:src/test/resources/myschema.xsd") .to("jms:queue:valid") .doCatch(ValidationException.class) .to("jms:queue:invalid") .doFinally() .to("jms:queue:finally") .end(); from("jms:queue:valid").to("mock:valid"); from("jms:queue:invalid").to("mock:invalid"); from("jms:queue:finally").to("mock:finally"); } }; }
@Override protected RouteBuilder createRouteBuilder() throws Exception { port = AvailablePortFinder.getNextAvailable(8000); return new RouteBuilder() { public void configure() { from("jetty:http://localhost:" + port + "/test") .convertBodyTo(String.class) .to("log:in") .doTry() .to("validator:OptimizationRequest.xsd") .transform(constant("<ok/>")) .doCatch(ValidationException.class) .transform(constant("<error/>")) .end() .to("log:out"); } }; }
/** * Gets {@link ErrorExtEnum} of specified exception. * * @param ex the exception * @return InternalErrorEnum */ public static ErrorExtEnum getError(Throwable ex) { if (ex instanceof IntegrationException) { return ((IntegrationException) ex).getError(); } else if (ex instanceof ValidationException) { return InternalErrorEnum.E102; } else if (ex instanceof IOException || ex instanceof WebServiceIOException) { return InternalErrorEnum.E103; } else if (ex instanceof CamelAuthorizationException || ex instanceof AccessDeniedException) { return InternalErrorEnum.E117; } else { return InternalErrorEnum.E100; } }
@Override protected RouteBuilder createRouteBuilder() throws Exception { final XmlSchemaValidator xmlSchemaValidator = new XmlSchemaValidator(EbmsUtils.fileFromClasspath("schemas/ebms-header-3_0-200704.xsd")); return new RouteBuilder() { @Override public void configure() throws Exception { from("direct:testValidator") .doTry() .setBody(xpath("//*[local-name()='Messaging']")) .convertBodyTo(InputStream.class) .process(xmlSchemaValidator) .to(mockValid) .doCatch(ValidationException.class) .to(mockInvalid) .routeId("testValidator"); } }; }
private CamelContext createCamelContext(boolean isCompact) throws Exception { CamelContext camelctx = new DefaultCamelContext(); camelctx.addRoutes(new RouteBuilder() { @Override public void configure() throws Exception { String jingURI = "jing:classpath:"; if (isCompact) { jingURI += "/schema.rnc?compactSyntax=true"; } else { jingURI += "/schema.rng"; } from("direct:start") .doTry() .to(jingURI) .to("mock:valid") .doCatch(ValidationException.class) .to("mock:invalid"); } }); return camelctx; }
protected RouteBuilder createRouteBuilder() { return new RouteBuilder() { public void configure() { errorHandler(deadLetterChannel("mock:error").redeliveryDelay(0).maximumRedeliveries(3)); onException(ValidationException.class).to("mock:invalid"); from("direct:start"). process(validating). to("mock:valid"); } }; }
protected RouteBuilder createRouteBuilder() { return new RouteBuilder() { public void configure() { from("direct:start") .doTry() .process(validator).to("mock:valid") .doCatch(ValidationException.class) .to("mock:invalid"); } }; }
public void testClosetMatch1() { setupPolicies(); OnExceptionDefinition result = strategy.getExceptionPolicy(policies, null, new ValidationException(null, "")); assertEquals(type1, result); result = strategy.getExceptionPolicy(policies, null, new ExchangeTimedOutException(null, 0)); assertEquals(type1, result); }
protected RouteBuilder createRouteBuilder() { return new RouteBuilder() { public void configure() { from("direct:start") .doTry() .process(validator).to("mock:valid") .doCatch(ValidationException.class) .doTry() .process(validator).to("mock:valid") .doCatch(ValidationException.class) .pipeline("log:a", "mock:invalid"); } }; }
protected RouteBuilder createRouteBuilder() { return new RouteBuilder() { public void configure() { errorHandler(deadLetterChannel("mock:error").redeliveryDelay(0).maximumRedeliveries(3)); onException(ValidationException.class).to("mock:invalid"); from("direct:start"). process(validator). to("mock:valid"); } }; }
protected RouteBuilder createRouteBuilder() { return new RouteBuilder() { public void configure() { onException(ValidationException.class).to("mock:invalid"); from("direct:start").bean("myBean").to("mock:valid"); } }; }
public void someMethod(String body, @Header("foo") String header, @ExchangeProperty("cheese") String cheese) throws ValidationException { assertEquals("old", cheese); if ("bar".equals(header)) { LOG.info("someMethod() called with valid header and body: " + body); } else { throw new ValidationException(null, "Invalid header foo: " + header); } }
protected RouteBuilder createRouteBuilder() { return new RouteBuilder() { public void configure() { errorHandler(deadLetterChannel("mock:error")); onException(ValidationException.class).to("mock:invalid"); from("direct:start"). process(validator). to("mock:valid"); } }; }
protected RouteBuilder createRouteBuilder() { return new RouteBuilder() { public void configure() { TryDefinition tryType = from("direct:start").doTry(). process(validator). to("mock:valid"); tryType.doCatch(ValidationException.class).to("mock:invalid"); tryType.doFinally().to("mock:all"); } }; }
public void process(Exchange exchange) throws Exception { order.add("VALIDATE"); Object value = exchange.getIn().getHeader("foo"); if (value == null) { throw new IllegalArgumentException("The foo header is not present."); } else if (!value.equals("bar")) { throw new ValidationException(exchange, "The foo header does not equal bar! Was: " + value); } }
protected RouteBuilder createTryCatchNoEnd() { return new RouteBuilder() { public void configure() { from("direct:a") .doTry() .process(validator) .process(toProcessor) .doCatch(ValidationException.class) .process(orderProcessor) .process(orderProcessor3) .end(); } }; }
protected RouteBuilder createTryCatchEnd() { return new RouteBuilder() { public void configure() { from("direct:a").doTry().process(validator).process(toProcessor) .doCatch(ValidationException.class).process(orderProcessor).end().process(orderProcessor3); } }; }
protected RouteBuilder createTryCatchFinallyNoEnd() { return new RouteBuilder() { public void configure() { from("direct:a").doTry().process(validator).process(toProcessor) .doCatch(ValidationException.class).process(orderProcessor).doFinally() .process(orderProcessor2).process(orderProcessor3); // continuation of the finallyBlock clause } }; }
protected RouteBuilder createTryCatchFinallyEnd() { return new RouteBuilder() { public void configure() { from("direct:a").doTry().process(validator).process(toProcessor) .doCatch(ValidationException.class).process(orderProcessor).doFinally() .process(orderProcessor2).end().process(orderProcessor3); } }; }
@Override protected RouteBuilder createRouteBuilder() throws Exception { return new RouteBuilder() { @Override public void configure() throws Exception { from("direct:start") .doTry() .to("validator:org/apache/camel/component/validator/schema.xsd") .to("mock:valid") .doCatch(ValidationException.class) .to("mock:invalid") .doFinally() .to("mock:finally") .end(); from("direct:startHeaders") .doTry() .to("validator:org/apache/camel/component/validator/schema.xsd?headerName=headerToValidate") .to("mock:valid") .doCatch(ValidationException.class) .to("mock:invalid") .doFinally() .to("mock:finally") .end(); from("direct:startNoHeaderException") .to("validator:org/apache/camel/component/validator/schema.xsd?headerName=headerToValidate") .to("mock:valid"); from("direct:startNullHeaderNoFail") .to("validator:org/apache/camel/component/validator/schema.xsd?headerName=headerToValidate&failOnNullHeader=false") .to("mock:valid"); from("direct:useNotASharedSchema") .to("validator:org/apache/camel/component/validator/schema.xsd?useSharedSchema=false") .to("mock:valid"); } }; }
public void process(Exchange exchange) throws Exception { String body = exchange.getIn().getBody(String.class); if ("London".equals(body)) { throw new ValidationException(exchange, "Forced exception by unit test"); } else if ("Madrid".equals(body)) { throw new RuntimeCamelException("Madrid is not a supported city"); } else if ("Dublin".equals(body)) { throw new ValidationException(exchange, "Dublin have good beer"); } exchange.getOut().setBody("Hello " + body); }
protected RouteBuilder createRouteBuilder() { return new RouteBuilder() { public void configure() { from("direct:start"). doTry(). process(new C24IOValidator()). to("mock:valid"). doCatch(ValidationException.class). to("mock:invalid"); } }; }
/** * Handles specified exception. * * @param ex the thrown exception * @param asynch {@code true} if it's asynchronous message processing otherwise synchronous processing * @return next route URI */ @Handler public String exceptionHandling(Exception ex, @Header(AsynchConstants.ASYNCH_MSG_HEADER) Boolean asynch) { Assert.notNull(ex, "the ex must not be null"); Assert.isTrue(BooleanUtils.isTrue(asynch), "it must be asynchronous message"); String nextUri; if (ExceptionUtils.indexOfThrowable(ex, ValidationException.class) >= 0 || ExceptionUtils.indexOfThrowable(ex, ValidationIntegrationException.class) >= 0) { Log.warn("Validation error, no further processing - " + ex.getMessage()); nextUri = AsynchConstants.URI_ERROR_FATAL; } else if (ExceptionUtils.indexOfThrowable(ex, BusinessException.class) >= 0) { Log.warn("Business exception, no further processing."); nextUri = AsynchConstants.URI_ERROR_FATAL; } else if (ExceptionUtils.indexOfThrowable(ex, NoDataFoundException.class) >= 0) { Log.warn("No data found, no further processing."); nextUri = AsynchConstants.URI_ERROR_FATAL; } else if (ExceptionUtils.indexOfThrowable(ex, MultipleDataFoundException.class) >= 0) { Log.warn("Multiple data found, no further processing."); nextUri = AsynchConstants.URI_ERROR_FATAL; } else if (ExceptionUtils.indexOfThrowable(ex, LockFailureException.class) >= 0) { Log.warn("Locking exception."); nextUri = AsynchConstants.URI_ERROR_HANDLING; } else { Log.error("Unspecified exception - " + ex.getClass().getSimpleName() + " (" + ex.getMessage() + ")"); nextUri = AsynchConstants.URI_ERROR_HANDLING; } return nextUri; }
@Test public void testMsvSchemaValidationSuccess() throws Exception { CamelContext camelctx = new DefaultCamelContext(); camelctx.addRoutes(new RouteBuilder() { @Override public void configure() throws Exception { from("direct:start") .doTry() .to("msv:/schema.rng") .to("mock:valid") .doCatch(ValidationException.class) .to("mock:invalid"); } }); MockEndpoint mockEndpointValid = camelctx.getEndpoint("mock:valid", MockEndpoint.class); mockEndpointValid.expectedMessageCount(1); MockEndpoint mockEndpointInvalid = camelctx.getEndpoint("mock:invalid", MockEndpoint.class); mockEndpointInvalid.expectedMessageCount(0); camelctx.start(); try { ProducerTemplate template = camelctx.createProducerTemplate(); template.sendBody("direct:start", ORDER_XML_VALID); mockEndpointValid.assertIsSatisfied(); mockEndpointInvalid.assertIsSatisfied(); } finally { camelctx.stop(); } }