import java.net.URL;
import java.util.Iterator;

import javax.xml.namespace.QName;
import javax.xml.soap.SOAPFactory;

//import javax.xml.soap.SOAPEnvelope;
//import javax.xml.soap.SOAPHeader;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMAttribute;
import org.apache.axiom.om.OMContainer;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.om.OMNode;
import org.apache.axiom.om.impl.builder.StAXBuilder;
import org.apache.axiom.om.impl.builder.StAXOMBuilder;
import org.apache.axiom.om.impl.llom.OMElementImpl;
import org.apache.axiom.soap.SOAPEnvelope;
import org.apache.axiom.soap.SOAPHeader;
import org.apache.axiom.soap.SOAPHeaderBlock;
import org.apache.axiom.soap.impl.dom.SOAPHeaderBlockImpl;
import org.apache.axiom.soap.impl.dom.SOAPMessageImpl;
import org.apache.axiom.soap.impl.dom.soap11.SOAP11HeaderBlockImpl;
import org.apache.axiom.soap.impl.dom.soap12.SOAP12HeaderBlockImpl;

import org.apache.axis2.AxisFault;
import org.apache.axis2.Constants.Configuration;
import org.apache.axis2.addressing.AddressingConstants;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.context.ConfigurationContextFactory;
import org.apache.axis2.context.MessageContext;
import org.apache.axis2.context.OperationContext;
import org.apache.axis2.description.WSDL2Constants;
import org.apache.axis2.engine.AxisConfiguration;
import org.apache.axis2.jaxws.message.databinding.impl.SOAPEnvelopeBlockImpl;
import org.apache.axis2.wsdl.WSDLConstants;
import org.apache.axis2.wsdl.WSDLConstants.WSDL11Constants;
import org.apache.neethi.PolicyEngine;
import org.apache.rampart.RampartMessageData;
import org.apache.xmlbeans.XmlOptions;
import org.apache.xmlbeans.impl.soap.SOAPHeaderElement;

import _0._1.core_types.cis.AddConsumerAccountType;
import _0._1.core_types.cis.LookupEntryDocument;

import IdentityServicesStub;
import services.xsd.AddRequestDocument;
import services.xsd.LookupRequestDocument;
import xsd.AddRequestDocument.AddRequest;
import xsd.LookupRequestDocument.LookupRequest;

public class IDServicesClient  {

     
	public static void main(String args[])  {
    try {
    	  ConfigurationContext ctx = ConfigurationContextFactory.createConfigurationContextFromFileSystem("C:/axis2-1.4.1/repository", null);

    	  IdentityServicesStub stub = new IdentityServicesStub(ctx);
    	  //stub = new UCXIdentityServicesStub("http://localhost:8080/services/UCXIdentityServices?wsdl");
    	  ServiceClient client = stub._getServiceClient();
	      
    	  Options options = new Options();
    	  options.setAction("");
    	  //options.setTo(new EndpointReference("https://localhost:8080/services/UCXIdentityServices?wsdl"));
    	  //options.setTo(new EndpointReference("http://localhost:8090/services/UCXIdentityServices?wsdl"));
    	  //options.setTo(new EndpointReference("https://localhost:8080/services/UCXIdentityServices"));
         
    	  
    	  //Load policy
    	  StAXOMBuilder builder = new StAXOMBuilder("C:/WebService-Client/policy.xml");
    	  //builder.getDocument().setCharsetEncoding("ISO-8859-1");
    	  System.out.print("getCharacterEncoding = "+builder.getCharacterEncoding());
    	  OMElement elem = builder.getDocumentElement();
    	  
    	  options.setProperty(RampartMessageData.KEY_RAMPART_POLICY, PolicyEngine.getPolicy(builder.getDocumentElement()));
    	  client.setOptions(options);
    	  //options.setProperty(Configuration.CHARACTER_SET_ENCODING, "ISO-8859-1");

		  //Including the modules for securing SOAP messages
				
    	  //client.engageModule("addressing");
    	  client.engageModule("rampart");	
          	  
    	  //Setting The Payload
    	  OMFactory factory = OMAbstractFactory.getOMFactory();
		  OMNamespace ns = factory.createOMNamespace("http://xxxxxx.com/xxxx/xxx/2009/03/xxxxxxx-services/xsd","xsd");
		    	  		 
		  elem = factory.createOMElement("LookupRequest", ns);
		  //elem.setNamespace(ns);
		  System.out.println("\n createOMElement \n" +elem.toString()+"\n");
		  
  	  LookupEntryDocument lookupEntryDoc= LookupEntryDocument.Factory.newInstance();
		  lookupEntryDoc.addNewLookupEntry().setObjectPSO("higGuid=37E7A89F-9843-DAAE-C519-F5F27674D2D2,OU=People,OU=CIS,DC=the,DC=com");
		
    	 elem.setText(lookupEntryDoc.xmlText());
          
   	   //elem.setText(lookRequestDoc.xmlText());
       //String test = elem.toString().replaceAll("&lt;", "<");
    	  
		  System.out.println("Before sendReceive !!!!!!!!!!!!!!!!!! \n");
		  System.out.println("Text sending: \n" +elem+"\n");
		  elem.setText(org.apache.commons.lang.StringEscapeUtils.escapeXml(elem.toString()));
		  //elem.setText(test);
	
		  //System.out.println("Text sending: \n" +elem.getText()+"\n");
		  OMElement resp = client.sendReceive(elem);
		  
		  //QName qn = new QName("Lookup");
		  //OMElement resp = client.sendReceive(qn,elem);
		  System.out.println("\n After sendReceive !!!!!!!!!!!!!!!!!! \n");

		  System.out.println("Response  =="+resp);
       } 
       catch(Exception e){
       	e.printStackTrace();
       	}
	}
}
	


	
	