View Javadoc

1   /*
2    * $Id: ValidateResponse.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.test.usecases.routing.lookup;
12  
13  import org.mule.api.transformer.TransformerException;
14  import org.mule.config.i18n.MessageFactory;
15  import org.mule.transformer.AbstractTransformer;
16  import org.mule.util.IOUtils;
17  
18  import java.io.InputStream;
19  
20  /**
21   * Throws an exception if the message does not contain "success".  In the real world, we might use XPath to 
22   * extract a particular tag or error code based on the expected response message format.
23   */
24  public class ValidateResponse extends AbstractTransformer
25  {
26      @Override
27      protected Object doTransform(Object src, String encoding) throws TransformerException
28      {
29          String response = null;
30          if (src instanceof InputStream)
31          {
32              response = IOUtils.toString((InputStream) src);
33          }
34          else if (src instanceof String)
35          {
36              response = (String) src;
37          }
38          
39          if (response != null && response.contains("<ErrorStatus>Success</ErrorStatus>"))
40          {
41              return response;
42          }
43          else
44          {
45              throw new TransformerException(MessageFactory.createStaticMessage("Invalid response from service: " + response));
46          }
47      }
48  }
49  
50