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.test.usecases.routing.lookup;
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".  In the real world, we might use XPath to 
18   * extract a particular tag or error code based on the expected response message format.
19   */
20  public class ValidateResponse extends AbstractTransformer
21  {
22      @Override
23      protected Object doTransform(Object src, String encoding) throws TransformerException
24      {
25          String response = null;
26          if (src instanceof InputStream)
27          {
28              response = IOUtils.toString((InputStream) src);
29          }
30          else if (src instanceof String)
31          {
32              response = (String) src;
33          }
34          
35          if (response != null && response.contains("<ErrorStatus>Success</ErrorStatus>"))
36          {
37              return response;
38          }
39          else
40          {
41              throw new TransformerException(MessageFactory.createStaticMessage("Invalid response from service: " + response));
42          }
43      }
44  }
45  
46