KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > demo > mortgage > gui > Axis2MortgageClient


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2005 EBM Websourcing, http://www.ebmwebsourcing.com/
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * -------------------------------------------------------------------------
19  * $Id:MortgageClient.java 10:42:15 ddesjardins $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.demo.mortgage.gui;
23
24 import java.io.ByteArrayInputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.logging.Level JavaDoc;
28 import java.util.logging.Logger JavaDoc;
29
30 import javax.xml.parsers.DocumentBuilder JavaDoc;
31 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
32 import javax.xml.parsers.ParserConfigurationException JavaDoc;
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 JavaDoc;
48 import org.w3c.dom.Node JavaDoc;
49 import org.xml.sax.SAXException JavaDoc;
50
51 /**
52  * This class is the client of the mortgage demonstration TODO : it's just a
53  * test for the moment
54  *
55  * @author ofabre - eBMWebsourcing
56  */

57 public class Axis2MortgageClient implements MortgageClient {
58
59     private Console console;
60
61     protected Logger JavaDoc 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 JavaDoc operation = "orchestrate";
67
68     public void submit(String JavaDoc firstName, String JavaDoc lastName, String JavaDoc address,
69             String JavaDoc salary, String JavaDoc propertyTaxes, String JavaDoc insurrance,
70             String JavaDoc autoPayment, String JavaDoc creditCards, String JavaDoc otherPayments) {
71         logger.log(Level.INFO, "");
72         try {
73
74             // uncomment to use axis generated stub (known bug : Multiple
75
// headers encountered!)
76
/*
77              * Create Port java binding
78              */

79             // MortgageProfilingStub port = new MortgageProfilingStub(
80
// "http://localhost:8083/axis2/services/MortgageProfilingService");
81
/*
82              * Create mortgage request object
83              */

84             // MortgageRequest mortgageRequest =
85
// createMortgageRequest(firstName,
86
// lastName, address, salary, propertyTaxes, insurrance,
87
// autoPayment, creditCards, otherPayments);
88
//
89
// MortgageProfilingStub.Orchestrate orchestrate = new
90
// MortgageProfilingStub.Orchestrate();
91
// orchestrate.setParam0(mortgageRequest);
92
/*
93              * send the request
94              */

95             // MortgageProfilingStub.OrchestrateResponse response = port
96
// .orchestrate(orchestrate);
97
/*
98              * Create the string response and print it on the console
99              */

100             // String stringResponse =
101
// createStringResponse(response.get_return());
102
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 JavaDoc stringResponse = createStringResponse(result);
114
115             console.setResponse(stringResponse);
116
117         } catch (Exception JavaDoc e) {
118             logger.log(Level.SEVERE, e.getMessage());
119         }
120     }
121
122     private String JavaDoc createStringResponse(OMElement result)
123             throws HandlingException {
124
125         String JavaDoc firstName = "";
126         String JavaDoc lastName = "";
127         String JavaDoc rateString = "";
128
129         Document JavaDoc doc = parseXml(result.toString());
130         Node JavaDoc responseValues = doc.getDocumentElement().getFirstChild();
131
132         List JavaDoc<Node JavaDoc> values = XMLUtil.getNodeChildren(responseValues);
133         for (Node JavaDoc 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 JavaDoc stringResponse = "";
144         if (new Float JavaDoc(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 JavaDoc parseXml(final String JavaDoc xmlString) throws HandlingException {
158
159         DocumentBuilderFactory JavaDoc docBuilderFactory = DocumentBuilderFactory
160                 .newInstance();
161         docBuilderFactory.setNamespaceAware(true);
162
163         DocumentBuilder JavaDoc docBuilder = null;
164
165         try {
166             docBuilder = docBuilderFactory.newDocumentBuilder();
167         } catch (ParserConfigurationException JavaDoc pce) {
168             throw new HandlingException("Bad XML parser configuration", pce);
169         }
170
171         Document JavaDoc document = null;
172
173         try {
174             document = docBuilder.parse(new ByteArrayInputStream JavaDoc(xmlString
175                     .getBytes()));
176         } catch (SAXException JavaDoc saxe) {
177             throw new HandlingException(
178                     "JBI descriptor is not well formed XML", saxe);
179         } catch (IOException JavaDoc ioe) {
180             throw new HandlingException("Can't read JBI descriptor", ioe);
181         }
182         return document;
183     }
184
185     /**
186      * Create the orchestrate OM element
187      *
188      * @param _firstName
189      * The first name of the customer
190      * @param _lastName
191      * The last name of the customer
192      * @param _address
193      * The address of the customer
194      * @param _salary
195      * The salary of the customer
196      * @param _propertyTaxes
197      * Property Taxes of the customer
198      * @param _insurrance
199      * Amount of insurrance of the customer
200      * @param _autoPayment
201      * All the auto payment of the customer
202      * @param _creditCards
203      * Amount of the credit cards of the customer
204      * @param _otherPayments
205      * Other payments (credits, subscriptions...) of the customer
206      * @return
207      */

208     private OMElement createPayload(String JavaDoc _firstName, String JavaDoc _lastName,
209             String JavaDoc _address, String JavaDoc _salary, String JavaDoc _propertyTaxes,
210             String JavaDoc _insurrance, String JavaDoc _autoPayment, String JavaDoc _creditCards,
211             String JavaDoc _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     /**
260      * Get the service name from the target EPR
261      *
262      * @return
263      */

264     private static String JavaDoc getServiceName() {
265         String JavaDoc eprName = targetEPR.getAddress();
266         return eprName
267                 .substring(eprName.lastIndexOf("/") + 1, eprName.length());
268     }
269
270     /**
271      * Create the Axis2 options
272      *
273      * @return
274      */

275     protected static Options createOptions() {
276
277         Options options = new Options();
278
279         // set destination address
280
options.setTo(targetEPR);
281
282         // set operation
283
options.setAction(operation);
284
285         options.setSoapVersionURI(SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI);
286         options.setTransportInProtocol(Constants.TRANSPORT_HTTP);
287
288         // FIXME
289
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     // /**
303
// * Create a MortgageRequest object setted with the customer information.
304
// *
305
// * @param firstName
306
// * The first name of the customer
307
// * @param lastName
308
// * The last name of the customer
309
// * @param address
310
// * The address of the customer
311
// * @param salary
312
// * The salary of the customer
313
// * @param propertyTaxes
314
// * Property Taxes of the customer
315
// * @param insurrance
316
// * Amount of insurrance of the customer
317
// * @param autoPayment
318
// * All the auto payment of the customer
319
// * @param creditCards
320
// * Amount of the credit cards of the customer
321
// * @param otherPayments
322
// * Other payments (credits, subscriptions...) of the customer
323
// * @return A <code>MortgageRequest</code> object. Must not be null.
324
// */
325
// protected MortgageRequest createMortgageRequest(String firstName,
326
// String lastName, String address, String salary,
327
// String propertyTaxes, String insurrance, String autoPayment,
328
// String creditCards, String otherPayments) {
329
// MortgageRequest mortgageRequest = new MortgageRequest();
330
// mortgageRequest.setAddress(address);
331
// mortgageRequest.setAutoPayment(new Float(autoPayment).floatValue());
332
// mortgageRequest.setCreditCards(new Float(creditCards).floatValue());
333
// mortgageRequest.setFirstName(firstName);
334
// mortgageRequest.setInsurance(new Float(insurrance).floatValue());
335
// mortgageRequest.setLastName(lastName);
336
// mortgageRequest.setOtherPayments(new Float(otherPayments).floatValue());
337
// mortgageRequest.setPropertyTaxes(new Float(propertyTaxes).floatValue());
338
// mortgageRequest.setSalary(new Float(salary).floatValue());
339
// return mortgageRequest;
340
// }
341

342     // /**
343
// * Create a response message that have to be print into the response text
344
// * box of the Console. This response is based on customer firstname and
345
// * lastname and mortgage rate included in the MortgageResponse.
346
// *
347
// * @param response
348
// * The <code>MortgageResponse</code> bo
349
// * @return a <code>String</code> response message
350
// */
351
// protected String createStringResponse(MortgageResponse response) {
352
// String stringResponse = "ERROR";
353
//
354
// String firstName = response.getFirstName();
355
//
356
// String lastName = response.getLastName();
357
//
358
// float rate = response.getMortgageRate();
359
//
360
// if (rate < 0) {
361
// stringResponse = "Mortgage refused for the customer " + lastName
362
// + " " + firstName;
363
// } else {
364
// stringResponse = "Mortgage accepted for the customer " + lastName
365
// + " " + firstName + " with the following rate : " + rate;
366
// }
367
//
368
// return stringResponse;
369
// }
370

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