View Javadoc

1   /*
2    * Copyright 2002,2004 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.commons.jelly.tags.soap;
17  
18  import java.net.MalformedURLException;
19  import java.rmi.RemoteException;
20  import java.util.Collection;
21  
22  import javax.xml.namespace.QName;
23  import javax.xml.rpc.ServiceException;
24  
25  import org.apache.axis.client.Call;
26  import org.apache.axis.client.Service;
27  import org.apache.commons.jelly.JellyTagException;
28  import org.apache.commons.jelly.MissingAttributeException;
29  import org.apache.commons.jelly.TagSupport;
30  import org.apache.commons.jelly.XMLOutput;
31  
32  /***
33   * Invokes a web service
34   *
35   * @author <a href="mailto:jim@bnainc.net">James Birchfield</a>
36   * @version $Revision: 155420 $
37   */
38  public class InvokeTag extends TagSupport {
39  
40      private String var;
41      private String endpoint = null;
42      private String namespace = null;
43      private String method = null;
44      private String username;
45      private String password;
46      private Service service;
47      private Object params;
48  
49      public InvokeTag() {
50      }
51  
52      // Tag interface
53      //-------------------------------------------------------------------------
54      public void doTag(XMLOutput output) throws MissingAttributeException, JellyTagException {
55          if (endpoint == null) {
56              throw new MissingAttributeException("endpoint");
57          }
58          if (namespace == null) {
59              throw new MissingAttributeException("namespace");
60          }
61          if (method == null) {
62              throw new MissingAttributeException("method");
63          }
64  
65          Object[] params = getParamArray();
66          if (params == null) {
67              params = new Object[]{ getBodyText() };
68          } else {
69              // invoke body just in case we have nested tags
70              invokeBody(output);
71          }
72  
73          Service service = getService();
74          if (service == null) {
75              service = createService();
76          }
77  
78          Object answer = null;
79          try {
80              Call call = (Call) service.createCall();
81  
82              // @todo Jelly should have native support for URL and QName
83              // directly on properties
84              call.setTargetEndpointAddress(new java.net.URL(endpoint));
85              call.setOperationName(new QName(namespace, method));
86  
87              if ( username != null && !username.equals("") ) {
88                  call.setUsername( username );
89                  call.setPassword( password );
90              }
91              
92              answer = call.invoke(params);
93          } catch (MalformedURLException e) {
94              throw new JellyTagException(e);
95          } catch (ServiceException e) {
96              throw new JellyTagException(e);
97          } catch (RemoteException e) {
98              throw new JellyTagException(e);
99          }
100 
101         if (var != null) {
102             context.setVariable(var, answer);
103         } else {
104             // should turn the answer into XML events...
105             throw new JellyTagException( "Not implemented yet; should stream results as XML events. Results: " + answer );
106         }
107     }
108 
109 
110     // Properties
111     //-------------------------------------------------------------------------
112     /***
113      * Sets the end point to which the invocation will occur
114      */
115     public void setEndpoint(String endpoint) {
116         this.endpoint = endpoint;
117     }
118 
119     /***
120      * Sets the namespace of the operation
121      */
122     public void setNamespace(String namespace) {
123         this.namespace = namespace;
124     }
125 
126     public void setMethod(String method) {
127         this.method = method;
128     }
129 
130     /***
131      * Returns the service to be used by this web service invocation.
132      * @return Service
133      */
134     public Service getService() {
135         return service;
136     }
137 
138     /***
139      * Sets the service to be used by this invocation.
140      * If none is specified then a default is used.
141      */
142     public void setService(Service service) {
143         this.service = service;
144     }
145 
146     /***
147      * Sets the name of the variable to output the results of the SOAP call to.
148      */
149     public void setVar(String var) {
150         this.var = var;
151     }
152 
153     /***
154      * Sets the parameters for this SOAP call. This can be an array or collection of
155      * SOAPBodyElements or types.
156      */
157     public void setParams(Object params) {
158         this.params = params;
159     }
160 
161     /***
162      * Set the password for the SOAP call.
163      */
164     public void setPassword(String password)
165     {
166         this.password = password;
167     }
168 
169     /***
170      * Set the username for the SOAP call.
171      */
172     public void setUsername(String username)
173     {
174         this.username = username;
175     }
176 
177 
178     // Implementation methods
179     //-------------------------------------------------------------------------
180 
181     /***
182      * Factory method to create a new default Service instance
183      */
184     protected Service createService() {
185         return new Service();
186     }
187 
188     /***
189      * Performs any type coercion on the given parameters to form an Object[]
190      * or returns null if no parameter has been specified
191      */
192     protected Object[] getParamArray() {
193         if (params == null) {
194             return null;
195         }
196         if (params instanceof Object[]) {
197             return (Object[]) params;
198         }
199         if (params instanceof Collection) {
200             Collection coll = (Collection) params;
201             return coll.toArray();
202         }
203         // lets just wrap the current object inside an array
204         return new Object[] { params };
205     }
206 }