1 22 package org.objectweb.petals.demo.mortgage.gui; 23 24 import java.io.ByteArrayInputStream ; 25 import java.io.IOException ; 26 import java.util.List ; 27 import java.util.logging.Level ; 28 import java.util.logging.Logger ; 29 30 import javax.xml.parsers.DocumentBuilder ; 31 import javax.xml.parsers.DocumentBuilderFactory ; 32 import javax.xml.parsers.ParserConfigurationException ; 33 34 import org.apache.axiom.om.OMAbstractFactory; 35 import org.apache.axiom.om.OMElement; 36 import org.apache.axiom.om.OMFactory; 37 import org.apache.axiom.om.OMNamespace; 38 import org.apache.axiom.soap.SOAP11Constants; 39 import org.apache.axis2.Constants; 40 import org.apache.axis2.addressing.AddressingConstants; 41 import org.apache.axis2.addressing.EndpointReference; 42 import org.apache.axis2.client.Options; 43 import org.apache.axis2.client.ServiceClient; 44 import org.objectweb.petals.component.common.HandlingException; 45 import org.objectweb.petals.demo.mortgage.gui.gui.Console; 46 import org.objectweb.petals.tools.jbicommon.util.XMLUtil; 47 import org.w3c.dom.Document ; 48 import org.w3c.dom.Node ; 49 import org.xml.sax.SAXException ; 50 51 57 public class Axis2MortgageClient implements MortgageClient { 58 59 private Console console; 60 61 protected Logger logger = Logger.getLogger(this.getClass().toString()); 62 63 private static EndpointReference targetEPR = new EndpointReference( 64 "http://localhost:8084/axis2/services/MortgageProfilingService"); 65 66 private static String operation = "orchestrate"; 67 68 public void submit(String firstName, String lastName, String address, 69 String salary, String propertyTaxes, String insurrance, 70 String autoPayment, String creditCards, String otherPayments) { 71 logger.log(Level.INFO, ""); 72 try { 73 74 79 84 95 100 OMElement payload = createPayload(firstName, lastName, address, 103 salary, propertyTaxes, insurrance, autoPayment, 104 creditCards, otherPayments); 105 106 Options options = createOptions(); 107 108 ServiceClient client = new ServiceClient(); 109 client.setOptions(options); 110 111 OMElement result = client.sendReceive(payload); 112 113 String stringResponse = createStringResponse(result); 114 115 console.setResponse(stringResponse); 116 117 } catch (Exception e) { 118 logger.log(Level.SEVERE, e.getMessage()); 119 } 120 } 121 122 private String createStringResponse(OMElement result) 123 throws HandlingException { 124 125 String firstName = ""; 126 String lastName = ""; 127 String rateString = ""; 128 129 Document doc = parseXml(result.toString()); 130 Node responseValues = doc.getDocumentElement().getFirstChild(); 131 132 List <Node > values = XMLUtil.getNodeChildren(responseValues); 133 for (Node node : values) { 134 if ("firstName".equalsIgnoreCase(node.getLocalName())) { 135 firstName = node.getTextContent(); 136 } else if ("lastName".equalsIgnoreCase(node.getLocalName())) { 137 lastName = node.getTextContent(); 138 } else if ("mortgageRate".equalsIgnoreCase(node.getLocalName())) { 139 rateString = node.getTextContent(); 140 } 141 } 142 143 String stringResponse = ""; 144 if (new Float (rateString).floatValue() < 0) { 145 stringResponse = "Mortgage refused for the customer " + lastName 146 + " " + firstName; 147 } else { 148 stringResponse = "Mortgage accepted for the customer " + lastName 149 + " " + firstName + " with the following rate : " 150 + rateString; 151 } 152 153 return stringResponse; 154 155 } 156 157 private Document parseXml(final String xmlString) throws HandlingException { 158 159 DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory 160 .newInstance(); 161 docBuilderFactory.setNamespaceAware(true); 162 163 DocumentBuilder docBuilder = null; 164 165 try { 166 docBuilder = docBuilderFactory.newDocumentBuilder(); 167 } catch (ParserConfigurationException pce) { 168 throw new HandlingException("Bad XML parser configuration", pce); 169 } 170 171 Document document = null; 172 173 try { 174 document = docBuilder.parse(new ByteArrayInputStream (xmlString 175 .getBytes())); 176 } catch (SAXException saxe) { 177 throw new HandlingException( 178 "JBI descriptor is not well formed XML", saxe); 179 } catch (IOException ioe) { 180 throw new HandlingException("Can't read JBI descriptor", ioe); 181 } 182 return document; 183 } 184 185 208 private OMElement createPayload(String _firstName, String _lastName, 209 String _address, String _salary, String _propertyTaxes, 210 String _insurrance, String _autoPayment, String _creditCards, 211 String _otherPayments) { 212 OMFactory fac = OMAbstractFactory.getOMFactory(); 213 OMNamespace omNs = fac.createOMNamespace( 214 "http://petals.objectweb.org/", getServiceName()); 215 OMElement method = fac.createOMElement(operation, omNs); 216 OMElement value = fac.createOMElement("param0", omNs); 217 218 OMElement address = fac.createOMElement("address", omNs); 219 address.addChild(fac.createOMText(value, _address)); 220 value.addChild(address); 221 222 OMElement autoPayment = fac.createOMElement("autoPayment", omNs); 223 autoPayment.addChild(fac.createOMText(value, _autoPayment)); 224 value.addChild(autoPayment); 225 226 OMElement creditCards = fac.createOMElement("creditCards", omNs); 227 creditCards.addChild(fac.createOMText(value, _creditCards)); 228 value.addChild(creditCards); 229 230 OMElement firstName = fac.createOMElement("firstName", omNs); 231 firstName.addChild(fac.createOMText(value, _firstName)); 232 value.addChild(firstName); 233 234 OMElement insurance = fac.createOMElement("insurance", omNs); 235 insurance.addChild(fac.createOMText(value, _insurrance)); 236 value.addChild(insurance); 237 238 OMElement otherPayments = fac.createOMElement("otherPayments", omNs); 239 otherPayments.addChild(fac.createOMText(value, _otherPayments)); 240 value.addChild(otherPayments); 241 242 OMElement propertyTaxes = fac.createOMElement("propertyTaxes", omNs); 243 propertyTaxes.addChild(fac.createOMText(value, _propertyTaxes)); 244 value.addChild(propertyTaxes); 245 246 OMElement salary = fac.createOMElement("salary", omNs); 247 salary.addChild(fac.createOMText(value, _salary)); 248 value.addChild(salary); 249 250 OMElement lastName = fac.createOMElement("lastName", omNs); 251 lastName.addChild(fac.createOMText(value, _lastName)); 252 value.addChild(lastName); 253 254 method.addChild(value); 255 256 return method; 257 } 258 259 264 private static String getServiceName() { 265 String eprName = targetEPR.getAddress(); 266 return eprName 267 .substring(eprName.lastIndexOf("/") + 1, eprName.length()); 268 } 269 270 275 protected static Options createOptions() { 276 277 Options options = new Options(); 278 279 options.setTo(targetEPR); 281 282 options.setAction(operation); 284 285 options.setSoapVersionURI(SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI); 286 options.setTransportInProtocol(Constants.TRANSPORT_HTTP); 287 288 options 290 .setProperty( 291 org.apache.axis2.Constants.Configuration.CHARACTER_SET_ENCODING, 292 "UTF-8"); 293 options.setProperty( 294 org.apache.axis2.transport.http.HTTPConstants.CHUNKED, 295 org.apache.axis2.Constants.VALUE_FALSE); 296 options.setProperty( 297 AddressingConstants.DISABLE_ADDRESSING_FOR_OUT_MESSAGES, 298 Boolean.TRUE); 299 return options; 300 } 301 302 342 371 public Console getConsole() { 372 return console; 373 } 374 375 public void setConsole(Console console) { 376 this.console = console; 377 } 378 379 } 380 | Popular Tags |