%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.commons.jelly.tags.soap.InvokeTag |
|
|
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: 1.6 $ |
|
37 | */ |
|
38 | public class InvokeTag extends TagSupport { |
|
39 | ||
40 | private String var; |
|
41 | 0 | private String endpoint = null; |
42 | 0 | private String namespace = null; |
43 | 0 | private String method = null; |
44 | private String username; |
|
45 | private String password; |
|
46 | private Service service; |
|
47 | private Object params; |
|
48 | ||
49 | 0 | public InvokeTag() { |
50 | 0 | } |
51 | ||
52 | // Tag interface |
|
53 | //------------------------------------------------------------------------- |
|
54 | public void doTag(XMLOutput output) throws MissingAttributeException, JellyTagException { |
|
55 | 0 | if (endpoint == null) { |
56 | 0 | throw new MissingAttributeException("endpoint"); |
57 | } |
|
58 | 0 | if (namespace == null) { |
59 | 0 | throw new MissingAttributeException("namespace"); |
60 | } |
|
61 | 0 | if (method == null) { |
62 | 0 | throw new MissingAttributeException("method"); |
63 | } |
|
64 | ||
65 | 0 | Object[] params = getParamArray(); |
66 | 0 | if (params == null) { |
67 | 0 | params = new Object[]{ getBodyText() }; |
68 | } else { |
|
69 | // invoke body just in case we have nested tags |
|
70 | 0 | invokeBody(output); |
71 | } |
|
72 | ||
73 | 0 | Service service = getService(); |
74 | 0 | if (service == null) { |
75 | 0 | service = createService(); |
76 | } |
|
77 | ||
78 | 0 | Object answer = null; |
79 | try { |
|
80 | 0 | Call call = (Call) service.createCall(); |
81 | ||
82 | // @todo Jelly should have native support for URL and QName |
|
83 | // directly on properties |
|
84 | 0 | call.setTargetEndpointAddress(new java.net.URL(endpoint)); |
85 | 0 | call.setOperationName(new QName(namespace, method)); |
86 | ||
87 | 0 | if ( username != null && !username.equals("") ) { |
88 | 0 | call.setUsername( username ); |
89 | 0 | call.setPassword( password ); |
90 | } |
|
91 | ||
92 | 0 | answer = call.invoke(params); |
93 | 0 | } catch (MalformedURLException e) { |
94 | 0 | throw new JellyTagException(e); |
95 | } catch (ServiceException e) { |
|
96 | 0 | throw new JellyTagException(e); |
97 | } catch (RemoteException e) { |
|
98 | 0 | throw new JellyTagException(e); |
99 | } |
|
100 | ||
101 | 0 | if (var != null) { |
102 | 0 | context.setVariable(var, answer); |
103 | } else { |
|
104 | // should turn the answer into XML events... |
|
105 | 0 | throw new JellyTagException( "Not implemented yet; should stream results as XML events. Results: " + answer ); |
106 | } |
|
107 | 0 | } |
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 | 0 | this.endpoint = endpoint; |
117 | 0 | } |
118 | ||
119 | /** |
|
120 | * Sets the namespace of the operation |
|
121 | */ |
|
122 | public void setNamespace(String namespace) { |
|
123 | 0 | this.namespace = namespace; |
124 | 0 | } |
125 | ||
126 | public void setMethod(String method) { |
|
127 | 0 | this.method = method; |
128 | 0 | } |
129 | ||
130 | /** |
|
131 | * Returns the service to be used by this web service invocation. |
|
132 | * @return Service |
|
133 | */ |
|
134 | public Service getService() { |
|
135 | 0 | 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 | 0 | this.service = service; |
144 | 0 | } |
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 | 0 | this.var = class="keyword">var; |
151 | 0 | } |
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 | 0 | this.params = params; |
159 | 0 | } |
160 | ||
161 | /** |
|
162 | * Set the password for the SOAP call. |
|
163 | */ |
|
164 | public void setPassword(String password) |
|
165 | { |
|
166 | 0 | this.password = password; |
167 | 0 | } |
168 | ||
169 | /** |
|
170 | * Set the username for the SOAP call. |
|
171 | */ |
|
172 | public void setUsername(String username) |
|
173 | { |
|
174 | 0 | this.username = username; |
175 | 0 | } |
176 | ||
177 | ||
178 | // Implementation methods |
|
179 | //------------------------------------------------------------------------- |
|
180 | ||
181 | /** |
|
182 | * Factory method to create a new default Service instance |
|
183 | */ |
|
184 | protected Service createService() { |
|
185 | 0 | 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 | 0 | if (params == null) { |
194 | 0 | return null; |
195 | } |
|
196 | 0 | if (params instanceof Object[]) { |
197 | 0 | return (Object[]) params; |
198 | } |
|
199 | 0 | if (params instanceof Collection) { |
200 | 0 | Collection coll = (Collection) params; |
201 | 0 | return coll.toArray(); |
202 | } |
|
203 | // lets just wrap the current object inside an array |
|
204 | 0 | return new Object[] { params }; |
205 | } |
|
206 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |