Search in sources :

Example 6 with XmppMalformedException

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

the class TcpConnection method connect.

/**
     * Start the tcp connection to a given ip/port pair.
     * @param addresse InetAddress The target internet address.
     * @param port int The target port.
     * @throws XmppException In case of a lower level exception.
     */
protected void connect(InetAddress addresse, int port) throws XmppException {
    SocketFactory socketFactory = SocketFactory.getDefault();
    try {
        socket = socketFactory.createSocket(addresse, port);
        socket.setKeepAlive(false);
        socket.setSoTimeout(3 * 60 * 1000);
        socket.setTcpNoDelay(true);
    } catch (IOException e) {
        close();
        throw new XmppTransportException("Can't connect", e);
    }
    FeatureNegotiationEngine engine;
    try {
        engine = new FeatureNegotiationEngine(socket);
    } catch (XmlPullParserException e) {
        close();
        throw new XmppMalformedException("Can't connect", e);
    } catch (IOException e) {
        close();
        throw new XmppTransportException("Can't connect", e);
    }
    engine.open(account);
    resourceJid = engine.bind(account.getResource());
    if (resourceJid == null) {
        close();
        throw new XmppTransportException("Can't bind");
    }
    Log.d(TAG, "Bound as " + resourceJid);
    xmppInput = engine.getXmppInputStream();
    xmppOutput = engine.getXmppOutputStream();
}
Also used : XmppMalformedException(com.googlecode.asmack.XmppMalformedException) SocketFactory(javax.net.SocketFactory) XmppTransportException(com.googlecode.asmack.connection.XmppTransportException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException)

Example 7 with XmppMalformedException

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

the class XmppInputStream method nextStanza.

/**
     * Pull the next stanza from the stream, throwing a XmppException on error.
     * @return Stanza The next stream stanza.
     * @throws XmppException In case of a xmpp error.
     */
public Stanza nextStanza() throws XmppException {
    Stanza stanza = null;
    try {
        stanza = XMLUtils.readStanza(parser);
    } catch (IllegalArgumentException e) {
        throw new XmppMalformedException("can't parse stanza", e);
    } catch (IllegalStateException e) {
        throw new XmppMalformedException("can't parse stanza", e);
    } catch (XmlPullParserException e) {
        throw new XmppMalformedException("can't parse stanza", e);
    } catch (IOException e) {
        throw new XmppTransportException("error during stanza read", e);
    } catch (ArrayIndexOutOfBoundsException e) {
        throw new XmppTransportException("XML reader b0rked", e);
    }
    if (debugEnabled) {
        Log.d(TAG, "Stanza: " + stanza.getXml());
    }
    lastReceiveTime = System.currentTimeMillis();
    return stanza;
}
Also used : XmppMalformedException(com.googlecode.asmack.XmppMalformedException) Stanza(com.googlecode.asmack.Stanza) XmppTransportException(com.googlecode.asmack.connection.XmppTransportException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException)

Example 8 with XmppMalformedException

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

the class PresenceBroadcastReceiver method onReceive.

/**
     * Receive a single stanza intent, check for xmpp <presence/> and
     * store it to the database.
     * @param Context The current application context.
     * @param intent The event Intent.
     */
@Override
public void onReceive(Context context, Intent intent) {
    Stanza stanza = intent.getParcelableExtra("stanza");
    if (!"presence".equals(stanza.getName())) {
        return;
    }
    String accountJid = XMPPUtils.getBareJid(stanza.getVia());
    String jid = XMPPUtils.getBareJid(stanza.getAttribute("from").getValue());
    StatusUpdate update = null;
    if (stanza.getAttribute("type") != null) {
        if ("unavailable".equals(stanza.getAttribute("type").getValue())) {
            update = mapper.getStatusUpdate(accountJid, jid);
            update.setPresence(Presence.OFFLINE);
            mapper.persist(update);
        }
    }
    try {
        update = mapper.getStatusUpdate(accountJid, jid);
        update.setPresence(Presence.AVAILABLE);
        Node node = stanza.getDocumentNode();
        Node show = XMLUtils.getFirstChild(node, null, "show");
        if (show != null) {
            String presence = show.getTextContent();
            if ("away".equals(presence)) {
                update.setPresence(Presence.AWAY);
            }
            if ("dnd".equals(presence)) {
                update.setPresence(Presence.DO_NOT_DISTURB);
            }
        }
        Node status = XMLUtils.getFirstChild(node, null, "status");
        if (status != null) {
            update.setStatus(status.getTextContent());
        }
        mapper.persist(update);
    } catch (XmppMalformedException e) {
        e.printStackTrace();
    }
}
Also used : XmppMalformedException(com.googlecode.asmack.XmppMalformedException) Stanza(com.googlecode.asmack.Stanza) Node(org.w3c.dom.Node)

Aggregations

XmppMalformedException (com.googlecode.asmack.XmppMalformedException)8 Node (org.w3c.dom.Node)6 Stanza (com.googlecode.asmack.Stanza)5 XmppTransportException (com.googlecode.asmack.connection.XmppTransportException)4 IOException (java.io.IOException)4 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)3 NodeList (org.w3c.dom.NodeList)2 Intent (android.content.Intent)1 Attribute (com.googlecode.asmack.Attribute)1 XmppIdentity (com.googlecode.asmack.XmppIdentity)1 KeyManagementException (java.security.KeyManagementException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 HashSet (java.util.HashSet)1 SocketFactory (javax.net.SocketFactory)1 SAXException (org.xml.sax.SAXException)1