View Javadoc

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