@Test public void parseBasicItem() throws Exception { XmlPullParser parser = PacketParserUtils.getParserFor( "<message from='pubsub.myserver.com' to='*****@*****.**' id='foo'>" + "<event xmlns='http://jabber.org/protocol/pubsub#event'>" + "<items node='testNode'>" + "<item id='testid1' />" + "</items>" + "</event>" + "</message>"); Stanza message = PacketParserUtils.parseMessage(parser); ExtensionElement eventExt = message.getExtension(PubSubNamespace.EVENT.getXmlns()); assertTrue(eventExt instanceof EventElement); EventElement event = (EventElement) eventExt; assertEquals(EventElementType.items, event.getEventType()); assertEquals(1, event.getExtensions().size()); assertTrue(event.getExtensions().get(0) instanceof ItemsExtension); assertEquals(1, ((ItemsExtension) event.getExtensions().get(0)).items.size()); ExtensionElement itemExt = ((ItemsExtension) event.getExtensions().get(0)).items.get(0); assertTrue(itemExt instanceof Item); assertEquals("testid1", ((Item) itemExt).getId()); }
@Test public void parseEmptyTag() throws Exception { String itemContent = "<foo xmlns='smack:test'><bar/></foo>"; XmlPullParser parser = PacketParserUtils.getParserFor( "<message from='pubsub.myserver.com' to='*****@*****.**' id='foo'>" + "<event xmlns='http://jabber.org/protocol/pubsub#event'>" + "<items node='testNode'>" + "<item id='testid1' >" + itemContent + "</item>" + "</items>" + "</event>" + "</message>"); Stanza message = PacketParserUtils.parseMessage(parser); ExtensionElement eventExt = message.getExtension(PubSubNamespace.EVENT.getXmlns()); assertTrue(eventExt instanceof EventElement); EventElement event = (EventElement) eventExt; assertEquals(EventElementType.items, event.getEventType()); assertEquals(1, event.getExtensions().size()); assertTrue(event.getExtensions().get(0) instanceof ItemsExtension); assertEquals(1, ((ItemsExtension) event.getExtensions().get(0)).items.size()); ExtensionElement itemExt = ((ItemsExtension) event.getExtensions().get(0)).items.get(0); assertTrue(itemExt instanceof PayloadItem<?>); PayloadItem<?> item = (PayloadItem<?>) itemExt; assertEquals("testid1", item.getId()); assertTrue(item.getPayload() instanceof SimplePayload); assertXMLEqual(itemContent, ((SimplePayload) item.getPayload()).toXML().toString()); }
protected Stanza(Stanza p) { id = p.getStanzaId(); to = p.getTo(); from = p.getFrom(); error = p.error; // Copy extensions for (ExtensionElement pe : p.getExtensions()) { addExtension(pe); } }
private OnceForThisStanza(XMPPTCPConnection xMPPTCPConnection, Stanza stanza) { this.connection = xMPPTCPConnection; this.id = stanza.getStanzaId(); if (StringUtils.isNullOrEmpty(this.id)) { throw new IllegalArgumentException("Stanza ID must be set"); } }
@Test public void parseComplexItem() throws Exception { String itemContent = "<entry xmlns='http://www.w3.org/2005/Atom'>" + "<title>Soliloquy</title>" + "<summary>" + "To be, or not to be: that is the question:" + "Whether 'tis nobler in the mind to suffer" + "The slings and arrows of outrageous fortune," + "Or to take arms against a sea of troubles," + "And by opposing end them?" + "</summary>" + "<link rel='alternate' type='text/html' href='http://denmark.lit/2003/12/13/atom03'/>" + "<id>tag:denmark.lit,2003:entry-32397</id>" + "<published>2003-12-13T18:30:02Z</published>" + "<updated>2003-12-13T18:30:02Z</updated>" + "</entry>"; XmlPullParser parser = PacketParserUtils.getParserFor( "<message from='pubsub.myserver.com' to='*****@*****.**' id='foo'>" + "<event xmlns='http://jabber.org/protocol/pubsub#event'>" + "<items node='testNode'>" + "<item id='testid1' >" + itemContent + "</item>" + "</items>" + "</event>" + "</message>"); Stanza message = PacketParserUtils.parseMessage(parser); ExtensionElement eventExt = message.getExtension(PubSubNamespace.EVENT.getXmlns()); EventElement event = (EventElement) eventExt; ExtensionElement itemExt = ((ItemsExtension) event.getExtensions().get(0)).items.get(0); assertTrue(itemExt instanceof PayloadItem<?>); PayloadItem<?> item = (PayloadItem<?>) itemExt; assertEquals("testid1", item.getId()); assertTrue(item.getPayload() instanceof SimplePayload); SimplePayload payload = (SimplePayload) item.getPayload(); assertEquals("entry", payload.getElementName()); assertEquals("http://www.w3.org/2005/Atom", payload.getNamespace()); assertXMLEqual(itemContent, payload.toXML().toString()); }
public boolean accept(Stanza stanza) { CharSequence stanzaId = stanza.getStanzaId(); if (StringUtils.isNullOrEmpty(stanzaId) || !this.id.equals(stanzaId)) { return false; } this.connection.removeRequestAckPredicate(this); return true; }
@Override public void onPacket(ConnectionItem connection, final String bareAddress, Stanza packet) { String from = packet.getFrom(); if (from == null) return; if (!(connection instanceof AccountItem) || !(packet instanceof Message)) return; String account = ((AccountItem) connection).getAccount(); Message message = (Message) packet; String session = message.getThread(); if (session == null) return; for (ExtensionElement packetExtension : packet.getExtensions()) if (packetExtension instanceof Feature) { Feature feature = (Feature) packetExtension; if (!feature.isValid()) continue; DataFormType type = feature.getDataFormType(); if (type == DataFormType.form) onFormReceived(account, from, bareAddress, session, feature); else if (type == DataFormType.submit) onSubmitReceived(account, from, bareAddress, session, feature); else if (type == DataFormType.result) onResultReceived(account, from, bareAddress, session, feature); } }
@Override public void processPacket(Stanza packet) { LOGGER.info("got public key: " + StringUtils.abbreviate(packet.toXML().toString(), 300)); PublicKeyPublish publicKeyPacket = (PublicKeyPublish) packet; if (publicKeyPacket.getType() == IQ.Type.set) { LOGGER.warning("ignoring public key packet with type 'set'"); return; } if (publicKeyPacket.getType() == IQ.Type.result) { byte[] keyData = publicKeyPacket.getPublicKey(); if (keyData == null) { LOGGER.warning("got public key packet without public key"); return; } mControl.setPGPKey(publicKeyPacket.getFrom(), keyData); } }
public boolean accept(Stanza packet) { return packet.hasExtension(elementName, namespace); }
@Override public boolean accept(Stanza stanza) { return stanza.getClass().equals(Message.class); }
public static Presence parsePresence(XmlPullParser xmlPullParser) throws XmlPullParserException, IOException, SmackException { ParserUtils.assertAtStartTag(xmlPullParser); int depth = xmlPullParser.getDepth(); Presence.Type type = Presence.Type.available; String attributeValue = xmlPullParser.getAttributeValue("", "type"); if (!(attributeValue == null || attributeValue.equals(""))) { type = Presence.Type.fromString(attributeValue); } Stanza presence = new Presence(type); presence.setTo(xmlPullParser.getAttributeValue("", PrivacyItem.SUBSCRIPTION_TO)); presence.setFrom(xmlPullParser.getAttributeValue("", PrivacyItem.SUBSCRIPTION_FROM)); presence.setStanzaId(xmlPullParser.getAttributeValue("", "id")); String languageAttribute = getLanguageAttribute(xmlPullParser); if (!(languageAttribute == null || "".equals(languageAttribute.trim()))) { presence.setLanguage(languageAttribute); } while (true) { switch (xmlPullParser.next()) { case VideoSize.HVGA /*2*/: String name = xmlPullParser.getName(); String namespace = xmlPullParser.getNamespace(); Object obj = -1; switch (name.hashCode()) { case -1165461084: if (name.equals("priority")) { obj = 1; break; } break; case -892481550: if (name.equals(Status.ELEMENT)) { obj = null; break; } break; case 3529469: if (name.equals("show")) { obj = 2; break; } break; case 96784904: if (name.equals(XMPPError.ERROR)) { obj = 3; break; } break; } switch (obj) { case VideoSize.QCIF /*0*/: presence.setStatus(xmlPullParser.nextText()); break; case VideoSize.CIF /*1*/: presence.setPriority(Integer.parseInt(xmlPullParser.nextText())); break; case VideoSize.HVGA /*2*/: obj = xmlPullParser.nextText(); if (!StringUtils.isNotEmpty(obj)) { LOGGER.warning( "Empty or null mode text in presence show element form " + presence.getFrom() + " with id '" + presence.getStanzaId() + "' which is invalid according to RFC6121 4.7.2.1"); break; } presence.setMode(Mode.fromString(obj)); break; case Version.API03_CUPCAKE_15 /*3*/: presence.setError(parseError(xmlPullParser)); break; default: try { addExtensionElement(presence, xmlPullParser, name, namespace); break; } catch (Throwable e) { LOGGER.log( Level.WARNING, "Failed to parse extension packet in Presence packet. Attributes: from=" + presence.getFrom() + " id=" + presence.getStanzaId(), e); break; } } case Version.API03_CUPCAKE_15 /*3*/: if (xmlPullParser.getDepth() != depth) { break; } return presence; default: break; } } }
public static Message parseMessage(XmlPullParser xmlPullParser) throws XmlPullParserException, IOException, SmackException { ParserUtils.assertAtStartTag(xmlPullParser); if ($assertionsDisabled || xmlPullParser.getName().equals(Message.ELEMENT)) { int depth = xmlPullParser.getDepth(); Stanza message = new Message(); message.setStanzaId(xmlPullParser.getAttributeValue("", "id")); message.setTo(xmlPullParser.getAttributeValue("", PrivacyItem.SUBSCRIPTION_TO)); message.setFrom(xmlPullParser.getAttributeValue("", PrivacyItem.SUBSCRIPTION_FROM)); String attributeValue = xmlPullParser.getAttributeValue("", "type"); if (attributeValue != null) { message.setType(Message.Type.fromString(attributeValue)); } attributeValue = getLanguageAttribute(xmlPullParser); if (attributeValue == null || "".equals(attributeValue.trim())) { attributeValue = Stanza.getDefaultLanguage(); } else { message.setLanguage(attributeValue); } String str = null; while (true) { switch (xmlPullParser.next()) { case VideoSize.HVGA /*2*/: String name = xmlPullParser.getName(); String namespace = xmlPullParser.getNamespace(); Object obj = -1; switch (name.hashCode()) { case -1867885268: if (name.equals("subject")) { obj = null; break; } break; case -874443254: if (name.equals("thread")) { obj = 2; break; } break; case 3029410: if (name.equals(Message.BODY)) { obj = 1; break; } break; case 96784904: if (name.equals(XMPPError.ERROR)) { obj = 3; break; } break; } String languageAttribute; switch (obj) { case VideoSize.QCIF /*0*/: languageAttribute = getLanguageAttribute(xmlPullParser); if (languageAttribute == null) { languageAttribute = attributeValue; } name = parseElementText(xmlPullParser); if (message.getSubject(languageAttribute) != null) { break; } message.addSubject(languageAttribute, name); break; case VideoSize.CIF /*1*/: languageAttribute = getLanguageAttribute(xmlPullParser); if (languageAttribute == null) { languageAttribute = attributeValue; } name = parseElementText(xmlPullParser); if (message.getBody(languageAttribute) != null) { break; } message.addBody(languageAttribute, name); break; case VideoSize.HVGA /*2*/: if (str != null) { break; } str = xmlPullParser.nextText(); break; case Version.API03_CUPCAKE_15 /*3*/: message.setError(parseError(xmlPullParser)); break; default: addExtensionElement(message, xmlPullParser, name, namespace); break; } case Version.API03_CUPCAKE_15 /*3*/: if (xmlPullParser.getDepth() != depth) { break; } message.setThread(str); return message; default: break; } } } throw new AssertionError(); }
public static void addExtensionElement( Stanza stanza, XmlPullParser xmlPullParser, String str, String str2) throws XmlPullParserException, IOException, SmackException { stanza.addExtension(parseExtensionElement(str, str2, xmlPullParser)); }