Java 类org.jivesoftware.smack.XMPPConnection 实例源码

项目:jmeter-bzm-plugins    文件:JMeterXMPPConnectionGuiTest.java   
public void displayGUI() throws Throwable {
    setSearchPaths();

    if (!GraphicsEnvironment.getLocalGraphicsEnvironment().isHeadlessInstance()) {
        JMeterXMPPConnection te = new JMeterXMPPConnection();
        te.setFromMode(XMPPConnection.FromMode.OMITTED.toString());

        JMeterXMPPConnectionGui gui = new JMeterXMPPConnectionGui();
        gui.configure(te);
        JDialog dialog = new JDialog();
        dialog.add(gui);

        dialog.setPreferredSize(new Dimension(800, 600));
        dialog.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
        dialog.pack();
        dialog.setVisible(true);
        while (dialog.isVisible()) {
            Thread.sleep(1000);
        }
    }
}
项目:SmackStudy    文件:XMPPUtil.java   
/**
 * 查询用户
 * @param xmppConnection
 * @param userName
 * @return
 * @throws XMPPException
 */
public static List<HashMap<String, String>> searchUsers(XMPPConnection xmppConnection, String userName) {
    List<HashMap<String, String>> results = new ArrayList<HashMap<String, String>>();
    try {
        UserSearchManager usm = new UserSearchManager(xmppConnection);
        Form searchForm = usm.getSearchForm(xmppConnection.getServiceName());
        Form answerForm = searchForm.createAnswerForm();
        answerForm.setAnswer("userAccount", true);
        answerForm.setAnswer("userPhote", userName);
        ReportedData data = usm.getSearchResults(answerForm, "search" + xmppConnection.getServiceName());
        Iterator<ReportedData.Row> it = data.getRows();
        while (it.hasNext()) {
            HashMap<String, String> user = new HashMap<String, String>();
            ReportedData.Row row = it.next();
            user.put("userAccount", row.getValues("userAccount").next().toString());
            user.put("userPhote", row.getValues("userPhote").next().toString());
            results.add(user);
        }
    } catch (XMPPException e) {
        Log.e("searchUsers", e.getMessage());
        e.printStackTrace();
    }
    return results;
}
项目:SmackStudy    文件:XMPPUtil.java   
/**
 * 删除好友
 * @param xmppConnection
 * @param userName
 * @return
 */
public static boolean removeUser(XMPPConnection xmppConnection, String userName) {
    try {
        RosterEntry entry = null;
        if (userName.contains("@")) {
            entry = xmppConnection.getRoster().getEntry(
                    userName + "@" + xmppConnection.getServiceName());
        } else {
            entry = xmppConnection.getRoster().getEntry(
                    userName + "@" + xmppConnection.getServiceName());
        }
        if (entry == null) {
            entry = xmppConnection.getRoster().getEntry(userName);
        }
        xmppConnection.getRoster().removeEntry(entry);
        return true;
    } catch (Exception e) {
        Log.e("removeUser", e.getMessage());
        e.printStackTrace();
        return false;
    }
}
项目:SmackStudy    文件:XMPPUtil.java   
/**
 * 登录
 * @param userName
 * @param password
 * @return
 */
public static XMPPConnection login(String userName,String password){
    XMPPConnection xmppConnection = createXMPPConnection();
    if (xmppConnection != null) {
        try {
            if (!xmppConnection.isConnected()) {
                xmppConnection.connect();
            }
            xmppConnection.login(userName, password);
        } catch (XMPPException e) {
            e.printStackTrace();
            xmppConnection = null;
        }
    }
    return xmppConnection;
}
项目:SmackStudy    文件:XMPPUtil.java   
/**
 * 删除好友
 * @param xmppConnection
 * @param userName
 * @return
 */
public static boolean removeUser(XMPPConnection xmppConnection,String userName) {
    try {
        RosterEntry entry = null;
        if (userName.contains("@")){
            entry = xmppConnection.getRoster().getEntry(
                    userName + "@" + xmppConnection.getServiceName());
        }else{
            entry = xmppConnection.getRoster().getEntry(
                    userName + "@" + xmppConnection.getServiceName());
        }
        if (entry == null){
            entry = xmppConnection.getRoster().getEntry(userName);
        }
        xmppConnection.getRoster().removeEntry(entry);
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}
项目:SmackStudy    文件:XMPPUtil.java   
/**
 * 查询用户
 * @param xmppConnection
 * @param userName
 * @return
 * @throws XMPPException
 */
public static List<HashMap<String, String>> searchUsers(XMPPConnection xmppConnection,String userName) {
    List<HashMap<String, String>> results = new ArrayList<HashMap<String, String>>();
    try {
        UserSearchManager usm = new UserSearchManager(xmppConnection);
        Form searchForm = usm.getSearchForm(xmppConnection.getServiceName());
        Form answerForm = searchForm.createAnswerForm();
        answerForm.setAnswer("userAccount", true);
        answerForm.setAnswer("userPhote", userName);
        ReportedData data = usm.getSearchResults(answerForm, "search" + xmppConnection.getServiceName());
        Iterator<ReportedData.Row> it = data.getRows();
        while (it.hasNext()) {
            HashMap<String, String> user = new HashMap<String, String>();
            ReportedData.Row row = it.next();
            user.put("userAccount", row.getValues("userAccount").next().toString());
            user.put("userPhote", row.getValues("userPhote").next().toString());
            results.add(user);
        }
    } catch (XMPPException e) {
        e.printStackTrace();
    }
    return results;
}
项目:SmackStudy    文件:XMPPUtil.java   
/**
 * 加入聊天室
 * @param xmppConnection
 * @param roomName
 * @param password
 * @return
 */
public static  MultiUserChat joinMultiUserChat(XMPPConnection xmppConnection,String roomName,String password,PacketListener packetListener){
    try {
        // 使用XMPPConnection创建一个MultiUserChat窗口
        MultiUserChat muc = new MultiUserChat(xmppConnection, roomName+ "@conference." + xmppConnection.getServiceName());
        // 聊天室服务将会决定要接受的历史记录数量
        DiscussionHistory history = new DiscussionHistory();
        history.setMaxChars(0);
        // 用户加入聊天室
        muc.join(xmppConnection.getUser(), password, history, SmackConfiguration.getPacketReplyTimeout());
        if(packetListener!=null){
            muc.addMessageListener(packetListener);
        }
        return muc;
    } catch (XMPPException e) {
        e.printStackTrace();
        return null;
    }
}
项目:SmackStudy    文件:XMPPUtil.java   
/**
 * 工厂模式获取连接对象 还没有连接服务器
 * @param connectionTimeOut   连接超时的时间
 * @param reconnectionAllowed 是否准许重连接
 * @param isPresence          是否在线
 * @param debugEnable         是否调试
 * @param securityMode        安全模式
 * @param connectionListener  连接监听器
 * @return
 */
public static XMPPConnection createXMPPConnection(int connectionTimeOut, boolean reconnectionAllowed, boolean isPresence, boolean debugEnable,
                                                  ConnectionConfiguration.SecurityMode securityMode, ConnectionListener connectionListener) {
    //设置是否开启DEBUG模式
    XMPPConnection.DEBUG_ENABLED = debugEnable;
    //设置连接地址、端口
    ConnectionConfiguration configuration = new ConnectionConfiguration(SERVERADDRESS, PORT);
    //设置服务器名称
    configuration.setServiceName(SERVERNAME);
    //设置是否需要SAS验证
    configuration.setSASLAuthenticationEnabled(false);
    //设置安全类型
    configuration.setSecurityMode(securityMode);
    //设置用户状态
    configuration.setSendPresence(isPresence);
    //设置是否准许重连接
    configuration.setReconnectionAllowed(reconnectionAllowed);
    //实例化连接对象
    XMPPConnection xmppConnection = new XMPPConnection(configuration);
    //添加连接监听器
    if (connectionListener != null) {
        xmppConnection.addConnectionListener(connectionListener);
    }
    return xmppConnection;
}
项目:SmackStudy    文件:XMPPUtil.java   
/**
 * 注册用户
 * @param xmppConnection
 * @param userName
 * @param password
 * @param attributes
 * @return
 */
public static boolean regist(XMPPConnection xmppConnection, String userName, String password, Map<String, String> attributes) {
    AccountManager accountManager = xmppConnection.getAccountManager();
    try {
        if (attributes != null) {
            accountManager.createAccount(userName, password, attributes);
        } else {
            accountManager.createAccount(userName, password);
        }
        return true;
    } catch (XMPPException e) {
        Log.e("regist", e.getMessage());
        e.printStackTrace();
        return false;
    }
}
项目:jmeter-bzm-plugins    文件:JMeterXMPPConnectionBase.java   
public XMPPConnection.FromMode getFromMode() {
    String str = getPropertyAsString(FROM_MODE, XMPPConnection.FromMode.USER.toString());
    if (str.equals(XMPPConnection.FromMode.USER.toString())) {
        return XMPPConnection.FromMode.USER;
    } else if (str.equals(XMPPConnection.FromMode.UNCHANGED.toString())) {
        return XMPPConnection.FromMode.UNCHANGED;
    } else if (str.equals(XMPPConnection.FromMode.OMITTED.toString())) {
        return XMPPConnection.FromMode.OMITTED;
    } else {
        throw new IllegalArgumentException("Unhandled value for fromMode: " + str);
    }
}
项目:BizareChat    文件:ReadReceiptManager.java   
private ReadReceiptManager(XMPPConnection connection) {
    super(connection);
    ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(connection);
    sdm.addFeature(ReadReceipt.NAMESPACE);
    connection.addAsyncStanzaListener(packet -> {
        ReadReceipt receipt = ReadReceipt.from((Message) packet);
        for (ReceiptReceivedListener listener : receiptReceivedListeners) {
            listener.onReceiptReceived(packet.getFrom(), packet.getTo(), receipt.getId(), packet);
        }
    }, MESSAGES_WITH_READ_RECEIPT);
}
项目:jmeter-bzm-plugins    文件:JMeterXMPPConnectionGui.java   
private void initFields() {
    address.setText("localhost");
    port.setText("5222");
    serviceName.setText("localhost");
    timeout.setText("1000");
    connectionClass.setSelectedItem(JMeterXMPPConnectionBase.Type.TCP);
    fromMode.setSelectedItem(XMPPConnection.FromMode.USER);
}
项目:jmeter-bzm-plugins    文件:Login.java   
@Override
public SampleResult perform(JMeterXMPPSampler sampler, SampleResult res) throws Exception {
    XMPPConnection conn = sampler.getXMPPConnection();
    String loginStr = sampler.getPropertyAsString(LOGIN);
    String pwdStr = sampler.getPropertyAsString(PASSWORD);
    String resStr = sampler.getPropertyAsString(RESOURCE);
    res.setSamplerData("Username: " + loginStr + "\nPassword: " + pwdStr + "\nResource: " + resStr);
    AbstractXMPPConnection absConn = (AbstractXMPPConnection) conn;
    if (loginStr.isEmpty()) {
        absConn.loginAnonymously();
    } else {
        absConn.login(loginStr, pwdStr, resStr);
    }
    return res;
}
项目:SmackStudy    文件:XMPPUtilTest.java   
@Test
public void testGetHostRooms() {
    XMPPConnection xmppConnection = getFullXMPPConnection();
    assertTrue(XMPPUtil.login(xmppConnection, "ranrandemo", "admin123"));
    assertNotNull(XMPPUtil.getHostRooms(xmppConnection));
    XMPPUtil.relaseXMPPConnection(xmppConnection);
}
项目:SmackStudy    文件:XMPPUtilTest.java   
@Test
public void testCreateRoom() {
    XMPPConnection xmppConnection = getFullXMPPConnection();
    assertTrue(XMPPUtil.login(xmppConnection, "ranrandemo", "admin123"));
    assertNotNull(XMPPUtil.createRoom(xmppConnection,"firstUserChat",null,null));
    XMPPUtil.relaseXMPPConnection(xmppConnection);
}
项目:SmackStudy    文件:XMPPUtilTest.java   
@Test
public void testGetRosterEntrysForGroup() {
    XMPPConnection xmppConnection = getFullXMPPConnection();
    assertTrue(XMPPUtil.login(xmppConnection, "ranrandemo", "admin123"));
    List<RosterEntry> rosterEntries=XMPPUtil.getRosterEntrysForGroup(xmppConnection, "friends");
    assertNotNull(rosterEntries);
    assertTrue(rosterEntries.size()>0);
    XMPPUtil.relaseXMPPConnection(xmppConnection);
}
项目:SmackStudy    文件:XMPPUtilTest.java   
@Test
public void testCreateChat() {
    XMPPConnection xmppConnection = getFullXMPPConnection();
    assertTrue(XMPPUtil.login(xmppConnection, "ranrandemo", "admin123"));
    assertNotNull(XMPPUtil.createChat(xmppConnection, "tom", null));
    XMPPUtil.relaseXMPPConnection(xmppConnection);
}
项目:BizareChat    文件:ReadReceiptManager.java   
public static synchronized ReadReceiptManager getInstanceFor(XMPPConnection connection) {
    ReadReceiptManager receiptManager = instances.get(connection);

    if (receiptManager == null) {
        receiptManager = new ReadReceiptManager(connection);
        instances.put(connection, receiptManager);
    }

    return receiptManager;
}
项目:SmackStudy    文件:XMPPUtilTest.java   
@Test
public void testSearchUsers() {
    XMPPConnection xmppConnection = getFullXMPPConnection();
    assertTrue(XMPPUtil.login(xmppConnection, "ranrandemo", "admin123"));
    List<HashMap<String, String>> users=XMPPUtil.searchUsers(xmppConnection, "liuqiang");
    assertNotNull(users);
    assertTrue(users.size()>0);
    XMPPUtil.relaseXMPPConnection(xmppConnection);
}
项目:SmackStudy    文件:XMPPUtilTest.java   
@Test
public void testRemoveUser() {
    XMPPConnection xmppConnection = getFullXMPPConnection();
    assertTrue(XMPPUtil.login(xmppConnection, "ranrandemo", "admin123"));
    assertTrue(XMPPUtil.removeUser(xmppConnection, "liuqiang"));
    XMPPUtil.relaseXMPPConnection(xmppConnection);
}
项目:SmackStudy    文件:XMPPUtil.java   
/**
 * 工厂模式获取XMPPConnection对象
 * @return
 */
public static XMPPConnection createXMPPConnection(){
    if(CONFIGURATION!=null){
        return new XMPPConnection(CONFIGURATION);
    }
    else{
        return null;
    }
}
项目:SmackStudy    文件:XMPPUtil.java   
/**
 * 登录
 * @param xmppConnection
 * @param userName
 * @param password
 * @return
 */
public static boolean login(XMPPConnection xmppConnection,String userName,String password){
    try {
        if (!xmppConnection.isConnected()) {
            xmppConnection.connect();
        }
        xmppConnection.login(userName, password);
        return true;
    } catch (XMPPException e) {
        e.printStackTrace();
        return false;
    }
}
项目:SmackStudy    文件:XMPPUtil.java   
/**
 * 设置用户状态
 * @param xmppConnection
 * @param type 状态
 * @param status 状态描述
 * @return
 */
public  static boolean  setPresence(XMPPConnection xmppConnection,Presence.Type type,String status){
    Presence presence=new Presence(type);
    presence.setStatus(status);
    try{
        xmppConnection.sendPacket(presence);
        return true;
    }catch (Exception e){
        return  false;
    }
}
项目:SmackStudy    文件:XMPPUtilTest.java   
@Test
public void testGetUserVCard() {
    XMPPConnection xmppConnection = getFullXMPPConnection();
    assertTrue(XMPPUtil.login(xmppConnection, "ranrandemo", "admin123"));
    assertNotNull(XMPPUtil.getUserVCard(xmppConnection, "ranrandemo"));
    XMPPUtil.relaseXMPPConnection(xmppConnection);
}
项目:BizareChat    文件:DeliveredReceiptManager.java   
private DeliveredReceiptManager(XMPPConnection connection) {
    super(connection);
    ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(connection);
    sdm.addFeature(DeliveredReceipt.NAMESPACE);
    connection.addAsyncStanzaListener(packet -> {
        DeliveredReceipt receipt = DeliveredReceipt.from((Message) packet);
        for (ReceiptReceivedListener listener : receiptReceivedListeners) {
            listener.onReceiptReceived(packet.getFrom(), packet.getTo(), receipt.getId(), packet);
        }
    }, MESSAGES_WITH_DELIVERED_RECEIPT);
}
项目:SmackStudy    文件:XMPPUtil.java   
/**
 * 添加一个分组
 * @param xmppConnection
 * @param groupName
 * @return
 */
public static boolean addGroup(XMPPConnection xmppConnection, String groupName) {
    try {
        xmppConnection.getRoster().createGroup(groupName);
        return true;
    } catch (Exception e) {
        Log.e("addGroup", e.getMessage());
        e.printStackTrace();
        return false;
    }
}
项目:SmackStudy    文件:XMPPUtil.java   
/**
 * 添加一个分组
 * @param xmppConnection
 * @param groupName
 * @return
 */
public static boolean addGroup(XMPPConnection xmppConnection,String groupName) {
    try {
        xmppConnection.getRoster().createGroup(groupName);
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}
项目:BizareChat    文件:DisplayedManager.java   
private DisplayedManager(XMPPConnection connection) {
    super(connection);
    ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(connection);
    sdm.addFeature(Displayed.NAMESPACE);
    connection.addAsyncStanzaListener(packet -> {
        Displayed receipt = Displayed.from((Message) packet);
        for (ReceiptReceivedListener listener : receiptReceivedListeners) {
            listener.onReceiptReceived(packet.getFrom(), packet.getTo(), receipt.getId(), packet);
        }
    }, MESSAGES_WITH_DISPLAYED);
}
项目:SmackStudy    文件:XMPPUtil.java   
/**
 * 添加好友 无分组
 * @param xmppConnection
 * @param userName 用户名
 * @param name 备注名
 * @return
 */
public static boolean addUserNoGroup(XMPPConnection xmppConnection,String userName, String name) {
    try {
        xmppConnection.getRoster().createEntry(userName, name, null);
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}
项目:SmackStudy    文件:XMPPUtil.java   
/**
 * 添加好友 有分组
 * @param xmppConnection
 * @param userName 用户名
 * @param name 备注名
 * @param groupName 分组名
 * @return
 */
public static boolean addUserHaveGroup(XMPPConnection xmppConnection,String userName, String name, String groupName) {
    try {
        Presence subscription = new Presence(Presence.Type.subscribed);
        subscription.setTo(userName);
        userName += "@" + xmppConnection.getServiceName();
        xmppConnection.sendPacket(subscription);
        xmppConnection.getRoster().createEntry(userName, name, new String[] { groupName });
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}
项目:SmackStudy    文件:XMPPUtilTest.java   
@Test
public void testGetUserImage() {
    XMPPConnection xmppConnection = getFullXMPPConnection();
    assertTrue(XMPPUtil.login(xmppConnection, "ranrandemo", "admin123"));
    assertNotNull(XMPPUtil.getUserImage(xmppConnection,"ranrandemo"));
    XMPPUtil.relaseXMPPConnection(xmppConnection);
}
项目:SmackStudy    文件:XMPPUtil.java   
/**
 * 添加好友 有分组
 * @param xmppConnection
 * @param userName       用户名
 * @param name           备注名
 * @param groupName      分组名
 * @return
 */
public static boolean addUserHaveGroup(XMPPConnection xmppConnection, String userName, String name, String groupName) {
    try {
        Presence subscription = new Presence(Presence.Type.subscribed);
        subscription.setTo(userName);
        userName += "@" + xmppConnection.getServiceName();
        xmppConnection.sendPacket(subscription);
        xmppConnection.getRoster().createEntry(userName, name, new String[]{groupName});
        return true;
    } catch (Exception e) {
        Log.e("addUser", e.getMessage());
        e.printStackTrace();
        return false;
    }
}
项目:SmackStudy    文件:XMPPUtil.java   
/**
 * 删除当前用户
 * @param xmppConnection
 * @return
 */
public static boolean deleteAccount(XMPPConnection xmppConnection) {
    try {
        xmppConnection.getAccountManager().deleteAccount();
        return true;
    } catch (XMPPException e) {
        e.printStackTrace();
        return false;
    }
}
项目:SmackStudy    文件:XMPPUtil.java   
/**
 * 修改当前用户的密码
 * @param xmppConnection
 * @param password
 * @return
 */
public static boolean changePassword(XMPPConnection xmppConnection,String password){
    try {
        xmppConnection.getAccountManager().changePassword(password);
        return true;
    } catch (XMPPException e) {
        e.printStackTrace();
        return false;
    }
}
项目:SmackStudy    文件:XMPPUtil.java   
/**
 * 获取用户的所有聊天室
 * @param xmppConnection
 * @return
 */
public static List<HostedRoom>  getHostRooms(XMPPConnection xmppConnection){
    try {
        new ServiceDiscoveryManager(xmppConnection);
        Collection<HostedRoom> hostrooms = MultiUserChat.getHostedRooms(xmppConnection,xmppConnection.getServiceName());
        return hostrooms.stream().collect(Collectors.toList());
    } catch (XMPPException e) {
        e.printStackTrace();
        return null;
    }
}
项目:SmackStudy    文件:XMPPUtil.java   
/**
 * 向其他用户发送文件
 * @param xmppConnection
 * @param toUser 接受文件的用户名
 * @param fileName 文件路径
 * @param remark 备注
 * @return
 */
public static boolean sendFileToUser(XMPPConnection xmppConnection,String toUser,String fileName,String remark){
    // 创建文件传输管理器
    FileTransferManager manager = new FileTransferManager(xmppConnection);
    // 创建输出的文件传输
    OutgoingFileTransfer transfer = manager.createOutgoingFileTransfer(toUser);
    // 发送文件
    try {
        transfer.sendFile(new File(fileName), remark);
        return  true;
    } catch (XMPPException e) {
        e.printStackTrace();
        return  false;
    }
}