View Javadoc

1   /*
2    * $Id: CxfMessageRequester.java 10961 2008-02-22 19:01:02Z dfeist $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.transport.cxf;
12  
13  import org.mule.DefaultMuleMessage;
14  import org.mule.api.MuleMessage;
15  import org.mule.api.config.MuleProperties;
16  import org.mule.api.endpoint.InboundEndpoint;
17  import org.mule.transport.AbstractMessageRequester;
18  
19  import java.util.Iterator;
20  import java.util.Properties;
21  
22  import org.apache.cxf.endpoint.ClientImpl;
23  
24  /**
25   * <code>AxisMessageDispatcher</code> is used to make soap requests via the Axis
26   * soap client.
27   */
28  public class CxfMessageRequester extends AbstractMessageRequester
29  {
30  
31      protected CxfConnector connector;
32      private ClientWrapper wrapper;
33      
34      public CxfMessageRequester(InboundEndpoint endpoint)
35      {
36          super(endpoint);
37          this.connector = (CxfConnector)endpoint.getConnector();
38      }
39  
40      protected void doConnect() throws Exception
41      {
42          wrapper = new ClientWrapper();
43          wrapper.setBus(connector.getCxfBus());
44          wrapper.setEndpoint(endpoint);
45          wrapper.initialize();
46      }
47  
48      protected void doDisconnect() throws Exception
49      {
50      }
51  
52      protected void doDispose()
53      {
54          // template method
55      }
56  
57  
58      /**
59       * Make a specific request to the underlying transport
60       * 
61       * @param timeout the maximum time the operation should block before returning.
62       *            The call should return immediately if there is data available. If
63       *            no data becomes available before the timeout elapses, null will be
64       *            returned
65       * @return the result of the request wrapped in a MuleMessage object. Null will be
66       *         returned if no data was avaialable
67       * @throws Exception if the call to the underlying protocal cuases an exception
68       */
69      protected MuleMessage doRequest(long timeout) throws Exception
70      {
71          ((ClientImpl)wrapper.getClient()).setSynchronousTimeout((int)timeout);
72  
73          String method = (String)endpoint.getProperty(MuleProperties.MULE_METHOD_PROPERTY);
74  
75          if (method == null) 
76          {
77              method = (String)endpoint.getProperty(CxfConstants.OPERATION);
78          }
79          
80          Properties params = endpoint.getEndpointURI().getUserParams();
81          Object args[] = new Object[params.size()];
82          int i = 0;
83          for (Iterator<Object> iterator = params.values().iterator(); iterator.hasNext(); i++)
84          {
85              args[i] = iterator.next().toString();
86          }
87  
88          Object[] response = wrapper.getClient().invoke(method, args);
89  
90          if (response != null && response.length == 1)
91          {
92              return new DefaultMuleMessage(response[0]);
93          }
94          else
95          {
96              return new DefaultMuleMessage(response);
97          }
98      }
99  
100     
101 }