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.tck.transformer;
8   
9   import org.mule.api.transformer.TransformerException;
10  import org.mule.config.i18n.MessageFactory;
11  import org.mule.transformer.AbstractTransformer;
12  import org.mule.util.IOUtils;
13  
14  import java.io.InputStream;
15  
16  /**
17   * Throws an exception if the message does not contain "success".
18   */
19  public class ValidateResponse extends AbstractTransformer
20  {
21      @Override
22      protected Object doTransform(Object src, String encoding) throws TransformerException
23      {
24          String response = null;
25          if (src instanceof String)
26          {
27              response = (String) src;
28          }
29          else if (src instanceof InputStream)
30          {
31              response = IOUtils.toString((InputStream) src);
32          }
33          
34          if (response != null && response.contains("success"))
35          {
36              return response;
37          }
38          else
39          {
40              throw new TransformerException(MessageFactory.createStaticMessage("Invalid response from service: " + response));
41          }
42      }
43  }
44  
45