Search in sources :

Example 6 with XMPPErrorException

use of org.jivesoftware.smack.XMPPException.XMPPErrorException in project Smack by igniterealtime.

the class IntTestUtil method deleteViaIbr.

public static void deleteViaIbr(XMPPTCPConnection connection) throws InterruptedException {
    // mechanisms
    if (!connection.isConnected()) {
        try {
            connection.connect().login();
        } catch (XMPPException | SmackException | IOException e) {
            LOGGER.log(Level.WARNING, "Exception reconnection account for deletion", e);
        }
    }
    final int maxAttempts = 3;
    AccountManager am = AccountManager.getInstance(connection);
    int attempts;
    for (attempts = 0; attempts < maxAttempts; attempts++) {
        try {
            am.deleteAccount();
        } catch (XMPPErrorException | NoResponseException e) {
            LOGGER.log(Level.WARNING, "Exception deleting account for " + connection, e);
            continue;
        } catch (NotConnectedException e) {
            LOGGER.log(Level.WARNING, "Exception deleting account for " + connection, e);
            try {
                connection.connect().login();
            } catch (XMPPException | SmackException | IOException e2) {
                LOGGER.log(Level.WARNING, "Exception while trying to re-connect " + connection, e);
            }
            continue;
        }
        LOGGER.info("Successfully deleted account of " + connection);
        break;
    }
    if (attempts > maxAttempts) {
        LOGGER.log(Level.SEVERE, "Could not delete account for connection: " + connection);
    }
}
Also used : XMPPErrorException(org.jivesoftware.smack.XMPPException.XMPPErrorException) NotConnectedException(org.jivesoftware.smack.SmackException.NotConnectedException) SmackException(org.jivesoftware.smack.SmackException) AccountManager(org.jivesoftware.smackx.iqregister.AccountManager) IOException(java.io.IOException) NoResponseException(org.jivesoftware.smack.SmackException.NoResponseException) XMPPException(org.jivesoftware.smack.XMPPException)

Example 7 with XMPPErrorException

use of org.jivesoftware.smack.XMPPException.XMPPErrorException in project Smack by igniterealtime.

the class IntTestUtil method deleteViaServiceAdministration.

public static void deleteViaServiceAdministration(XMPPTCPConnection connection, Configuration config) {
    EntityBareJid accountToDelete = connection.getUser().asEntityBareJid();
    final int maxAttempts = 3;
    int attempts;
    for (attempts = 0; attempts < maxAttempts; attempts++) {
        connection.disconnect();
        try {
            connection.connect().login(config.adminAccountUsername, config.adminAccountPassword);
        } catch (XMPPException | SmackException | IOException | InterruptedException e) {
            LOGGER.log(Level.WARNING, "Exception deleting account for " + connection, e);
            continue;
        }
        ServiceAdministrationManager adminManager = ServiceAdministrationManager.getInstanceFor(connection);
        try {
            adminManager.deleteUser(accountToDelete);
        } catch (NoResponseException | XMPPErrorException | NotConnectedException | InterruptedException e) {
            LOGGER.log(Level.WARNING, "Exception deleting account for " + connection, e);
            continue;
        }
    }
    if (attempts > maxAttempts) {
        LOGGER.log(Level.SEVERE, "Could not delete account for connection: " + connection);
    }
}
Also used : ServiceAdministrationManager(org.jivesoftware.smackx.admin.ServiceAdministrationManager) XMPPErrorException(org.jivesoftware.smack.XMPPException.XMPPErrorException) NotConnectedException(org.jivesoftware.smack.SmackException.NotConnectedException) SmackException(org.jivesoftware.smack.SmackException) IOException(java.io.IOException) NoResponseException(org.jivesoftware.smack.SmackException.NoResponseException) XMPPException(org.jivesoftware.smack.XMPPException) EntityBareJid(org.jxmpp.jid.EntityBareJid)

Example 8 with XMPPErrorException

use of org.jivesoftware.smack.XMPPException.XMPPErrorException in project Smack by igniterealtime.

the class FileTransferIntegrationTest method genericfileTransferTest.

private void genericfileTransferTest() throws Exception {
    final ResultSyncPoint<String, Exception> resultSyncPoint = new ResultSyncPoint<>();
    final FileTransferListener receiveListener = new FileTransferListener() {

        @Override
        public void fileTransferRequest(FileTransferRequest request) {
            byte[] dataReceived = null;
            IncomingFileTransfer ift = request.accept();
            try {
                InputStream is = ift.recieveFile();
                ByteArrayOutputStream os = new ByteArrayOutputStream();
                int nRead;
                byte[] buf = new byte[1024];
                while ((nRead = is.read(buf, 0, buf.length)) != -1) {
                    os.write(buf, 0, nRead);
                }
                os.flush();
                dataReceived = os.toByteArray();
                if (Arrays.equals(dataToSend, dataReceived)) {
                    resultSyncPoint.signal("Received data matches send data. \\o/");
                } else {
                    resultSyncPoint.signal(new Exception("Received data does not match"));
                }
            } catch (SmackException | IOException | XMPPErrorException | InterruptedException e) {
                resultSyncPoint.signal(e);
            }
        }
    };
    ftManagerTwo.addFileTransferListener(receiveListener);
    OutgoingFileTransfer oft = ftManagerOne.createOutgoingFileTransfer(conTwo.getUser());
    oft.sendStream(new ByteArrayInputStream(dataToSend), "hello.txt", dataToSend.length, "A greeting");
    int duration = 0;
    while (!oft.isDone()) {
        switch(oft.getStatus()) {
            case error:
                throw new Exception("Filetransfer error: " + oft.getError());
            default:
                LOGGER.info("Filetransfer status: " + oft.getStatus() + ". Progress: " + oft.getProgress());
                break;
        }
        Thread.sleep(1000);
        if (++duration > MAX_FT_DURATION) {
            throw new Exception("Max duration reached");
        }
    }
    resultSyncPoint.waitForResult(MAX_FT_DURATION * 1000);
    ftManagerTwo.removeFileTransferListener(receiveListener);
}
Also used : XMPPErrorException(org.jivesoftware.smack.XMPPException.XMPPErrorException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) SmackException(org.jivesoftware.smack.SmackException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) SmackException(org.jivesoftware.smack.SmackException) IOException(java.io.IOException) XMPPErrorException(org.jivesoftware.smack.XMPPException.XMPPErrorException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ResultSyncPoint(org.igniterealtime.smack.inttest.util.ResultSyncPoint) ByteArrayInputStream(java.io.ByteArrayInputStream) ResultSyncPoint(org.igniterealtime.smack.inttest.util.ResultSyncPoint)

Example 9 with XMPPErrorException

use of org.jivesoftware.smack.XMPPException.XMPPErrorException in project Smack by igniterealtime.

the class Socks5ByteStreamManagerTest method shouldFailIfTargetDoesNotAcceptSocks5Bytestream.

/**
     * Invoking {@link Socks5BytestreamManager#establishSession(org.jxmpp.jid.Jid, String)} should fail if the
     * target does not accept a SOCKS5 Bytestream. See <a
     * href="http://xmpp.org/extensions/xep-0065.html#usecase-alternate">XEP-0065 Section 5.2 A2</a>
     */
@Test
public void shouldFailIfTargetDoesNotAcceptSocks5Bytestream() {
    // disable clients local SOCKS5 proxy
    Socks5Proxy.setLocalSocks5ProxyEnabled(false);
    // get Socks5ByteStreamManager for connection
    Socks5BytestreamManager byteStreamManager = Socks5BytestreamManager.getBytestreamManager(connection);
    /**
         * create responses in the order they should be queried specified by the XEP-0065
         * specification
         */
    // build discover info that supports the SOCKS5 feature
    DiscoverInfo discoverInfo = Socks5PacketUtils.createDiscoverInfo(targetJID, initiatorJID);
    discoverInfo.addFeature(Bytestream.NAMESPACE);
    // return that SOCKS5 is supported if target is queried
    protocol.addResponse(discoverInfo, Verification.correspondingSenderReceiver, Verification.requestTypeGET);
    // build discover items containing a proxy item
    DiscoverItems discoverItems = Socks5PacketUtils.createDiscoverItems(xmppServer, initiatorJID);
    Item item = new Item(proxyJID);
    discoverItems.addItem(item);
    // return the proxy item if XMPP server is queried
    protocol.addResponse(discoverItems, Verification.correspondingSenderReceiver, Verification.requestTypeGET);
    // build discover info for proxy containing information about being a SOCKS5 proxy
    DiscoverInfo proxyInfo = Socks5PacketUtils.createDiscoverInfo(proxyJID, initiatorJID);
    Identity identity = new Identity("proxy", proxyJID.toString(), "bytestreams");
    proxyInfo.addIdentity(identity);
    // return the socks5 bytestream proxy identity if proxy is queried
    protocol.addResponse(proxyInfo, Verification.correspondingSenderReceiver, Verification.requestTypeGET);
    // build a socks5 stream host info containing the address and the port of the
    // proxy
    Bytestream streamHostInfo = Socks5PacketUtils.createBytestreamResponse(proxyJID, initiatorJID);
    streamHostInfo.addStreamHost(proxyJID, proxyAddress, 7778);
    // return stream host info if it is queried
    protocol.addResponse(streamHostInfo, Verification.correspondingSenderReceiver, Verification.requestTypeGET);
    // build error packet to reject SOCKS5 Bytestream
    XMPPError.Builder builder = XMPPError.getBuilder(XMPPError.Condition.not_acceptable);
    IQ rejectPacket = new ErrorIQ(builder);
    rejectPacket.setFrom(targetJID);
    rejectPacket.setTo(initiatorJID);
    // return error packet as response to the bytestream initiation
    protocol.addResponse(rejectPacket, Verification.correspondingSenderReceiver, Verification.requestTypeSET);
    try {
        // start SOCKS5 Bytestream
        byteStreamManager.establishSession(targetJID, sessionID);
        fail("exception should be thrown");
    } catch (XMPPErrorException e) {
        protocol.verifyAll();
        assertEquals(rejectPacket.getError(), e.getXMPPError());
    } catch (Exception e) {
        fail(e.getMessage());
    }
}
Also used : DiscoverInfo(org.jivesoftware.smackx.disco.packet.DiscoverInfo) Item(org.jivesoftware.smackx.disco.packet.DiscoverItems.Item) ErrorIQ(org.jivesoftware.smack.packet.ErrorIQ) Bytestream(org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream) XMPPErrorException(org.jivesoftware.smack.XMPPException.XMPPErrorException) ErrorIQ(org.jivesoftware.smack.packet.ErrorIQ) IQ(org.jivesoftware.smack.packet.IQ) DiscoverItems(org.jivesoftware.smackx.disco.packet.DiscoverItems) XMPPError(org.jivesoftware.smack.packet.XMPPError) Identity(org.jivesoftware.smackx.disco.packet.DiscoverInfo.Identity) SmackException(org.jivesoftware.smack.SmackException) FeatureNotSupportedException(org.jivesoftware.smack.SmackException.FeatureNotSupportedException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) XMPPErrorException(org.jivesoftware.smack.XMPPException.XMPPErrorException) XmppStringprepException(org.jxmpp.stringprep.XmppStringprepException) XMPPException(org.jivesoftware.smack.XMPPException) Test(org.junit.Test)

Example 10 with XMPPErrorException

use of org.jivesoftware.smack.XMPPException.XMPPErrorException in project Smack by igniterealtime.

the class Socks5ByteStreamRequestTest method shouldFailIfRequestHasNoStreamHosts.

/**
     * Accepting a SOCKS5 Bytestream request should fail if the request doesn't contain any Socks5
     * proxies.
     * 
     * @throws Exception should not happen
     */
@Test
public void shouldFailIfRequestHasNoStreamHosts() throws Exception {
    try {
        // build SOCKS5 Bytestream initialization request with no SOCKS5 proxies
        Bytestream bytestreamInitialization = Socks5PacketUtils.createBytestreamInitiation(initiatorJID, targetJID, sessionID);
        // get SOCKS5 Bytestream manager for connection
        Socks5BytestreamManager byteStreamManager = Socks5BytestreamManager.getBytestreamManager(connection);
        // build SOCKS5 Bytestream request with the bytestream initialization
        Socks5BytestreamRequest byteStreamRequest = new Socks5BytestreamRequest(byteStreamManager, bytestreamInitialization);
        // accept the stream (this is the call that is tested here)
        byteStreamRequest.accept();
        fail("exception should be thrown");
    } catch (XMPPErrorException e) {
        assertTrue(e.getXMPPError().getDescriptiveText("en").contains("Could not establish socket with any provided host"));
    }
    // verify targets response
    assertEquals(1, protocol.getRequests().size());
    Stanza targetResponse = protocol.getRequests().remove(0);
    assertTrue(IQ.class.isInstance(targetResponse));
    assertEquals(initiatorJID, targetResponse.getTo());
    assertEquals(IQ.Type.error, ((IQ) targetResponse).getType());
    assertEquals(XMPPError.Condition.item_not_found, ((IQ) targetResponse).getError().getCondition());
}
Also used : Bytestream(org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream) XMPPErrorException(org.jivesoftware.smack.XMPPException.XMPPErrorException) Stanza(org.jivesoftware.smack.packet.Stanza) IQ(org.jivesoftware.smack.packet.IQ) Test(org.junit.Test)

Aggregations

XMPPErrorException (org.jivesoftware.smack.XMPPException.XMPPErrorException)27 NoResponseException (org.jivesoftware.smack.SmackException.NoResponseException)10 IQ (org.jivesoftware.smack.packet.IQ)9 Test (org.junit.Test)9 IOException (java.io.IOException)7 SmackException (org.jivesoftware.smack.SmackException)7 NotConnectedException (org.jivesoftware.smack.SmackException.NotConnectedException)7 Stanza (org.jivesoftware.smack.packet.Stanza)6 Bytestream (org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream)6 XMPPError (org.jivesoftware.smack.packet.XMPPError)5 DiscoverInfo (org.jivesoftware.smackx.disco.packet.DiscoverInfo)5 InputStream (java.io.InputStream)4 XMPPException (org.jivesoftware.smack.XMPPException)4 XMPPConnection (org.jivesoftware.smack.XMPPConnection)3 DiscoverItems (org.jivesoftware.smackx.disco.packet.DiscoverItems)3 FileNotFoundException (java.io.FileNotFoundException)2 ArrayList (java.util.ArrayList)2 TimeoutException (java.util.concurrent.TimeoutException)2 AbstractSmackIntegrationTest (org.igniterealtime.smack.inttest.AbstractSmackIntegrationTest)2 SmackIntegrationTest (org.igniterealtime.smack.inttest.SmackIntegrationTest)2