Exception handling by composition
Hello, I'm looking for some hints, ideally a clear example if anybody can help. I try to write reusable route components wherever possible, and where I'm having difficulty is with the encapsulation of onException clauses to hide specialist exception classes from my generic route. Something like: PluggableErrorHandler myPluggableErrorHandler = new PluggableErrorHandler() { public void include(){ onException (MySpecialistException.class) .handle(true)... } }; configure() { myPluggableErrorHandler.include(); onException (SomeCoreException.class) .handle(true)... from(direct:dostuff) } I guess I need to write a custom ErrorHandler? But it's not jumping out at me from the docs, and looks like there could be some pitfalls so I wonder if I'm not looking at it right. The route is transactional and should remain so, so any custom ErrorHandler should not interfere with that, and also I want my wrapped onException clause to be evaluated first, before the other global onException clauses. Any guidance will be gratefully received. Thanks in advance. Naseem
RE: Help Accessing Remote SOAP Service with Authentication via CXF
Hello Willem, Thank you for your response. Perhaps this could be my ignorance with Spring and CXF, but do it seems that the link you gave shows how to configure the incoming service to accept a username token rather than the outgoing service. Would either the wssec.xml file [1] or the Client [1] class in that same package be what I need? I have added the spring-boot-starter to my pom.xml and registered my RouteBuilder class as a Component so Spring will automatically run it on startup successfully. However, I don't know how to get the bean definitions from Spring into the Camel route. If I create the CXF endpoint or client bean in Spring, how do I get Camel to reference it? Sorry if it's a stupid question. [1] https://github.com/apache/camel/blob/master/components/camel-cxf/src/test/resources/org/apache/camel/component/cxf/wssecurity/client/wssec.xml [2] https://github.com/apache/camel/blob/master/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/wssecurity/client/Client.java Isaiah Inuwa -Original Message- From: Willem Jiang Sent: Tuesday, January 8, 2019 8:15 PM To: users@camel.apache.org Subject: Re: Help Accessing Remote SOAP Service with Authentication via CXF Hi, I think you may need find the right moment to setup the CXFEndpoint. It could be meaningless if the camel route is loaded and the CXFConsumer is created. Can you try to use the Spring configuration to setup the cxfEndpoint this way[1]? [1] https://github.com/apache/camel/blob/master/components/camel-cxf/src/test/resources/org/apache/camel/component/cxf/wssecurity/camel/camel-context.xml Willem Jiang Twitter: willemjiang Weibo: 姜宁willem On Wed, Jan 9, 2019 at 5:59 AM Isaiah Inuwa wrote: > > Hello everyone, > > This is my first foray into Camel. I am looking into alternatives to > MuleSoft ever since they released Mule 4.0 and dropped tooling for the > Community Edition. > > My main question is how to configure a CXF client in Camel with > UsernameToken authentication. (I have more questions besides this, but > I'll start here.) My task is to synchronize contact information > between our local system and a third-party service using SOAP. Here is the > basic flow: > > - Read JSON objects from RabbitMQ > - Convert Objects to JAXB Objects required for third-party service > - POST objects to SOAP service > > I am able to read from the RabbitMQ instance, but I am confused on how > to use CXF to access the remote service. Most of the information I see > concerns using CXF as a service rather than as a client. Here is what I have > so far: > > // Application.java > public class Application { > > private static CamelContext context; > public static void main(String args[]) throws Exception { > context = new DefaultCamelContext(); > context.addRoutes(new MyRouteBuilder()); > context.start(); > } > } > > //MyRouteBuilder.java > public class MyRouteBuilder extends RouteBuilder { > > @Override > public void configure() throws Exception { > > from("rabbitmq:...") > .to("direct:start"); > > JsonDataFormat jsonDataFormat = new JsonDataFormat(); > jsonDataFormat.setLibrary(JsonLibrary.Jackson); > jsonDataFormat.setUnmarshalType(ContactInfo.class); > > from("direct:start") > .unmarshal(jsonDataFormat) > .process(new ContactInfoToOSCContactProcessor()) // returns a JAXB > Object generated by service WSDL > .choice() > ... > .to("direct:updatecontact"); > > String contactServiceUri = > "cxf://https://thirdparty.example.com/ContactService; > + "?serviceClass=" + ContactService.class.getCanonicalName() > + "=contactservice.wsdl"; > > from("direct:updatecontact") > .setHeader("operationName", constant("updateContact")) > .to(contactServiceUri) > .unmarshal(soapDF) > .log("The response was ${body[0]}"); > } > } > > > > From here, I get an error: > > org.apache.cxf.ws.policy.PolicyException: A encryption username needs to be > > declared. > > Makes sense, because I need to set the UsernameToken for the request. > I am pretty sure that with CXF, I need to add an outgoing interceptors > like this, so I tried creating a CXF Endpoint from the Camel Context and > modifying it there: > > ... > CxfEndpoint contactServiceEndpoint = (CxfEndpoint) > getContext().getEndpoint(contactServiceUri); > Map outProps = new HashMap<>(); > outProps.put(WSHandlerConstants.ACTION, WSHandlerConstants.USERNAME_TOKEN + > " " + WSHandlerConstants.TIMESTAMP); > outProps.put(WSHandlerConstants.PASSWORD_TYPE, "PasswordDigest"); > outProps.put(WSHandlerConstants.USER, "psadmin"); > outProps.put(WSHandlerConstants.PW_CALLBACK_CLASS, > UTPasswordCallback.class.getName()); > WSS4JOutInterceptor outInterceptor = new WSS4JOutInterceptor(outProps); >
Implications of Netty vs REST configuration?
I have a bundle with a shared Netty server configured for port and it works fine from a unit test or curl call. However, if I add the Swagger API and then hit it with a browser, it doesn't show the port and the call from the plugin in fails. If I add the .host().port() shown below, it all works fine. My concern is that I'm double configuring and the possibility of things getting out of synch is high. I also don't know if adding that host/port to the restConfifugration is overriding or ignoring the use of the shared Netty server. Obviously it would better if that were picked up automatically somehow. In any case, the JavaDoc and documentation for this is pretty scant so I really can't tell what the ramifications are. If it's double configuration that's a pain but can be dealt with but it if it's overriding or ignoring the Netty server altogether that's a no-go. restConfiguration().component("netty4-http") .endpointProperty("nettySharedHttpServer","#sharedNettyHttpServer") .bindingMode(RestBindingMode.json) .dataFormatProperty("prettyPrint", "true") //.host("0.0.0.0").port() //Hast to be added for Swagger to work. .apiContextPath("/api-doc") .apiProperty("api.title", "Memeber ID API") .apiProperty("api.version", "1.1") // and enable CORS .apiProperty("cors", "true");
Re: Potential bug in BeanExpression/HttpMessage
Hi Micael Yeah that smells like a bug. You are welcome to log a JIRA and if possible provide an unit test, and/or an attempted fix. We love contributions https://github.com/apache/camel/blob/master/CONTRIBUTING.md On Tue, Jan 8, 2019 at 6:36 PM Micael Kirouac wrote: > > Hi, > > I think I found a potential bug when using a bean to set a property if a > message is coming through http. I wanted to check here before opening a bug > in case I am missing something. It is very similar to the following bug which > has already been fixed: > https://stackoverflow.com/questions/40293128/camel-rest-service-throws-exception-when-returning-null/40317432#40317432. > > The difference is that instead of returning null, an exception is thrown from > the bean method. When that happen, I see the same behavior as described in > the stackoverflow post above. I included an example on how to reproduce at > the end of this email. Here is what I think happens: > > Problem starts in BeanExpression: > 1. The exchange is copied, BeanExpression: line 194: Exchange resultExchange > = ExchangeHelper.createCopy(exchange, true) > 2. The out body is then retrieved from the exchange, BeanExpression: line > 201: result = resultExchange.getOut().getBody(); > 3. Since out() is null, the in() message is copied to the out(), this makes a > copy of the HttpMessage, which is copied along with the original > ServletRequest (which contains a closed stream) DefaultExchange.class line > 317. > 4. To copy the message from in to out, the method newInstance() from > HttpMessage class is called. HttpMessage line 85 > 5. At this point, the new instance of HttpMessage tries to read the stream > from the original message which is already closed, IOException. > > Example just below, I would be expecting the EXCEPTION_CAUGHT to be the > RuntimeException I throw but instead it's a RuntimeBeanExpressionException > effectively wrapping a IOException. > > public void configureTestException() { > restDefinitionV1 = restDefinitionV1 > .get("/throwException") > .route() > .onException(Exception.class) >.process(exchange -> {System.out.println("Caught exception of type: " > + exchange.getProperty(Exchange.EXCEPTION_CAUGHT).getClass());} ) >.handled(true) > .end() >.setProperty("test").method(new TestBean(), "throwException") > .endRest(); > } > > public static class TestBean { > public void throwException() { > throw new RuntimeException("Excepting this exception to be handled by > onException"); > } > } > > -- Claus Ibsen - http://davsclaus.com @davsclaus Camel in Action 2: https://www.manning.com/ibsen2
Re: Camel & OpenWhisk
Hi, Guillaume did some work on that take a look at: https://github.com/gnodet/openwhisk-runtime-camel https://github.com/gnodet/openwhisk-camel zoran On Tue, Jan 8, 2019 at 10:53 PM Krzysztof Sobkowiak wrote: > > Hi > > Is there somewhere a sample/demo with Apache OpenWhisk action using Apache > Camel? > > Best regards > Krzysztof > > > > > -- > Krzysztof Sobkowiak (@ksobkowiak) > > JEE & OSS Architect, Integration Architect > Apache Software Foundation Member (http://apache.org/) > Apache ServiceMix Committer & PMC Member (http://servicemix.apache.org/) > Senior Delivery Architect @ Capgemini SSC (http://www.capgeminisoftware.pl/) -- Zoran Regvart