Useful utility for allowing Java code to make Ajax calls, yet the Java code
can make these calls via Dynamic Proxies created from Java interfaces for
the remote server(s).
Example:
Assume you have a tomcat instance running a JSON Command Servlet, like com.cedarsoftware's or
Spring MVC.
Assume you have a Java interface 'Explorer' that is mapped to a Java bean that you are allowing
to be called through RESTful JSON calls (Ajax / XHR).
Explorer has methods on it, like getFiles(userId), etc.
You need to use a SessionAware (JSESSIONID only) or CookieAware UrlInvocationHandler to interact
with the server so that the cookies will be placed on all requests. In Javascript within browsers,
this is taken care of for you. Not so in the Java side.
Map cookies = new HashMap();
String url = "http://www.mycompany.com:80/json/"
InvocationHandler handler = new CookieAwareUrlInvocationHandler(new URL(url), cookies);
// This will handle all cookies, or you could use (where 'sessionId' holds the value of the JSESSIONID
InvocationHandler handler = new SessionAwareUrlInvocationHandler(new URL(url), sessionId);
Explorer explorer = (Explorer) ProxyFactory.create(Explorer.class, handler);
At this point, your Java code can do this:
List files = explorer.getFiles(userId);