现在的spark都整合了smack么
1个回答
展开全部
package org.jivesoftware.spark.ui;
import java.util.ArrayList;
import org.jivesoftware.spark.util.log.Log;
import org.jivesoftware.spark.util.ModelUtil;
import org.jivesoftware.resource.Res;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.RosterGroup;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.RosterPacket;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.spark.SparkManager;
import org.jivesoftware.spark.UserManager;
/**
* 处理所有订阅请求
* @author jian
*
*/
public class MySubscription {
private String jid;
//构造函数
public MySubscription(){
}
/**
* 调用
* @param jid 主键ID
*/
public void invoke(final String jid){
this.jid=jid;
final Roster roster=SparkManager.getConnection().getRoster();//返回好友册
//如果用户已经在好友当中,则不显示
RosterEntry entry=roster.getEntry(jid);//返回对应的用户
if(entry!=null&&entry.getType()==RosterPacket.ItemType.to){
Presence response=new Presence(Presence.Type.subscribed);
response.setTo(jid);
SparkManager.getConnection().sendPacket(response);
return;
}
UserManager userManager=SparkManager.getUserManager();
String username=userManager.getNickname(userManager.getFullJID(jid));
username=username==null?StringUtils.parseName(UserManager.unescapeJID(jid)):username;//给usernam赋值
boolean addEntry=addEntry(username);
GetGroupName();
if(addEntry){
Presence response=new Presence(Presence.Type.subscribed);
response.setTo(jid);
SparkManager.getConnection().sendPacket(response);
}
}
/**
* 同意添加好友
*/
private boolean addEntry(String username){
String errorMessage=Res.getString("title.error");
String groupName="Friends";
//获取该用户组是否存在
ContactGroup contactGroup=SparkManager.getWorkspace().getContactList().getContactGroup(groupName);
boolean isSharedGroup = contactGroup!=null&&contactGroup.isSharedGroup();
System.out.println("用户组存在?"+isSharedGroup);
if(isSharedGroup){
errorMessage=Res.getString("message.cannot.add.contact.to.shared.group");
}else if(!ModelUtil.hasLength(username)){
errorMessage=Res.getString("message.specify.contact.jid");
}else if(!ModelUtil.hasLength(groupName)){
errorMessage=Res.getString("message.specify.group");
}else if(ModelUtil.hasLength(username)&&ModelUtil.hasLength(groupName)&&!isSharedGroup){
System.out.println("添加好友");
System.out.println("直接输出Group组:"+groupName);
addEntity(jid, username, groupName);
return true;
}
return false;
}
/**
* 把新用户添加到好友名单
* @param jid 账号
* @param username 昵称
* @param groupName 组名
*/
private RosterEntry addEntity(String jid,String username,String groupName){
String[] groups={groupName};
System.out.println("开始添加了哦");
for(int i=0;i<groups.length;i++){
System.out.println(groups[i]);
}
Roster roster=SparkManager.getConnection().getRoster();
RosterEntry userEntry=roster.getEntry(jid);
boolean isSubscribed=true;
if(userEntry!=null){
isSubscribed=userEntry.getGroups().size()==0;
}
if(isSubscribed){
try {
roster.createEntry(jid, username, new String[]{groupName});
} catch (XMPPException e) {
Log.error("不能添加新的好友"+jid,e);
}
return roster.getEntry(jid);
}
try {
RosterGroup rosterGroup=roster.getGroup(groupName);
if(rosterGroup==null){
rosterGroup=roster.createGroup(groupName);
}
if(userEntry==null){
roster.createEntry(jid, username, groups);
userEntry=roster.getEntry(jid);
}else {
userEntry.setName(username);
rosterGroup.addEntry(userEntry);
}
userEntry=roster.getEntry(jid);
} catch (XMPPException ex) {
Log.error(ex);
}
return userEntry;
}
private void GetGroupName(){
ArrayList<String> list=new ArrayList<String>();
for(ContactGroup group:SparkManager.getWorkspace().getContactList().getContactGroups()){
if(!group.isOfflineGroup()&&!"Unfiled".equalsIgnoreCase(group.getGroupName())&&!group.isSharedGroup()){
list.add(group.getGroupName());
System.out.println(group.getGroupName());
}
}
}
}
import java.util.ArrayList;
import org.jivesoftware.spark.util.log.Log;
import org.jivesoftware.spark.util.ModelUtil;
import org.jivesoftware.resource.Res;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.RosterGroup;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.RosterPacket;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.spark.SparkManager;
import org.jivesoftware.spark.UserManager;
/**
* 处理所有订阅请求
* @author jian
*
*/
public class MySubscription {
private String jid;
//构造函数
public MySubscription(){
}
/**
* 调用
* @param jid 主键ID
*/
public void invoke(final String jid){
this.jid=jid;
final Roster roster=SparkManager.getConnection().getRoster();//返回好友册
//如果用户已经在好友当中,则不显示
RosterEntry entry=roster.getEntry(jid);//返回对应的用户
if(entry!=null&&entry.getType()==RosterPacket.ItemType.to){
Presence response=new Presence(Presence.Type.subscribed);
response.setTo(jid);
SparkManager.getConnection().sendPacket(response);
return;
}
UserManager userManager=SparkManager.getUserManager();
String username=userManager.getNickname(userManager.getFullJID(jid));
username=username==null?StringUtils.parseName(UserManager.unescapeJID(jid)):username;//给usernam赋值
boolean addEntry=addEntry(username);
GetGroupName();
if(addEntry){
Presence response=new Presence(Presence.Type.subscribed);
response.setTo(jid);
SparkManager.getConnection().sendPacket(response);
}
}
/**
* 同意添加好友
*/
private boolean addEntry(String username){
String errorMessage=Res.getString("title.error");
String groupName="Friends";
//获取该用户组是否存在
ContactGroup contactGroup=SparkManager.getWorkspace().getContactList().getContactGroup(groupName);
boolean isSharedGroup = contactGroup!=null&&contactGroup.isSharedGroup();
System.out.println("用户组存在?"+isSharedGroup);
if(isSharedGroup){
errorMessage=Res.getString("message.cannot.add.contact.to.shared.group");
}else if(!ModelUtil.hasLength(username)){
errorMessage=Res.getString("message.specify.contact.jid");
}else if(!ModelUtil.hasLength(groupName)){
errorMessage=Res.getString("message.specify.group");
}else if(ModelUtil.hasLength(username)&&ModelUtil.hasLength(groupName)&&!isSharedGroup){
System.out.println("添加好友");
System.out.println("直接输出Group组:"+groupName);
addEntity(jid, username, groupName);
return true;
}
return false;
}
/**
* 把新用户添加到好友名单
* @param jid 账号
* @param username 昵称
* @param groupName 组名
*/
private RosterEntry addEntity(String jid,String username,String groupName){
String[] groups={groupName};
System.out.println("开始添加了哦");
for(int i=0;i<groups.length;i++){
System.out.println(groups[i]);
}
Roster roster=SparkManager.getConnection().getRoster();
RosterEntry userEntry=roster.getEntry(jid);
boolean isSubscribed=true;
if(userEntry!=null){
isSubscribed=userEntry.getGroups().size()==0;
}
if(isSubscribed){
try {
roster.createEntry(jid, username, new String[]{groupName});
} catch (XMPPException e) {
Log.error("不能添加新的好友"+jid,e);
}
return roster.getEntry(jid);
}
try {
RosterGroup rosterGroup=roster.getGroup(groupName);
if(rosterGroup==null){
rosterGroup=roster.createGroup(groupName);
}
if(userEntry==null){
roster.createEntry(jid, username, groups);
userEntry=roster.getEntry(jid);
}else {
userEntry.setName(username);
rosterGroup.addEntry(userEntry);
}
userEntry=roster.getEntry(jid);
} catch (XMPPException ex) {
Log.error(ex);
}
return userEntry;
}
private void GetGroupName(){
ArrayList<String> list=new ArrayList<String>();
for(ContactGroup group:SparkManager.getWorkspace().getContactList().getContactGroups()){
if(!group.isOfflineGroup()&&!"Unfiled".equalsIgnoreCase(group.getGroupName())&&!group.isSharedGroup()){
list.add(group.getGroupName());
System.out.println(group.getGroupName());
}
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询