1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.example.bookstore.transformers; |
8 | |
|
9 | |
import org.mule.api.MuleMessage; |
10 | |
import org.mule.api.transformer.TransformerException; |
11 | |
import org.mule.example.bookstore.Book; |
12 | |
import org.mule.example.bookstore.BookstoreAdminMessages; |
13 | |
import org.mule.transformer.AbstractMessageTransformer; |
14 | |
import org.mule.transformer.types.DataTypeFactory; |
15 | |
import org.mule.util.StringUtils; |
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
public class HttpRequestToBook extends AbstractMessageTransformer |
23 | |
{ |
24 | |
public HttpRequestToBook() |
25 | |
{ |
26 | 0 | super(); |
27 | 0 | registerSourceType(DataTypeFactory.OBJECT); |
28 | 0 | setReturnDataType(DataTypeFactory.create(Book.class)); |
29 | 0 | } |
30 | |
|
31 | |
@Override |
32 | |
public Object transformMessage(MuleMessage message, String outputEncoding) throws TransformerException |
33 | |
{ |
34 | 0 | String author = message.getInboundProperty("author"); |
35 | 0 | String title = message.getInboundProperty("title"); |
36 | 0 | String price = message.getInboundProperty("price"); |
37 | |
|
38 | 0 | if (StringUtils.isBlank(author)) |
39 | |
{ |
40 | 0 | throw new TransformerException(BookstoreAdminMessages.missingAuthor(), this); |
41 | |
} |
42 | 0 | if (StringUtils.isBlank(title)) |
43 | |
{ |
44 | 0 | throw new TransformerException(BookstoreAdminMessages.missingTitle(), this); |
45 | |
} |
46 | 0 | if (StringUtils.isBlank(price)) |
47 | |
{ |
48 | 0 | throw new TransformerException(BookstoreAdminMessages.missingPrice(), this); |
49 | |
} |
50 | |
|
51 | 0 | return new Book(author, title, Double.parseDouble(price)); |
52 | |
} |
53 | |
} |