1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
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 | |
|
21 | |
|
22 | |
public class RestRequestToCustomerRequest extends AbstractEventAwareTransformer |
23 | |
{ |
24 | |
|
25 | |
public RestRequestToCustomerRequest() |
26 | 0 | { |
27 | 0 | setReturnClass(CustomerQuoteRequest.class); |
28 | 0 | } |
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 | 0 | name = getParam(context, "customerName"); |
40 | 0 | ssn = Integer.parseInt(getParam(context, "ssn")); |
41 | 0 | amount = Double.parseDouble(getParam(context, "loanAmount")); |
42 | 0 | duration = Integer.parseInt(getParam(context, "loanDuration")); |
43 | |
} |
44 | 0 | catch (Exception e) |
45 | |
{ |
46 | 0 | throw new TransformerException(this, e); |
47 | 0 | } |
48 | |
|
49 | 0 | Customer c = new Customer(name, ssn); |
50 | 0 | CustomerQuoteRequest request = new CustomerQuoteRequest(c, amount, duration); |
51 | 0 | return request; |
52 | |
} |
53 | |
|
54 | |
protected String getParam(UMOEventContext context, String name) throws NullPointerException |
55 | |
{ |
56 | 0 | String value = context.getMessage().getStringProperty(name, null); |
57 | 0 | if (value == null) |
58 | |
{ |
59 | 0 | throw new IllegalArgumentException("Parameter '" + name + "' must be set on the request"); |
60 | |
} |
61 | 0 | return value; |
62 | |
} |
63 | |
} |