Search in sources :

Example 1 with AMPExtension

use of org.jivesoftware.smackx.amp.packet.AMPExtension in project Smack by igniterealtime.

the class AMPExtensionTest method isCorrectFromXmlErrorHandling.

@Test
public void isCorrectFromXmlErrorHandling() throws Exception {
    AMPExtensionProvider ampProvider = new AMPExtensionProvider();
    XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
    parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
    parser.setInput(INCORRECT_RECEIVING_STANZA_STREAM, "UTF-8");
    assertEquals(XmlPullParser.START_TAG, parser.next());
    assertEquals(AMPExtension.ELEMENT, parser.getName());
    ExtensionElement extension = ampProvider.parse(parser);
    assertTrue(extension instanceof AMPExtension);
    AMPExtension amp = (AMPExtension) extension;
    assertEquals(0, amp.getRulesCount());
    assertEquals(AMPExtension.Status.alert, amp.getStatus());
    assertEquals("bernardo@hamlet.lit/elsinore", amp.getFrom());
    assertEquals("francisco@hamlet.lit", amp.getTo());
}
Also used : AMPExtensionProvider(org.jivesoftware.smackx.amp.provider.AMPExtensionProvider) XmlPullParser(org.xmlpull.v1.XmlPullParser) ExtensionElement(org.jivesoftware.smack.packet.ExtensionElement) AMPExtension(org.jivesoftware.smackx.amp.packet.AMPExtension) Test(org.junit.Test)

Example 2 with AMPExtension

use of org.jivesoftware.smackx.amp.packet.AMPExtension in project Smack by igniterealtime.

the class AMPExtensionTest method isCorrectFromXmlDeserialization.

@Test
public void isCorrectFromXmlDeserialization() throws Exception {
    AMPExtensionProvider ampProvider = new AMPExtensionProvider();
    XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
    parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
    parser.setInput(CORRECT_SENDING_STANZA_STREAM, "UTF-8");
    assertEquals(XmlPullParser.START_TAG, parser.next());
    assertEquals(AMPExtension.ELEMENT, parser.getName());
    ExtensionElement extension = ampProvider.parse(parser);
    assertTrue(extension instanceof AMPExtension);
    AMPExtension amp = (AMPExtension) extension;
    assertEquals(9, amp.getRulesCount());
}
Also used : AMPExtensionProvider(org.jivesoftware.smackx.amp.provider.AMPExtensionProvider) XmlPullParser(org.xmlpull.v1.XmlPullParser) ExtensionElement(org.jivesoftware.smack.packet.ExtensionElement) AMPExtension(org.jivesoftware.smackx.amp.packet.AMPExtension) Test(org.junit.Test)

Example 3 with AMPExtension

use of org.jivesoftware.smackx.amp.packet.AMPExtension in project Smack by igniterealtime.

the class AMPExtensionProvider method parse.

/**
     * Parses a AMPExtension stanza(/packet) (extension sub-packet).
     *
     * @param parser the XML parser, positioned at the starting element of the extension.
     * @return a PacketExtension.
     * @throws IOException 
     * @throws XmlPullParserException 
     */
@Override
public AMPExtension parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException {
    final String from = parser.getAttributeValue(null, "from");
    final String to = parser.getAttributeValue(null, "to");
    final String statusString = parser.getAttributeValue(null, "status");
    AMPExtension.Status status = null;
    if (statusString != null) {
        try {
            status = AMPExtension.Status.valueOf(statusString);
        } catch (IllegalArgumentException ex) {
            LOGGER.severe("Found invalid amp status " + statusString);
        }
    }
    AMPExtension ampExtension = new AMPExtension(from, to, status);
    String perHopValue = parser.getAttributeValue(null, "per-hop");
    if (perHopValue != null) {
        boolean perHop = Boolean.parseBoolean(perHopValue);
        ampExtension.setPerHop(perHop);
    }
    boolean done = false;
    while (!done) {
        int eventType = parser.next();
        if (eventType == XmlPullParser.START_TAG) {
            if (parser.getName().equals(AMPExtension.Rule.ELEMENT)) {
                String actionString = parser.getAttributeValue(null, AMPExtension.Action.ATTRIBUTE_NAME);
                String conditionName = parser.getAttributeValue(null, AMPExtension.Condition.ATTRIBUTE_NAME);
                String conditionValue = parser.getAttributeValue(null, "value");
                AMPExtension.Condition condition = createCondition(conditionName, conditionValue);
                AMPExtension.Action action = null;
                if (actionString != null) {
                    try {
                        action = AMPExtension.Action.valueOf(actionString);
                    } catch (IllegalArgumentException ex) {
                        LOGGER.severe("Found invalid rule action value " + actionString);
                    }
                }
                if (action == null || condition == null) {
                    LOGGER.severe("Rule is skipped because either it's action or it's condition is invalid");
                } else {
                    AMPExtension.Rule rule = new AMPExtension.Rule(action, condition);
                    ampExtension.addRule(rule);
                }
            }
        } else if (eventType == XmlPullParser.END_TAG) {
            if (parser.getName().equals(AMPExtension.ELEMENT)) {
                done = true;
            }
        }
    }
    return ampExtension;
}
Also used : AMPExtension(org.jivesoftware.smackx.amp.packet.AMPExtension)

Example 4 with AMPExtension

use of org.jivesoftware.smackx.amp.packet.AMPExtension in project Smack by igniterealtime.

the class AMPExtensionTest method isCorrectToXmlTransform.

@Test
public void isCorrectToXmlTransform() throws IOException {
    String correctStanza = toString(CORRECT_SENDING_STANZA_STREAM);
    AMPExtension ext = new AMPExtension();
    ext.addRule(new AMPExtension.Rule(AMPExtension.Action.alert, new AMPDeliverCondition(AMPDeliverCondition.Value.direct)));
    ext.addRule(new AMPExtension.Rule(AMPExtension.Action.drop, new AMPDeliverCondition(AMPDeliverCondition.Value.forward)));
    ext.addRule(new AMPExtension.Rule(AMPExtension.Action.error, new AMPDeliverCondition(AMPDeliverCondition.Value.gateway)));
    ext.addRule(new AMPExtension.Rule(AMPExtension.Action.notify, new AMPDeliverCondition(AMPDeliverCondition.Value.none)));
    ext.addRule(new AMPExtension.Rule(AMPExtension.Action.notify, new AMPDeliverCondition(AMPDeliverCondition.Value.stored)));
    ext.addRule(new AMPExtension.Rule(AMPExtension.Action.notify, new AMPExpireAtCondition("2004-09-10T08:33:14Z")));
    ext.addRule(new AMPExtension.Rule(AMPExtension.Action.notify, new AMPMatchResourceCondition(AMPMatchResourceCondition.Value.any)));
    ext.addRule(new AMPExtension.Rule(AMPExtension.Action.notify, new AMPMatchResourceCondition(AMPMatchResourceCondition.Value.exact)));
    ext.addRule(new AMPExtension.Rule(AMPExtension.Action.notify, new AMPMatchResourceCondition(AMPMatchResourceCondition.Value.other)));
    assertEquals(correctStanza, ext.toXML());
}
Also used : AMPExtension(org.jivesoftware.smackx.amp.packet.AMPExtension) Test(org.junit.Test)

Aggregations

AMPExtension (org.jivesoftware.smackx.amp.packet.AMPExtension)4 Test (org.junit.Test)3 ExtensionElement (org.jivesoftware.smack.packet.ExtensionElement)2 AMPExtensionProvider (org.jivesoftware.smackx.amp.provider.AMPExtensionProvider)2 XmlPullParser (org.xmlpull.v1.XmlPullParser)2