Search in sources :

Example 1 with SynapseSubscription

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

the class SubscriptionMessageBuilder method createSubscription.

/**
 * (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:ew="http://www.example.com/warnings" >
 * (06)   <s12:Header>
 * (07)     <wsa:Action>
 * (08)       http://schemas.xmlsoap.org/ws/2004/08/eventing/Subscribe
 * (09)     </wsa:Action>
 * (10)     <wsa:MessageID>
 * (11)       uuid:e1886c5c-5e86-48d1-8c77-fc1c28d47180
 * (12)     </wsa:MessageID>
 * (13)     <wsa:ReplyTo>
 * (14)      <wsa:Address>http://www.example.com/MyEvEntsink</wsa:Address>
 * (15)      <wsa:ReferenceProperties>
 * (16)        <ew:MySubscription>2597</ew:MySubscription>
 * (17)      </wsa:ReferenceProperties>
 * (18)     </wsa:ReplyTo>
 * (19)     <wsa:To>http://www.example.org/oceanwatch/EventSource</wsa:To>
 * (20)   </s12:Header>
 * (21)   <s12:Body>
 * (22)     <wse:Subscribe>
 * (23)       <wse:EndTo>
 * (24)         <wsa:Address>
 * (25)           http://www.example.com/MyEventSink
 * (26)         </wsa:Address>
 * (27)         <wsa:ReferenceProperties>
 * (28)           <ew:MySubscription>2597</ew:MySubscription>
 * (29)         </wsa:ReferenceProperties>
 * (30)       </wse:EndTo>
 * (31)       <wse:Delivery>
 * (32)         <wse:NotifyTo>
 * (33)           <wsa:Address>
 * (34)             http://www.other.example.com/OnStormWarning
 * (35)           </wsa:Address>
 * (36)           <wsa:ReferenceProperties>
 * (37)             <ew:MySubscription>2597</ew:MySubscription>
 * (38)           </wsa:ReferenceProperties>
 * (39)         </wse:NotifyTo>
 * (40)       </wse:Delivery>
 * (41)       <wse:Expires>2004-06-26T21:07:00.000-08:00</wse:Expires>
 * (42)       <wse:Filter xmlns:ow="http://www.example.org/oceanwatch"
 * (43)           Dialect="http://www.example.org/topicFilter" >
 * (44)         weather.storms
 * (45)       </wse:Filter>
 * (46)     </wse:Subscribe>
 * (47)   </s12:Body>
 * (48) </s12:Envelope>
 *
 * @param mc The MessageContext from which to create the SynapseSubscription
 * @return The SynapseSubscription
 */
public static SynapseSubscription createSubscription(MessageContext mc) {
    SynapseSubscription subscription = null;
    OMElement notifyToElem;
    OMElement elem = mc.getEnvelope().getBody().getFirstChildWithName(SUBSCRIBE_QNAME);
    if (elem != null) {
        OMElement deliveryElem = elem.getFirstChildWithName(DELIVERY_QNAME);
        if (deliveryElem != null) {
            notifyToElem = deliveryElem.getFirstChildWithName(NOTIFY_TO_QNAME);
            if (notifyToElem != null) {
                subscription = new SynapseSubscription(EventingConstants.WSE_DEFAULT_DELIVERY_MODE);
                subscription.setAddressUrl(notifyToElem.getFirstElement().getText());
                subscription.setEndpointUrl(notifyToElem.getFirstElement().getText());
                subscription.setSubManUrl(mc.getTo().getAddress());
            } else {
                handleException("NotifyTo element not found in the subscription message");
            }
        } else {
            handleException("Delivery element is not found in the subscription message");
        }
        OMElement filterElem = elem.getFirstChildWithName(FILTER_QNAME);
        if (subscription != null && filterElem != null) {
            OMAttribute dialectAttr = filterElem.getAttribute(ATT_DIALECT);
            if (dialectAttr != null && dialectAttr.getAttributeValue() != null) {
                subscription.setFilterDialect(dialectAttr.getAttributeValue());
                subscription.setFilterValue(filterElem.getText());
            } else {
                handleException("Error in creating subscription. Filter dialect not defined");
            }
        }
        OMElement expiryElem = elem.getFirstChildWithName(EXPIRES);
        if (expiryElem != null) {
            Calendar calendarExpires = null;
            try {
                if (expiryElem.getText().startsWith("P")) {
                    calendarExpires = ConverterUtil.convertToDuration(expiryElem.getText()).getAsCalendar();
                } else {
                    calendarExpires = ConverterUtil.convertToDateTime(expiryElem.getText());
                }
            } catch (Exception e) {
                log.error("Error converting the expiration date ," + e.toString());
                setExpirationFault(subscription);
            }
            Calendar calendarNow = Calendar.getInstance();
            if ((isValidDate(expiryElem.getText(), calendarExpires)) && (calendarNow.before(calendarExpires))) {
                subscription.setExpires(calendarExpires);
            } else {
                setExpirationFault(subscription);
            }
        }
    } else {
        handleException("Subscribe element is required as the payload of the subscription message");
    }
    return subscription;
}
Also used : SynapseSubscription(org.apache.synapse.eventing.SynapseSubscription) Calendar(java.util.Calendar) OMElement(org.apache.axiom.om.OMElement) OMAttribute(org.apache.axiom.om.OMAttribute) SynapseException(org.apache.synapse.SynapseException)

Example 2 with SynapseSubscription

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

the class SubscriptionMessageBuilder method createUnSubscribeMessage.

/**
 * create request for unsubscribr request
 * (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/Unsubscribe
 * (09)     </wsa:Action>
 * (10)     <wsa:MessageID>
 * (11)       uuid:2653f89f-25bc-4c2a-a7c4-620504f6b216
 * (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:Unsubscribe />
 * (25)   </s12:Body>
 * (26) </s12:Envelope>
 *
 * @param mc The MessageContext from which to create the SynapseSubscription
 * @return The SynapseSubscription
 */
public static SynapseSubscription createUnSubscribeMessage(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());
    return subscription;
}
Also used : SynapseSubscription(org.apache.synapse.eventing.SynapseSubscription) OMElement(org.apache.axiom.om.OMElement)

Example 3 with SynapseSubscription

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

the class MessageBuilderTest method testSubscriptionMessageBuilderScenarioThree.

public void testSubscriptionMessageBuilderScenarioThree() {
    String addressUrl = "http://synapse.test.com/eventing/subscriptions";
    Date date = new Date(System.currentTimeMillis() + 3600000);
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    String message = "<wse:Renew xmlns:wse=\"http://schemas.xmlsoap.org/ws/2004/08/eventing\">\n" + "   <wse:Expires>" + ConverterUtil.convertToString(cal) + "</wse:Expires>\n" + "</wse:Renew>";
    try {
        MessageContext msgCtx = TestUtils.getAxis2MessageContext(message, null).getAxis2MessageContext();
        msgCtx.setTo(new EndpointReference(addressUrl));
        String id = addIdentifierHeader(msgCtx);
        SubscriptionMessageBuilder.resetErrorInfo();
        SynapseSubscription sub = SubscriptionMessageBuilder.createRenewSubscribeMessage(msgCtx);
        assertEquals(id, sub.getId());
        assertEquals(addressUrl, sub.getAddressUrl());
        assertEquals(date, sub.getExpires().getTime());
        assertNull(SubscriptionMessageBuilder.getErrorCode());
        assertNull(SubscriptionMessageBuilder.getErrorReason());
        assertNull(SubscriptionMessageBuilder.getErrorSubCode());
    } catch (Exception e) {
        e.printStackTrace();
        fail("Error while constructing the sample subscription request: " + e.getMessage());
    }
}
Also used : SynapseSubscription(org.apache.synapse.eventing.SynapseSubscription) Calendar(java.util.Calendar) MessageContext(org.apache.axis2.context.MessageContext) Date(java.util.Date) EndpointReference(org.apache.axis2.addressing.EndpointReference)

Example 4 with SynapseSubscription

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

the class MessageBuilderTest method testSubscriptionMessageBuilderScenarioFour.

public void testSubscriptionMessageBuilderScenarioFour() {
    String addressUrl = "http://synapse.test.com/eventing/subscriptions";
    String message = "<wse:GetStatus xmlns:wse=\"http://schemas.xmlsoap.org/ws/2004/08/eventing\"/>";
    try {
        MessageContext msgCtx = TestUtils.getAxis2MessageContext(message, null).getAxis2MessageContext();
        msgCtx.setTo(new EndpointReference(addressUrl));
        String id = addIdentifierHeader(msgCtx);
        SubscriptionMessageBuilder.resetErrorInfo();
        SynapseSubscription sub = SubscriptionMessageBuilder.createGetStatusMessage(msgCtx);
        assertEquals(id, sub.getId());
        assertEquals(addressUrl, sub.getAddressUrl());
        assertNull(SubscriptionMessageBuilder.getErrorCode());
        assertNull(SubscriptionMessageBuilder.getErrorReason());
        assertNull(SubscriptionMessageBuilder.getErrorSubCode());
    } catch (Exception e) {
        e.printStackTrace();
        fail("Error while constructing the sample subscription request: " + e.getMessage());
    }
}
Also used : SynapseSubscription(org.apache.synapse.eventing.SynapseSubscription) MessageContext(org.apache.axis2.context.MessageContext) EndpointReference(org.apache.axis2.addressing.EndpointReference)

Example 5 with SynapseSubscription

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

the class ResponseMessageBuilderTest method testSubscriptionResponse.

public void testSubscriptionResponse() {
    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:SubscribeResponse xmlns:wse=\"http://schemas.xmlsoap.org/ws/2004/08/eventing\">" + "<wse:SubscriptionManager>" + "<wsa:Address xmlns:wsa=\"http://schemas.xmlsoap.org/ws/2004/08/addressing\">" + addressUrl + "</wsa:Address>" + "<wsa:ReferenceParameters xmlns:wsa=\"http://schemas.xmlsoap.org/ws/2004/08/addressing\">" + "<wse:Identifier>" + id + "</wse:Identifier>" + "</wsa:ReferenceParameters>" + "</wse:SubscriptionManager>" + "</wse:SubscribeResponse>";
    try {
        MessageContext msgCtx = TestUtils.getAxis2MessageContext("<empty/>", null).getAxis2MessageContext();
        ResponseMessageBuilder builder = new ResponseMessageBuilder(msgCtx);
        SOAPEnvelope env = builder.genSubscriptionResponse(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)

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