Example #1
0
  public boolean openConnection() {
    new AndroidSmackInitializer().initialize();
    try {
      if (null == connection || !connection.isAuthenticated()) {
        // XMPPConnection.DEBUG_ENABLED = true;// ����DEBUGģʽ
        // ��������
        XMPPTCPConnectionConfiguration.Builder builder = XMPPTCPConnectionConfiguration.builder();
        builder.setHost(SERVER_HOST);
        builder.setServiceName(SERVER_NAME);
        builder.setPort(SERVER_PORT);
        builder.setSendPresence(true);
        builder.setCompressionEnabled(false);
        builder.setSecurityMode(SecurityMode.disabled);

        XMPPTCPConnectionConfiguration config = builder.build();
        connection = new XMPPTCPConnection(config);
        try {
          connection.connect();
        } catch (SmackException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        } // ���ӵ�������
        // ���ø���Provider��������ã�����޷��������
        configureConnection(new ProviderManager());
        return true;
      }
    } catch (XMPPException xe) {
      xe.printStackTrace();
      connection = null;
    }
    return false;
  }
  /**
   * If the target is not connected to the local SOCKS5 proxy an exception should be thrown.
   *
   * @throws Exception should not happen
   */
  @Test
  public void shouldFailIfTargetIsNotConnectedToLocalSocks5Proxy() throws Exception {

    // start a local SOCKS5 proxy
    Socks5Proxy.setLocalSocks5ProxyPort(proxyPort);
    Socks5Proxy socks5Proxy = Socks5Proxy.getSocks5Proxy();
    socks5Proxy.start();

    // build stream host information for local SOCKS5 proxy
    StreamHost streamHost =
        new StreamHost(connection.getUser(), socks5Proxy.getLocalAddresses().get(0));
    streamHost.setPort(socks5Proxy.getPort());

    // create digest to get the socket opened by target
    String digest = Socks5Utils.createDigest(sessionID, initiatorJID, targetJID);

    Socks5ClientForInitiator socks5Client =
        new Socks5ClientForInitiator(streamHost, digest, connection, sessionID, targetJID);

    try {
      socks5Client.getSocket(10000);

      fail("exception should be thrown");
    } catch (SmackException e) {
      assertTrue(e.getMessage().contains("target is not connected to SOCKS5 proxy"));
      protocol.verifyAll(); // assert no XMPP messages were sent
    }

    socks5Proxy.stop();
  }
Example #3
0
  public boolean login(String account, String password) {
    boolean succ = false;
    try {
      if (getConnection() == null) return false;

      getConnection().login(account, password);
      succ = true;

      // ����ھQ��B
      //			Presence presence = new Presence(Presence.Type.available);
      //			getConnection().sendPacket(presence);
      // ����B�ӱO
      //			connectionListener = new TaxiConnectionListener();
      //			getConnection().addConnectionListener(connectionListener);

    } catch (SmackException e) {
      succ = false;
      e.printStackTrace();
    } catch (IOException e) {
      succ = false;
      e.printStackTrace();
    } catch (XMPPException xe) {
      succ = false;
      xe.printStackTrace();
    }
    return succ;
  }
Example #4
0
  public static void main(String[] args) {
    SmackConfiguration.DEBUG = true;
    SmackConfiguration.setDefaultPacketReplyTimeout(10 * 1000);

    XMPPTCPConnectionConfiguration.Builder configBuilder = XMPPTCPConnectionConfiguration.builder();
    configBuilder.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
    configBuilder.setUsernameAndPassword("registeruser2", "123456");

    configBuilder.setServiceName("ejabberddemo.com");
    configBuilder.setDebuggerEnabled(true).build();

    // 必须在用户状态为离线状态
    configBuilder.setSendPresence(false);

    AbstractXMPPConnection connection = new XMPPTCPConnection(configBuilder.build());
    try {
      connection.connect();
    } catch (SmackException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } catch (XMPPException e) {
      e.printStackTrace();
    }

    // FIXME
    OfflineMessageManager offlineManager = new OfflineMessageManager(connection);

    try {
      connection.login();
    } catch (XMPPException e) {
      e.printStackTrace();
    } catch (SmackException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }

    try {
      System.out.println(offlineManager.supportsFlexibleRetrieval());
      System.out.println(offlineManager.getMessageCount());
    } catch (SmackException.NoResponseException e) {
      e.printStackTrace();
    } catch (XMPPException.XMPPErrorException e) {
      e.printStackTrace();
    } catch (SmackException.NotConnectedException e) {
      e.printStackTrace();
    }
    // 上线
    Presence presence = new Presence(Presence.Type.available);
    try {
      connection.sendPacket(presence);
    } catch (SmackException.NotConnectedException e) {
      e.printStackTrace();
    }
  }
Example #5
0
  @Override
  public void run() {
    super.run();
    try {
      SASLAuthentication.unBlacklistSASLMechanism("PLAIN");
      SASLAuthentication.blacklistSASLMechanism("DIGEST-MD5");
      if (!getUserName().equals(""))
        getXmpptcpConnection().login(getUserName(), getPassword(), getResource());
      else getXmpptcpConnection().loginAnonymously();

      if (getAccountListener() != null) {
        getAccountListener().onSucess();
      }
    } catch (XMPPException e) {
      e.printStackTrace();
      if (getAccountListener() != null) getAccountListener().onError();
    } catch (SmackException e) {
      e.printStackTrace();
      if (getAccountListener() != null) getAccountListener().onError();
    } catch (IOException e) {
      e.printStackTrace();
      if (getAccountListener() != null) getAccountListener().onError();
    }
  }
Example #6
0
 /** 建立XMPP连接 */
 public boolean connect() throws RemoteException {
   // 已经连接
   if (connection.isConnected()) {
     return true;
   } else {
     try {
       // 开始连接
       connection.connect();
       if (connectionListener == null) {
         // 添加一个连接监听器
         connectionListener = new IMClientConnectListener();
       }
       connection.addConnectionListener(connectionListener);
       return true;
     } catch (SmackException e) {
       e.printStackTrace();
     } catch (IOException e) {
       e.printStackTrace();
     } catch (XMPPException e) {
       e.printStackTrace();
     }
   }
   return false;
 }
Example #7
0
 /** 登陆XMPP服务器 */
 public boolean login() throws RemoteException {
   // 未建立XMPP连接
   if (!connection.isConnected()) {
     return false;
   }
   // 应经登陆过
   if (connection.isAuthenticated()) {
     return true;
   } else {
     // 开始登陆
     try {
       connection.login(account, password, imService.getString(R.string.app_name));
       if (messageListener == null) {
         messageListener = new MessageListener();
       }
       // 添加消息监听器
       connection.addPacketListener(messageListener, new PacketTypeFilter(Message.class));
       Roster roster = connection.getRoster();
       if (rosterListener == null) {
         rosterListener = new IMClientRosterListener();
       }
       // 添加花名册监听器
       roster.addRosterListener(rosterListener);
       // 获取花名册
       if (roster != null && roster.getEntries().size() > 0) {
         Uri uri = null;
         for (RosterEntry entry : roster.getEntries()) {
           // 获取联系人名片信息
           VCard vCard = new VCard();
           vCard.load(connection, entry.getUser());
           // 用户名称
           String userName = StringUtils.parseName(entry.getUser());
           // 用户备注
           String remarks = entry.getName();
           // 通讯录的名称
           String name = "";
           // 名称与备注判断
           if (userName.equals(remarks) && vCard != null) {
             // 使用联系人的昵称
             name = vCard.getNickName();
           } else {
             // 使用备注
             name = remarks;
           }
           if (vCard != null) {
             IM.saveAvatar(vCard.getAvatar(), StringUtils.parseName(entry.getUser()));
           }
           ContentValues values = new ContentValues();
           values.put(ContactsProvider.ContactColumns.ACCOUNT, entry.getUser());
           values.put(ContactsProvider.ContactColumns.NAME, name);
           String sortStr = PinYin.getPinYin(name);
           values.put(ContactsProvider.ContactColumns.SORT, sortStr);
           values.put(
               ContactsProvider.ContactColumns.SECTION,
               sortStr.substring(0, 1).toUpperCase(Locale.ENGLISH));
           // 储存联系人
           if (imService
                   .getContentResolver()
                   .update(
                       ContactsProvider.CONTACT_URI,
                       values,
                       ContactsProvider.ContactColumns.ACCOUNT + " = ?",
                       new String[] {entry.getUser()})
               == 0) {
             uri = imService.getContentResolver().insert(ContactsProvider.CONTACT_URI, values);
           }
         }
         // 发生改变,通知刷新
         if (uri != null) {
           imService.getContentResolver().notifyChange(uri, null);
         }
       }
     } catch (XMPPException e) {
       e.printStackTrace();
     } catch (SmackException e) {
       e.printStackTrace();
     } catch (IOException e) {
       e.printStackTrace();
     }
     return true;
   }
 }
Example #8
0
  public static void main(String[] args) {
    String username = "******",
        password = "******",
        resource = "viewt",
        serviceName = "openfire-test",
        host = "192.168.1.117";
    host = "127.0.0.1";
    XMPPTCPConnectionConfiguration config =
        XMPPTCPConnectionConfiguration.builder()
            //				  .setUsernameAndPassword(username, password)
            .setServiceName(serviceName)
            .setHost(host)
            .setPort(5222)
            //				  .setDebuggerEnabled(true)
            .setSecurityMode(SecurityMode.disabled)
            .build();
    //		AbstractXMPPConnection conn2 = new XMPPTCPConnection(username,password,"127.0.0.1");
    AbstractXMPPConnection conn2 = new XMPPTCPConnection(config);
    try {
      //			SASLAuthentication.registerSASLMechanism(new SASLPlainMechanism());
      conn2.connect();
      //			conn2.login();
      conn2.login(username, password, resource);

      final Roster roster = Roster.getInstanceFor(conn2);

      Set<RosterEntry> set = roster.getEntries();
      Iterator<RosterEntry> iterator = set.iterator();
      while (iterator.hasNext()) {
        System.out.println(iterator.next().getName());
      }
      //			roster.createGroup("family");
      //			roster.createEntry("elijah@openfire-test", "elijah", new String[]{"family"});
      roster.setSubscriptionMode(Roster.SubscriptionMode.reject_all);
      //			roster.
      roster.addRosterListener(
          new RosterListener() {

            public void presenceChanged(Presence presence) {
              System.out.println("Presence changed: " + presence.getFrom() + " " + presence);
            }

            public void entriesUpdated(Collection<String> addresses) {
              System.out.println("Presence changed: entriesUpdated");
            }

            public void entriesDeleted(Collection<String> addresses) {
              System.out.println("Presence changed: entriesDeleted");
            }

            public void entriesAdded(Collection<String> addresses) {
              System.out.println("Presence changed: " + addresses.iterator().next());
              // Roster.SubscriptionMode.reject_all;
            }
          });
    } catch (SmackException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } catch (XMPPException e) {
      e.printStackTrace();
    }

    Scanner sc = new Scanner(System.in);
    String str = null;
    while (!"bye".equals(str = sc.next())) {
      System.out.println(str);
    }
    sc.close();
    conn2.disconnect();
  }