Package org.jivesoftware.smack

Examples of org.jivesoftware.smack.Connection


     */
    public static Connection createMockedConnection(final Protocol protocol,
                    String initiatorJID, String xmppServer) {

        // mock XMPP connection
        Connection connection = mock(Connection.class);
        when(connection.getUser()).thenReturn(initiatorJID);
        when(connection.getServiceName()).thenReturn(xmppServer);

        // mock packet collector
        PacketCollector collector = mock(PacketCollector.class);
        when(connection.createPacketCollector(isA(PacketFilter.class))).thenReturn(
                        collector);
        Answer<Object> addIncoming = new Answer<Object>() {

            public Object answer(InvocationOnMock invocation) throws Throwable {
                protocol.getRequests().add((Packet) invocation.getArguments()[0]);
View Full Code Here


     * Bytestream requests are registered.
     *
     * @throws XMPPException should not happen
     */
    public void testRespondWithErrorOnInBandBytestreamRequest() throws XMPPException {
        Connection targetConnection = getConnection(0);

        Connection initiatorConnection = getConnection(1);

        Open open = new Open("sessionID", 1024);
        open.setFrom(initiatorConnection.getUser());
        open.setTo(targetConnection.getUser());

        PacketCollector collector = initiatorConnection.createPacketCollector(new PacketIDFilter(
                        open.getPacketID()));
        initiatorConnection.sendPacket(open);
        Packet result = collector.nextResult();

        assertNotNull(result.getError());
        assertEquals(XMPPError.Condition.no_acceptable.toString(), result.getError().getCondition());

View Full Code Here

     *
     * @throws Exception should not happen
     */
    public void testInBandBytestreamWithIQStanzas() throws Exception {

        Connection initiatorConnection = getConnection(0);
        Connection targetConnection = getConnection(1);

        // test data
        Random rand = new Random();
        final byte[] data = new byte[dataSize];
        rand.nextBytes(data);
        final SynchronousQueue<byte[]> queue = new SynchronousQueue<byte[]>();

        InBandBytestreamManager targetByteStreamManager = InBandBytestreamManager.getByteStreamManager(targetConnection);

        InBandBytestreamListener incomingByteStreamListener = new InBandBytestreamListener() {

            public void incomingBytestreamRequest(InBandBytestreamRequest request) {
                InputStream inputStream;
                try {
                    inputStream = request.accept().getInputStream();
                    byte[] receivedData = new byte[dataSize];
                    int totalRead = 0;
                    while (totalRead < dataSize) {
                        int read = inputStream.read(receivedData, totalRead, dataSize - totalRead);
                        totalRead += read;
                    }
                    queue.put(receivedData);
                }
                catch (Exception e) {
                    fail(e.getMessage());
                }
            }

        };
        targetByteStreamManager.addIncomingBytestreamListener(incomingByteStreamListener);

        InBandBytestreamManager initiatorByteStreamManager = InBandBytestreamManager.getByteStreamManager(initiatorConnection);

        OutputStream outputStream = initiatorByteStreamManager.establishSession(
                        targetConnection.getUser()).getOutputStream();

        // verify stream
        outputStream.write(data);
        outputStream.flush();
        outputStream.close();
View Full Code Here

     *
     * @throws Exception should not happen
     */
    public void testInBandBytestreamWithMessageStanzas() throws Exception {

        Connection initiatorConnection = getConnection(0);
        Connection targetConnection = getConnection(1);

        // test data
        Random rand = new Random();
        final byte[] data = new byte[dataSize];
        rand.nextBytes(data);
        final SynchronousQueue<byte[]> queue = new SynchronousQueue<byte[]>();

        InBandBytestreamManager targetByteStreamManager = InBandBytestreamManager.getByteStreamManager(targetConnection);

        InBandBytestreamListener incomingByteStreamListener = new InBandBytestreamListener() {

            public void incomingBytestreamRequest(InBandBytestreamRequest request) {
                InputStream inputStream;
                try {
                    inputStream = request.accept().getInputStream();
                    byte[] receivedData = new byte[dataSize];
                    int totalRead = 0;
                    while (totalRead < dataSize) {
                        int read = inputStream.read(receivedData, totalRead, dataSize - totalRead);
                        totalRead += read;
                    }
                    queue.put(receivedData);
                }
                catch (Exception e) {
                    fail(e.getMessage());
                }
            }

        };
        targetByteStreamManager.addIncomingBytestreamListener(incomingByteStreamListener);

        InBandBytestreamManager initiatorByteStreamManager = InBandBytestreamManager.getByteStreamManager(initiatorConnection);
        initiatorByteStreamManager.setStanza(StanzaType.MESSAGE);

        OutputStream outputStream = initiatorByteStreamManager.establishSession(
                        targetConnection.getUser()).getOutputStream();

        // verify stream
        outputStream.write(data);
        outputStream.flush();
        outputStream.close();
View Full Code Here

     *
     * @throws Exception should not happen
     */
    public void testBiDirectionalInBandBytestream() throws Exception {

        Connection initiatorConnection = getConnection(0);

        Connection targetConnection = getConnection(1);

        // test data
        Random rand = new Random();
        final byte[] data = new byte[dataSize];
        rand.nextBytes(data);

        final SynchronousQueue<byte[]> queue = new SynchronousQueue<byte[]>();

        InBandBytestreamManager targetByteStreamManager = InBandBytestreamManager.getByteStreamManager(targetConnection);

        InBandBytestreamListener incomingByteStreamListener = new InBandBytestreamListener() {

            public void incomingBytestreamRequest(InBandBytestreamRequest request) {
                try {
                    InBandBytestreamSession session = request.accept();
                    OutputStream outputStream = session.getOutputStream();
                    outputStream.write(data);
                    outputStream.flush();
                    InputStream inputStream = session.getInputStream();
                    byte[] receivedData = new byte[dataSize];
                    int totalRead = 0;
                    while (totalRead < dataSize) {
                        int read = inputStream.read(receivedData, totalRead, dataSize - totalRead);
                        totalRead += read;
                    }
                    queue.put(receivedData);
                }
                catch (Exception e) {
                    fail(e.getMessage());
                }
            }

        };
        targetByteStreamManager.addIncomingBytestreamListener(incomingByteStreamListener);

        InBandBytestreamManager initiatorByteStreamManager = InBandBytestreamManager.getByteStreamManager(initiatorConnection);

        InBandBytestreamSession session = initiatorByteStreamManager.establishSession(targetConnection.getUser());

        // verify stream
        byte[] receivedData = new byte[dataSize];
        InputStream inputStream = session.getInputStream();
        int totalRead = 0;
View Full Code Here

     *
     * @return the session to send/receive data
     * @throws XMPPException if stream is invalid.
     */
    public InBandBytestreamSession accept() throws XMPPException {
        Connection connection = this.manager.getConnection();

        // create In-Band Bytestream session and store it
        InBandBytestreamSession ibbSession = new InBandBytestreamSession(connection,
                        this.byteStreamRequest, this.byteStreamRequest.getFrom());
        this.manager.getSessions().put(this.byteStreamRequest.getSessionID(), ibbSession);

        // acknowledge request
        IQ resultIQ = IQ.createResultIQ(this.byteStreamRequest);
        connection.sendPacket(resultIQ);

        return ibbSession;
    }
View Full Code Here

    public static String getNickname(SarosNet sarosNet, JID jid) {

        if (sarosNet == null)
            return null;

        Connection connection = sarosNet.getConnection();
        if (connection == null)
            return null;

        Roster roster = connection.getRoster();
        if (roster == null)
            return null;

        RosterEntry entry = roster.getEntry(jid.getBase());
        if (entry == null)
View Full Code Here

            monitor = SubMonitor.convert(monitor);

        monitor.beginTask("Registering account...", 3);

        try {
            Connection connection = new XMPPConnection(server);
            monitor.worked(1);

            connection.connect();
            monitor.worked(1);

            String errorMessage = isAccountCreationPossible(connection,
                username);
            if (errorMessage != null)
                throw new XMPPException(errorMessage);

            monitor.worked(1);

            AccountManager manager = connection.getAccountManager();
            manager.createAccount(username, password);
            monitor.worked(1);

            connection.disconnect();
        } catch (XMPPException e) {
            String message = e.getMessage();
            XMPPError error = e.getXMPPError();
            if (error != null) {
                message = error.getMessage();
View Full Code Here

     */
    @Test
    public void shouldHaveOneManagerForEveryConnection() {

        // mock two connections
        Connection connection1 = mock(Connection.class);
        Connection connection2 = mock(Connection.class);

        /*
         * create service discovery managers for the connections because the
         * ConnectionCreationListener is not called when creating mocked connections
         */
 
View Full Code Here

     */
    @Test
    public void shouldReplyErrorIfSessionIsUnknown() throws Exception {

        // mock connection
        Connection connection = mock(Connection.class);

        // initialize InBandBytestreamManager to get the DataListener
        InBandBytestreamManager byteStreamManager = InBandBytestreamManager.getByteStreamManager(connection);

        // get the DataListener from InBandByteStreamManager
View Full Code Here

TOP

Related Classes of org.jivesoftware.smack.Connection

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.