incubator-agila-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Davide Ling <lin...@libero.it>
Subject Re: get back the response message
Date Sun, 05 Feb 2006 10:05:07 GMT
On Saturday 04 February 2006 21:14, tnguyen@symcor.com wrote:
> Davide, thanks for the response.  Could you (or anyone) send me a sample of
> how to construct a soap request and send it to AgilaEngine?

I'm sorry if the example below is quite long,
but the key step are the SOAP headers.

You need three headers: 'partner', 'port' and 'operation', all with namespace
'http://www.apache.org/agila'.

Another (maybe) interesting thing is how to build up the order item list.
Item list is an ItemType collection in the WSDL document:
<xsd:complexType name="itemType">
   <xsd:sequence>
       <xsd:element name="productCode" type="xsd:string" />
       <xsd:element default="not specified"
                           name="productName" type="xsd:string" />
       <xsd:element default="1" name="quantity"
                             type="xsd:integer" />
       <xsd:element name="rowPrice" type="xsd:double" />
   </xsd:sequence>
</xsd:complexType>

<xsd:complexType name="itemListType">
        <xsd:sequence>
               <xsd:element maxOccurs="unbounded" minOccurs="1"
                              name="item" type="tns:itemType" />
        </xsd:sequence>
</xsd:complexType>

To send the SOAP message see sendSOAPMessage method in the SOAPMessageHandler
class (the 2nd code section).

Ah... this example would not be a good code example... and
would not be good english example too. I'm sorry.

// ---- The order message builder method
public SOAPMessage createSOAPMessage() throws SOAPException {
        MessageFactory messageFactory = MessageFactory.newInstance();
        SOAPMessage message = messageFactory.createMessage();
        
        SOAPFactory soapFactory = SOAPFactory.newInstance();
        
        SOAPMessageHandler soapMessageHandler = new SOAPMessageHandler();
        
        // ----------- Headers creation -------------
        SOAPHeader header = message.getSOAPHeader();
               
        soapMessageHandler.createHeaderElement(message, "partner", "tws",
                   "http://www.apache.org/agila","orderingPL");
        soapMessageHandler.createHeaderElement(message, "port", "tws",
                   "http://www.apache.org/agila","orderPT");
        soapMessageHandler.createHeaderElement(message, "operation", "tws",
                   "http://www.apache.org/agila","sendOrder");
        // ---------------------------------------------------        

        SOAPBody body = message.getSOAPBody();
        
        SOAPElement orderMessageElement =
             soapMessageHandler.createIntermediateNode(body , "orderMessage",
                     "tn","http://davideling.altervista.org/divisione-wsdl/");
        
        soapMessageHandler.createTextSOAPElement(orderMessageElement, "name",
                     orderMessage.getName());
        soapMessageHandler.createTextSOAPElement(orderMessageElement,
                   "surname", orderMessage.getSurname());
        
        SOAPElement address = soapMessageHandler.createIntermediateNode(
                    orderMessageElement, "address");
        
        soapMessageHandler.createTextSOAPElement(address, "street", 
                               orderMessage.getAddress().getStreet());
        soapMessageHandler.createTextSOAPElement(address, "number",
                               orderMessage.getAddress().getNumber());
        soapMessageHandler.createTextSOAPElement(address, "city",
                               orderMessage.getAddress().getCity());
        soapMessageHandler.createTextSOAPElement(address, "zip",
                               orderMessage.getAddress().getZip());
        soapMessageHandler.createTextSOAPElement(address, "country",
                               orderMessage.getAddress().getCountry());
        
        soapMessageHandler.createTextSOAPElement(
              orderMessageElement, "taxNumber", orderMessage.getTaxNumber());
        soapMessageHandler.createTextSOAPElement(orderMessageElement,
              "cardNumber", orderMessage.getCardNumber());
        
        SOAPElement itemList = soapMessageHandler.createIntermediateNode(
                                            orderMessageElement,  "itemList");
        
        Iterator<Item> iterator = orderMessage.getItemList().iterator();
        Item item;
        SOAPElement itemSOAPElement;
        while (iterator.hasNext()) {
            item = iterator.next();
            itemSOAPElement = soapMessageHandler.createIntermediateNode(
                                       itemList, "item");
            soapMessageHandler.createTextSOAPElement(
                      itemSOAPElement, "productCode", item.getProductCode());
            soapMessageHandler.createIntSOAPElement(
                      itemSOAPElement, "quantity", item.getQuantity());
        }
        
        soapMessageHandler.createTextSOAPElement(
                orderMessageElement, "shipMode", orderMessage.getShipMode());
        soapMessageHandler.createDoubleSOAPElement(
              orderMessageElement, "totalPrice",orderMessage.getTotalPrice());
        soapMessageHandler.createTextSOAPElement(orderMessageElement,
                     "emailAddress", orderMessage.getEmailAddress());
        
        message.saveChanges();
        
        return message;   
    }

// --------- The Helper class
public class SOAPMessageHandler {
    private SOAPFactory soapFactory;
    
    /** Creates a new instance of SOAPMessageBuilder */
    public SOAPMessageHandler() throws SOAPException {
        soapFactory = SOAPFactory.newInstance();
    }
    
    public void createHeaderElement(SOAPMessage message, String 
headerElementName, String prefix, String namespace, String headerElementText)
        throws SOAPException {
        SOAPHeader header = message.getSOAPHeader();
        SOAPHeaderElement operationElement = 
header.addHeaderElement(soapFactory.createName(headerElementName,prefix,namespace));
        operationElement.addTextNode(headerElementText);
    }
    
    public SOAPElement createIntermediateNode(final SOAPElement father, String 
elementName, String prefix, String namespace)
        throws SOAPException {
        Name nodeName = soapFactory.createName(elementName, prefix, 
namespace);
        return father.addChildElement(nodeName);
    }
    
    public SOAPElement createIntermediateNode(final SOAPElement father, String 
elementName)
        throws SOAPException {
        Name nodeName = soapFactory.createName(elementName);
        return father.addChildElement(nodeName);
    }
    
    public void createTextSOAPElement(final SOAPElement father, String 
newElemntName, String newElementText)
        throws SOAPException {

        Name partName = soapFactory.createName(newElemntName);
        SOAPElement newElement = father.addChildElement(partName);
        newElement.addTextNode(newElementText);
    }
    
    public void createTextSOAPElement(final SOAPElement father, String 
newElemntName, String newElementText, String prefix, String namespace)
        throws SOAPException {

        Name partName = soapFactory.createName(newElemntName, prefix, 
namespace);
        SOAPElement newElement = father.addChildElement(partName);
        newElement.addTextNode(newElementText);
    }
    
    public void createIntSOAPElement(final SOAPElement father, String 
newElemntName, int newElementValue)
        throws SOAPException {

        Name partName = soapFactory.createName(newElemntName);
        SOAPElement newElement = father.addChildElement(partName);
        newElement.addTextNode(""+newElementValue);
    }
    
    public void createDoubleSOAPElement(final SOAPElement father, String 
newElemntName, double newElementValue)
        throws SOAPException {

        Name partName = soapFactory.createName(newElemntName);
        SOAPElement newElement = father.addChildElement(partName);
        newElement.addTextNode(""+newElementValue);
    }
    
    public static SOAPMessage sendSOAPMessage(SOAPMessage message, URL 
serviceUrl) throws SOAPException {
        SOAPConnectionFactory connectionFactory = 
SOAPConnectionFactory.newInstance();
        SOAPConnection connection = connectionFactory.createConnection();
        SOAPMessage response = connection.call(message,  serviceUrl);
        connection.close();
        return response;
    }

I hope these things can help you
Bye

-- 
Davide Ling
Sito Personale - http://davideling.altervista.org
Key fingerprint = 284A 0FB9 F9F6 763C D429  E02B AA5D 483A 7E45 D2A6

Mime
View raw message