smack_用户的基本操作crud

smack_用户的基本操作crud(附源码)

源码地址:https://github.com/carsonWuu/openfire

一、项目实现:

0、连接

1、用户登录

2、增加用户

3、增加好友

4、修改密码

5、删除用户

6、获取好友

7、删除好友

8、完整源码

二、源码介绍:

0、用户连接

import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;

public class openConnection {
    String host="192.168.1.101";
    int port=5222;
    public openConnection(){
        super();
    }
    public openConnection(String host){
        super();
        this.host=host;
        
    }
    public XMPPConnection getconnection(){
        ConnectionConfiguration config = new ConnectionConfiguration(this.host,this.port);
        XMPPConnection conn=null;
        try{
            conn= new XMPPConnection(config);
            conn.connect();
        }
        catch(XMPPException e){
            e.printStackTrace();
        }
        return conn;
    }
    public String getHost(){
        return this.host;
    }
    public void setHost(String host){
        this.host = host;
    }
    
}
View Code

1、用户登录

public boolean login(){
        boolean ret= true;
        try{
            connection.login(this.username,this.psw);
            roster= connection.getRoster();
            roster.addRosterListener(new RosterListener(){

                @Override
                public void entriesAdded(Collection<String> arg0) {
                    // TODO Auto-generated method stub
                    
                }

                @Override
                public void entriesDeleted(Collection<String> arg0) {
                    // TODO Auto-generated method stub
                    
                }

                @Override
                public void entriesUpdated(Collection<String> arg0) {
                    // TODO Auto-generated method stub
                    
                }

                @Override
                public void presenceChanged(Presence arg0) {
                    // TODO Auto-generated method stub
                    //System.out.println("Presence changed: "+arg0);
                    
                }
                
            });
            Presence presence= new Presence(Presence.Type.available);
            presence.setMode(Presence.Mode.available);
            connection.sendPacket(presence);
        }
        catch(Exception e){
            ret = false;
        }
        return ret;
    }
View Code

2、增加用户

public boolean create(){//create user
        boolean ret=true;
        try{
            connection.getAccountManager().createAccount(this.username,this.psw,this.attr);
        }
        catch(Exception e){
            ret = false;
        }
        return ret;
    }
View Code

3、增加好友

public boolean addFriends(String user){//add a friends like:[[email protected]]
        return addFriends(user,"");
    }
    public boolean addFriends(String user,String nickname){
        return addFriends(user,nickname,new String[]{"Friends"});
    }
    public boolean addFriends(String user,String nickname,String []s){
        boolean ret= true;
        roster.setSubscriptionMode(Roster.SubscriptionMode.accept_all);
        try{
            roster.createEntry(user, nickname, s);
        }
        catch(Exception e){
            ret = false;
        }
        return ret;
    }
View Code

4、修改密码

public boolean updatePASSWORD(String newPasw){//update password
        boolean ret= true;
        try{
            connection.getAccountManager().changePassword(newPasw);
        }catch(Exception e){
            ret = false;
        }
        return ret;
    }
View Code

5、删除用户

public boolean delete(){//delete user himself
        boolean ret= true;
        try{
            connection.getAccountManager().deleteAccount();
        }catch(Exception e){
            ret = false;
        }
        return ret;
    }
View Code

6、获取好友

public HashMap getFriends(){//get all friends;
        return getFriends("");
    }
    public HashMap getFriends(String s){
        HashMap hashmap = new HashMap();
        roster = connection.getRoster();
        Collection collection =roster.getEntries();
        int i=1;
        for(Iterator it = collection.iterator();it.hasNext();i++){
            hashmap.put(i,it.next());
        }
        System.out.println(hashmap);
        return hashmap;
    }
View Code

7、删除好友

    public boolean deleteFriend(String name){//delete one friend
        boolean ret = true;
        roster = connection.getRoster();
        try{
            roster.removeEntry(roster.getEntry(name));
        }
        catch(Exception e){
            ret = false;
        }
        return ret;
        
    }
View Code

三、完整源码:

1、openConnection.java

package com.test.smack.crud;

import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;

public class openConnection {
    String host="192.168.1.101";
    int port=5222;
    public openConnection(){
        super();
    }
    public openConnection(String host){
        super();
        this.host=host;
        
    }
    public XMPPConnection getconnection(){
        ConnectionConfiguration config = new ConnectionConfiguration(this.host,this.port);
        XMPPConnection conn=null;
        try{
            conn= new XMPPConnection(config);
            conn.connect();
        }
        catch(XMPPException e){
            e.printStackTrace();
        }
        return conn;
    }
    public String getHost(){
        return this.host;
    }
    public void setHost(String host){
        this.host = host;
    }
    
}
View Code

2、userCrud.java

package com.test.smack.crud;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.RosterListener;
public class userCrud {
    XMPPConnection connection = new openConnection().getconnection();
    String username="";//用户名
    String psw="";//密码
    Map attr;//用户基本信息
    Roster roster;
    Presence presence = new Presence(Presence.Type.available);
    public userCrud(){
        
    }
    public userCrud setUser(String username,String psw){//
        this.username= username;
        this.psw=psw;
        
        return this;
    }
    public userCrud(String username,String psw){
        super();
        this.username=username;
        this.psw = psw;
        this.attr=new HashMap();
        this.attr.put("name","add");
        this.attr.put("email","[email protected]");
        this.attr.put("dept","unknown");
//        System.out.println(connection.getAccountManager());
    }
    
    public boolean create(){//create user
        boolean ret=true;
        try{
            connection.getAccountManager().createAccount(this.username,this.psw,this.attr);
        }
        catch(Exception e){
            ret = false;
        }
        return ret;
    }
    public boolean delete(){//delete user himself
        boolean ret= true;
        try{
            connection.getAccountManager().deleteAccount();
        }catch(Exception e){
            ret = false;
        }
        return ret;
    }
    public boolean updatePASSWORD(String newPasw){//update password
        boolean ret= true;
        try{
            connection.getAccountManager().changePassword(newPasw);
        }catch(Exception e){
            ret = false;
        }
        return ret;
    }
    public boolean login(){
        boolean ret= true;
        try{
            connection.login(this.username,this.psw);
            roster= connection.getRoster();
            roster.addRosterListener(new RosterListener(){

                @Override
                public void entriesAdded(Collection<String> arg0) {
                    // TODO Auto-generated method stub
                    
                }

                @Override
                public void entriesDeleted(Collection<String> arg0) {
                    // TODO Auto-generated method stub
                    
                }

                @Override
                public void entriesUpdated(Collection<String> arg0) {
                    // TODO Auto-generated method stub
                    
                }

                @Override
                public void presenceChanged(Presence arg0) {
                    // TODO Auto-generated method stub
                    //System.out.println("Presence changed: "+arg0);
                    
                }
                
            });
            Presence presence= new Presence(Presence.Type.available);
            presence.setMode(Presence.Mode.available);
            connection.sendPacket(presence);
        }
        catch(Exception e){
            ret = false;
        }
        return ret;
    }
    public boolean addFriends(String user){//add a friends like:[[email protected]]
        return addFriends(user,"");
    }
    public boolean addFriends(String user,String nickname){
        return addFriends(user,nickname,new String[]{"Friends"});
    }
    public boolean addFriends(String user,String nickname,String []s){
        boolean ret= true;
        roster.setSubscriptionMode(Roster.SubscriptionMode.accept_all);
        try{
            roster.createEntry(user, nickname, s);
        }
        catch(Exception e){
            ret = false;
        }
        return ret;
    }
    public HashMap getFriends(){//get all friends;
        return getFriends("");
    }
    public HashMap getFriends(String s){
        HashMap hashmap = new HashMap();
        roster = connection.getRoster();
        Collection collection =roster.getEntries();
        int i=1;
        for(Iterator it = collection.iterator();it.hasNext();i++){
            hashmap.put(i,it.next());
        }
        System.out.println(hashmap);
        return hashmap;
    }
    public boolean deleteFriend(String name){//delete one friend
        boolean ret = true;
        roster = connection.getRoster();
        try{
            roster.removeEntry(roster.getEntry(name));
        }
        catch(Exception e){
            ret = false;
        }
        return ret;
        
    }
//    public void getAllEntries(){//get all friends
//        roster=connection.getRoster();  
//        System.out.println("\""+this.username+"\"的好友:");  
//        Iterator it = roster.getEntries().iterator();  
//        
//        while(it.hasNext()){  
//            
//            System.out.println(it.next());  
//        }     
//          
//    }
    
}
View Code

3、Main.java

package com.test.smack.crud;

public class Main {
    public static void main(String []args){
        userCrud user1 = new userCrud("admin","123456");
        userCrud user2 = new userCrud("wcs","123456");
        user1.login();
        user2.login();
        
        user1.getFriends();
        user2.getFriends();
        
        user2.addFriends("[email protected]");
//        user1.setUser("add1", "123456").create();
        userCrud user3 = new userCrud("add1","123456");
        user3.login();
        user3.addFriends("[email protected]");
        
//        user3.addFriends("admin");
        user3.getFriends();
        
//        user3.updatePASSWORD("123456");
        //user3.updatePASSWORD("add1");
        //user1.delete();
        
        //user1.setUser("add2", "123465").create();
        
    }
}
View Code

猜你喜欢

转载自www.cnblogs.com/carsonwuu/p/9262675.html