Search in sources :

Example 6 with SynapseSubscription

use of org.apache.synapse.eventing.SynapseSubscription in project wso2-synapse by wso2.

the class ResponseMessageBuilderTest method testUnsubscriptionResponse.

public void testUnsubscriptionResponse() {
    String id = UIDGenerator.generateURNString();
    String addressUrl = "http://synapse.test.com/eventing/sunscriptions";
    SynapseSubscription sub = new SynapseSubscription();
    sub.setId(id);
    sub.setSubManUrl(addressUrl);
    String expected = "<wse:UnsubscribeResponse xmlns:wse=\"http://schemas.xmlsoap.org/ws/2004/08/eventing\"/>";
    try {
        MessageContext msgCtx = TestUtils.getAxis2MessageContext("<empty/>", null).getAxis2MessageContext();
        ResponseMessageBuilder builder = new ResponseMessageBuilder(msgCtx);
        SOAPEnvelope env = builder.genUnSubscribeResponse(sub);
        OMElement resultOm = env.getBody().getFirstElement();
        OMElement expectedOm = AXIOMUtil.stringToOM(expected);
        assertTrue(compare(expectedOm, resultOm));
    } catch (Exception e) {
        fail("Error while constructing the test message context: " + e.getMessage());
    }
}
Also used : SynapseSubscription(org.apache.synapse.eventing.SynapseSubscription) OMElement(org.apache.axiom.om.OMElement) MessageContext(org.apache.axis2.context.MessageContext) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope)

Example 7 with SynapseSubscription

use of org.apache.synapse.eventing.SynapseSubscription 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 8 with SynapseSubscription

use of org.apache.synapse.eventing.SynapseSubscription in project wso2-synapse by wso2.

the class SubscriptionMessageBuilder method createRenewSubscribeMessage.

/**
 * (01) <s12:Envelope
 * (02)     xmlns:s12="http://www.w3.org/2003/05/soap-envelope"
 * (03)     xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing"
 * (04)     xmlns:wse="http://schemas.xmlsoap.org/ws/2004/08/eventing"
 * (05)     xmlns:ow="http://www.example.org/oceanwatch" >
 * (06)   <s12:Header>
 * (07)     <wsa:Action>
 * (08)       http://schemas.xmlsoap.org/ws/2004/08/eventing/Renew
 * (09)     </wsa:Action>
 * (10)     <wsa:MessageID>
 * (11)       uuid:bd88b3df-5db4-4392-9621-aee9160721f6
 * (12)     </wsa:MessageID>
 * (13)     <wsa:ReplyTo>
 * (14)      <wsa:Address>http://www.example.com/MyEventSink</wsa:Address>
 * (15)     </wsa:ReplyTo>
 * (16)     <wsa:To>
 * (17)       http://www.example.org/oceanwatch/SubscriptionManager
 * (18)     </wsa:To>
 * (19)     <wse:Identifier>
 * (20)       uuid:22e8a584-0d18-4228-b2a8-3716fa2097fa
 * (21)     </wse:Identifier>
 * (22)   </s12:Header>
 * (23)   <s12:Body>
 * (24)     <wse:Renew>
 * (25)       <wse:Expires>2004-06-26T21:07:00.000-08:00</wse:Expires>
 * (26)     </wse:Renew>
 * (27)   </s12:Body>
 * (28) </s12:Envelope>
 *
 * @param mc MessageContext from which to create the SynapseSubscription
 * @return The SynapseSubscription
 */
public static SynapseSubscription createRenewSubscribeMessage(MessageContext mc) {
    SynapseSubscription subscription = new SynapseSubscription();
    OMElement elem = mc.getEnvelope().getHeader().getFirstChildWithName(IDENTIFIER);
    String id = elem.getText();
    subscription.setId(id);
    subscription.setAddressUrl(mc.getTo().getAddress());
    OMElement renewElem = mc.getEnvelope().getBody().getFirstChildWithName(RENEW);
    if (renewElem != null) {
        OMElement expiryElem = renewElem.getFirstChildWithName(EXPIRES);
        if (expiryElem != null) {
            if (!(expiryElem.getText().startsWith("*"))) {
                Calendar calendarExpires = null;
                try {
                    if (expiryElem.getText().startsWith("P")) {
                        calendarExpires = ConverterUtil.convertToDuration(expiryElem.getText()).getAsCalendar();
                    } else {
                        calendarExpires = ConverterUtil.convertToDateTime(expiryElem.getText());
                    }
                } catch (Exception e) {
                    setExpirationFault(subscription);
                }
                Calendar calendarNow = Calendar.getInstance();
                if ((isValidDate(expiryElem.getText(), calendarExpires)) && (calendarNow.before(calendarExpires))) {
                    subscription.setExpires(calendarExpires);
                } else {
                    setExpirationFault(subscription);
                }
                subscription.setExpires(calendarExpires);
            } else {
                setExpirationFault(subscription);
            }
        } else {
            setExpirationFault(subscription);
        }
    }
    return subscription;
}
Also used : SynapseSubscription(org.apache.synapse.eventing.SynapseSubscription) Calendar(java.util.Calendar) OMElement(org.apache.axiom.om.OMElement) SynapseException(org.apache.synapse.SynapseException)

Example 9 with SynapseSubscription

use of org.apache.synapse.eventing.SynapseSubscription in project wso2-synapse by wso2.

the class SubscriptionMessageBuilder method createGetStatusMessage.

/**
 * (01) <s12:Envelope
 * (02)     xmlns:s12="http://www.w3.org/2003/05/soap-envelope"
 * (03)     xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing"
 * (04)     xmlns:wse="http://schemas.xmlsoap.org/ws/2004/08/eventing"
 * (05)     xmlns:ow="http://www.example.org/oceanwatch" >
 * (06)   <s12:Header>
 * (07)     <wsa:Action>
 * (08)       http://schemas.xmlsoap.org/ws/2004/08/eventing/GetStatus
 * (09)     </wsa:Action>
 * (10)     <wsa:MessageID>
 * (11)       uuid:bd88b3df-5db4-4392-9621-aee9160721f6
 * (12)     </wsa:MessageID>
 * (13)     <wsa:ReplyTo>
 * (14)       <wsa:Address>http://www.example.com/MyEventSink</wsa:Address>
 * (15)     </wsa:ReplyTo>
 * (16)     <wsa:To>
 * (17)       http://www.example.org/oceanwatch/SubscriptionManager
 * (18)     </wsa:To>
 * (19)     <wse:Identifier>
 * (20)       uuid:22e8a584-0d18-4228-b2a8-3716fa2097fa
 * (21)     </wse:Identifier>
 * (22)   </s12:Header>
 * (23)   <s12:Body>
 * (24)     <wse:GetStatus />
 * (25)   </s12:Body>
 * (26) </s12:Envelope>
 *
 * @param mc The MessageContext from which to extract the SynapseSubscription
 * @return The SynapseSubscription
 */
public static SynapseSubscription createGetStatusMessage(MessageContext mc) {
    SynapseSubscription subscription = new SynapseSubscription();
    subscription.setAddressUrl(mc.getTo().getAddress());
    OMElement elem = mc.getEnvelope().getHeader().getFirstChildWithName(IDENTIFIER);
    String id = elem.getText();
    subscription.setId(id);
    return subscription;
}
Also used : SynapseSubscription(org.apache.synapse.eventing.SynapseSubscription) OMElement(org.apache.axiom.om.OMElement)

Example 10 with SynapseSubscription

use of org.apache.synapse.eventing.SynapseSubscription 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

SynapseSubscription (org.apache.synapse.eventing.SynapseSubscription)15 OMElement (org.apache.axiom.om.OMElement)10 MessageContext (org.apache.axis2.context.MessageContext)9 Calendar (java.util.Calendar)7 Date (java.util.Date)5 EndpointReference (org.apache.axis2.addressing.EndpointReference)5 SOAPEnvelope (org.apache.axiom.soap.SOAPEnvelope)4 SynapseException (org.apache.synapse.SynapseException)3 OMAttribute (org.apache.axiom.om.OMAttribute)2 Iterator (java.util.Iterator)1 SynapseEventSource (org.apache.synapse.eventing.SynapseEventSource)1 DefaultInMemorySubscriptionManager (org.apache.synapse.eventing.managers.DefaultInMemorySubscriptionManager)1 Test (org.junit.Test)1 SubscriptionManager (org.wso2.eventing.SubscriptionManager)1 EventException (org.wso2.eventing.exceptions.EventException)1