Wednesday, June 24, 2009

Rest Web Services

main() {
req = context.createSubRequest("active:fetch-customer");
req.addArgument("id", "cust:012345");
resp = context.issueSubRequest(req);
context.createResponseFrom(resp);
}

public class RESTClient {
private static String toEpr = "http://localhost:8080/axis2/services/MyService";

public static void main(String[] args) throws AxisFault {
Options options = new Options();
options.setTo(new EndpointReference(toEpr));

options.setProperty(Constants.Configuration.ENABLE_REST, Constants.VALUE_TRUE);
ServiceClient sender = new ServiceClient();
sender.engageModule(new QName(Constants.MODULE_ADDRESSING));
sender.setOptions(options);
OMElement result = sender.sendReceive(getPayload());
try {
XMLStreamWriter writer = XMLOutputFactory.newInstance()
.createXMLStreamWriter(System.out);
result.serialize(writer);
writer.flush();
} catch (XMLStreamException e) {
e.printStackTrace();
} catch (FactoryConfigurationError e) {
e.printStackTrace();
}
}
private static OMElement getPayload() {
OMFactory fac = OMAbstractFactory.getOMFactory();
OMNamespace omNs = fac.createOMNamespace(
"http://example1.org/example1", "example1");
OMElement method = fac.createOMElement("echo", omNs);
OMElement value = fac.createOMElement("Text", omNs);
value.addChild(fac.createOMText(value, "Axis2 Echo String "));
method.addChild(value);
return method;
}
}

3 comments:

  1. The code represent the rest client side code
    just look at it, how we can make a rest call to echo method with text as parametes

    Have fun guys with rest !
    Change the code with ur method names and parameter ,,:)

    ReplyDelete
  2. Given that every resource in a RESTful service is represented by a URL, it is easy to write a client for such a web service. The following is the code for a simple Web Service client for the flickr web services interface.
    There's More
    package main;import java.net.HttpURLConnection;import java.net.InetSocketAddress;import java.net.Proxy;import java.net.SocketAddress;import java.net.URL;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import org.w3c.dom.Document;public class FlickrClient {public static void main(String[] args) {String flickrURL = "http://api.flickr.com/services/rest/?method=flickr.test.echo&name=value&api_key=[yourflickrkey]";try {SocketAddress addr = new InetSocketAddress("[proxy]", 9090);Proxy proxy = new Proxy(Proxy.Type.HTTP, addr);URL u = new URL("http://api.flickr.com/services/rest/?method=flickr.test.echo&name=value&api_key=[yourflickrkey]");HttpURLConnection uc = (HttpURLConnection) u.openConnection(proxy);uc.setRequestProperty("Accept", "*/*");uc.setRequestProperty("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");uc.setRequestProperty("Accept-Language", "en-us,en;q=0.5");uc.setRequestProperty("Keep-Alive", "300");uc.setRequestProperty("ucection", "keep-alive");String proxyUser = "[netUserId]";String proxyPassword = "[netPassword]";uc.setRequestProperty("Proxy-Authorization", "NTLM " + new sun.misc.BASE64Encoder().encode((proxyUser + ":" + proxyPassword).getBytes()));DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();Document doc = docBuilder.parse(uc.getInputStream());System.out.println(doc.getDocumentElement().getTagName());System.out.println();} catch (Exception e) {e.printStackTrace();}}}

    ReplyDelete
  3. import org.apache.commons.httpclient.*;
    import org.apache.commons.httpclient.methods.*;
    import org.apache.commons.httpclient.params.HttpMethodParams;

    import java.io.*;

    public class HttpClientTutorial {

    private static String url = "http://www.apache.org/";

    public static void main(String[] args) {
    // Create an instance of HttpClient.
    HttpClient client = new HttpClient();

    // Create a method instance.
    GetMethod method = new GetMethod(url);

    // Provide custom retry handler is necessary
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
    new DefaultHttpMethodRetryHandler(3, false));

    try {
    // Execute the method.
    int statusCode = client.executeMethod(method);

    if (statusCode != HttpStatus.SC_OK) {
    System.err.println("Method failed: " + method.getStatusLine());
    }

    // Read the response body.
    byte[] responseBody = method.getResponseBody();

    // Deal with the response.
    // Use caution: ensure correct character encoding and is not binary data
    System.out.println(new String(responseBody));

    } catch (HttpException e) {
    System.err.println("Fatal protocol violation: " + e.getMessage());
    e.printStackTrace();
    } catch (IOException e) {
    System.err.println("Fatal transport error: " + e.getMessage());
    e.printStackTrace();
    } finally {
    // Release the connection.
    method.releaseConnection();
    }
    }
    }

    ReplyDelete