All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ow2.petals.binding.soap.listener.outgoing.PetalsServiceClient Maven / Gradle / Ivy

The newest version!
/**
 * Copyright (c) 2008-2012 EBM WebSourcing, 2012-2023 Linagora
 * 
 * This program/library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 2.1 of the License, or (at your
 * option) any later version.
 * 
 * This program/library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program/library; If not, see http://www.gnu.org/licenses/
 * for the GNU Lesser General Public License version 2.1.
 */

package org.ow2.petals.binding.soap.listener.outgoing;

import javax.xml.namespace.QName;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.soap.SOAP12Constants;
import org.apache.axiom.soap.SOAPBody;
import org.apache.axiom.soap.SOAPEnvelope;
import org.apache.axiom.soap.SOAPFactory;
import org.apache.axis2.AxisFault;
import org.apache.axis2.client.OperationClient;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.context.MessageContext;
import org.apache.axis2.description.AxisService;
import org.apache.axis2.wsdl.WSDLConstants;
import org.apache.commons.pool2.impl.GenericObjectPool;

/**
 * A {@link org.apache.axis2.client.ServiceClient} extension to be able to get the entire SOAPBody of the service
 * response. This is because Axis2 only returns the first element of the {@link SOAPBody} which will not work with
 * multiref responses.
 * 
 * @see AXIS2-2408
 * @author Christophe HAMERLING - EBM WebSourcing
 */
public class PetalsServiceClient extends org.apache.axis2.client.ServiceClient {

    /**
     * The pool from which this service client has been taken: it is set when borrowed, and unset before return. It is
     * used to avoid complex operations to find the pool back on return.
     */
    private GenericObjectPool pool;

    /**
     * Creates a new instance of ServiceClient
     * 
     * @param configContext
     * @param axisService
     * @throws AxisFault
     */
    public PetalsServiceClient(final ConfigurationContext configContext, final AxisService axisService)
            throws AxisFault {
        super(configContext, axisService);
    }

    /**
     * Copied from {@link ServiceClient}
     */
    protected void fillSOAPEnvelope(final MessageContext messageContext, final OMElement xmlPayload)
            throws AxisFault {
        messageContext.setServiceContext(getServiceContext());

        final SOAPFactory soapFactory = getSOAPFactory();
        final SOAPEnvelope envelope = soapFactory.getDefaultEnvelope();

        if (xmlPayload != null) {
            envelope.getBody().addChild(xmlPayload);
        }
        addHeadersToEnvelope(envelope);

        messageContext.setEnvelope(envelope);
    }

    /**
     * Copied from {@link ServiceClient}
     */
    protected SOAPFactory getSOAPFactory() {
        final String soapVersionURI = getOptions().getSoapVersionURI();

        if (SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI.equals(soapVersionURI)) {
            return OMAbstractFactory.getSOAP12Factory();
        } else {
            // make the SOAP 1.1 the default SOAP version
            return OMAbstractFactory.getSOAP11Factory();
        }
    }

    /**
     * Inspired by {@link ServiceClient#sendReceive(QName, OMElement)},
     * {@link ServiceClient#fireAndForget(QName, OMElement)} and others
     */
    public MessageContext sendReceive2(final QName operation, final OMElement xmlPayload) throws AxisFault {

        final MessageContext messageContext = new MessageContext();
        fillSOAPEnvelope(messageContext, xmlPayload);

        final OperationClient operationClient = createClient(operation);
        operationClient.addMessageContext(messageContext);

        // TODO use operationClient.setCallback(AxisCallback) and do not block
        // we would first need to add a thread pool to the configuration context to use our thread...?
        // no it doesn't make sense, if we have to block a thread, so let's block this one... or use the real async
        // send! (which seems a bit more complex... not sure it's a good idea for now)
        operationClient.execute(true);

        return operationClient.getMessageContext(WSDLConstants.MESSAGE_LABEL_IN_VALUE);
    }

    public void setPool(GenericObjectPool pool) {
        this.pool = pool;
    }

    public GenericObjectPool getPool() {
        return pool;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy