View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.example.bookstore.transformers;
8   
9   import org.mule.api.transformer.TransformerException;
10  import org.mule.example.bookstore.web.HtmlTemplate;
11  import org.mule.transformer.AbstractTransformer;
12  import org.mule.transformer.types.DataTypeFactory;
13  
14  /**
15   * A call to addBook() returns a Long representing the number of
16   * books in the catalog.  This transformer wraps the Long into
17   * a nice HTML page.
18   */
19  public class AddBookResponse extends AbstractTransformer
20  {
21      public AddBookResponse()
22      {
23          super();
24          registerSourceType(DataTypeFactory.create(Long.class));
25          setReturnDataType(DataTypeFactory.STRING);
26      }
27  
28      @Override
29      protected Object doTransform(Object src, String outputEncoding) throws TransformerException
30      {
31          Long numBooks = (Long) src;
32          String response = "Catalog now contains: " + numBooks + " book(s)";
33          return HtmlTemplate.wrapHtmlBody(response);
34      }
35  }