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.transport.soap.axis;
8   
9   import org.mule.api.MuleMessage;
10  import org.mule.api.config.MuleProperties;
11  import org.mule.api.endpoint.EndpointURI;
12  import org.mule.api.endpoint.ImmutableEndpoint;
13  import org.mule.api.endpoint.InboundEndpoint;
14  import org.mule.endpoint.MuleEndpointURI;
15  import org.mule.transport.AbstractMessageRequester;
16  
17  import java.util.Iterator;
18  import java.util.Properties;
19  
20  import javax.xml.soap.SOAPEnvelope;
21  
22  import org.apache.axis.AxisProperties;
23  import org.apache.axis.EngineConfiguration;
24  import org.apache.axis.Message;
25  import org.apache.axis.client.Call;
26  import org.apache.axis.client.Service;
27  import org.apache.axis.configuration.FileProvider;
28  
29  /**
30   * <code>AxisMessageDispatcher</code> is used to make soap requests via the Axis
31   * soap client.
32   */
33  public class AxisMessageRequester extends AbstractMessageRequester
34  {
35  
36      protected EngineConfiguration clientConfig;
37      protected AxisConnector connector;
38      protected Service service;
39  
40      public AxisMessageRequester(InboundEndpoint endpoint)
41      {
42          super(endpoint);
43          this.connector = (AxisConnector)endpoint.getConnector();
44          AxisProperties.setProperty("axis.doAutoTypes", Boolean.toString(connector.isDoAutoTypes()));
45      }
46  
47      protected void doConnect() throws Exception
48      {
49          if (service == null)
50          {
51              service = createService(endpoint);
52          }
53      }
54  
55      protected void doDisconnect() throws Exception
56      {
57          if (service != null)
58          {
59              service = null;
60          }
61      }
62  
63      protected void doDispose()
64      {
65          // template method
66      }
67  
68      protected synchronized EngineConfiguration getClientConfig(ImmutableEndpoint endpoint)
69      {
70          if (clientConfig == null)
71          {
72              // Allow the client config to be set on the endpoint
73              String config;
74              config = (String)endpoint.getProperty(AxisConnector.AXIS_CLIENT_CONFIG_PROPERTY);
75  
76              if (config != null)
77              {
78                  clientConfig = new FileProvider(config);
79              }
80              else
81              {
82                  clientConfig = connector.getClientProvider();
83              }
84          }
85          return clientConfig;
86      }
87  
88      protected Service createService(ImmutableEndpoint endpoint) throws Exception
89      {
90          // Create a simple axis service without wsdl
91          EngineConfiguration config = getClientConfig(endpoint);
92          return new Service(config);
93      }
94  
95      /**
96       * Make a specific request to the underlying transport
97       *
98       * @param timeout the maximum time the operation should block before returning.
99       *            The call should return immediately if there is data available. If
100      *            no data becomes available before the timeout elapses, null will be
101      *            returned
102      * @return the result of the request wrapped in a MuleMessage object. Null will be
103      *         returned if no data was avaialable
104      * @throws Exception if the call to the underlying protocal cuases an exception
105      */
106     protected MuleMessage doRequest(long timeout) throws Exception
107     {
108         Call call = new Call(service);
109         String uri = endpoint.getEndpointURI().toString();
110         call.setSOAPActionURI(uri);
111         call.setTargetEndpointAddress(uri);
112 
113         Properties params = endpoint.getEndpointURI().getUserParams();
114         String method = (String)params.remove(MuleProperties.MULE_METHOD_PROPERTY);
115         call.setOperationName(method);
116 
117         String args[] = new String[params.size()];
118         int i = 0;
119         for (Iterator iterator = params.values().iterator(); iterator.hasNext(); i++)
120         {
121             args[i] = iterator.next().toString();
122         }
123 
124         call.setOperationName(method);
125         call.setProperty(MuleProperties.MULE_ENDPOINT_PROPERTY, endpoint);
126         call.setProperty(MuleProperties.MULE_CONTEXT_PROPERTY, connector.getMuleContext());
127 
128         Object result = call.invoke(method, args);
129         return AxisMessageDispatcher.createMessage(result, call, connector.getMuleContext());
130     }
131 
132     public MuleMessage request(String endpoint, Object[] args) throws Exception
133     {
134         Call call = new Call(service);
135 
136         call.setSOAPActionURI(endpoint);
137         call.setTargetEndpointAddress(endpoint);
138 
139         if (!endpoint.startsWith("axis:"))
140         {
141             endpoint = "axis:" + endpoint;
142         }
143         EndpointURI ep = new MuleEndpointURI(endpoint, connector.getMuleContext());
144         String method = (String)ep.getParams().remove(MuleProperties.MULE_METHOD_PROPERTY);
145         call.setOperationName(method);
146 
147         call.setOperationName(method);
148         Object result = call.invoke(method, args);
149         return AxisMessageDispatcher.createMessage(result, call, connector.getMuleContext());
150     }
151 
152     public MuleMessage request(String endpoint, SOAPEnvelope envelope) throws Exception
153     {
154         Call call = new Call(service);
155 
156         call.setSOAPActionURI(endpoint);
157         call.setTargetEndpointAddress(endpoint);
158         Object result = call.invoke(new Message(envelope));
159         return AxisMessageDispatcher.createMessage(result, call, connector.getMuleContext());
160     }
161 
162 }