Source code: org/apache/axis/client/Stub.java
1 /*
2 * Copyright 2001-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
17 package org.apache.axis.client;
18
19 import org.apache.axis.AxisFault;
20 import org.apache.axis.message.SOAPHeaderElement;
21 import org.apache.axis.utils.Messages;
22
23 import javax.xml.namespace.QName;
24 import javax.xml.rpc.JAXRPCException;
25 import javax.xml.rpc.Service;
26 import javax.xml.rpc.ServiceException;
27 import java.net.MalformedURLException;
28 import java.net.URL;
29 import java.util.Iterator;
30 import java.util.Properties;
31 import java.util.Vector;
32
33 /**
34 * This class is the base for all generated stubs.
35 */
36
37 public abstract class Stub implements javax.xml.rpc.Stub {
38
39 protected Service service = null;
40
41 // If maintainSessionSet is true, then setMaintainSession
42 // was called and it set the value of maintainSession.
43 // Use that value when getting the new Call object.
44 // If maintainSession HAS NOT been set, then the
45 // Call object uses the default maintainSession
46 // from the Service.
47 protected boolean maintainSessionSet = false;
48 protected boolean maintainSession = false;
49
50 protected Properties cachedProperties = new Properties();
51 protected String cachedUsername = null;
52 protected String cachedPassword = null;
53 protected URL cachedEndpoint = null;
54 protected Integer cachedTimeout = null;
55 protected QName cachedPortName = null;
56
57 // Support for Header
58 private Vector headers = new Vector();
59
60 // Support for Attachments
61 private Vector attachments = new Vector();
62
63 // Flag to determine whether this is the first call to register type mappings.
64 // This need not be synchronized because firstCall is ONLY called from within
65 // a synchronized block in the generated stub code.
66 private boolean firstCall = true;
67
68 // The last call object
69 protected Call _call = null;
70
71 /**
72 * Is this the first time the type mappings are being registered?
73 */
74 protected boolean firstCall() {
75 boolean ret = firstCall;
76 firstCall = false;
77 return ret;
78 } // firstCall
79
80 /**
81 * Sets the value for a named property. JAX-RPC 1.0 specification
82 * specifies a standard set of properties that may be passed
83 * to the Stub._setProperty method. These properties include:
84 * <UL>
85 * <LI>javax.xml.rpc.security.auth.username: Username for the HTTP Basic Authentication
86 * <LI>javax.xml.rpc.security.auth.password: Password for the HTTP Basic Authentication
87 * <LI>javax.xml.rpc.service.endpoint.address: Target service endpoint address.
88 * <LI>[TBD: Additional properties]
89 * </UL>
90 *
91 * @param name - Name of the property
92 * @param value - Value of the property
93 */
94 public void _setProperty(String name, Object value) {
95 if (name == null || value == null) {
96 throw new JAXRPCException(
97 Messages.getMessage(name == null ?
98 "badProp03" : "badProp04"));
99 }
100 else if (name.equals(Call.USERNAME_PROPERTY)) {
101 if (!(value instanceof String)) {
102 throw new JAXRPCException(
103 Messages.getMessage("badProp00", new String[] {
104 name, "java.lang.String", value.getClass().getName()}));
105 }
106 cachedUsername = (String) value;
107 }
108 else if (name.equals(Call.PASSWORD_PROPERTY)) {
109 if (!(value instanceof String)) {
110 throw new JAXRPCException(
111 Messages.getMessage("badProp00", new String[] {
112 name, "java.lang.String", value.getClass().getName()}));
113 }
114 cachedPassword = (String) value;
115 }
116 else if (name.equals(Stub.ENDPOINT_ADDRESS_PROPERTY)) {
117 if (!(value instanceof String)) {
118 throw new JAXRPCException(
119 Messages.getMessage("badProp00", new String[] {
120 name, "java.lang.String", value.getClass().getName()}));
121 }
122 try {
123 cachedEndpoint = new URL ((String) value);
124 }
125 catch (MalformedURLException mue) {
126 throw new JAXRPCException(mue.getMessage());
127 }
128 }
129 else if (name.equals(Call.SESSION_MAINTAIN_PROPERTY)) {
130 if (!(value instanceof Boolean)) {
131 throw new JAXRPCException(
132 Messages.getMessage("badProp00", new String[]
133 {name,
134 "java.lang.Boolean",
135 value.getClass().getName()}));
136 }
137 maintainSessionSet = true;
138 maintainSession = ((Boolean) value).booleanValue();
139 }
140 else if (name.startsWith("java.") || name.startsWith("javax.")) {
141 throw new JAXRPCException(
142 Messages.getMessage("badProp05", name));
143 }
144 else {
145 cachedProperties.put(name, value);
146 }
147 } // _setProperty
148
149 /**
150 * Gets the value of a named property.
151 *
152 * @param name
153 *
154 * @return the value of a named property.
155 */
156 public Object _getProperty(String name) {
157 if (name == null) {
158 throw new JAXRPCException(
159 Messages.getMessage("badProp05", name));
160 }
161 else {
162 if (name.equals(Call.USERNAME_PROPERTY)) {
163 return cachedUsername;
164 }
165 else if (name.equals(Call.PASSWORD_PROPERTY)) {
166 return cachedPassword;
167 }
168 else if (name.equals(Stub.ENDPOINT_ADDRESS_PROPERTY)) {
169 return cachedEndpoint.toString();
170 }
171 else if (name.equals(Call.SESSION_MAINTAIN_PROPERTY)) {
172 return maintainSessionSet ? (maintainSession ? Boolean.TRUE : Boolean.FALSE) : null;
173 }
174 else if (name.startsWith("java.") || name.startsWith("javax.")) {
175 throw new JAXRPCException(
176 Messages.getMessage("badProp05", name));
177 }
178 else {
179 return cachedProperties.get(name);
180 }
181 }
182 } // _getProperty
183
184 /**
185 * Remove a property from this instance of the Stub
186 * NOTE: This is NOT part of JAX-RPC and is an Axis extension.
187 *
188 * @param name the name of the property to remove
189 * @return the value to which the key had been mapped, or null if the key did not have a mapping.
190 */
191 public Object removeProperty(String name) {
192 return cachedProperties.remove(name);
193 }
194 /**
195 * Return the names of configurable properties for this stub class.
196 */
197 public Iterator _getPropertyNames() {
198 return cachedProperties.keySet().iterator();
199 } // _getPropertyNames
200
201 /**
202 * Set the username.
203 */
204 public void setUsername(String username) {
205 cachedUsername = username;
206 } // setUsername
207
208 /**
209 * Get the user name
210 */
211 public String getUsername() {
212 return cachedUsername;
213 } // getUsername
214
215 /**
216 * Set the password.
217 */
218 public void setPassword(String password) {
219 cachedPassword = password;
220 } // setPassword
221
222 /**
223 * Get the password
224 */
225 public String getPassword() {
226 return cachedPassword;
227 } // getPassword
228
229 /**
230 * Get the timeout value in milliseconds. 0 means no timeout.
231 */
232 public int getTimeout() {
233 return cachedTimeout == null ? 0 : cachedTimeout.intValue();
234 } // getTimeout
235
236 /**
237 * Set the timeout in milliseconds.
238 */
239 public void setTimeout(int timeout) {
240 cachedTimeout = new Integer(timeout);
241 } // setTimeout
242
243 /**
244 * Get the port name.
245 */
246 public QName getPortName() {
247 return cachedPortName;
248 } // getPortName
249
250 /**
251 * Set the port QName.
252 */
253 public void setPortName(QName portName) {
254 cachedPortName = portName;
255 } // setPortName
256
257 /**
258 * Set the port name.
259 */
260 public void setPortName(String portName) {
261 setPortName(new QName(portName));
262 } // setPortName
263
264 /**
265 * If set to true, session is maintained; if false, it is not.
266 */
267 public void setMaintainSession(boolean session) {
268 maintainSessionSet = true;
269 maintainSession = session;
270 cachedProperties.put(Call.SESSION_MAINTAIN_PROPERTY, session ? Boolean.TRUE : Boolean.FALSE);
271 } // setmaintainSession
272
273
274 /**
275 * Set the header
276 * @param namespace
277 * @param partName that uniquely identify a header object.
278 * @param headerValue Object that is sent in the request as a SOAPHeader
279 */
280 public void setHeader(String namespace, String partName, Object headerValue) {
281 headers.add(new SOAPHeaderElement(namespace, partName, headerValue));
282 }
283
284 /**
285 * Set the header
286 */
287 public void setHeader(SOAPHeaderElement header) {
288 headers.add(header);
289 }
290
291 /**
292 * Extract attachments
293 * @param call
294 */
295 public void extractAttachments(Call call) {
296 attachments.clear();
297 if(call.getResponseMessage() != null) {
298 Iterator iterator = call.getResponseMessage().getAttachments();
299 while(iterator.hasNext()){
300 attachments.add(iterator.next());
301 }
302 }
303 }
304
305 /**
306 * Add an attachment
307 * @param handler
308 */
309 public void addAttachment(Object handler) {
310 attachments.add(handler);
311 }
312
313 /**
314 * Get the header element
315 */
316 public SOAPHeaderElement getHeader(String namespace, String partName) {
317 for(int i=0;i<headers.size();i++) {
318 SOAPHeaderElement header = (SOAPHeaderElement)headers.get(i);
319 if(header.getNamespaceURI().equals(namespace) &&
320 header.getName().equals(partName))
321 return header;
322 }
323 return null;
324 }
325
326 /**
327 * Get a response header element
328 */
329 public SOAPHeaderElement getResponseHeader(String namespace, String partName) {
330 try
331 {
332 if (_call == null)
333 return null;
334 return _call.getResponseMessage().getSOAPEnvelope().getHeaderByName(namespace, partName);
335 }
336 catch (Exception e)
337 {
338 return null;
339 }
340 }
341
342 /**
343 * Get the array of header elements
344 */
345 public SOAPHeaderElement[] getHeaders() {
346 SOAPHeaderElement[] array = new SOAPHeaderElement[headers.size()];
347 headers.copyInto(array);
348 return array;
349 }
350
351 /**
352 * Get the array of response header elements
353 */
354 public SOAPHeaderElement[] getResponseHeaders() {
355 SOAPHeaderElement[] array = new SOAPHeaderElement[0];
356 try
357 {
358 if (_call == null)
359 return array;
360 Vector h = _call.getResponseMessage().getSOAPEnvelope().getHeaders();
361 array = new SOAPHeaderElement[h.size()];
362 h.copyInto(array);
363 return array;
364 }
365 catch (Exception e)
366 {
367 return array;
368 }
369 }
370
371 /**
372 * Get the array of attachments
373 * The attachment array is cleared after this, so it is a destructive operation.
374 * @return the array of attachments that was in the message, or an empty array if
375 * there were none
376 */
377 public Object[] getAttachments() {
378 Object[] array = new Object[attachments.size()];
379 attachments.copyInto(array);
380 attachments.clear();
381 return array;
382 }
383
384 /**
385 * This method clears both requestHeaders and responseHeaders hashtables.
386 */
387 public void clearHeaders() {
388 headers.clear();
389 }
390
391 /**
392 * This method clears the request attachments.
393 */
394 public void clearAttachments() {
395 attachments.clear();
396 }
397
398 protected void setRequestHeaders(org.apache.axis.client.Call call) throws AxisFault {
399 // Set the call headers.
400 SOAPHeaderElement[] headers = getHeaders();
401 for(int i=0;i<headers.length;i++){
402 call.addHeader(headers[i]);
403 }
404 }
405
406 /**
407 * copy the attachments from the stub to the call object. After doing so,
408 * the local set of attachments are cleared.
409 * @param call call object to configure
410 * @throws AxisFault
411 */
412 protected void setAttachments(org.apache.axis.client.Call call) throws AxisFault {
413 // Set the attachments.
414 Object[] attachments = getAttachments();
415 for(int i=0;i<attachments.length;i++){
416 call.addAttachmentPart(attachments[i]);
417 }
418 clearAttachments();
419 }
420
421 /**
422 * Provide access to the service object. Not part of JAX-RPC
423 *
424 * @return the service object for this stub
425 */
426 public Service _getService() {
427 return service;
428 }
429
430 /**
431 * Creates a call from the service.
432 * @return
433 */
434 public Call _createCall() throws ServiceException {
435 _call = (Call) service.createCall();
436
437 // TODO: There is a lot of code in the generated stubs that
438 // can be moved here.
439 return _call;
440 }
441
442 /**
443 * Returns last Call object associated with this stub.
444 */
445 public Call _getCall() {
446 return _call;
447 }
448
449 /**
450 * Helper method for updating headers from the response.
451 *
452 * Deprecated, since response headers should not be
453 * automatically reflected back into the stub list.
454 *
455 *
456 * @deprecated This method has been changed to a no-op but remains
457 * in the code to keep compatibility with pre-1.1
458 * generated stubs.
459 */
460 protected void getResponseHeaders(org.apache.axis.client.Call call) throws AxisFault {
461 }
462
463 }