Camel to route external REST web service - spring-mvc

I'm working on spring MVC and using Apache camel to integrate external services.
I wanted to use Apache Camel route to make a Webservice call.
Like my local REST service (http://localhostsmiliex.xx:8080/users) fetching data from external REST service (http://xxx:000/users) and wanted routing to fetch external data.
Which Apache component would be suitable for a web-service route such as Jetty or producer template?

Have you tried HTTP4 or HTTP ?
http://camel.apache.org/http4.html

Use ProducerTemplate, it works like a charm for calling external endpoints REST, DB, SOAP etc..
You can either autowire it
#Autowired
ProducerTempalete prodcuerTemplate
prodcuerTemplate.sendBody("http://xyz...", "<hello>world!</hello>");
or
ProducerTemplate template = exchange.getContext().createProducerTemplate();
// send to default endpoint
template.sendBody("<hello>world!</hello>");
// send to a specific queue
template.sendBody("http://xyz...", "<hello>world!</hello>");

Related

Spring Boot : Apache CXF SOAP with #RestController for rest ws

I'm working SOAP and REST together into the same application. Rest web service with #RestController and SOAP with apache cxf.
Rest ws and soap have the same path, for example:
Rest: GET http://localhost:8080/ws/person
SOAP: http://localhost:8080/ws/findPerson
For configuring cxf servlet, i create the following method
#Bean
public ServletRegistrationBean cxfServletRegistration() {
return new ServletRegistrationBean(new CXFServlet(), "/ws/*"); }
SOAP Service are running fine after change but REST (#RestController) stop working, but if I disable the method cxfServletRegistration(), the rest WS working fine.
Could you suggest any solution to make all WS working together ?
You can't, because each servlet must "own" its listening basepath. Despite the lack of an explicit registration, RestControllers listen on a base path (default /*) Do you actually need to use #RestController? CXF has REST support via JAX-RS.
Otherwise, I would suggest to separate your REST and SOAP functionality, such as having REST on /model/... and SOAP on /api/... or some such separation.

Is there a component in camel for a service built on Spring RestTemplate?

I have a REST Service running in Tomcat server. I created this REST Service using Spring RESTTemplate. So is there a component in CAMEL to use this exposed Web service?
i have used http component. i am aware that we cannot use http component to expose a service. So please let me know which component to use.Here is a detailed description of the problem.
Service created in rest Template runs in a server which you can see in from part of camel code
i use camel to expose this service to another http service and the response of this service is the response from the other service.
so my camel code looks like this:
from("http://localhost:8080/rest/emp/dummy").to(http://anotherweservice.com")
I get this error.
Failed to create route route1: Route(route1)[[From[]] -> [process[com.routes..... because of uri must be specified and not empty
Have a look at http://camel.apache.org/how-to-use-camel-as-a-http-proxy-between-a-client-and-server.html.
Using the Jetty component, this looks as follows:
from("jetty:http://0.0.0.0:8080/myapp?matchOnUriPrefix=true")
.log("myapp: httpPath = ${header.CamelHttpPath}")
.to("jetty:http://localhost:8082/remoteapp?bridgeEndpoint=true&throwExceptionOnFailure=false")
In this example, the request is forwarded to another Jetty Camel endpoint:
from("jetty:http://0.0.0.0:8082/remoteapp?matchOnUriPrefix=true")
.log("remoteapp: httpPath = ${header.CamelHttpPath}")
.setBody(simple("${header.CamelHttpPath}"));
The HTTP path /hello/world is transfered to the endpoint, as can be seen when the service is invoked with http://localhost:8080/myapp/hello/world. In this case the body /hello/world is returned. And in the log it says:
INFO myapp: httpPath = /hello/world
INFO remoteapp: httpPath = /hello/world

Where to hook up authentication in Grizzly?

I'm using a Grizzly HttpServer which has two HttpHandler instances registered:
under /api/* there is an Jersey REST - style application offering the API of the product, and
under /* there is an StaticHttpHandler which serves static HTML / JavaScript content (which, among other things, talks to the API under /api/
For authentication I'm currently securing only the API using a Jersey ContainerRequestFilter implementing HTTP Basic Auth, which looks quite similar to what is presented in another SO question.
But as requirements changed, now I'd like to require authentication for all requests hitting the server. So I'd like to move the authentication one level up, from Jersey to Grizzly. Unfortunately, I'm completely lost figuring out where I can hook up a "request filter" (or whatever it is called) in Grizzly. Can someone point me to the relevant API to accomplish this?
The easiest solution would leverage the Grizzly embedded Servlet support.
This of course would mean you'd need to do a little work to migrate your current HttpHandler logic over to Servlets - but that really shouldn't be too difficult as the HttpHandler API is very similar.
I'll give some high level points on doing this.
HttpServer server = HttpServlet.createSimpleServer(<docroot>, <host>, <port>);
// use "" for <context path> if you want the context path to be /
WebappContext ctx = new WebappContext(<logical name>, <context path>);
// do some Jersey initialization here
// Register the Servlets that were converted from HttpHandlers
ServletRegistration s1 = ctx.addServlet(<servlet name>, <Servlet instance or class name>);
s1.addMapping(<url pattern for s1>);
// Repeat for other Servlets ...
// Now for the authentication Filter ...
FilterRegistration reg = ctx.addFilter(<filter name>, <filter instance or class name>);
// Apply this filter to all requests
reg.addMapping(null, "/*");
// do any other additional initialization work ...
// "Deploy" ctx to the server.
ctx.deploy(server);
// start the server and test ...
NOTE: The dynamic registration of Servlets and Filters is based off the Servlet 3.0 API, so if you want information on how to deal with Servlet listeners, init parameters, etc., I would recommend reviewing the Servlet 3.0 javadocs.
NOTE2: The Grizzly Servlet implementation is not 100% compatible with the Servlet specification. It doesn't support standard Servlet annotations, or deployment of traditional Servlet web application archive deployment.
Lastly, there are examples of using the embedded Servlet API here
The "hookup" part can be done using a HttpServerProbe (tested with Grizzly 2.3.5):
srv.getServerConfiguration().getMonitoringConfig().getWebServerConfig()
.addProbes(new HttpServerProbe.Adapter() {
#Override
public void onRequestReceiveEvent(HttpServerFilter filter,
Connection connection, Request request) {
...
}
#Override
public void onRequestCompleteEvent(HttpServerFilter filter,
Connection connection, Response response) {
}
});
For the "linking" to the ContainerRequestFilter you might want to have a look at my question:
UnsupportedOperationException getUserPrincipal

Thinktecture.IdentityModel.45, Routing, wants to invoke identity controller

I'm trying to use Thinktecture.IdentityModel.45 for authentication in ASP.NET Web API.
I'm trying to get the Basic Authentication to work. And have downloaded the source and got the sample to work. (JsBasicAuth).
We have Web API in the same project as a MVC application. And when the test client calls ~/api/identity all handlers and authorization work. But then the framework (web api) tries to invoke a controller called "identity" and the call fails.
{"Message":"No HTTP resource was found that matches the request URI 'http://localhost/app/api/identity'.","MessageDetail":"No type was found that matches the controller named 'identity'."}
Do I need to exclude /identity /token from the routing? What am I missing?
I now discovered the Common project in the sample solution. There is a IdentityController there. And I didn't have that in my own project. Now it works! :)

equivalent of httpwebrequest in spring mvc

In my controller, based on user post, i want to make a call to an external provider site to get process some data and get results.
example: say i want to invoke google map api at the following url
http://maps.googleapis.com/maps/api/directions/output?
This call returns an xml object with data, which I would like to use.
Only using google as an example here, but concept is the same.
How would I make the call in spring mvc controller or in the business[service] layer?
I came across jersey client which allows me to go against RESTful service.
This is the link I used:
Jersey Sample Client
Jersey Sample Client from oracle
I think you can open a http request inside a controller /Service ( Service sounds better )
I remember doing in other web framewoks with Apache http client (http://hc.apache.org/ ) but I would try google-api-client or google-api-client-servlet .
I m trying right now .

Resources