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

fr.inria.eventcloud.webservices.listeners.WsnCompoundEventNotificationListener Maven / Gradle / Ivy

/**
 * Copyright (c) 2011-2013 INRIA.
 * 
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Affero General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any
 * later version.
 * 
 * This program 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 General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see 
 **/
package fr.inria.eventcloud.webservices.listeners;

import javax.xml.namespace.QName;
import javax.xml.ws.WebServiceException;

import org.oasis_open.docs.wsn.bw_2.NotificationConsumer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import fr.inria.eventcloud.api.CompoundEvent;
import fr.inria.eventcloud.api.SubscriptionId;
import fr.inria.eventcloud.api.listeners.CompoundEventNotificationListener;
import fr.inria.eventcloud.translators.wsn.TranslationException;
import fr.inria.eventcloud.translators.wsn.WsnConstants;
import fr.inria.eventcloud.translators.wsn.WsnHelper;
import fr.inria.eventcloud.translators.wsn.WsnTranslator;
import fr.inria.eventcloud.webservices.factories.WsClientFactory;

/**
 * A {@link CompoundEventNotificationListener} which invokes a WS-Notification
 * web service.
 * 
 * @author lpellegr
 */
public class WsnCompoundEventNotificationListener extends
        CompoundEventNotificationListener {

    private static final long serialVersionUID = 160L;

    private static final Logger log =
            LoggerFactory.getLogger(WsnCompoundEventNotificationListener.class);

    private final QName streamQName;

    private final String subscriberWsEndpointUrl;

    private transient WsnTranslator translator;

    private transient NotificationConsumer subscriberWsClient;

    /**
     * Creates a {@link WsnCompoundEventNotificationListener} with the specified
     * subscriber web service endpoint URL to invoke the WS-Notification web
     * service to notify.
     * 
     * @param streamUrl
     *            the stream URL.
     * @param subscriberWsEndpointUrl
     *            the subscriber web service endpoint URL.
     */
    public WsnCompoundEventNotificationListener(String streamUrl,
            String subscriberWsEndpointUrl) {
        this.subscriberWsEndpointUrl = subscriberWsEndpointUrl;

        int index = streamUrl.lastIndexOf('/');

        this.streamQName =
                new QName(
                        streamUrl.substring(0, index + 1),
                        streamUrl.substring(index + 1), "s");
    }

    private synchronized WsnTranslator getTranslator() {
        if (this.translator == null) {
            this.translator =
                    new WsnTranslator(this.streamQName.getNamespaceURI());
        }

        return this.translator;
    }

    private synchronized NotificationConsumer getSubscriberWsClient() {
        if (this.subscriberWsClient == null) {
            this.subscriberWsClient =
                    WsClientFactory.createWsClient(
                            NotificationConsumer.class,
                            this.subscriberWsEndpointUrl);
        }

        return this.subscriberWsClient;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void onNotification(SubscriptionId id, CompoundEvent solution) {
        try {
            this.getSubscriberWsClient()
                    .notify(
                            WsnHelper.createNotifyMessage(
                                    this.subscriberWsEndpointUrl,
                                    this.streamQName,
                                    solution.getGraph()
                                            .getURI()
                                            .endsWith(
                                                    WsnConstants.SIMPLE_TOPIC_EXPRESSION_MARKER),
                                    this.getTranslator().translate(solution)));

            log.info(
                    "Subscriber {} notified about:\n{}",
                    this.subscriberWsEndpointUrl, solution);
        } catch (TranslationException e) {
            log.error("Error during translation", e);
        } catch (WebServiceException e) {
            log.error("Failed to send notification to "
                    + this.subscriberWsEndpointUrl, e.getCause());
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String getSubscriberUrl() {
        return this.subscriberWsEndpointUrl;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy