View Javadoc

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