How to call a RESTful web service in the async way? - java

I am wondering how to call a RESTful web service asynchronously? Any suggestions?

Since REST is based on HTTP, which is a synchronous request/response protocol, your only alternative is to wrap it in an asynch call on the client side.

Two possible solutions, depending if you want push or pull.
1) The caller can add a callback URL in the request HTTP header. This URL will be called by the service when the process is finished.
2) The service can add a process URL in the answer HTTP header. the caller can thus get information about the advance of his request and get the result when it is done.

You could always use client side code to async the sync call for the RestFul Web Service Call.

Related

Receive an http post from an external server with servlet

on my public web application running on JBoss 5 with Java 6 I need to implement something that receive an http post from another external server with some parameters (server to server call) .
The server that call me doesn't need an answer, just standard http code reponse( es 200 - OK).
Can I use a servlet for this?
If not what should I use? (I cant'use restfull or soap ws).
To my knowledge, your best bet for given situation is REST or SOAP. But you already mentioned that you cannot use that.
I don't see any reason why a servlet can't be used here. You have request & response objects in servlet doPost method. You can manipulate the response object to set appropriate response code back.

RUN code after sending SOAP response in java

My web service get a SOAP request from user. After doing all the stuff in service could I send back the user response and then run some code ?
Actually I want to log the output of my service. If I run the code that logs the output and if that logging service fails, I could not send back any response due to that error.
IS there a way that I send user response of my service and then run logging service after that ?
I tried close method of SOAP handler but it runs before my service has returned response.
Essentially you need to find a callback (http://cleancodedevelopment-qualityseal.blogspot.co.uk/2012/10/understanding-callbacks-with-java.html) you can hook into after the http response has been accepted back from the server.
See here for a similar question: SOAP web service callback architecture? that was answered.
You should use callback function to sure that responsing is finished
Use try-catch to control the errors in your code. For your case you can control if your logging service fails or not and send the response to user.
Exceptions try
If you want to use something more complex you can use an async approach using queues or events.
in JEE you can use "#Asynchronous" annotation for the method that need to log. It will run asynchronously, so it will not block the return of the webservice.
If you need the result of that method, you will need manage to store the reference of the Future, for example in a singleton, then get the esult in a second Webservice call.

Can we use GWT RPC Servlet as Oauth callback URL?

I am thinking if its possible to user a GWT RPC Servlet (like MyServiceImpl) as callback URL for oauth? The oauth provider will call the callback URL and then pass URL parameters to that callback, is it possible that capture those URL parameter in the RPC servlet?
It is possible to do something like this. RemoteServlet is just another normal servlet, you can override its doPost() and doGet() methods. You will just need to filter incoming requests, if it is an Oauth callback handle it, if it is GWT-RPC request (you can find this out by checking for specific GWT HTTP headers), just delegate it to the super class.
But in reality it is better to keep those two things separated. There can't be a real reason why one servlet should be handling Oauth callbacks and GWT-RPC requests.
The deserialization that GWT uses would make this pretty tricky. It also has a lot of restrictions through it's whitelisting of the exact way in which data can be read from the request. You would probably be better off to override the 'service' method and intercept any oauth callbacks before GWT gets to them.

Call an external web service from a servlet

I'm developing a servlet that gets a name of a web service and could be forward the request to an external web service, for example: http://www.webservice.com/...
I have build a response wrapper that intercept response output but I can't forward request to an external web service, it works only if I redirect the request to a servlet that is on same server.
Example:
request.getRequestDispatcher("aMyServlet").forward(request, response) // WORKS
request.getRequestDispatcher("http://www.webservice.com/...").forward(request, response)
Doesn't because Tomcat searches http://www.webservice.com/... on the server as a local resource.
How can I do external request?
Thanks
forward method that you are using is used for communicating between server resources, (eg: servlet to servlet as you have found out) If you want to redirect to another location, you can use the HttpServletResponse's sendRedirect method.
Better option is to
Perform your own HTTP request and stream the results back to the
browser. This sounds harder than it is. Basically you create a
java.net.HttpURLConnection with the URL of the web site you want to
"redirect" to. This can actually contain the query parameters (as long as
they're not too large) since it will never be sent to the user's browser
either and will not appear in the browser URL bar. Open the connection, get
the content and write it to the Servlet's OutputStream.
To make any request to an outside service, you'll have to explicitly make a new HTTP request and handle its response. Take a look at the HttpUrlConnection class.
You don't mention what kind of service you want to invoke, but either way, your servlet is functioning as a service client, so you should be looking at service client technologies.
For invoking REST style services, java.net.URL or Apache Commons HttpClient can be used to send a request for a URL and fetch the reponse.
For invoking SOAP services, you can use Apache Axis, or Java WSIT.

Google protocol buffers and servlets

I am wondering how I can use google protocol buffers to accept a request and send a response back to a client? I am thinking about writing a servlet which will take a request.
Is the following trail of thought the correct way to implement this:
1. Have a .proto file which is the message definition for the incoming request.
2. Write a servlet which accepts this request, does various tasks like querying database
and then sends a response. Will this response require a separate .proto message definition with all the fields that make up the response?
3. Will the client just invoke the doGet() method of my servlet and pass the request, it should then return a response as a protobuff object?
Any suggestion or idea will be very much appreciated.
Typically you'd want a request message and a response message, yes. You'd also probably want a method name to describe the action - that's certainly how the built-in PB services work.
The client wouldn't invoke doGet() - it would make a request (probably a POST rather than a GET) and your servlet would receive it.
Now, ideally you could have a general "ProtocolBufferServlet" which could service the requests by handing them off to services implementing the appropriate interfaces.
I suggest you look at the documentation for Protocol Buffer services and the Java services generated code for more information. You could implement an RpcChannel which worked over servlets, or get the client to make the HTTP post directly. You'd probably use dependency injection of some kind at the server side to tell the servlet what was implementing the service.
HI,
I have this up and running. I ended up posting a http request as a post to my servlet. I was able to take the request protocol buffer, read the request, do some processing and then send back a response. It was actually really simple once I got it working. We used the 1 .proto file to define the request and response message structure.

Categories

Resources