• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 726
  • Last Modified:

Creating SOAP with Axis2

Hi,

i am using axis2 version .95. I am trying to build a SOAP client. I have the following code from the Axis website:


import org.apache.axis2.AxisFault;
import org.apache.axis2.Constants;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.ws.commons.om.OMElement;

import org.apache.ws.commons.om.OMAbstractFactory;
import org.apache.ws.commons.om.OMFactory;
import org.apache.ws.commons.om.OMNamespace;

import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import java.io.StringWriter;


public class SOAPTest {
    private static EndpointReference targetEPR = new EndpointReference("http://localhost:8080/axis2/services/MyService");
      /**
       * @param args
       */
      public static void main(String[] args) {
        try {
            OMElement payload = getEchoOMElement();
            Options options = new Options();
            options.setTo(targetEPR);
            options.setTransportInProtocol(Constants.TRANSPORT_HTTP);

            //Blocking invocation
            ServiceClient sender = new ServiceClient();
            sender.setOptions(options);
            OMElement result = sender.sendReceive(payload);

            StringWriter writer = new StringWriter();
            result.serialize(XMLOutputFactory.newInstance()
                    .createXMLStreamWriter(writer));
            writer.flush();

            System.out.println(writer.toString());

        } catch (AxisFault axisFault) {
            axisFault.printStackTrace();
        } catch (XMLStreamException e) {
            e.printStackTrace();
        }
      }
      
    public static OMElement getEchoOMElement() {
        OMFactory fac = OMAbstractFactory.getOMFactory();
        OMNamespace omNs = fac.createOMNamespace(
                "http://example1.org/example1", "example1");
        OMElement method = fac.createOMElement("echo", omNs);
        OMElement value = fac.createOMElement("Text", omNs);
        value.addChild(fac.createText(value, "Axis2 Echo String "));
        method.addChild(value);
       
        return method;
    }


All of the examples i have seen are similar to this, however i need something different. I already have the SOAP body as an XML file out on the file system. I would like to read in this file rather than creating the nodes, etc as in the example above. Is there a way to do this? please provide sample code.

Thanks,
Reza
0
da4thrza
Asked:
da4thrza
  • 4
  • 3
1 Solution
 
Mayank SAssociate Director - Product EngineeringCommented:
If you already have it in a file, you can read it directly:

http://www.javaalmanac.com/egs/java.io/ReadLinesFromFile.html
0
 
da4thrzaAuthor Commented:
i know how to read a text file. that is not my problem. What i have in a file is the soap body, not the whole envelope. i need to  know how to bring that into the rest of the envelope
0
 
Mayank SAssociate Director - Product EngineeringCommented:
In your existing code, whereever you create the soap body (inside the envelope), you need to replace that part by the String that you read from the file.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
da4thrzaAuthor Commented:
if you look at the code sample above, the body is being generated as such:
    public static OMElement getEchoOMElement() {
        OMFactory fac = OMAbstractFactory.getOMFactory();
        OMNamespace omNs = fac.createOMNamespace(
                "http://example1.org/example1", "example1");
        OMElement method = fac.createOMElement("echo", omNs);
        OMElement value = fac.createOMElement("Text", omNs);
        value.addChild(fac.createText(value, "Axis2 Echo String "));
        method.addChild(value);
       
        return method;
    }

this... each node is being created individually. i want to know how to stick an xml file in there insetead
0
 
Mayank SAssociate Director - Product EngineeringCommented:
I haven't worked with that API, but basically it looks like:

>> value.addChild(fac.createText(value, "Axis2 Echo String "));

will have to be replaced with value.addChild ( fac.createText ( value, "THE TEXT READ FROM THE FILE" ) ) ;

where THE TEXT READ FROM THE FILE is what you read from the file.
0
 
da4thrzaAuthor Commented:
thanks... ill give that a shot!
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Provided what you have in the file is exactly what is supposed to be the replacement for "Axis2 Echo String "
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now