Archive for the ‘webservice’ tag
Accessing RESTful WebServices with Prototype.js
We can make Ajax requests using Prototype.js (version 1.6.0.3) thus:
var url = ...;
new Ajax.Request(
url,
{
method:'DELETE',
onComplete:function doMessage(response){
alert(response.status);
}
});
While working on this code, we found out that the method parameter accepted only GET or POST requests. When we give any other request type like DELETE or PUT, the Prototype.js library converted the request to POST.
Arun Jeganath found this post where the same issue is discussed. The problem was solved when we commented these lines from the Prototype.js code:
if (!['get', 'post'].include(this.method)) {
//simulate other verbs over post
params['_method'] = this.method;
this.method = 'post';
}
WebService Client in JAX-WS (Java 6 and above)
Given a WSDL URL, you may generate the Java client classes using the JDK provided tool wsimport. The basic usage:
$ wsimport <wsdl_url>
The common command-line parameters supported by this tool:
| -p | specifies the target package |
| -d | folder where generated class files are placed |
MetaWeblog API in Java
Recently we were assigned with the task of writing code to remotely insert an entry to a blog. And we had to select the Blogging software. So we started with the search for a blogging software which exposed its API. To our amazement, we found XML-RPC ruling the blogging API world. And one software independent standard for it is the MetaWeblog API.
After selecting the API, we had to find a software supporting this API. This assignment was for a demo we were to show. So we wanted a minimal blogging software with no configuration. We found Pebble (we used version 2.3.1). This software is cool. Installation is breeze, just put the WAR file in an application server, and you are ready to use it!
Now came the implementation part. I assumed the existence of a Java library implementing this MetaWeblog API. But did not find any. So the next obvious step: use some XML-RPC library in Java to implement the call. I had some bad opinion about Apache’s XML-RPC library. I had used it long back ago, and I hated the way it was implemented. Since then I have been using Python to access XML-RPC services. But, this time, the job required use of Java.
So we ventured to the next step: finding an alternative OpenSource Java XML-RPC library. We found RoX. We were happy because RoX was born out of frustration of using Apache’s XML-RPC engine. We implemented it. And RoX did not work for Pebble (still not able to figure out if it was a mistake on our part, or RoX really did have a bug). So switched to Apache (version 3.1.1). The 3.x API seems to have been simplified. So finally we wrote:
import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
...
XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
config.setServerURL(new URL("http://localhost:8080/pebble/xmlrpc/"));
XmlRpcClient client = new XmlRpcClient();
client.setConfig(config);
// Params needed: blogid, username, password, struct, publish
// First create the fourth parameter, struct:
Map<string, String> m = new HashMap<string, String>();
m.put("title", "Hello World " + Calendar.getInstance().toString());
m.put("link", "http://www.indiwiz.com/");
m.put("description", "This is the content of the post!");
Object[] params = new Object[]{"default", "username", "password", m, true};
String ret = (String) client.execute("metaWeblog.newPost", params);
System.out.println(ret);
And then we delivered our demo!
Quick WebService Client in Groovy
Writing WebService client in Groovy is simple.
import groovy.net.soap.SoapClient
def proxy = new SoapClient("http://localhost:7777/path/to/endpoint?WSDL")
def result = proxy.sayHello("Subhash")
println result
sayHello() is the operation name.
More details here. See the Installation part, you need to put the Groovy SOAP Jar in ${user.home}/.groovy/lib.
Quick WebService in Java 6
To write a quick JAX-WS webservice in Java 6:
package test;
import javax.xml.ws.Endpoint;
@javax.jws.WebService
public class MyWebService{
@javax.jws.WebMethod
public String getQuote(String name){
return "1234.56";
}
public static void main(String[] arg){
Endpoint end = Endpoint.create(new MyWebService());
end.publish("http://localhost:1000/MyWebService");
}
}
To compile and run:
$ apt -d bin/ MyWebService.java $ java -cp bin test.MyWebService