Monday, November 26, 2007

Example calling axis endpoint and jwsdp endpoint




private void doAxisImpl(){
try {

Service service = new Service();
Call call = (Call) service.createCall();
String url = "http://cwcdev.corporate.act.org/schedule/ALMVTC.WSDL";
call.setTargetEndpointAddress(new java.net.URL(url));

org.apache.axis.message.SOAPEnvelope env = new org.apache.axis.message.SOAPEnvelope();
org.apache.axis.message.SOAPBodyElement sbe
//= new org.apache.axis.message.SOAPBodyElement(XMLUtils.StringToElement("http://cwcdev.corporate.act.org/schedule/ALMVTC.WSDL", "getVersion", ""));
= new org.apache.axis.message.SOAPBodyElement(XMLUtils.StringToElement("http://actcenters.com/ALMVTC/message/", "sayHello", ""));
SOAPFactory factory = SOAPFactoryImpl.newInstance();
Name name = factory.createName("name");
SOAPElement param = sbe.addChildElement(name);
param.addTextNode("James Zhang");

call.setProperty(Call.SOAPACTION_USE_PROPERTY, new Boolean(true));
call.setProperty(Call.SOAPACTION_URI_PROPERTY, "http://actcenters.com/ALMVTC/action/Schedule.sayHello");
call.addParameter("name", Constants.XSD_STRING, ParameterMode.IN);

env.addBodyElement(sbe);

//XMLUtils.PrettyElementToStream(env.getAsDOM(), System.out);
//env = new SignedSOAPEnvelope(env, "http://cwcdev.corporate.act.org");

System.out.println("\n============= Request ==============");
XMLUtils.PrettyElementToStream(env.getAsDOM(), System.out);

call.invoke(env);

MessageContext mc = call.getMessageContext();
System.out.println("\n============= Response ==============");
XMLUtils.PrettyElementToStream(mc.getResponseMessage().getSOAPEnvelope().getAsDOM(), System.out);
}
catch (Exception e) {
e.printStackTrace();
}
}

jwsdp ex




private void doSOAPImpl(){

/**/

SOAPConnection soapConn = null;
try {
MessageFactory msgFactory = MessageFactory.newInstance();
SOAPMessage msg = msgFactory.createMessage();
msg.setProperty(SOAPMessage.WRITE_XML_DECLARATION, "true");
msg.setProperty(SOAPMessage.CHARACTER_SET_ENCODING, "utf-8");
msg.getMimeHeaders().addHeader("SOAPAction", "http://actcenters.com/ALMVTC/action/Schedule.sayHello");
// SOAPPart soapPart = msg.getSOAPPart();
// SOAPEnvelope soapEnvelope = soapPart.getEnvelope();

// for the tutorial web service, we don't have any headers,
// so get the "free" one and detach it:
//SOAPHeader soapHeader = soapEnvelope.getHeader();
//soapHeader.detachNode();

// to the body, add the document we want to send to the
// ping request web service:
SOAPBody body = msg.getSOAPBody();
SOAPFactory soapFactory = SOAPFactory.newInstance();
Name bodyName = soapFactory.createName("sayHello", "wsdlns", "http://actcenters.com/ALMVTC/message/"
);



SOAPBodyElement bodyElement = body.addBodyElement(bodyName);
Name paramName = soapFactory.createName("name");
SOAPElement param = bodyElement.addChildElement(paramName);
param.addTextNode("James Zhang");

// echo the document first:
System.out.println("\nAbout to send the following SOAPMessage:\n");
msg.writeTo(System.out);
String url = "http://cwcdev.corporate.act.org/schedule/ALMVTC.WSDL";
System.out.println("\nto endpoint URL "+url+"\n");

// ...then send it, and echo the response:
SOAPConnectionFactory soapConnFactory = SOAPConnectionFactory.newInstance();
soapConn = soapConnFactory.createConnection();

SOAPMessage responseMsg = soapConn.call(msg, url);
System.out.println("\nGot the following SOAPMessage in response:\n");
responseMsg.writeTo(System.out);

} catch (UnsupportedOperationException e) {
logger.error(e.getMessage());
} catch (SOAPException e) {
logger.error(e.getMessage());
} catch (IOException e) {
logger.error(e.getMessage());
}finally{
try {
soapConn.close();
} catch (SOAPException e) {
e.printStackTrace();
}
}
}

No comments: