import java.net.URL; import java.util.Iterator; import javax.xml.soap.MessageFactory; import javax.xml.soap.MimeHeaders; import javax.xml.soap.SOAPBody; import javax.xml.soap.Name; import javax.xml.soap.SOAPBodyElement; import javax.xml.soap.SOAPConnection; import javax.xml.soap.SOAPConnectionFactory; import javax.xml.soap.SOAPElement; import javax.xml.soap.SOAPFactory; import javax.xml.soap.SOAPFault; import javax.xml.soap.SOAPMessage; /* * Client Class for Calculator based on only SAAJ API. * * ============================================ * When Who What * ============================================ * 2007-06-07 Olle Kullberg Created. * ============================================ */ public class SAAJCalculatorClient { private final static String CALC_ENDPOINT_URL="http://localhost:8080/calculator/Calculator"; private final static String CALC_NAMESPACE = "http://www.programmera.net/ws/Calculator"; public static void main (String[] args) throws Exception { System.out.println("===================================="); System.out.println("Main 1. Check parameters"); System.out.println("===================================="); // First chech nr of args. if (args.length != 3){ System.err.println("Method is DIV or MUL."); System.err.println("Arguments must be integers."); System.exit(2); } int a1=0; int a2=0; String command="NONE"; float floatRes=0; // First try to convert the 2 last arguments try{ command= args[0]; a1= Integer.parseInt(args[1]); a2= Integer.parseInt(args[2]); }catch(Exception e){ System.err.println("Usage: java SAAJCalculatorClient method arg1 arg2"); System.err.println("Method is DIV or MUL."); System.err.println("Arguments must be integers."); System.exit(2); } // Check if command is ok String operationStr="", partStr="", param1Str="", param2Str=""; if ( command.equalsIgnoreCase("MUL") ){ System.out.println("Operation is multiplication."); operationStr="multiplication"; partStr="multiplicationRequestPart"; param1Str="multiplicand"; param2Str="multiplier"; }else if ( command.equalsIgnoreCase("DIV")){ System.out.println("Operation is division."); operationStr="division"; partStr="divisionRequestPart"; param1Str="dividend"; param2Str="divisor"; }else{ System.err.println ("Usage: java Main method arg1 arg2"); System.err.println ("Method must be: MUL or DIV."); System.exit(2); } System.out.println("===================================="); System.out.println("Main 2. Build SOAP message."); System.out.println("===================================="); SOAPFactory soapFactory = SOAPFactory.newInstance(); MessageFactory msgFactory= MessageFactory.newInstance(); SOAPMessage message = msgFactory.createMessage(); System.out.println("1. Detach the SOAP header."); message.getSOAPHeader().detachNode(); System.out.println("2. Create SOAP body."); SOAPBody body = message.getSOAPBody(); System.out.println("3. Add the operation element (division)."); Name operationName = soapFactory.createName(operationStr,"cal",CALC_NAMESPACE); SOAPBodyElement operationBE= body.addBodyElement(operationName); System.out.println("4. Add the request part element."); Name requestPartName = soapFactory.createName(partStr); SOAPElement requestPartE = operationBE.addChildElement(requestPartName); System.out.println("5. Add the first parameter element."); Name parameter1Name = soapFactory.createName(param1Str); SOAPElement parameter1E = requestPartE.addChildElement(parameter1Name); parameter1E.addTextNode(a1+""); System.out.println("6. Add the second parameter element."); Name parameter2Name = soapFactory.createName(param2Str); SOAPElement parameter2E = requestPartE.addChildElement(parameter2Name); parameter2E.addTextNode(a2+""); // This is a fix if the SOAPAction header is not set. // Axis, for example, does this automatically. System.out.println("7. Add empty SOAPAction mime header."); MimeHeaders mimeHeader = message.getMimeHeaders(); mimeHeader.addHeader("SOAPAction",""); System.out.println("8. SOAP message finished, print it:"); message.writeTo(System.out); System.out.println(""); System.out.println("===================================="); System.out.println("Main 3. Send request"); System.out.println("===================================="); System.out.println("1. Create SOAP connection factory."); SOAPConnectionFactory connectionFactory = SOAPConnectionFactory.newInstance(); System.out.println("2. Create SOAP connection."); SOAPConnection connection =connectionFactory.createConnection(); SOAPMessage response=null; if(connection != null){ System.out.println("3. Call enpoint URL="+CALC_ENDPOINT_URL); response = connection.call(message, new URL(CALC_ENDPOINT_URL)); System.out.println("Response retrieved."); }else{ System.err.println("No connection to "+CALC_ENDPOINT_URL); } System.out.println("===================================="); System.out.println("Main 4. Fetch Response"); System.out.println("===================================="); response.writeTo(System.out); System.out.println(""); if(response != null){ SOAPBody respBody= response.getSOAPBody(); if (respBody.hasFault()){ System.err.println("Is fault"); SOAPFault respFault = respBody.getFault(); System.err.println("fault code= "+respFault.getFaultCode()); System.err.println("fault string= "+respFault.getFaultString()); System.err.println("fault detail= "+respFault.getDetail().getTextContent()); System.exit(2); }else{ Iterator iter1 = respBody.getChildElements(); if( iter1.hasNext()){ SOAPElement element1= (SOAPElement) iter1.next(); Iterator iter2= element1.getChildElements(); if ( iter2.hasNext()){ SOAPElement element2= (SOAPElement) iter2.next(); String result= element2.getValue(); System.out.println("Result="+result); }else{ System.err.println("Response malformed."); } }else{ System.err.println("Response malformed."); } } }else{ System.err.println("No response delivered!"); } System.out.println("Program finished!"); } }