Search in sources :

Example 1 with Attribute

use of com.googlecode.asmack.Attribute in project AsmackService by rtreffer.

the class SyncAdapter method getRosterRequest.

/**
     * Create a stanza to retrieve the roster of a xmpp account.
     * @param account The xmpp account.
     * @return A roster iq stanza.
     */
private Stanza getRosterRequest(final Account account) {
    long syncCount = getAndIncrementSyncCount(account);
    List<Attribute> attributes = new ArrayList<Attribute>(3);
    attributes.add(new Attribute("type", null, "get"));
    attributes.add(new Attribute("id", null, "rostersync-" + Long.toHexString(syncCount)));
    Stanza stanza = new Stanza("iq", "", account.name, "<iq><query xmlns='jabber:iq:roster'/></iq>", attributes);
    String fullJid = null;
    try {
        fullJid = service.getFullJidByBare(account.name);
    } catch (RemoteException e1) {
        e1.printStackTrace();
    }
    stanza.addAttribute(new Attribute("from", null, fullJid));
    return stanza;
}
Also used : Attribute(com.googlecode.asmack.Attribute) Stanza(com.googlecode.asmack.Stanza) ArrayList(java.util.ArrayList) RemoteException(android.os.RemoteException)

Example 2 with Attribute

use of com.googlecode.asmack.Attribute in project AsmackService by rtreffer.

the class DiscoReceiver method onReceive.

/**
     * Called on incoming stanzas and replies on service discovery requests.
     * @param context The current context.
     * @param intent The stanza intent.
     */
@Override
public void onReceive(Context context, Intent intent) {
    Stanza stanza = intent.getParcelableExtra("stanza");
    // only accept IQ stanzas
    if (!"iq".equals(stanza.getName())) {
        return;
    }
    // of type="get"
    if (!"get".equals(stanza.getAttributeValue("type"))) {
        return;
    }
    // from / to / id are mandatory
    Attribute from = stanza.getAttribute("from");
    Attribute to = stanza.getAttribute("to");
    Attribute id = stanza.getAttribute("id");
    if (id == null || from == null || to == null) {
        return;
    }
    try {
        Node node = stanza.getDocumentNode();
        Node query = XMLUtils.getFirstChild(node, "http://jabber.org/protocol/disco#info", "query");
        if (query == null || !query.hasAttributes()) {
            return;
        }
        Node discoAttributeNode = query.getAttributes().getNamedItem("node");
        String discoNode = null;
        if (discoAttributeNode != null) {
            discoNode = discoAttributeNode.getTextContent();
        }
        // we got a disco, reply
        StringBuilder payload = new StringBuilder("<iq type='result'");
        payload.append(" from='");
        payload.append(XMLUtils.xmlEscape(to.getValue()));
        payload.append("' to='");
        payload.append(XMLUtils.xmlEscape(from.getValue()));
        payload.append("'>");
        payload.append("<query xmlns='");
        payload.append("http://jabber.org/protocol/disco#info");
        if (discoNode != null) {
            payload.append("' node='");
            payload.append(XMLUtils.xmlEscape(discoNode));
        }
        payload.append("'>");
        String myJid = XMPPUtils.getBareJid(to.getValue());
        for (XmppIdentity identity : Database.getIdentities(context, myJid, null)) {
            payload.append("<identity");
            if (identity.getCategory().length() > 0) {
                payload.append(" category='");
                payload.append(XMLUtils.xmlEscape(identity.getCategory()));
                payload.append('\'');
            }
            if (identity.getType().length() > 0) {
                payload.append(" type='");
                payload.append(XMLUtils.xmlEscape(identity.getType()));
                payload.append('\'');
            }
            if (identity.getLang().length() > 0) {
                payload.append(" lang='");
                payload.append(XMLUtils.xmlEscape(identity.getLang()));
                payload.append('\'');
            }
            if (identity.getName().length() > 0) {
                payload.append(" name='");
                payload.append(XMLUtils.xmlEscape(identity.getName()));
                payload.append('\'');
            }
            payload.append("/>");
        }
        for (String feature : Database.getFeatures(context, myJid, null)) {
            payload.append("<feature var='");
            payload.append(XMLUtils.xmlEscape(feature));
            payload.append("'/>");
        }
        payload.append("</query></iq>");
        Stanza discoReply = new Stanza("iq", "", XMPPUtils.getBareJid(to.getValue()), payload.toString(), Arrays.asList(new Attribute[] { id }));
        intent = new Intent();
        intent.setAction(XmppTransportService.XMPP_STANZA_SEND_INTENT);
        intent.putExtra("stanza", discoReply);
        intent.addFlags(Intent.FLAG_FROM_BACKGROUND);
        context.sendBroadcast(intent, XmppTransportService.XMPP_STANZA_SEND_INTENT);
    } catch (XmppMalformedException e) {
        // Impossible
        Log.e("AsmackService", "Please report (impossible condition)", e);
    }
}
Also used : XmppMalformedException(com.googlecode.asmack.XmppMalformedException) Attribute(com.googlecode.asmack.Attribute) XmppIdentity(com.googlecode.asmack.XmppIdentity) Stanza(com.googlecode.asmack.Stanza) Node(org.w3c.dom.Node) Intent(android.content.Intent)

Example 3 with Attribute

use of com.googlecode.asmack.Attribute in project AsmackService by rtreffer.

the class XmppTransportService method send.

/**
     * Send a stanza via the first matching connection.
     * @param stanza The stanza to send.
     * @return True on success.
     */
public boolean send(Stanza stanza) {
    Log.d(TAG, "Sending stanza " + stanza.getName() + " via " + stanza.getVia());
    String via = stanza.getVia();
    if (via == null) {
        Log.w(TAG, "Sending stanza without via");
        return false;
    }
    if ("iq".equals(stanza.getName())) {
        Attribute id = stanza.getAttribute("id");
        if (id == null) {
            Log.w(TAG, "Sending iq without id");
            return false;
        }
    }
    Connection connection = getConnectionForJid(via);
    if (connection == null) {
        Log.w(TAG, "No connection for " + via);
        return false;
    }
    try {
        connection.send(stanza);
        return true;
    } catch (XmppException e) {
        Log.e(TAG, "Connection failed, dropping...", e);
        try {
            connection.close();
        } catch (XmppException e1) {
            Log.d(TAG, "Closing a broken connection failed.", e);
        }
    }
    Log.e(TAG, "No stream for " + via);
    return false;
}
Also used : Attribute(com.googlecode.asmack.Attribute) XmppException(com.googlecode.asmack.XmppException)

Example 4 with Attribute

use of com.googlecode.asmack.Attribute in project AsmackService by rtreffer.

the class XmppTransportService method sendFromAllResources.

/**
     * Send a stanza via this service, through all resource jids.
     * @param stanza The stanza to send.
     */
public void sendFromAllResources(Stanza stanza) {
    Log.d(TAG, "Sending stanza " + stanza.getName() + " via *");
    for (AccountConnection state : connections.values()) {
        if (state.getCurrentState() != State.Connected) {
            continue;
        }
        Connection connection = state.getConnection();
        stanza.addAttribute(new Attribute("from", "", connection.getResourceJid()));
        try {
            state.getConnection().send(stanza);
        } catch (XmppException e) {
            Log.w(TAG, "Problem sending staza " + stanza.getName(), e);
        }
    }
}
Also used : Attribute(com.googlecode.asmack.Attribute) XmppException(com.googlecode.asmack.XmppException)

Example 5 with Attribute

use of com.googlecode.asmack.Attribute in project AsmackService by rtreffer.

the class XmppOutputStream method send.

/**
     * Send a stanza through this stream. The stanza will be merged and
     * validated.
     * @param stanza Stanza The stanza to send.
     * @throws XmppException In case of an error.
     */
public void send(Stanza stanza) throws XmppException {
    XmlPullParser xmlPullParser;
    try {
        xmlPullParser = XMLUtils.getXMLPullParser();
    } catch (XmlPullParserException e) {
        throw new XmppException("Can't create xml parser", e);
    }
    try {
        xmlPullParser.setInput(new ByteArrayInputStream(stanza.getXml().getBytes()), "UTF-8");
    } catch (XmlPullParserException e) {
        throw new XmppException("Can't parse input", e);
    }
    StringWriter stringWriter = new StringWriter();
    XmlSerializer xmlSerializer;
    try {
        xmlSerializer = XMLUtils.getXMLSerializer();
    } catch (XmlPullParserException e) {
        throw new XmppException("Can't create xml serializer", e);
    }
    try {
        xmlSerializer.setOutput(stringWriter);
    } catch (IllegalArgumentException e) {
        throw new XmppException("Please report", e);
    } catch (IllegalStateException e) {
        throw new XmppException("Please report", e);
    } catch (IOException e) {
        throw new XmppException("Please report", e);
    }
    try {
        xmlSerializer.startTag(stanza.getNamespace(), stanza.getName());
        HashSet<String> addedAttributes = new HashSet<String>();
        if (stanza.getAttributes() != null) {
            for (Attribute attr : stanza.getAttributes()) {
                addedAttributes.add(attr.getNamespace() + "\0" + attr.getName());
                xmlSerializer.attribute(attr.getNamespace(), attr.getName(), attr.getValue());
            }
        }
        xmlPullParser.nextTag();
        int attributeCount = xmlPullParser.getAttributeCount();
        for (int i = 0; i < attributeCount; i++) {
            String key = xmlPullParser.getAttributeNamespace(i) + "\0" + xmlPullParser.getAttributeName(i);
            if (addedAttributes.contains(key)) {
                continue;
            }
            xmlSerializer.attribute(xmlPullParser.getAttributeNamespace(i), xmlPullParser.getAttributeName(i), xmlPullParser.getAttributeValue(i));
        }
        if (xmlPullParser.isEmptyElementTag()) {
            xmlSerializer.endTag(stanza.getNamespace(), stanza.getName());
        } else {
            XMLUtils.copyXML(xmlPullParser, xmlSerializer);
        }
        xmlSerializer.endDocument();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        new XmppException("Please report", e);
        e.printStackTrace();
    } catch (XmlPullParserException e) {
        e.printStackTrace();
    }
    sendUnchecked(stringWriter.toString());
}
Also used : XmppException(com.googlecode.asmack.XmppException) Attribute(com.googlecode.asmack.Attribute) XmlPullParser(org.xmlpull.v1.XmlPullParser) IOException(java.io.IOException) StringWriter(java.io.StringWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) XmlSerializer(org.xmlpull.v1.XmlSerializer) HashSet(java.util.HashSet)

Aggregations

Attribute (com.googlecode.asmack.Attribute)5 XmppException (com.googlecode.asmack.XmppException)3 Stanza (com.googlecode.asmack.Stanza)2 Intent (android.content.Intent)1 RemoteException (android.os.RemoteException)1 XmppIdentity (com.googlecode.asmack.XmppIdentity)1 XmppMalformedException (com.googlecode.asmack.XmppMalformedException)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Node (org.w3c.dom.Node)1 XmlPullParser (org.xmlpull.v1.XmlPullParser)1 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)1 XmlSerializer (org.xmlpull.v1.XmlSerializer)1