Using Camel to get data from a web service - java

There is a lot of information for using Camel with JMS, but I can’t find much on using Camel to request data from a web service. I am wondering if this is even possible to do this directly, or do I need to use some kind of JMS/queue?
Ideally, I want my service (using java DSL) to send out a request to another web service, retrieve the data, and then store this in a file locally. Is it possible to do this in Camel using a simple custom RouteBuilder and a camelContext? I’ve tried setting up my routes using
`from(“http:..”).to(“file:...");`
but this doesn’t seem to work, as it doesn’t seem to get the data from the web service. From what I've read, I was under the impression that the endpoint http: will build the request automatically and route this to a file.
I am now wondering what I could try next, and if this is even possible.

It is definitely possible. The best example to start with is the CXF-Proxy Example. This shows how to invoke a remote web-service from Camel. Feel free to ask specific questions if you run into issues and we can help.

Also you could use Spring Web Services Component
http://camel.apache.org/spring-web-services.html
In this case, your route would look like:
from("<some event to trigger the route>")
.to("spring-ws:<endpoint of the WS you want to use>")
.to("file:<write the WS result>")

If you don't mind trying things out at the SOAP level you could try out the approaches found here http://camel.apache.org/soap.html link. Its worth a look even if you end up using something a little more high level.

Related

Build an Web UI with AngularJS for a java project?

I would like to add a Web User Interface for an existing Java project I did time ago. I learned basics of AngularJS on codeschool.com courses and now I know how to send an http or REST request to get data for my web UI.
My Java project has a set of methods that elaborate some data from a local database and return integers or integers arrays.
The goal of the interface would be to show a bunch of charts and data directly from that Java project.
What would be the most appropriate way to do this? I heard of implementing REST services on my Java project but it seems overkill for the purpose and i'm totally confused by all the frameworks for this. What would you use?
Thanks everyone for your answers!
I would use SprinvMVC to provide data from server to client side.
Here is my project from which you can start and learn basics :
https://github.com/xwid/thaimaster
Basicly you should create spring controllers mapped to urls, by doing it this way, you will be able to retrive server data using angular js.
http://www.javabeat.net/spring-mvc-angularjs-integration/
If you don't want to use a full REST framework such as Jersey, another possibility would be to use an embeddable HTTP server (there are several) and handle the requests yourself. This would mean that requests to something like /myapp/ would return your AngularJS filled HTML page, and requests to /resources/* would provide with REST functionality.
This would give you a standalone Java program that doesn't need a servlet container, but it would be a somewhat hacky solution. Not production grade, but you'd probably learn something from having to handle the HTTP traffic yourself.
With the Spring Framework's SprigBoot, it's quite easy to implement a REST service and have a runnable java application. You can follow this Building a RESTful Web Service guide from spring.io. They are clear and quickly understandable.
Also if you are not already familiar, through these guides you can have a glimpse of gradle and maven as well.

Email fetcher using Play Framework

I need to develop an IMAP poller which pings an email server every few seconds and fetches every new email which arrives.
I've done it once for another application, but there I used an inbound mail channel from Spring Integration.
I just started "playing" with Play, and am not sure what the best way to achieve this is. I know that JavaMail already offers the possibility to fetch mails, but I am not sure how to actually package this. Should this be a separate module, a separate plugin, a service, or sth?
Should the polling functionality be implemented as a job?
NOTE: It is a web application BTW, although the description above may suggest it is not.
There are a few options to solve this:
1) Use java in a Job to poll the IMAP server at regular intervals
documentation on creating a Job is available and is pretty straight forward, just setup the job to run every minute or 5 minutes and then add the code to actually check for new emails.
http://www.playframework.org/documentation/1.2.4/jobs
If you're looking for how to check for new emails on IMAP then have a look through stack exchange there. For example, to poll gmail check out this question: Getting mail from GMail into Java application using IMAP
2) Use camel module to poll IMAP server with a custom route/processor
This is a heavyweight solution and only recommended if you want to make use of other features of Apache Camel.
The module is available here: http://www.playframework.org/modules/camel
Using camel to poll for IMAP messages is fairly easy once you get your head around how to use camel, the specific info for the IMAP route is here: http://camel.apache.org/mail.html
In my opinion you shouldn't use Play at all for this — if I understand your requirements correctly. Play is a web framework intended to handle HTTP requests. Your requirements say nothing about HTTP at all, so a large part of Play! would be useless.
You could use Play's server runtime and Job (and cron) architecture to run this, but you would be misusing the facilities of the framework for something for which they were never intended. You may also be inheriting requirements from Play that you wouldn't ever actually need for an application/service like the one you want to build (for example the Python runtime).
I think you should not use Play for this, but rather create this as a simple, straight-forward Java application using Spring. With Spring's scheduling capabilities you can just as easily implement what you want.
Naturally, when you intend to build a web front-end on top of this in the future, that would make it a completely different story.

Simple web service using Amazon SES

I would like to use AWS and especially Amazon Simple Email Service (SES) in order to setup a webservice allowing to send emails.
Emails would be triggered by some kind of POST request to the webservice with some authentication, and the webservice would then just send the email on behalf of the web service user.
Where should I start? The webservice part looks dead simple but I'm not familiar at all with web services. I still need to run some kind of multithreaded webserver since there could be a bunch of concurrent requests.
Is there a way for me to write a very simple web server in Ruby or Java to do that? Any pointer appreciated.
After some digging, the simplest solution is to use something like node.js or sinatra. The nice thing is that these are almost self-contained and require barely any configuration. They provide a very easy way to reply to HTTP requests.
Once you get one of these, you start the script with a port.
The small webservice will now listen on that port. You only need to take action based on the requests passed as parameter.
And yeah, now it sounds so obvious...
their documentation is good way to start. its quite easy to start, may encounter difficulties based on your problem domains.
you also need to implement something on top of this service.
http://docs.amazonwebservices.com/ses/latest/DeveloperGuide/index.html
http://docs.amazonwebservices.com/ses/latest/APIReference/
http://docs.amazonwebservices.com/ses/latest/DeveloperGuide/
Good Luck!
I would start with looking at AWS sdk Java.
They seem to support SES.
http://docs.amazonwebservices.com/AWSJavaSDK/latest/javadoc/index.html
Good Luck!

Implementation of web service

I just want to implement a service in java that will:
take some arguments, then search the database
return the JSON object of the fetched data
I need help to identify the ways through which I can implement this thing.
e.g. Suppose I am getting the name of the book as argument I want to render.
On service part, I have to fetch book data and convert it to JSON and write/return to response.
I was looking at the Apache Axis2 but I am not sure that I am going in the right direction.
So, pls help.
Need guidelines not implementation.
Thanks
I would suggest using JAX-RS based services which would be ideal for your scenario as you want json data. These are pretty easy to get started with. Jersey is a widely used frameworks. Also see RESTEasy.
If you are returning the data in JSON then you probably don't need to implement a full web service, which uses XML for both the request and the response.
A normal dynamic web application (written as a Java Servlet) will be able to read request parameters in the HTTP payload and return a JSON-encoded HTTP response.
However you need to consider your clients; if they are only able to access web services then you need to forget about a JSON response and simply objectify the response. However if the clients can access web resources without issue then go with the servlet approach.
If you need to go with web services then look at the Metro 2 framework.
One way to do this is to keep it standards-based.
If you are using the JEE5/6 framework, your best bet would be to go with JAX-WS - comes built-in with the JSE too (if I remember correctly)
You really just have to annotate a POJO with #WebService to achieve this.
Regarding creating a JSON response, a good bet is to stick with the implementation from http://code.google.com/p/google-gson/ ; simple and straightforward
Axis2 can handle/support the webservice related part, iaw, transforming java objects into JSON and vice-versa and providing an easy-to-use API for the communication part.
Hibernate or JPA could be useful for database related tasks, although it might be easier to just use JDBC to send some simple SQL commands to the database (especially if the database already existst).

AJAX Using a REST API Without Server Side Framework

I am building a REST based API (Read only) for accessing services of my application. I plan on writing a web application that will leverage these APIs to provide basic information on the status of the application. I plan to use AJAX (using jQuery) to show the information.
Originally I planned on using Grails, Spring MVC, RoR or one of the web frameworks to handle the back end of my application. The REST APIs I will be providing though are already built on a stanalone REST framework so I would only be leveraging the web framework for the core application and not the business logic. In the future, I might need the server side framework to handle other tasks but for now most of the work is done in the REST APIs.
My question is, should I bother with using a web application framework on the server side? I should be able to make all the API calls I need from AJAX directly from the browser. I cannot think of much I would need to do on the server side. Would it make sense to have the application be standard HTML + AJAX + REST?
Its hard to say without actually knowing more about your current setup. This is what your situation sounds like:
You already have an app ready to go with all of the business logic contained. Sounds like its written in Java?
You already have the services written and exposed through a REST api using some other standalone framework. Meaning that if you wanted to, you could access the data right now with the browser without any extra work.
You have not yet built the web application, but when you do, it will get all of its content from the REST api using XHR and jquery. I say that, because otherwise, I would think that you would already be using some kind of framework to generate the other content.
If I am correct in my assumptions, then I would say that you have no need for an additional framework layer. Grails, RoR, SpringMVC my use ajax, and aid in exposing REST services, but the bulk of what they provide is an easy way to make an application that must generate html on the server, deal with form submissions, and handle sessions in a request/response cycle. It doesn't really sound like you'll be doing any of that, and it will likely make your app more complicated.
If you did at some point need the things that rails etc. provides, I would say that you may not need to use rails to expose the rest apis you have now. You could use rails just for what you need, and continue to use what you have for the REST api.
Well, the AJAX calls need to pull data from a server somewhere. If the goal is to avoid a complicated setup on the server side, CherryPy can keep the server side code VERY small.
I've written a simple exapmle below. The first class would be where you put the logic for your ReST API. The code below the class is all you need to get the server up and running.
Install Python 2.6, save the code below to restExample.py. Then in your command line run the python file by doing "python restExample.py". Point your browser to http://localhost:8080/blog/999 and see some JSON come back.
import cherrypy
import json
# Create the controller
class Blog_Controller(object):
def get(self, entryID):
cherrypy.response.headers['Content-Type'] = 'application/json'
return json.dumps({'title':'Entry Title from DB', 'entry_text': 'Text From DB'})
def update(self, entryID, titleFromPOSTFormInput, textFromPOSTFormInput):
# Update DB with passed in arguments. entryID comes from URL,
# other two entries come from POST
cherrypy.response.headers['Content-Type'] = 'application/json'
return json.dumps({'success':True})
# Setup URL routes
d = cherrypy.dispatch.RoutesDispatcher()
d.connect(name='blog_entry', route='blog/:entryID', action='get',
controller=Blog_Controller(),
conditions={'method': ['GET']})
d.connect(name='blog_entry', route='blog/update/:entryID', action='update',
controller=Blog_Controller(),
conditions={'method': ['POST']})
config = { '/' : { 'request.dispatch': d } }
cherrypy.tree.mount(root=None, config=config)
# Start the webserver
engine = cherrypy.engine
try:
engine.start()
except:
sys.exit(1)
else:
engine.block()
It sounds like this might be a good use case for GWT with Restlet.
Forgive me for tooting my own horn, but if you are doing AJAX REST stuff with jQuery, you should probably check out my JSON-REST plugin:
http://plugins.jquery.com/project/rest
If you are getting XML back, then this won't be as useful, but you may still be able to adapt some of the code to your needs.

Categories