Inline ... On Tuesday, December 03, 2013 5:24 PM jadelomeiri <[email protected]> wrote: > Hello, > > This is the solution I finally got. I hope it will be helpful for people > having the same problem as I had. > > 1) In my events java class I wrote the following code (which is an event > calling the method that deals with SOAP): > > * > public static String callMySoap(HttpServletRequest > request,HttpServletResponse response) > { > request.setAttribute("_EVENT_MESSAGE_", "You received this SOAP > response:" + MySoapClient()); > return "success"; > } > > > > public static SOAPMessage MySoapClient() > { > try { > // Create SOAP Connection > SOAPConnectionFactory soapConnectionFactory = > SOAPConnectionFactory.newInstance(); > SOAPConnection soapConnection = > soapConnectionFactory.createConnection(); > > // Send SOAP Message to SOAP Server > String url = "http://www.webservicex.net/geoipservice.asmx"; > SOAPMessage soapResponse = > soapConnection.call(createSOAPRequest(), url); > > soapConnection.close(); > return soapResponse; > } catch (Exception e) { > System.err.println("Error occurred while sending SOAP Request to > Server"); > e.printStackTrace(); > return null; > } > > } > > private static SOAPMessage createSOAPRequest() throws Exception { > MessageFactory messageFactory = MessageFactory.newInstance(); > SOAPMessage soapMessage = messageFactory.createMessage(); > SOAPPart soapPart = soapMessage.getSOAPPart(); > > String serverURI = "http://www.webservicex.net/"; > > // SOAP Envelope > SOAPEnvelope envelope = soapPart.getEnvelope(); > envelope.addNamespaceDeclaration("web", serverURI); > > /* > Constructed SOAP Request Message: > <SOAP-ENV:Envelope > xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" > xmlns:example="http://www.webservicex.net/"> > <SOAP-ENV:Header/> > <SOAP-ENV:Body> > <web:GetGeoIP> > <web:IPAddressl>192.168.1.2</web:IPAddress> > </web:GetGeoIP> > </SOAP-ENV:Body> > </SOAP-ENV:Envelope> > */ > > // SOAP Body > SOAPBody soapBody = envelope.getBody(); > SOAPElement soapBodyElem = soapBody.addChildElement("GetGeoIP", > "web"); > SOAPElement soapBodyElem1 = > soapBodyElem.addChildElement("IPAddress", "web"); > soapBodyElem1.addTextNode("192.168.1.2"); > > > MimeHeaders headers = soapMessage.getMimeHeaders(); > headers.addHeader("SOAPAction", serverURI + "GetGeoIP"); > > soapMessage.saveChanges(); > > /* Print the request message */ > System.out.print("Request SOAP Message = "); > soapMessage.writeTo(System.out); > System.out.println(); > > return soapMessage; > } > * > > 2) I added the following request-map to point to my event: > > * > <request-map uri="callMySoap"> > <event type="java" path="org.ofbiz.testapp.testapp.LearningEvents" > invoke="callMySoap"/> > <response name="success" type="view" value="FeedbackMessages"/> > </request-map> > * > > 3) I added the following view-map: > > * > <view-map name="FeedbackMessages" type="screen" > page="component://testapp/widget/TestAppScreens.xml#FeedbackMessages"/> > * > > which points to the following screen widget: > * > <screen name="FeedbackMessages"> > <section> > <widgets> > <decorator-screen name="main-decorator" > location="${parameters.mainDecoratorLocation}"> > <decorator-section name="title"> > <label text="Exploring all placeholders for Feedback"/> > </decorator-section> > <decorator-section name="body"> > > </decorator-section> > </decorator-screen> > </widgets> > </section> > </screen> > * > > > Note: > step 3 is not necessary. I only used it to see something on my screen. > > > Result: > I can now see in real time the SOAP message that is being sent & the > response that is received. > and I can also this screen: > <http://ofbiz.135035.n4.nabble.com/file/n4646047/result.png> > > > Now: > the only thing I still need to do is some parsing on the SOAP response > message to be able to deal with the variables that interest me in that > message. I also need to enhance my code to make it more "generic" so that it > becomes somewhat dynamic and works with not only that specific Web Service > that I had to hardcode. > > Do you have any suggestions for that? I do not want to be reinventing the > wheel!
I did not look into the details and did not use SOAPConnection yet. But yes most of the time you need to handle yourself the SOAP response. Another solution could be to use an ESB (ServiceMix for instance) to do the transformations of the SOAP envelopes. Disclaimer: I did not go that way yet.. Maybe it will be easier than trying to generalise by yourself. This said having a generalisation embedded in OFBiz would be great! Jacques
