001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019:
020: package org.apache.axis2.client;
021:
022: import org.apache.axiom.om.*;
023: import org.apache.axiom.soap.*;
024: import org.apache.axis2.AxisFault;
025: import org.apache.axis2.Constants;
026: import org.apache.axis2.wsdl.WSDLConstants;
027: import org.apache.axis2.addressing.EndpointReference;
028: import org.apache.axis2.context.MessageContext;
029: import org.apache.axis2.description.AxisService;
030: import org.apache.axis2.description.WSDL2Constants;
031: import org.apache.axis2.i18n.Messages;
032: import org.apache.axis2.transport.http.HTTPConstants;
033: import org.apache.commons.httpclient.Header;
034:
035: import java.util.ArrayList;
036: import java.util.Iterator;
037:
038: /**
039: * Base class for generated client stubs. This defines several client API
040: * (<code>public</code>) methods shared between all types of stubs, along with
041: * some <code>protected</code> methods intended for use by the actual stub
042: * implementation code. The client API method names start with a leading
043: * underscore character to avoid conflicts with actual implementation methods.
044: */
045: public abstract class Stub {
046:
047: protected AxisService _service;
048: protected ArrayList modules = new ArrayList();
049:
050: protected ServiceClient _serviceClient;
051:
052: /**
053: * Get service client implementation used by this stub.
054: *
055: * @return service client
056: */
057: public ServiceClient _getServiceClient() {
058: return _serviceClient;
059: }
060:
061: /**
062: * Set service client implementation used by this stub. Once set, the
063: * service client is owned by this stub and will automatically be removed
064: * from the configuration when use of the stub is done.
065: *
066: * @param _serviceClient
067: */
068: public void _setServiceClient(ServiceClient _serviceClient) {
069: this ._serviceClient = _serviceClient;
070: }
071:
072: /**
073: * Create a SOAP message envelope using the supplied options.
074: * TODO generated stub code should use this method, or similar method taking
075: * an operation client
076: *
077: * @param options
078: * @return generated
079: * @throws SOAPProcessingException
080: */
081: protected static SOAPEnvelope createEnvelope(Options options)
082: throws SOAPProcessingException {
083: return getFactory(options.getSoapVersionURI())
084: .getDefaultEnvelope();
085: }
086:
087: /**
088: * Get Axiom factory appropriate to selected SOAP version.
089: *
090: * @param soapVersionURI
091: * @return factory
092: */
093: protected static SOAPFactory getFactory(String soapVersionURI) {
094:
095: if (SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI
096: .equals(soapVersionURI)) {
097: return OMAbstractFactory.getSOAP11Factory();
098: } else if (SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI
099: .equals(soapVersionURI)) {
100: return OMAbstractFactory.getSOAP12Factory();
101: } else {
102: throw new RuntimeException(Messages
103: .getMessage("unknownsoapversion"));
104: }
105: }
106:
107: /**
108: * Finalize method called by garbage collection. This is overridden to
109: * support cleanup of any associated resources.
110: *
111: * @throws Throwable
112: */
113: protected void finalize() throws Throwable {
114: super .finalize();
115: cleanup();
116: }
117:
118: /**
119: * Cleanup associated resources. This removes the axis service from the
120: * configuration.
121: *
122: * @throws AxisFault
123: */
124: public void cleanup() throws AxisFault {
125: _service.getAxisConfiguration().removeService(
126: _service.getName());
127: }
128:
129: /**
130: * sets the epr of the service client to given value
131: *
132: * @param address
133: */
134:
135: protected void setServiceClientEPR(String address) {
136: EndpointReference toEPRFromServiceClient = _serviceClient
137: .getOptions().getTo();
138: toEPRFromServiceClient.setAddress(address);
139: }
140:
141: /**
142: * add an http header with name and value to message context
143: *
144: * @param messageContext
145: * @param name
146: * @param value
147: */
148: protected void addHttpHeader(MessageContext messageContext,
149: String name, String value) {
150: java.lang.Object headersObj = messageContext
151: .getProperty(HTTPConstants.HTTP_HEADERS);
152: if (headersObj == null) {
153: headersObj = new java.util.ArrayList();
154: }
155: java.util.List headers = (java.util.List) headersObj;
156: Header header = new Header();
157: header.setName(name);
158: header.setValue(value);
159: headers.add(header);
160: messageContext.setProperty(HTTPConstants.HTTP_HEADERS, headers);
161: }
162:
163: /**
164: * sets the propertykey and propertyValue as a pair to operation client
165: *
166: * @param operationClient
167: * @param propertyKey
168: * @param propertyValue
169: */
170:
171: protected void addPropertyToOperationClient(
172: OperationClient operationClient, String propertyKey,
173: Object propertyValue) {
174: operationClient.getOptions().setProperty(propertyKey,
175: propertyValue);
176: }
177:
178: protected void addPropertyToOperationClient(
179: OperationClient operationClient, String propertyKey,
180: boolean value) {
181: addPropertyToOperationClient(operationClient, propertyKey,
182: new Boolean(value));
183: }
184:
185: protected void addPropertyToOperationClient(
186: OperationClient operationClient, String propertyKey,
187: int value) {
188: addPropertyToOperationClient(operationClient, propertyKey,
189: new Integer(value));
190: }
191:
192: protected void setMustUnderstand(OMElement headerElement,
193: OMNamespace omNamespace) {
194: OMFactory omFactory = OMAbstractFactory.getOMFactory();
195: OMAttribute mustUnderstandAttribute = omFactory
196: .createOMAttribute(SOAP12Constants.ATTR_MUSTUNDERSTAND,
197: omNamespace, "true");
198: headerElement.addAttribute(mustUnderstandAttribute);
199: }
200:
201: protected void addHeader(OMElement omElementToadd,
202: SOAPEnvelope envelop, boolean mustUnderstand) {
203: SOAPHeaderBlock soapHeaderBlock = envelop.getHeader()
204: .addHeaderBlock(omElementToadd.getLocalName(),
205: omElementToadd.getNamespace());
206: soapHeaderBlock.setMustUnderstand(mustUnderstand);
207: OMNode omNode = null;
208: for (Iterator iter = omElementToadd.getChildren(); iter
209: .hasNext();) {
210: omNode = (OMNode) iter.next();
211: soapHeaderBlock.addChild(omNode);
212: }
213:
214: }
215:
216: protected void addHeader(OMElement omElementToadd,
217: SOAPEnvelope envelop) {
218: addHeader(omElementToadd, envelop, false);
219: }
220:
221: }
|