KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > samples > loanbroker > esb > transformers > RestRequestToCustomerRequest


1 /*
2  * $Id: RestRequestToCustomerRequest.java 3798 2006-11-04 04:07:14Z aperepel $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.samples.loanbroker.esb.transformers;
12
13 import org.mule.samples.loanbroker.esb.message.Customer;
14 import org.mule.samples.loanbroker.esb.message.CustomerQuoteRequest;
15 import org.mule.transformers.AbstractEventAwareTransformer;
16 import org.mule.umo.UMOEventContext;
17 import org.mule.umo.transformer.TransformerException;
18
19 /**
20  * Converts parameters on the message into a CustomerQuoteRequest object
21  *
22  * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
23  * @version $Revision: 3798 $
24  */

25 public class RestRequestToCustomerRequest extends AbstractEventAwareTransformer
26 {
27     /**
28      * Serial version
29      */

30     private static final long serialVersionUID = -5017931788994993161L;
31
32     public RestRequestToCustomerRequest()
33     {
34         setReturnClass(CustomerQuoteRequest.class);
35     }
36
37     public Object JavaDoc transform(Object JavaDoc src, String JavaDoc encoding, UMOEventContext context) throws TransformerException
38     {
39
40         String JavaDoc name;
41         int ssn;
42         double amount;
43         int duration;
44         try
45         {
46             name = getParam(context, "customerName");
47             ssn = Integer.parseInt(getParam(context, "ssn"));
48             amount = Double.parseDouble(getParam(context, "loanAmount"));
49             duration = Integer.parseInt(getParam(context, "loanDuration"));
50         }
51         catch (Exception JavaDoc e)
52         {
53             throw new TransformerException(this, e);
54         }
55
56         Customer c = new Customer(name, ssn);
57         CustomerQuoteRequest request = new CustomerQuoteRequest(c, amount, duration);
58         return request;
59     }
60
61     protected String JavaDoc getParam(UMOEventContext context, String JavaDoc name) throws NullPointerException JavaDoc
62     {
63         String JavaDoc value = context.getMessage().getStringProperty(name, null);
64         if (value == null)
65         {
66             throw new NullPointerException JavaDoc("Parameter '" + name + "' must be set on the request");
67         }
68         return value;
69     }
70 }
71
Popular Tags