View Javadoc

1   /*
2    * $Id: RestRequestToCustomerRequest.java 7963 2007-08-21 08:53:15Z dirk.olmes $
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 CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.examples.loanbroker.transformers;
12  
13  import org.mule.examples.loanbroker.messages.Customer;
14  import org.mule.examples.loanbroker.messages.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  public class RestRequestToCustomerRequest extends AbstractEventAwareTransformer
23  {
24  
25      public RestRequestToCustomerRequest()
26      {
27          setReturnClass(CustomerQuoteRequest.class);
28      }
29  
30      public Object transform(Object src, String encoding, UMOEventContext context) throws TransformerException
31      {
32          String name;
33          int ssn;
34          double amount;
35          int duration;
36  
37          try
38          {
39              name = getParam(context, "customerName");
40              ssn = Integer.parseInt(getParam(context, "ssn"));
41              amount = Double.parseDouble(getParam(context, "loanAmount"));
42              duration = Integer.parseInt(getParam(context, "loanDuration"));
43          }
44          catch (Exception e)
45          {
46              throw new TransformerException(this, e);
47          }
48  
49          Customer c = new Customer(name, ssn);
50          CustomerQuoteRequest request = new CustomerQuoteRequest(c, amount, duration);
51          return request;
52      }
53  
54      protected String getParam(UMOEventContext context, String name) throws NullPointerException
55      {
56          String value = context.getMessage().getStringProperty(name, null);
57          if (value == null)
58          {
59              throw new IllegalArgumentException("Parameter '" + name + "' must be set on the request");
60          }
61          return value;
62      }
63  }