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.functional;
8   
9   import org.mule.api.MuleEventContext;
10  
11  /**
12   * A test callback that writes the results of a service invocation to the response output stream
13   * of the event
14   * This should only be used when testing Asynchronous calls with the {@link FunctionalTestComponent} otherwise
15   * you will get duplicate messages, since both this class and the {@link FunctionalTestComponent} will write
16   * a return message back to the callee.
17   *
18   * @see org.mule.tck.functional.FunctionalTestComponent
19   */
20  public class ResponseWriterCallback extends CounterCallback
21  {
22      @Override
23      public void eventReceived(MuleEventContext context, Object component) throws Exception
24      {
25          if (context.getExchangePattern().hasResponse())
26          {
27              throw new IllegalStateException("The ResponseWriterCallback should not be used for synchronous tests as it will cause two copies of the message to be written back to the client");
28          }
29          super.eventReceived(context, component);
30  
31          String result = context.getMessageAsString() + " Received Async";
32          if (context.getOutputStream() == null)
33          {
34              throw new IllegalArgumentException("event context does not have an OutputStream associated");
35          }
36  
37          context.getOutputStream().write(result.getBytes());
38          context.getOutputStream().flush();
39      } 
40  }