> -----Original Message-----
> From: Maria Fernandes [mailto:[EMAIL PROTECTED]]
> Sent: Tuesday, November 25, 2003 7:07 AM
> To: [EMAIL PROTECTED]
> Subject: Using the SAAJ API to send messages in AXIS
>
>
> Hello
>
> Could anyone please tell me how to send messages using the SAAJ API
> in AXIS. I am a newbie so any help would be greatly appreciated
> Any links to SAAJ API and AXIS
I haven't tested this with Axis, but here's some example code for using SAAJ to send messages that I tested with JWSDP. Hopefully it will work without changes under Axis. Look for "connection.call(" in the code below.
---
package com.ociweb.temperature;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.soap.*;
import org.w3c.dom.Node;
/**
* This is an example of a web service client that uses SAAJ.
* for an RPC-style web service.
* WSDL for this service is at
* http://www.xmethods.net/sd/TemperatureService.wsdl
* @author R. Mark Volkmann, Object Computing, Inc.
*/
public class Client {
private static final String ENDPOINT =
"http://services.xmethods.net:80/soap/servlet/rpcrouter";
private static final String NAMESPACE = "urn:xmethods-Temperature";
private static final String OPERATION = "getTemp";
private static final String SOAP_ENCODING_NS =
"http://schemas.xmlsoap.org/soap/encoding/";
private static final String SOAP_ENVELOPE_NS =
"http://schemas.xmlsoap.org/soap/envelope/";
private SOAPElement zipElement;
private SOAPMessage request;
public static void main(String[] args) throws Exception {
Client client = new Client();
String zip = "63304";
System.out.println("temperature in " + zip +
" is " + client.getTemperature(zip));
}
public Client() throws MalformedURLException, SOAPException {
MessageFactory mf = MessageFactory.newInstance();
request = mf.createMessage();
request.getSOAPHeader().detachNode(); // not using SOAP headers
SOAPBody body = request.getSOAPBody();
// Specify that the SOAP encoding style is being used.
SOAPFactory soapFactory = SOAPFactory.newInstance();
Name name = soapFactory.createName
("encodingStyle", "SOAP-ENV", SOAP_ENVELOPE_NS);
body.addAttribute(name, SOAP_ENCODING_NS);
SOAPElement operationElement =
body.addChildElement(OPERATION, "n", NAMESPACE);
zipElement = operationElement.addChildElement("zipcode");
}
private static void dumpMessage(String name, SOAPMessage message)
throws IOException, SOAPException {
System.out.println(name + " message is");
message.writeTo(System.out);
System.out.println();
}
public float getTemperature(String zipCode)
throws IOException, SOAPException {
// Populate request message with parameter values.
zipElement.addTextNode(zipCode);
//request.saveChanges(); // When is this needed?
dumpMessage("request", request);
// Make the call.
SOAPConnectionFactory scf = SOAPConnectionFactory.newInstance();
SOAPConnection connection = scf.createConnection();
SOAPMessage response = connection.call(request, new URL(ENDPOINT));
connection.close();
dumpMessage("response", response);
// Get result out of response message using DOM.
SOAPBody body = response.getSOAPBody();
SOAPElement responseElement =
getFirstChild(body, OPERATION + "Response");
SOAPElement returnElement = getFirstChild(responseElement, "return");
String value = returnElement.getValue();
zipElement.removeContents(); // prepare for future calls
return new Float(value).floatValue();
}
private static SOAPElement getFirstChild(Node parent, String localName) {
Node child = parent.getFirstChild();
while (child != null) {
if (localName.equals(child.getLocalName())) break;
child = child.getNextSibling();
}
return (SOAPElement) child;
}
}
-------------------------------------------------------------------------------------
A.G. Edwards & Sons' outgoing and incoming e-mails are electronically
archived and subject to review and/or disclosure to someone other
than the recipient.
-------------------------------------------------------------------------------------
