package it.bigatti.soap; import java.io.*; import java.util.*; import javax.xml.parsers.*; import javax.xml.soap.*; import javax.xml.messaging.*; import org.jaxen.*; import org.jaxen.dom.*; import org.saxpath.*; import org.w3c.dom.*; import org.xml.sax.*; public class SOAPClient { String serviceURI; String operation; String operationURI; Map parameters; String soapAction; String buffer = null; SOAPMessage response; DocumentBuilderFactory factory = null; DocumentBuilder builder = null; Document document = null; public SOAPClient() throws ParserConfigurationException { this( null ); } public SOAPClient( String serviceURI ) throws ParserConfigurationException { this.serviceURI = serviceURI; factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware( true ); builder = factory.newDocumentBuilder(); parameters = new HashMap(); } public void setOperationData( String operation, String operationURI ) { this.operation = operation; this.operationURI = operationURI; } public void addParameter( String name, String value ) { parameters.put( name, value ); } public Object invoke() throws SOAPException, SAXException, IOException { SOAPElement element; document = null; buffer = null; MessageFactory mf = MessageFactory.newInstance(); SOAPMessage msg = mf.createMessage(); SOAPPart sp = msg.getSOAPPart(); SOAPEnvelope env = sp.getEnvelope(); SOAPHeader hdr = env.getHeader(); SOAPBody bdy = env.getBody(); env.setEncodingStyle("http://schemas.xmlsoap.org/soap/encoding/"); SOAPBodyElement gltp = bdy.addBodyElement( env.createName(operation, "m", operationURI)); Iterator param = parameters.keySet().iterator(); while( param.hasNext() ) { String name = (String)param.next(); String value = (String)parameters.get( name ); element = gltp.addChildElement( env.createName( name ) ).addTextNode( value ); } if( soapAction != null ) { MimeHeaders mh = msg.getMimeHeaders(); mh.setHeader( "SOAPAction", "\"" + soapAction + "\"" ); msg.saveChanges(); } URLEndpoint endpoint = new URLEndpoint( serviceURI ); SOAPConnectionFactory scf = SOAPConnectionFactory.newInstance(); SOAPConnection conn = scf.createConnection(); response = conn.call( msg, endpoint ); if( response != null ) { buffer = messageToString( response ); document = builder.parse( new ByteArrayInputStream( buffer.getBytes() ) ); } return buffer; } XPath createXPath( String query ) throws SOAPException, JaxenException { //Uses DOM to XPath mapping XPath xpath = new DOMXPath( query ); //Define a namespaces used in response SimpleNamespaceContext nsContext = new SimpleNamespaceContext(); SOAPPart sp = response.getSOAPPart(); SOAPEnvelope env = sp.getEnvelope(); SOAPBody bdy = env.getBody(); //Add namespaces from SOAP envelope addNamespaces( nsContext, env ); //Add namespaces of top body element Iterator bodyElements = bdy.getChildElements(); while( bodyElements.hasNext() ) { SOAPElement element = (SOAPElement)bodyElements.next(); addNamespaces( nsContext, element ); } xpath.setNamespaceContext( nsContext ); return xpath; } void addNamespaces( SimpleNamespaceContext context, SOAPElement element ) { Iterator namespaces = element.getNamespacePrefixes(); while( namespaces.hasNext() ) { String prefix = (String)namespaces.next(); String uri = element.getNamespaceURI( prefix ); context.addNamespace( prefix, uri ); //System.out.println( "prefix " + prefix + " " + uri ); } } public Map getValuesAsMap( String query ) throws SOAPException, JaxenException, IllegalArgumentException, SAXException, IOException { XPath xpath = createXPath( query ); List results = (List)xpath.selectNodes( document ); Map ritorno = new HashMap(); Iterator iter = results.iterator(); while( iter.hasNext() ) { Object element = iter.next(); if( element instanceof org.w3c.dom.Node ) { org.w3c.dom.Node node = (org.w3c.dom.Node)element; ritorno.put( node.getNodeName(), nodeContent( node ) ); } } return ritorno; } public List getValuesAsList( String query ) throws SOAPException, JaxenException, IllegalArgumentException, SAXException, IOException { XPath xpath = createXPath( query ); List results = (List)xpath.selectNodes( document ); List ritorno = new LinkedList(); Iterator iter = results.iterator(); while( iter.hasNext() ) { Object element = iter.next(); if( element instanceof org.w3c.dom.Node ) { ritorno.add( nodeContent( (org.w3c.dom.Node)element ) ); } else { ritorno.add( element ); } } return ritorno; } public String getValue( String query ) throws SOAPException, JaxenException, ParserConfigurationException, IllegalArgumentException, SAXException, IOException { String result = null; List list = getValuesAsList( query ); if( list.size() == 0 ) { result = null; } else if( list.size() == 1 ) { result = (String)list.get(0); } else { result = list.toString(); } return result; } String messageToString( SOAPMessage msg ) throws SOAPException, IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); msg.writeTo( out ); return out.toString(); } String nodeContent( org.w3c.dom.Node nodo ) { String result = ""; NodeList childes = nodo.getChildNodes(); for( int i=0; i