Search in sources :

Example 1 with EventException

use of org.wso2.eventing.exceptions.EventException in project wso2-synapse by wso2.

the class SynapseEventSource method receive.

/**
 * Override the Message receiver method to accept subscriptions and events
 *
 * @param mc message context
 * @throws AxisFault
 */
public void receive(MessageContext mc) throws AxisFault {
    // Create synapse message context from the axis2 message context
    SynapseConfiguration synCfg = (SynapseConfiguration) mc.getConfigurationContext().getAxisConfiguration().getParameter(SynapseConstants.SYNAPSE_CONFIG).getValue();
    SynapseEnvironment synEnv = (SynapseEnvironment) mc.getConfigurationContext().getAxisConfiguration().getParameter(SynapseConstants.SYNAPSE_ENV).getValue();
    org.apache.synapse.MessageContext smc = new Axis2MessageContext(mc, synCfg, synEnv);
    // initialize the response message builder using the message context
    ResponseMessageBuilder messageBuilder = new ResponseMessageBuilder(mc);
    try {
        if (EventingConstants.WSE_SUBSCRIBE.equals(mc.getWSAAction())) {
            // add new subscription to the SynapseSubscription store through subscription manager
            processSubscriptionRequest(mc, messageBuilder);
        } else if (EventingConstants.WSE_UNSUBSCRIBE.equals(mc.getWSAAction())) {
            // Unsubscribe the matching subscription
            processUnSubscribeRequest(mc, messageBuilder);
        } else if (EventingConstants.WSE_GET_STATUS.equals(mc.getWSAAction())) {
            // Response with the status of the subscription
            processGetStatusRequest(mc, messageBuilder);
        } else if (EventingConstants.WSE_RENEW.equals(mc.getWSAAction())) {
            // Renew subscription
            processReNewRequest(mc, messageBuilder);
        } else {
            // Treat as an Event
            if (log.isDebugEnabled()) {
                log.debug("Event received");
            }
            dispatchEvents(smc);
        }
    } catch (EventException e) {
        handleException("Subscription manager processing error", e);
    }
}
Also used : EventException(org.wso2.eventing.exceptions.EventException) SynapseEnvironment(org.apache.synapse.core.SynapseEnvironment) ResponseMessageBuilder(org.apache.synapse.eventing.builders.ResponseMessageBuilder) SynapseConfiguration(org.apache.synapse.config.SynapseConfiguration) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Example 2 with EventException

use of org.wso2.eventing.exceptions.EventException in project wso2-synapse by wso2.

the class SynapseEventSource method processGetStatusRequest.

/**
 * Process the GetStatus message request
 *
 * @param mc             axis2 message context
 * @param messageBuilder respose message builder
 * @throws AxisFault     axis fault
 * @throws EventException event
 */
private void processGetStatusRequest(MessageContext mc, ResponseMessageBuilder messageBuilder) throws AxisFault, EventException {
    Subscription subscription = SubscriptionMessageBuilder.createGetStatusMessage(mc);
    if (log.isDebugEnabled()) {
        log.debug("GetStatus request recived for SynapseSubscription ID : " + subscription.getId());
    }
    subscription = subscriptionManager.getSubscription(subscription.getId());
    if (subscription != null) {
        if (log.isDebugEnabled()) {
            log.debug("Sending GetStatus responce for SynapseSubscription ID : " + subscription.getId());
        }
        // send the responce
        SOAPEnvelope soapEnvelope = messageBuilder.genGetStatusResponse(subscription);
        dispatchResponse(soapEnvelope, EventingConstants.WSE_GET_STATUS_RESPONSE, mc, false);
    } else {
        // Send the Fault responce
        if (log.isDebugEnabled()) {
            log.debug("GetStatus failed, sending fault response");
        }
        SOAPEnvelope soapEnvelope = messageBuilder.genFaultResponse(mc, EventingConstants.WSE_FAULT_CODE_RECEIVER, "EventSourceUnableToProcess", "Subscription Not Found", "");
        dispatchResponse(soapEnvelope, EventingConstants.WSA_FAULT, mc, true);
    }
}
Also used : SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) Subscription(org.wso2.eventing.Subscription)

Example 3 with EventException

use of org.wso2.eventing.exceptions.EventException in project wso2-synapse by wso2.

the class EventSourceFactory method createEventSource.

@SuppressWarnings({ "UnusedDeclaration" })
public static SynapseEventSource createEventSource(OMElement elem, Properties properties) {
    SynapseEventSource eventSource = null;
    OMAttribute name = elem.getAttribute(new QName(XMLConfigConstants.NULL_NAMESPACE, "name"));
    if (name == null) {
        handleException("The 'name' attribute is required for a event source de");
    } else {
        eventSource = new SynapseEventSource(name.getAttributeValue());
    }
    OMElement subscriptionManagerElem = elem.getFirstChildWithName(SUBSCRIPTION_MANAGER_QNAME);
    if (eventSource != null && subscriptionManagerElem != null) {
        OMAttribute clazz = subscriptionManagerElem.getAttribute(new QName(XMLConfigConstants.NULL_NAMESPACE, "class"));
        if (clazz != null) {
            String className = clazz.getAttributeValue();
            try {
                Class subscriptionManagerClass = Class.forName(className);
                SubscriptionManager manager = (SubscriptionManager) subscriptionManagerClass.newInstance();
                Iterator itr = subscriptionManagerElem.getChildrenWithName(PROPERTIES_QNAME);
                while (itr.hasNext()) {
                    OMElement propElem = (OMElement) itr.next();
                    String propName = propElem.getAttribute(new QName("name")).getAttributeValue();
                    String propValue = propElem.getAttribute(new QName("value")).getAttributeValue();
                    if (propName != null && !"".equals(propName.trim()) && propValue != null && !"".equals(propValue.trim())) {
                        propName = propName.trim();
                        propValue = propValue.trim();
                        PasswordManager passwordManager = PasswordManager.getInstance();
                        String key = eventSource.getName() + "." + propName;
                        if (passwordManager.isInitialized() && passwordManager.isTokenProtected(key)) {
                            eventSource.putConfigurationProperty(propName, propValue);
                            propValue = passwordManager.resolve(propValue);
                        }
                        manager.addProperty(propName, propValue);
                    }
                }
                eventSource.setSubscriptionManager(manager);
                eventSource.getSubscriptionManager().init();
            } catch (ClassNotFoundException e) {
                handleException("SubscriptionManager class not found", e);
            } catch (IllegalAccessException e) {
                handleException("Unable to access the SubscriptionManager object", e);
            } catch (InstantiationException e) {
                handleException("Unable to instantiate the SubscriptionManager object", e);
            }
        } else {
            handleException("SynapseSubscription manager class is a required attribute");
        }
    } else {
        handleException("SynapseSubscription Manager has not been specified for the event source");
    }
    try {
        createStaticSubscriptions(elem, eventSource);
    } catch (EventException e) {
        handleException("Static subscription creation failure", e);
    }
    return eventSource;
}
Also used : SynapseEventSource(org.apache.synapse.eventing.SynapseEventSource) EventException(org.wso2.eventing.exceptions.EventException) QName(javax.xml.namespace.QName) PasswordManager(org.wso2.securevault.PasswordManager) OMElement(org.apache.axiom.om.OMElement) SubscriptionManager(org.wso2.eventing.SubscriptionManager) Iterator(java.util.Iterator) OMAttribute(org.apache.axiom.om.OMAttribute)

Example 4 with EventException

use of org.wso2.eventing.exceptions.EventException in project wso2-synapse by wso2.

the class EventSourceFactory method createStaticSubscriptions.

/**
 * Generate the static subscriptions
 *
 * @param elem containing the static subscription configurations
 * @param synapseEventSource event source to which the static subscriptions belong to
 * @throws EventException in-case of a failure in creating static subscriptions
 */
private static void createStaticSubscriptions(OMElement elem, SynapseEventSource synapseEventSource) throws EventException {
    for (Iterator iterator = elem.getChildrenWithName(SUBSCRIPTION_QNAME); iterator.hasNext(); ) {
        SynapseSubscription synapseSubscription = new SynapseSubscription();
        OMElement elmSubscription = (OMElement) iterator.next();
        synapseSubscription.setId(elmSubscription.getAttribute(ID_QNAME).getAttributeValue());
        // process the filter
        OMElement elmFilter = elmSubscription.getFirstChildWithName(FILTER_QNAME);
        OMAttribute dialectAttr = elmFilter.getAttribute(FILTER_DIALECT_QNAME);
        if (dialectAttr != null && dialectAttr.getAttributeValue() != null) {
            OMAttribute sourceAttr = elmFilter.getAttribute(FILTER_SOURCE_QNAME);
            if (sourceAttr != null) {
                synapseSubscription.setFilterDialect(dialectAttr.getAttributeValue());
                synapseSubscription.setFilterValue(sourceAttr.getAttributeValue());
            } else {
                handleException("Error in creating static subscription. Filter source not defined");
            }
        } else {
            handleException("Error in creating static subscription. Filter dialect not defined");
        }
        OMElement elmEndpoint = elmSubscription.getFirstChildWithName(ENDPOINT_QNAME);
        if (elmEndpoint != null) {
            OMElement elmAddress = elmEndpoint.getFirstChildWithName(ADDRESS_QNAME);
            if (elmAddress != null) {
                OMAttribute uriAttr = elmAddress.getAttribute(EP_URI_QNAME);
                if (uriAttr != null) {
                    synapseSubscription.setEndpointUrl(uriAttr.getAttributeValue());
                    synapseSubscription.setAddressUrl(uriAttr.getAttributeValue());
                } else {
                    handleException("Error in creating static subscription. URI not defined");
                }
            } else {
                handleException("Error in creating static subscription. Address not defined");
            }
        } else {
            handleException("Error in creating static subscription. Endpoint not defined");
        }
        OMElement elmExpires = elmSubscription.getFirstChildWithName(EXPIRES_QNAME);
        if (elmExpires != null) {
            try {
                if (elmExpires.getText().startsWith("P")) {
                    synapseSubscription.setExpires(ConverterUtil.convertToDuration(elmExpires.getText()).getAsCalendar());
                } else {
                    synapseSubscription.setExpires(ConverterUtil.convertToDateTime(elmExpires.getText()));
                }
            } catch (Exception e) {
                handleException("Error in creating static subscription. invalid date format", e);
            }
        } else {
            synapseSubscription.setExpires(null);
        }
        synapseSubscription.setStaticEntry(true);
        synapseEventSource.getSubscriptionManager().subscribe(synapseSubscription);
    }
}
Also used : SynapseSubscription(org.apache.synapse.eventing.SynapseSubscription) Iterator(java.util.Iterator) OMElement(org.apache.axiom.om.OMElement) OMAttribute(org.apache.axiom.om.OMAttribute) SynapseException(org.apache.synapse.SynapseException) EventException(org.wso2.eventing.exceptions.EventException)

Example 5 with EventException

use of org.wso2.eventing.exceptions.EventException in project wso2-synapse by wso2.

the class EventSourceSerializerTest method testSerializeEvent4.

/**
 * Test SerialEvent and assert OMElement returned is not null.
 *
 * @throws XMLStreamException - XMLStreamException
 * @throws EventException     - EventException
 */
@Test
public void testSerializeEvent4() throws XMLStreamException, EventException {
    String inputXML = "      <eventSource name=\"SampleEventSource\" xmlns=\"http://ws.apache.org/ns/synapse\">\n" + "            <subscriptionManager class=\"org.apache.synapse.eventing.managers." + "DefaultInMemorySubscriptionManager\">\n" + "                <property name=\"topicHeaderName\" value=\"Topic\"/>\n" + "                <property name=\"topicHeaderNS\" value=\"http://apache.org/aip\"/>\n" + "            </subscriptionManager>\n" + "            <subscription id=\"mySubscription\">\n" + "                 <filter source =\"synapse/event/test\" dialect=\"http://synapse.apache.org/" + "eventing/dialect/topicFilter\"/>\n" + "                 <endpoint><address uri=\"http://localhost:9000/services/" + "SimpleStockQuoteService\"/></endpoint>\n" + "            </subscription>\n" + "            <subscription id=\"mySubscription2\">\n" + "                 <filter source =\"synapse/event/test\" dialect=\"http://synapse.apache.org/" + "eventing/dialect/topicFilter\"/>\n" + "                 <endpoint><address uri=\"http://localhost:9000/services/" + "SimpleStockQuoteService\"/></endpoint>\n" + "                 <expires>2020-06-27T21:07:00.000-08:00</expires>\n" + "            </subscription>\n" + "      </eventSource>\n";
    OMElement element = AXIOMUtil.stringToOM(inputXML);
    SynapseEventSource synapseEventSource = new SynapseEventSource("Test");
    SubscriptionManager subscriptionManager = new DefaultInMemorySubscriptionManager();
    subscriptionManager.addProperty("Name", "Test");
    SynapseSubscription synapseSubscription = new SynapseSubscription();
    synapseSubscription.setStaticEntry(true);
    Date date = new Date(System.currentTimeMillis() + 3600000);
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    synapseSubscription.setExpires(cal);
    subscriptionManager.subscribe(synapseSubscription);
    synapseEventSource.setSubscriptionManager(subscriptionManager);
    OMElement omElement = EventSourceSerializer.serializeEventSource(element, synapseEventSource);
    Assert.assertNotNull("OMElement cannot be null.", omElement);
}
Also used : SynapseSubscription(org.apache.synapse.eventing.SynapseSubscription) SynapseEventSource(org.apache.synapse.eventing.SynapseEventSource) Calendar(java.util.Calendar) OMElement(org.apache.axiom.om.OMElement) DefaultInMemorySubscriptionManager(org.apache.synapse.eventing.managers.DefaultInMemorySubscriptionManager) SubscriptionManager(org.wso2.eventing.SubscriptionManager) DefaultInMemorySubscriptionManager(org.apache.synapse.eventing.managers.DefaultInMemorySubscriptionManager) Date(java.util.Date) Test(org.junit.Test)

Aggregations

EventException (org.wso2.eventing.exceptions.EventException)6 Calendar (java.util.Calendar)3 Date (java.util.Date)3 QName (javax.xml.namespace.QName)3 OMElement (org.apache.axiom.om.OMElement)3 MessageContext (org.apache.axis2.context.MessageContext)3 Iterator (java.util.Iterator)2 OMAttribute (org.apache.axiom.om.OMAttribute)2 SynapseEventSource (org.apache.synapse.eventing.SynapseEventSource)2 SynapseSubscription (org.apache.synapse.eventing.SynapseSubscription)2 SubscriptionManager (org.wso2.eventing.SubscriptionManager)2 SOAPEnvelope (org.apache.axiom.soap.SOAPEnvelope)1 SynapseException (org.apache.synapse.SynapseException)1 SynapseConfiguration (org.apache.synapse.config.SynapseConfiguration)1 SynapseEnvironment (org.apache.synapse.core.SynapseEnvironment)1 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)1 ResponseMessageBuilder (org.apache.synapse.eventing.builders.ResponseMessageBuilder)1 DefaultInMemorySubscriptionManager (org.apache.synapse.eventing.managers.DefaultInMemorySubscriptionManager)1 Test (org.junit.Test)1 Subscription (org.wso2.eventing.Subscription)1