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;
}
}