users@jax-rpc.java.net

SOAP Message don't work in NB55

From: Nelson Brizuela <nelson.brizuela_at_conexiongroup.com>
Date: Fri, 20 Apr 2007 15:39:05 -0300

Hi Kumar;

I develop web services with method type "SOAPMessage" and SAAJ API, but don't
work for me, i think that the problem may be on the client side, but i can't see
  what's the problem.
The test was made with JAX-RPC and JAXWS and the result is the same (NB55 -
JDK1.5.0_09 - SJSAS9).

Reading the tutorial from SUN, IBM, and other from google should be
straightforward the xml element transport to client. I like to work this in NB
environment.

The client receive the soap message until "getSOAPMessageResponse" element
without payload, what's wrong in my procedure, if you can help me, i paste the
service and client code.

Regards --Nelson

** SERVER SIDE **

package service;

import javax.jws.WebService;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.net.URL;
import java.util.Iterator;
import javax.xml.soap.Name;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.AttachmentPart;
import javax.activation.DataHandler;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPFactory;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
import javax.xml.transform.stream.StreamSource;
import javax.jws.soap.*;
import javax.jws.soap.SOAPBinding.*;
import javax.xml.ws.BindingType;
import javax.xml.ws.Service;
import javax.xml.ws.ServiceMode;
/**
  * @author
  */
@WebService(portName = "SoapPort", targetNamespace = "http://mywork",
serviceName = "jaxws05")
@SOAPBinding(style=Style.DOCUMENT, use=Use.LITERAL,
parameterStyle=ParameterStyle.WRAPPED)
@BindingType(value="http://schemas.xmlsoap.org/wsdl/soap/http")
@ServiceMode(value=Service.Mode.MESSAGE)
public class jaxws05 {

     public SOAPMessage getMessage() {
     String msg = null;
     SOAPMessage message = null;
     try {
         MessageFactory mf = MessageFactory.newInstance();
          message = mf.createMessage();
         SOAPHeader header = message.getSOAPHeader();
         header.detachNode();
         SOAPFactory soapFactory = SOAPFactory.newInstance();
         SOAPBody body = message.getSOAPBody();
         //Populate the body
         Name listingElementName = soapFactory.createName("document_Transfer",
"dt",
             "http://mywork");
         SOAPBodyElement listingElement = body.addBodyElement(listingElementName);

         Name attname = soapFactory.createName("ID");
         listingElement.addAttribute(attname, "property_2006");

         SOAPElement listingname = listingElement.addChildElement("name");
         listingname.addTextNode("invoice");
         SOAPElement listingdoc = listingElement.addChildElement("document");
         listingdoc.addTextNode("1");
         SOAPElement listingtel = listingElement.addChildElement("telephone");
         listingtel.addTextNode("021-550.281");

         String frontDocumID = "property_2006_attach01";
         SOAPElement frontImRef = listingElement.addChildElement("frontDocum");
         Name hrefAttName = soapFactory.createName("href");
         frontImRef.addAttribute(hrefAttName, frontDocumID);

         URL url = new URL("file:///home/a.xml");
         DataHandler dataHandler = new DataHandler(url);
         AttachmentPart att = message.createAttachmentPart(dataHandler);
         att.setContentId(frontDocumID);
         message.addAttachmentPart(att);

         } catch (Exception e) {
             System.out.println(e.getMessage());
           }
         return message;
         }
}

** CLIENT SIDE **

package jwc5;

import java.io.*;
import java.net.*;
import java.util.Iterator;

import javax.xml.soap.AttachmentPart;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.Name;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPFactory;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPPart;
/**
  * @author
  */
public class Main {
     static Object content;
     static String sreply;

     /** Creates a new instance of Main */
     public Main() {
     }
     /**
      * @param args the command line arguments
      */
     public static void main(String[] args) {
         // TODO code application logic here
         try {
             SOAPConnectionFactory scf = SOAPConnectionFactory.newInstance();
             SOAPConnection con = scf.createConnection();
             MessageFactory mf = MessageFactory.newInstance();
             javax.xml.soap.SOAPMessage message = mf.createMessage();
             SOAPPart sp = message.getSOAPPart();
             SOAPEnvelope envelope = sp.getEnvelope();
             SOAPBody body = envelope.getBody();
             SOAPHeader header = envelope.getHeader();
             header.detachNode();

             SOAPFactory soapFactory = SOAPFactory.newInstance();

             Name ElementName = soapFactory.createName("getMessage", "ns1",
"http:mywork");
             SOAPBodyElement sbe = body.addBodyElement(ElementName);

             SOAPElement nombre = se.addChildElement("name");
             SOAPElement documentNro = se.addChildElement("document");
             SOAPElement telefono = se.addChildElement("telephone");

             message.saveChanges();
             URL urlendpoint = new
URL("http://localhost:8081/jaxws05/jaxws05?WSDL");
             javax.xml.soap.SOAPMessage reply = con.call(message, urlendpoint);

             ByteArrayOutputStream baos = null;
             try {
                 baos = new ByteArrayOutputStream();
                 reply.writeTo(baos);
                 sreply = baos.toString();
             } catch(Exception e) {
                 e.printStackTrace();
             }
             System.out.println("Message client convert to text:----- \n" + sreply);
             // Now extract the attachments
             Iterator iterator = reply.getAttachments();
             while (iterator.hasNext()) {
                 AttachmentPart attached = (AttachmentPart) iterator.next();
                 String id = attached.getContentId();
                 String type = attached.getContentType();
                 System.out.println("***Attachment client " + id + " has content
type " + type);
                 content = attached.getContent();
                 System.out.println("Attachment contains:\n" + content);
             }
             con.close();
         } catch(Exception ex) {
             // TODO handle custom exceptions here
         }
     }
     }