package axis2;

import org.apache.axis2.AxisFault;
import org.apache.axis2.Constants;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.ws.commons.om.OMAbstractFactory;
import org.apache.ws.commons.om.OMElement;
import org.apache.ws.commons.om.OMFactory;
import org.apache.ws.commons.om.OMNamespace;

public class XmlEchoClient {
    private static EndpointReference targetEPR = new EndpointReference("http://st-dbennett:8080/axis2/services/XmlEcho");

    public static void main(String[] args) {
        try {
            Options options = new Options();
            options.setTo(targetEPR);
            options.setTransportInProtocol(Constants.TRANSPORT_HTTP);

            OMFactory fac = OMAbstractFactory.getOMFactory();
            OMNamespace omNs = fac.createOMNamespace("http://hdayservice.com", "HdayService");
            OMElement payload = fac.createOMElement("echoString", omNs);
            OMElement value = fac.createOMElement("Text", omNs);
            value.setText("Hello, Mark (from Java via Axis2 Client)");
            payload.addChild(value);

            ServiceClient sender = null;
            sender = new ServiceClient();
            sender.setOptions(options);
            // Sync Version
            OMElement result = sender.sendReceive(payload);
            System.out.println(result.getFirstElement().getText());
        } catch (AxisFault axisFault) {
            axisFault.printStackTrace();
        }
    }
}
