How can I cosume a GET REST call and mapping to a java bean (object) through Apache Camel? - apache

I am new in apache camel. I want to do a GET REST call to get data and then I want to mapping these data to my Java bean. How can I do that with camel? I want to do it in a spring MVC web application.
I know how to do it with RestTemplate for example, but I want to use apache camel.
I've checked this documentation http://camel.apache.org/cxfrs.html but still I don't know how to set up for accomplishing this.
Please if you can provide some examples will be great.

There are a few different options. I'll walk through one...
First, define your rest configuration with bindingMode=auto
restConfiguration()
.component("jetty").host("0.0.0.0").port(9000)
.bindingMode(RestBindingMode.auto);
Next, when you define your particular rest service, specify a type (this is the type of the incoming body:
rest("/")
.put("/A/{subpath1}/{subpath2}")
.type(MyPojo.class)
.to("direct:XYZ");
That's it! The unmarshalling will be magical ;)
Alternatively, you can unmarshal things yourself.
If you'd like to see a working example of the above, check out this program: it has a main() to test it. https://github.com/DariusX/CamelSandbox/blob/master/CamelSandbox/src/main/java/com/zerses/camelsandbox/rest/RestConsumerBindingTest.java

Related

Jax-rs URI call

I'm using HttpClient to access some api(Kubernetes) but I want to switch to jax-rs. Can the same functionality be achieved from Jax-rs? I was accessing the APIs in a java function. I'm a newbie to jax-rs and so far I know that it's use to create APIs.
Any help would be appreciable.
https://docs.oracle.com/javaee/7/api/javax/ws/rs/client/package-summary.html is the package summary for the javax.ws.rs.client package. with examples and directions below. Basically,
use a ClientBuilder to get a Client (with heavyweight/common stuff
like http/https parameters)
Create a WebTarget to denote an endpoint (including template placeholders)
Resolve that target (to get another WebTarget) with parameters
Invoke it to get and process the results (comes back in a Response object)

single WCF endpoint for all commands in Nservicebus

We are trying to build a Nservicebus service that can communicated with form and wpf based clients using WCF. I have read that you can inherit from WcfService.
like:
public class ThirdPartyWebSvc : WcfService<ThirdPartyCmd, ThirdPartyCmdResponse>
And then you simple create a endpoint in the app.config and you done like described here. but the problem is that i have to create a endpoint for every command.
I would like to have a single endpoint that excepts any command and returns its response.
public class ThirdPartyWebSvc : WcfService<ICommand, IMessage>
Can someone point me in the right direction? Using Nservicebus for client communication can't be done for us and i don't want to build a proxy like server unless thats the only way to do it.
Thanks
So from what I can gather, you want to expose a WCF service operation which consumers can call to polymorphically pass one of a number of possible commands to, and then have the service route that command to the correct NServiceBus endpoint which then handles the command.
Firstly, in order to achieve this you should forget about using the NserviceBus.WcfService base class, because to use this you must closely follow the guidance in the article you linked in your post.
Instead, you could:
design your service operation contract to accept polymorphic requests by using the ServiceKnownType attribute on your operation definition, adding all possible command types,
host the service using a regular System.ServiceModel.ServiceHost(), and then configure an NserviceBus.IBus in the startup of your hosted WCF service, and
define your UnicastBusConfig config section in your service config file by adding all the command types along with the recipient queue addresses
However, you now have the following drawbacks:
Because of the requirement to be able to pass in implementations of ICommand into the service, you will need to recompile your operation contract each time you need to add a new command type.
You will need to manage a large quantity of routing information in the config file, and if any of the recipient endpoints change, you will need to change your service config.
If your service has availability problems then no more messages to any of your NSB endpoints.
You will need to write code to handle what to do if you do not receive a response message from the NSB endpoints in a timely manner, and this logic may depend on the type of command sent.
I hope you are beginning to see how centralizing this functionality is not a great idea.
All the above problems would go away if you could get your clients to send commands to the bus in the standard way, but without msmq how can you do that?
Well, for a start you could look at using one of the other supported transports.
If none of these work for you and you have to use WCF hosted services, then you must follow the guidance in the linked article. This guidance is there to steer you in the correct direction - multiple WCF services sounds like a pain, until you try to centralize them into a single service - then the pain gets bigger, not less.

Consuming web service using wsdl in Domino

We use web service Consumers\ Providors quite a bit in our application, both LS and java design elements.
I have a web service (wcf) wsdl that I am trying to import into a LotusScript Web Consumer design element and when saving I am getting the following error:
Not a member: INVOKE
...on line indicated below.
Function GetCopyDestinations(service As XSD_STRING, ocsCode As XSD_STRING, Fault1 As InteropErrorInfoStruct_n1) As ArrayOfInteropMessageDestination_n1
Set GetCopyDestinations = Service.Invoke("GetCopyDestinations", service, ocsCode, Fault1) <<<<=====ERROR ON THIS LINE
End Function
I take this to be that the wsdl --> Web service classes transformer does not like the structure of the wsdl...? I work with the developer who is creating the service we are consuming, so I can get the structure changed, but, from error above, I am not really sure where to start. I assume this means it does not think the Object Service has the method .Invoke().
I've had a look in the file: lsxsd.lss, which LS web service elements include. The "stub" class in the LS web service inherits from PortTypeBase which is in the lsxsd.lss file, PortTypeBase does not have an invoke() method, so I am not quite sure how\ where this method is inherited from. Other LS web services we use successfully, have exactly the same structure, so it would appear this is correct.
The only thing I can think of is to get my colleague to strip the wsdl back to bare bones, then add 1 method, see if it imports, add another, see if it imports, etc.
As a side note, I tried to import the ws into a java consumer, it imports fine. I then construct the stub in exactly the same way I do for all our other java consumer services, which it appears to do successfully. When I run my test harness agent, the stub is created, but it errors when trying to run any of the actions:
java.rmi.RemoteException: No operation description was found for the service
I take this to mean, it can't find the action in the wsdl file, or something similar...? The end point url and PortName appear correct if I output as it is running.
As a side note to this...the service I am attempting to consume above, we have a version working in production now, it is in a LS web consumer, but new one has had new actions added. For a sanity check, I then imported the "live" wsdl...which we know works...into a LS web consumer design element and run a test harness agent, works as expected...good. Then I imported "live" wsdl into a java consumer design element. When running the service, we get the same error...!?
java.rmi.RemoteException: No operation description was found for the service
So now I'm really, really confused. I would like to post the wsdl here, but I think NDA etc., will not allow me, but if it's any help, I am constructing the stub as follows:
ITKInteropV10Stub stub = (ITKInteropV10Stub) new ITKInteropServiceV10_ServiceLocator().getITKInteropV10();
This is a similar stub constructor pattern to all other java web services I consume.
I have been googling\ searching for a couple of days now, searching for errors listed above, I'm really struggling to find a way forward.
Ideally, solving the import in the LS web consumer would work best, since back end logic is currently in LS Script libs which will access this service. If I get it working in java, that's fine as well, it just makes the retrieval of data from the Web Service into the LS libs a bit more complex.
Any comments\ suggestion would be welcomed with open arms!
Regards
Nick
LotusScript is not case sensitive. You have a variable called "service" of XSD_STRING and another object called "Service" which looks like it might be a NotesWebServiceEngine object? (internal LS object).

Passing client context using Unity in WCF service application

I have a WCF service application (actually, it uses WCF Web API preview 5) that intercepts each request and extracts several header values passed from the client. The idea is that the 'interceptor' will extract these values and setup a ClientContext object that is then globally available within the application for the duration of the request. The server is stateless, so the context is per-call.
My problem is that the application uses IoC (Unity) for dependency injection so there is no use of singleton's, etc. Any class that needs to use the context receives it via DI.
So, how do I 'dynamically' create a new context object for each request and make sure that it is used by the container for the duration of that request? I also need to be sure that it is completely thread-safe in that each request is truly using the correct instance.
UPDATE
So I realize as I look into the suggestions below that part of my problem is encapsulation. The idea is that the interface used for the context (IClientContext) contains only read-only properties so that the rest of the application code doesn't have the ability to make changes. (And in a team development environment, if the code allows it, someone will inevitably do it.)
As a result, in my message handler that intercepts the request, I can get an instance of the type implementing the interface from the container but I can't make use of it. I still want to only expose a read-only interface to all other code but need a way to set the property values. Any ideas?
I'm considering implementing two interfaces, one that provides read-only access and one that allows me to initialize the instance. Or casting the resolved object to a type that allows me to set the values. Unfortunately, this isn't fool-proof either but unless someone has a better idea, it might be the best I can do.
Read Andrew Oakley's Blog on WCF specific lifetime managers. He creates a UnityOperationContextLifetimeManager:
we came up with the idea to build a Unity lifetime manager tied to
WCF's OperationContext. That way, our container objects would live
only for the lifetime of the request...
Configure your context class with that lifetime manager and then just resolve it. It should give you an "operation singleton".
Sounds like you need a Unity LifetimeManager. See this SO question or this MSDN article.

Is it possible to implement a redirector / reverse proxy using RequestInterceptor from WCF REST Starter kit

I am attempting to implement a reverse proxy using the RequestInterceptor from WCF REST starter kit. I am able to set the basic header properties and configure the calls. I am getting stuck with the following aspects:
Returning an appropriate response - my webservice can return text+xml, image or json. I am not able to send the appropriate response type. The Message.CreateMessage overloads are all SOAP aligned i.e. they accept only Xml constructs, so I am not able to send either JSON or image streams. I need to convert them into XElements - I am definitely doing something wrong here.
I also want the reverse proxy to be functioning well in the presence of cookies, gzip/deflate and SSL.
Based on the above requirements, do you think it makes sense to do this using REST starter kit? The Requestinterceptor was fairly easy to plug into, however the rest of the code is driving me nuts.
There is a mapping between both JSON and arbitrary binary content to XML which is used in messages for WCF (see some examples at http://blogs.msdn.com/b/carlosfigueira/archive/2011/04/19/wcf-extensibility-message-inspectors.aspx), so you can use Message.CreateMessage to create non-XML messages as well.
Having said that, it's really not intuitive to do that in WCF as of now. The new libraries in the WCF Web API - http://wcf.codeplex.com - provide a very nice way of intercepting / redirecting / bypassing the WCF pipeline especifically for HTTP messages. Also, it support multiple formats in a native way (i.e., without need to do some mapping to XML).