THIS IS A TEST INSTANCE. ALL YOUR CHANGES WILL BE LOST!!!!
...
Code Block | ||||
---|---|---|---|---|
| ||||
package abc; import java.io.IOException; import java.io.PrintWriter; import java.util.Iterator; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.xml.soap.MessageFactory; import javax.xml.soap.SOAPBody; import javax.xml.soap.SOAPConnection; import javax.xml.soap.SOAPConnectionFactory; 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 org.w3c.dom.Node; public class ConverterHandler extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet { static final long serialVersionUID = 1L; public ConverterHandler() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String dollars = request.getParameter("amount"); if (dollars != null && dollars.trim().length() > 0) { String rupees = null, euros = null; try { rupees = returnResult(createSOAPMessage("dollarToRupees", dollars)); euros = returnResult(createSOAPMessage("rupeesToEuro", rupees)); } catch (SOAPException e) { // TODO Auto-generated catch block e.printStackTrace(); } PrintWriter out = response.getWriter(); out.println("<br><br><br>"); out.println(dollars + " Dollars equals to " + rupees + " Rupees"); out.println("<br>"); out.println(rupees + " Rupees equals to " + euros + " Euros"); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } public SOAPMessage createSOAPMessage(String operation, String arg) throws SOAPException { String urn = "http://jaxws.samples.geronimo.apache.org"; MessageFactory messageFactory; SOAPMessage message = null; messageFactory = MessageFactory.newInstance(); message = messageFactory.createMessage(); SOAPPart soapPart = message.getSOAPPart(); SOAPEnvelope envelope = soapPart.getEnvelope(); SOAPBody body = envelope.getBody(); SOAPElement bodyElement = body.addChildElement(envelope.createName( operation, "ns1", "urn:" + urn)); bodyElement.addChildElement("arg0").addTextNode(arg); message.saveChanges(); return message; } public String returnResult(SOAPMessage message) throws SOAPException { String destination = "http://localhost:8080/jaxws-converter/converter"; SOAPConnectionFactory soapConnFactory = SOAPConnectionFactory .newInstance(); SOAPConnection connection = soapConnFactory.createConnection(); SOAPMessage reply = connection.call(message, destination); SOAPPart soapPart = reply.getSOAPPart(); SOAPEnvelope envelope = soapPart.getEnvelope(); SOAPBody body = envelope.getBody(); Iterator iter = body.getChildElements(); Node resultOuter = ((Node) iter.next()).getFirstChild(); Node result = resultOuter.getFirstChild(); connection.close(); return result.getNodeValue(); } } |
- Let us have a brief look at the code that we added in ConverterHandler.java
- createSOAPMessage() - Here we will create a new SOAP message from MessageFactory instance and set the SOAP body of message according to the request and format needed by WSDL file.
SOAP request message that is returned by createSOAPMessage for operation ("dollarToRupees") and argument ("23") looks like this:Code Block title SOAP Request Message <?xml version="1.0" encoding="UTF-8"?> <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> <soapenv:Body> <ns1:dollarToRupees xmlns:ns1="urn:http://jaxws.samples.geronimo.apache.org"> <arg0>23</arg0> </ns1:dollarToRupees> </soapenv:Body> </soapenv:Envelope>
- returnResult() - This function processes the SOAP response message sent by the Web service and returns the result. This function uses call method over a SOAP Connection to send the request and receive the response.
SOAP response message that is returned by Web Service for the above SOAP Request message looks like this:Code Block title SOAP Response Message <?xml version="1.0" encoding="UTF-8"?> <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> <soapenv:Header/> <soapenv:Body> <dlwmin:dollarToRupeesResponse xmlns:dlwmin="http://jaxws.samples.geronimo.apache.org"> <axis2ns1:return>933.34</axis2ns1:return> </dlwmin:dollarToRupeesResponse> </soapenv:Body> </soapenv:Envelope>
- Here the SOAP Response is parsed by using the functions present in SAAJ API. Also observe that call is a blocking call which means that it will continue waiting until it receives a response.
- createSOAPMessage() - Here we will create a new SOAP message from MessageFactory instance and set the SOAP body of message according to the request and format needed by WSDL file.
This concludes the development section of our web based client.
...