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

org.ow2.petals.binding.soap.listener.incoming.SoapExternalListener Maven / Gradle / Ivy

The newest version!
/**
 * Copyright (c) 2007-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.incoming;

import static org.ow2.petals.binding.soap.SoapConstants.Axis2.SOAP_EXTERNAL_LISTENER_SERVICE_PARAM;

import java.util.StringTokenizer;
import java.util.logging.Level;

import org.apache.axis2.AxisFault;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.description.AxisService;
import org.apache.axis2.engine.AxisConfiguration;
import org.ow2.petals.binding.soap.ServiceContext;
import org.ow2.petals.binding.soap.SoapComponent;
import org.ow2.petals.binding.soap.SoapComponentContext;
import org.ow2.petals.binding.soap.listener.incoming.jetty.AxisServletServer;
import org.ow2.petals.binding.soap.util.AxisServicesHelper;
import org.ow2.petals.binding.soap.util.SUPropertiesHelper;
import org.ow2.petals.component.framework.api.configuration.SuConfigurationParameters;
import org.ow2.petals.component.framework.api.exception.PEtALSCDKException;
import org.ow2.petals.component.framework.jbidescriptor.generated.Consumes;
import org.ow2.petals.component.framework.listener.AbstractExternalListener;

/**
 * The SOAP external listener.
 * @author Christophe HAMERLING - EBM WebSourcing
 */
public class SoapExternalListener extends AbstractExternalListener {

    private String serviceName;

    private ServiceContext context;

    @Override
    public void init() {
        this.serviceName = SUPropertiesHelper.getServiceName(getExtensions());

        // allow address (the SUManager deploy should have already warned about deprecation)
        if (this.serviceName == null) {
            this.serviceName = SUPropertiesHelper.getAddress(getExtensions());
        }

        context = getComponent().getSoapContext().getConsumersManager().getServiceContext(getConsumes());
    }

    @Override
    public SoapComponent getComponent() {
        return (SoapComponent) super.getComponent();
    }

    /**
     * As there is a single SOAP listener for all incoming soap requests, this
     * method only references the given address. External calls for non
     * registered addresses will be ignored.
     * 
     * @throws PEtALSCDKException
     */
    @Override
    public void start() throws PEtALSCDKException {
        /*
         * Register new Axis2 service only on consumes nodes. Create an AxisService for each requested endpoint. If an
         * enpoint has not been found, simply log a severe error.
         */
        this.getLogger().log(Level.FINE, "Starting listening on " + this.serviceName);

        this.createAxisService();

        final String redirect = SUPropertiesHelper.getHttpRedirection(getExtensions());
        if (redirect != null) {
            AxisServletServer httpServer = getComponent().getExternalListenerManager().getHttpServer();
            // Comma-separated list of redirection aliases
            StringTokenizer st = new StringTokenizer(redirect, ",;|");
            while (st.hasMoreTokens()) {
                httpServer.addRedirect(st.nextToken().trim(), this.serviceName);
            }
        }

        final AxisConfiguration axisConf = getComponent().getSoapContext().getAxis2ConfigurationContext()
                .getAxisConfiguration();

        try {
            final AxisService axisService = axisConf.getService(this.serviceName);
            axisService.addParameter(SOAP_EXTERNAL_LISTENER_SERVICE_PARAM, this);
        } catch (AxisFault af) {
            throw new PEtALSCDKException(af);
        }
    }

    /**
     * Unreference the given address. After address removal, it will be impossible to contact service from outside.
     */
    @Override
    public void stop() throws PEtALSCDKException {
        this.getLogger().log(Level.FINE, "Stopping listening on " + this.serviceName);

        final SuConfigurationParameters extensions = getExtensions();

        final String redirect = extensions.get("http-services-redirection");
        if (redirect != null) {
            // Comma-separated list of redirection aliases
            StringTokenizer st = new StringTokenizer(redirect, ",;|");
            while (st.hasMoreTokens()) {
                getComponent().getExternalListenerManager().getHttpServer().removeRedirect(st.nextToken().trim());
            }
        }

        AxisServicesHelper.unregisterAxisService(getComponent().getSoapContext(), extensions);
    }

    /**
     * Create the Axis service that will handle incoming SOAP calls whatever the transport layer.
     * 
     */
    private void createAxisService() throws PEtALSCDKException {

        final SoapComponentContext soapContext = getComponent().getSoapContext();

        if (getLogger().isLoggable(Level.FINE)) {
            getLogger().fine("Registering a new service into the Axis context: '" + serviceName + "'");
        }

        try {
            // try to find if the axisService has already been registered
            final ConfigurationContext axisConfigContext = soapContext.getAxis2ConfigurationContext();
            final AxisConfiguration axisConfig = axisConfigContext.getAxisConfiguration();
            if (axisConfig.getService(serviceName) != null) {
                // The Axis service already exists
                // TODO shouldn't we fail SU deployment instead?!
                if (getLogger().isLoggable(Level.WARNING)) {
                    getLogger().log(Level.WARNING, "The service '" + serviceName
                            + "' is already registered in Axis, you can't register it again");
                }
            } else {
                // The Axis service does not exist, we create it.
                AxisServicesHelper.registerAxisService(serviceName, context, soapContext);

            }
        } catch (final AxisFault e) {
            throw new PEtALSCDKException("Cannot register Service '" + serviceName + "' into the Axis context", e);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy