Code of WS sender client with Java?

81 views
Skip to first unread message

Hannou

unread,
May 8, 2009, 12:54:34 PM5/8/09
to Hermes 2.0 Discussion List
Hello everyone,

Can anyone help me how to write a class java for invoking the ws
sender, receiver of hermes2?
I tried but with no result:(
I need the code of the class please!

best regards,
hana

Steve Chan

unread,
May 9, 2009, 12:47:45 AM5/9/09
to Hermes 2.0 Discussion List
Hana,

I don't know which part of your code goes wrong since i don't have
your code.
But i guess you could try reading this first
http://community.cecid.hku.hk/index.php/product/article/writing_hermes_2_ws_client_under_java/
It should have enough guidance to walk you through how to write a
client in java.
Hope this helps,

Regards,
Steve

Hana Bouafif

unread,
May 10, 2009, 6:36:49 AM5/10/09
to cecid-...@googlegroups.com
Hi Steve,
Thanks for your reponse.
This is my class of senderWS:

import java.io.File;
import java.net.URL;
import java.net.MalformedURLException;
import java.util.Iterator;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.xml.soap.AttachmentPart;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.Name;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPFactory;
import javax.xml.soap.SOAPMessage;
import java.net.URI;


public class cliente{

        private String hermes2SenderWSURLStr = "http://127.0.0.1:8080/corvus/httpd/ebms/sender";
        private String hermes2ReceiverListWSURLStr ="http://127.0.0.1:8080/corvus/httpd/ebms/receiver_list";
        private String hermes2ReceiverWSURLStr = "http://127.0.0.1:8080/corvus/httpd/ebms/receiver";
        private String nsURI = "http://service.ebms.edi.cecid.hku.hk/";

        private String nsPrefix="tns";

        private URL senderWSURL;
        /*
         * campos necesarios para poder enviar el mensaje.
         */
        private String cpaId = "cpaID";
        private String service = "http://127.0.0.1:8080/corvus/httpd/ebms/sender";
        private String action = "order";
        private String conversationId = "convId";
        private String fromPartyId = "http://127.0.0.1:8080/corvus/httpd/ebms/sender";
        private String fromPartyType = "http://127.0.0.1:8080/corvus/httpd/ebms/sender";
        private String toPartyId = "localhost";
        private String toPartyType = "localhost";
        private String refToMessageId = "refToMessageId";
        private String serviceType= "serviceType";
        private String newMessageId ="Message01";

        public static void main(String[] args) throws Exception {
                cliente cli= new cliente();
                cli.run();
           
    }

        private void run() throws Exception {
                envoie();
        }

    private void envoie () throws Exception {

                System.out.println("Envoie de message au serveur Hermes2!! ...");

                 SOAPConnection soapConn =SOAPConnectionFactory.newInstance().createConnection();
                SOAPMessage request = MessageFactory.newInstance().createMessage();

                SOAPBody soapBody = request.getSOAPBody();
                soapBody.addChildElement(createElement("cpaId", nsPrefix, nsURI,cpaId));
                soapBody.addChildElement(createElement("service", nsPrefix, nsURI,service));
                soapBody.addChildElement(createElement("action", nsPrefix, nsURI,action));
                soapBody.addChildElement(createElement("convId", nsPrefix, nsURI,conversationId));
                soapBody.addChildElement(createElement("fromPartyId", nsPrefix,nsURI, fromPartyId));
                soapBody.addChildElement(createElement("fromPartyType", nsPrefix,nsURI, fromPartyType));
                soapBody.addChildElement(createElement("toPartyId", nsPrefix, nsURI,toPartyId));
                soapBody.addChildElement(createElement("toPartyType", nsPrefix,nsURI, toPartyType));
                soapBody.addChildElement(createElement("refToMessageId", nsPrefix,nsURI, refToMessageId));
                soapBody.addChildElement(createElement("serviceType", nsPrefix,nsURI, serviceType));
               
               

                        /*payload [] payloads = { (new payload("cpa.xml", "application/xml"))};
                        

                        for (int i=0; i < payloads.length; i++)
                        {
                        AttachmentPart attachmentPart = request.createAttachmentPart();
                        FileDataSource fileDS = new FileDataSource(new File(payloads[i].getFilePath()));
                        attachmentPart.setDataHandler(new DataHandler(fileDS));
                        attachmentPart.setContentType(payloads[i].getContentType());
                        request.addAttachmentPart(attachmentPart);
                           }*/
               
                AttachmentPart attachmentPart = request.createAttachmentPart();
                FileDataSource fileDS = new FileDataSource(new File("C:/purchase_order.xml"));
                attachmentPart.setDataHandler(new DataHandler(fileDS));
                attachmentPart.setContentType("application/xml");
                request.addAttachmentPart(attachmentPart);

                request.saveChanges();
               

                /* Envoie la réponse à Hermes en retournant un id message de service Web sender*/
               
                SOAPMessage response = soapConn.call(request,hermes2SenderWSURLStr);
                SOAPBody responseBody = response.getSOAPBody();

              if (!responseBody.hasFault())
               {
                   SOAPElement messageIdElement = getFirstChild(responseBody,"message_id", nsURI);
                   System.out.println("Le message est envoyé!!!");
                   //return messageIdElement == null ? null :messageIdElement.getValue();
                   System.out.println(messageIdElement == null ? null : messageIdElement.getValue());

                } else
                {
                     
                throw new SOAPException(responseBody.getFault().getFaultString());
              
               
                }
           
              
               //return this.newMessageId;
               //System.out.println("Le message est envoyé!!!");
              
               //return (this.newMessageId);
              
               }
  
         /* Creation de la méthode createElement qui créé un Element SOAP avec namespace Prefix nsPrefix
           et namespace URL nsURI et une valeur textuelle qui est le dernier element dans la méthode  */

        private SOAPElement createElement(String localName, String nsPrefix, String nsURI, String value)throws SOAPException
        {
        SOAPElement soapElement =SOAPFactory.newInstance().createElement(localName, nsPrefix, nsURI);
        soapElement.addTextNode(value);
        return soapElement;
        }

        private SOAPElement getFirstChild(SOAPElement soapElement,String childLocalName, String childNsURI) throws SOAPException {
        Name childName =SOAPFactory.newInstance().createName(childLocalName, null,childNsURI);
        Iterator childIter = soapElement.getChildElements(childName);
        if (childIter.hasNext())
        return (SOAPElement)childIter.next();
                 return null;
                }
       
}


and i have thes errors:

Envoie de message au serveur Hermes2!! ...
10 mai 2009 11:29:50 com.sun.xml.messaging.saaj.client.p2p.HttpSOAPConnection post
GRAVE: SAAJ0008: Bad Response; Introuvable
Exception in thread "main" com.sun.xml.messaging.saaj.SOAPExceptionImpl: java.security.PrivilegedActionException: com.sun.xml.messaging.saaj.SOAPExceptionImpl: Bad response: (404Introuvable
    at com.sun.xml.messaging.saaj.client.p2p.HttpSOAPConnection.call(HttpSOAPConnection.java:191)
    at cliente.envoie(cliente.java:100)
    at cliente.run(cliente.java:53)
    at cliente.main(cliente.java:48)
Caused by: java.security.PrivilegedActionException: com.sun.xml.messaging.saaj.SOAPExceptionImpl: Bad response: (404Introuvable
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.xml.messaging.saaj.client.p2p.HttpSOAPConnection.call(HttpSOAPConnection.java:185)
    ... 3 more
Caused by: com.sun.xml.messaging.saaj.SOAPExceptionImpl: Bad response: (404Introuvable
    at com.sun.xml.messaging.saaj.client.p2p.HttpSOAPConnection.post(HttpSOAPConnection.java:368)
    at com.sun.xml.messaging.saaj.client.p2p.HttpSOAPConnection$PriviledgedPost.run(HttpSOAPConnection.java:214)
    ... 5 more

CAUSE:

java.security.PrivilegedActionException: com.sun.xml.messaging.saaj.SOAPExceptionImpl: Bad response: (404Introuvable
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.xml.messaging.saaj.client.p2p.HttpSOAPConnection.call(HttpSOAPConnection.java:185)
    at cliente.envoie(cliente.java:100)
    at cliente.run(cliente.java:53)
    at cliente.main(cliente.java:48)
Caused by: com.sun.xml.messaging.saaj.SOAPExceptionImpl: Bad response: (404Introuvable
    at com.sun.xml.messaging.saaj.client.p2p.HttpSOAPConnection.post(HttpSOAPConnection.java:368)
    at com.sun.xml.messaging.saaj.client.p2p.HttpSOAPConnection$PriviledgedPost.run(HttpSOAPConnection.java:214)
    ... 5 more


I will be so happy if you help me to solve this problem;

best regards,
hana

 




2009/5/9 Steve Chan <steve....@gmail.com>

Steve Chan

unread,
May 11, 2009, 6:30:10 AM5/11/09
to Hermes 2.0 Discussion List
Hana,

i am just wondering, if your hermes2WSSenderUrl is accessible or not.
you could try accessing the address http://127.0.0.1:8080/corvus/httpd/ebms/sender
through a browser
to see if the location is accessible.

Regards,
Steve

Hana Bouafif

unread,
May 11, 2009, 6:49:44 AM5/11/09
to cecid-...@googlegroups.com
Hi Steve,

thanks for your response, i'm so happy because i succeed in sendind a message to hermes2 and i have the id of the created msg:)
I should write the WS retreiving to retreive the payload to partner2?
This is my message ebXML:


------=_Part_3_22452904.1242033818890
Content-Type: text/xml; charset=UTF-8
Content-Id: <soappart>

<?xml version="1.0" encoding="UTF-8" ?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://schemas.xmlsoap.org/soap/envelope/ http://www.oasis-open.org/committees/ebxml-msg/schema/envelope.xsd"><SOAP-ENV:Header xsi:schemaLocation="http://www.oasis-open.org/committees/ebxml-msg/schema/msg-header-2_0.xsd http://www.oasis-open.org/committees/ebxml-msg/schema/msg-header-2_0.xsd"><eb:MessageHeader xmlns:eb="http://www.oasis-open.org/committees/ebxml-msg/schema/msg-header-2_0.xsd" SOAP-ENV:mustUnderstand="1" eb:version="2.0" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><eb:From><eb:PartyId eb:type="http://127.0.0.1:8080/corvus/httpd/ebms/sender">http://127.0.0.1:8080/corvus/httpd/ebms/inbound</eb:PartyId></eb:From><eb:To><eb:PartyId eb:type="localhost">localhost</eb:PartyId></eb:To><eb:CPAId>A_TO_B_CPAID</eb:CPAId><eb:ConversationId>convId</eb:ConversationId><eb:Service eb:type="serviceType">http://127.0.0.1:8080/corvus/httpd/ebms/sender/inbound</eb:Service><eb:Action>A_SEND_TO_B</eb:Action><eb:MessageData><eb:MessageId>20090511-1...@192.168.1.2</eb:MessageId><eb:Timestamp>2009-05-11T11:23:38.593+02:00</eb:Timestamp><eb:RefToMessageId>refToMessageId</eb:RefToMessageId></eb:MessageData></eb:MessageHeader></SOAP-ENV:Header><SOAP-ENV:Body xsi:schemaLocation="http://www.oasis-open.org/committees/ebxml-msg/schema/msg-header-2_0.xsd http://www.oasis-open.org/committees/ebxml-msg/schema/msg-header-2_0.xsd"><eb:Manifest xmlns:eb="http://www.oasis-open.org/committees/ebxml-msg/schema/msg-header-2_0.xsd" eb:version="2.0"><eb:Reference eb:id="Payload-0" xlink:href="cid:Payload-0" xlink:type="simple"/></eb:Manifest></SOAP-ENV:Body></SOAP-ENV:Envelope>
------=_Part_3_22452904.1242033818890
Content-Type: application/xml
Content-ID: <Payload-0>


------=_Part_3_22452904.1242033818890--


It's correct?

best regards,
hana


2009/5/11 Steve Chan <steve....@gmail.com>

Steve Chan

unread,
May 12, 2009, 10:58:06 PM5/12/09
to Hermes 2.0 Discussion List
Hey Hana,

Good to hear that you are able to send successfully.
The message seems alright, but wondering if you are attaching an empty
attachment?

Regards,
Steve

Hana Bouafif

unread,
May 13, 2009, 11:24:21 AM5/13/09
to cecid-...@googlegroups.com
Hi Steve,

Yes you have right, i attached an empty attachement but this is my message response now:


------=_Part_0_1337463.1242201331906

Content-Type: text/xml; charset=UTF-8
Content-Id:  <soappart>

<?xml version="1.0" encoding="UTF-8" ?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://schemas.xmlsoap.org/soap/envelope/ http://www.oasis-open.org/committees/ebxml-msg/schema/envelope.xsd"><SOAP-ENV:Header xsi:schemaLocation="http://www.oasis-open.org/committees/ebxml-msg/schema/msg-header-2_0.xsd http://www.oasis-open.org/committees/ebxml-msg/schema/msg-header-2_0.xsd"><eb:MessageHeader xmlns:eb="http://www.oasis-open.org/committees/ebxml-msg/schema/msg-header-2_0.xsd" SOAP-ENV:mustUnderstand="1" eb:version="2.0" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><eb:From><eb:PartyId eb:type="http://127.0.0.1:8080/corvus/httpd/ebms/sender">http://127.0.0.1:8080/corvus/httpd/ebms/inbound</eb:PartyId></eb:From><eb:To><eb:PartyId eb:type="localhost">localhost</eb:PartyId></eb:To><eb:CPAId>A_TO_B_CPAID</eb:CPAId><eb:ConversationId>convId</eb:ConversationId><eb:Service eb:type="serviceType">http://127.0.0.1:8080/corvus/httpd/ebms/sender/inbound</eb:Service><eb:Action>A_SEND_TO_B</eb:Action><eb:MessageData><eb:MessageId>20090513-0...@192.168.1.16</eb:MessageId><eb:Timestamp>2009-05-13T09:55:31.859+02:00</eb:Timestamp><eb:RefToMessageId>refToMessageId</eb:RefToMessageId></eb:MessageData></eb:MessageHeader></SOAP-ENV:Header><SOAP-ENV:Body xsi:schemaLocation="http://www.oasis-open.org/committees/ebxml-msg/schema/msg-header-2_0.xsd http://www.oasis-open.org/committees/ebxml-msg/schema/msg-header-2_0.xsd"><eb:Manifest xmlns:eb="http://www.oasis-open.org/committees/ebxml-msg/schema/msg-header-2_0.xsd" eb:version="2.0"><eb:Reference eb:id="Payload-0" xlink:href="cid:Payload-0" xlink:type="simple"/></eb:Manifest></SOAP-ENV:Body></SOAP-ENV:Envelope>
------=_Part_0_1337463.1242201331906
Content-ID:  <Payload-0>
Content-Type: application/xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<options><option name="modeipc">1</option><option name="sha512password">CF83E1357EEFB8BDF1542850D66D8007D620E4050B5715DC83F4A921D36CE9CE47D0D13C5D85F2B0FF8318D2877EEC2F63B931BD47417A81A538327AF927DA3E</option><option name="tcpport">48113</option><option name="udpport">48113</option><optionsuser><optionuser name="cdromhard">true</optionuser><optionuser name="debug">1</optionuser><optionuser name="inettimeout">5</optionuser><optionuser name="mccryptalgo">1</optionuser><optionuser name="mcidenewioctl">false</optionuser><optionuser name="modules">all</optionuser><optionuser name="nivsecurite">2</optionuser><optionuser name="numapplis">-1</optionuser><optionuser name="numeventlog">50</optionuser><optionuser name="oslight">false</optionuser><optionuser name="setmaj">true</optionuser><optionuser name="setpartages">true</optionuser><optionuser name="smart">true</optionuser></optionsuser></options>

------=_Part_0_1337463.1242201331906--


I have another problem; how can i dowload the payload of my message soap??
I tried the code of clientWSReceiverList but i have a problem with the iterator:

Iterator messageIdElementIter = (Iterator) getChildren (messageIdsElement, "messageId", nsURI);

//Method of getFirstChild !!!!!!!!!!!!!!!!!! IT's RIGHT????????????


private SOAPElement getFirstChild(SOAPElement soapElement,String childLocalName, String childNsURI) throws SOAPException {
         Name childName =SOAPFactory.newInstance().createName(childLocalName, null,childNsURI);
         Iterator childIter = soapElement.getChildElements(childName);
         if (childIter.hasNext())
         return (SOAPElement)childIter.next();
                  return null;
                 }


//Method of getChildren !!!!!!!!!!!!!!!!!! IT's RIGHT????????????

    private SOAPElement getChildren(SOAPElement soapElement,String childLocalName, String childNsURI) throws SOAPException {

         Name childName =SOAPFactory.newInstance().createName(childLocalName, null,childNsURI);
        Iterator childIter = soapElement.getChildElements(childName);
         if (childIter.hasNext())
         return (SOAPElement)childIter.next();
                  return null;
                 }


And this my code to dowload the payload:

               AttachmentPart attachmentPart = (AttachmentPart) attachmentPartIter.next();
               InputStream ins = attachmentPart.getDataHandler().getInputStream();
               // Do something I/O to extract the payload to physical file.              
               BufferedWriter bf= new BufferedWriter(new FileWriter(fp));
               bf.write(ins.toString());
               bf.close();

But i found my fp all the time without any data (empty) :(
Have you any idea how can i store my payload in my a physical file??

kind regards,
hana




2009/5/13 Steve Chan <steve....@gmail.com>

Steve Chan

unread,
May 15, 2009, 3:43:26 AM5/15/09
to Hermes 2.0 Discussion List
Hey Hana,

The idea is that, you need to retrieve the corresponding available
message id through receiver_list.
Then you get each of the message through receiver.
Hope this helps you.

Regards,
Steve
Reply all
Reply to author
Forward
0 new messages