openfire+smack添加好友申请 Smack 4.1.8 and 4.2.0-beta2 添加好友请求以及确认
openfire+smack添加好友申请 Smack 4.1.8 and 4.2.0-beta2 添加好友请求以及确认
android下的XMPP对应smack-4.2.1,实现登录,注册,发单聊,加聊天室,发群聊等简单功能
推荐
tigase开发专栏
推荐
tigase开发比笔记
注册用户批量查询
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import tigase.db.DBInitException;
import tigase.db.NonAuthUserRepository;
import tigase.db.RepositoryFactory;
import tigase.db.UserRepository;
import tigase.server.Iq;
import tigase.server.Packet;
import tigase.util.TigaseStringprepException;
import tigase.xml.Element;
import tigase.xmpp.Authorization;
import tigase.xmpp.BareJID;
import tigase.xmpp.NotAuthorizedException;
import tigase.xmpp.PacketErrorTypeException;
import tigase.xmpp.StanzaType;
import tigase.xmpp.XMPPException;
import tigase.xmpp.XMPPPreprocessorIfc;
import tigase.xmpp.XMPPProcessor;
import tigase.xmpp.XMPPProcessorIfc;
import tigase.xmpp.XMPPResourceConnection;
import tigase.xmpp.impl.roster.RosterAbstract;
public class TalkKingRoster extends XMPPProcessor implements XMPPProcessorIfc, XMPPPreprocessorIfc{
private static Logger log = Logger.getLogger(TalkKingRoster.class.getName());
public static final String ID = "talkKing:" + RosterAbstract.XMLNS;
public static final String XMLNS = "talkKing:roster";
private static final String[] XMLNSS = {XMLNS};
private static final String[][] ELEMENTS = {Iq.IQ_QUERY_PATH};
private static final Element[] FEATURES = { new Element("roster", new String[] { "xmlns" }, new String[] { XMLNS }) };
@Override
public String id() {
// TODO Auto-generated method stub
return ID;
}
@Override
public boolean preProcess(Packet packet, XMPPResourceConnection session,
NonAuthUserRepository repo, Queue<Packet> results,
Map<String, Object> settings) {
// TODO Auto-generated method stub
return false;
}
@Override
public void process(Packet packet, XMPPResourceConnection session,
NonAuthUserRepository repo, Queue<Packet> results,
Map<String, Object> settings) throws XMPPException {
// TODO Auto-generated method stub
if (log.isLoggable(Level.FINEST)) {
log.finest("Processing packet: " + packet.toString());
}
if(session==null){
if(log.isLoggable(Level.FINE)){
log.log(Level.FINE,"Session is null, ignoring packet: {0}",packet);
return;
}
}
if (packet.getStanzaFrom()!= null && session.isUserId(packet.getStanzaFrom().getBareJID())&& !session.isAuthorized()) {
if ( log.isLoggable( Level.FINE ) ){
log.log( Level.FINE, "Session is not authorized, ignoring packet: {0}", packet );
}
return;
}
try {
if (!session.isServerSession() && (packet.getStanzaFrom() != null ) && !session.isUserId(packet.getStanzaFrom().getBareJID())) {
// RFC says: ignore such request
log.log( Level.WARNING, "Roster request ''from'' attribute doesn't match "
+ "session: {0}, request: {1}", new Object[] { session, packet } );
return;
}
StanzaType type = packet.getType();
String xmlns = packet.getElement().getXMLNSStaticStr( Iq.IQ_QUERY_PATH );
if (xmlns == XMLNS && type == StanzaType.get){
List<Element> items = packet.getElemChildrenStaticStr(Iq.IQ_QUERY_PATH);
if (items!=null) {
String uri = System.getProperty( "user-db-uri" );
UserRepository userRepository = RepositoryFactory.getUserRepository( null, uri, null );
String serverDomain = session.getDomainAsJID().getDomain();
Set<BareJID> found = new HashSet<BareJID>();
for (Element item : items) {
if (!item.getName().equals("item")) {
continue;
}
BareJID jid = BareJID.bareJIDInstance(item.getAttributeStaticStr("jid"));
String domain = jid.getDomain();
BareJID localJid = BareJID.bareJIDInstance(jid.getLocalpart(),serverDomain);
boolean isLocalJid = domain.equals(serverDomain);
if (isLocalJid) {
if (userRepository.userExists(localJid)) {
found.add(jid);
}
}
}
Element query = new Element(Iq.QUERY_NAME);
query.setXMLNS(XMLNS);
for (BareJID bareJID : found) {
Element item = new Element("item");
item.setAttribute("jid", bareJID.toString());
query.addChild(item);
}
results.offer(packet.okResult(query, 0));
packet.processedBy(ID);
}
}
} catch (NotAuthorizedException e){
log.log( Level.WARNING, "Received roster request but user session is not authorized yet: {0}", packet );
try {
results.offer( Authorization.NOT_AUTHORIZED.getResponseMessage( packet,
"You must authorize session first.", true ) );
}
catch (PacketErrorTypeException pe) {
// ignored
}
} catch (TigaseStringprepException e) {
// TODO Auto-generated catch block
} catch (DBInitException e) {
// TODO Auto-generated catch block
log.log( Level.WARNING, "Database problem, please contact admin:", e );
try {
results.offer( Authorization.INTERNAL_SERVER_ERROR.getResponseMessage( packet,
"Database access problem, please contact administrator.", true ) );
}
catch (PacketErrorTypeException pe) {
// ignored
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public Set<StanzaType> supTypes() {
return new HashSet<StanzaType>(Arrays.asList(StanzaType.get));
}
@Override
public String[][] supElementNamePaths() {
// TODO Auto-generated method stub
return ELEMENTS;
}
@Override
public String[] supNamespaces() {
// TODO Auto-generated method stub
return XMLNSS;
}
@Override
public Element[] supStreamFeatures(XMPPResourceConnection session) {
if (log.isLoggable(Level.FINEST) && (session != null)) {
log.finest("VHostItem: " + session.getDomain());
}
if (session != null) {
return FEATURES;
}
else {
return null;
}
}
}
一般问题解决方案
1、网上的账号搜索代码异常:
/**
* 查询用户
*
* @param userName userName
* @return List<HashMap<String, String>>
*/
public List<HashMap<String, String>> searchUsers(String userName) {
if (getConnection() == null)
return null;
HashMap<String, String> user;
List<HashMap<String, String>> results = new ArrayList<>();
try {
UserSearchManager usm = new UserSearchManager(getConnection());
// Form searchForm = usm.getSearchForm(getConnection().getServiceName());
// Form searchForm = usm.getSearchForm(getConnection().getXMPPServiceDomain());
DomainBareJid searchDomainBareJid=JidCreate.domainBareFrom("search." + getConnection().getXMPPServiceDomain());
Form searchForm = usm.getSearchForm(searchDomainBareJid);
if (searchForm == null)
return null;
Form answerForm = searchForm.createAnswerForm();
answerForm.setAnswer("userAccount", true);
answerForm.setAnswer("userPhote", userName);
// DomainBareJid domainBareJid=JidCreate.domainBareFrom("search" + getConnection().getServiceName());
// ReportedData data = usm.getSearchResults(answerForm, getConnection().getXMPPServiceDomain());
ReportedData data = usm.getSearchResults(answerForm, searchDomainBareJid);
List<ReportedData.Row> rowList = data.getRows();
for (ReportedData.Row row : rowList) {
user = new HashMap<>();
user.put("userAccount", row.getValues("userAccount").toString());
user.put("userPhote", row.getValues("userPhote").toString());
results.add(user);
// 若存在,则有返回,UserName一定非空,其他两个若是有设,一定非空
}
} catch
(SmackException | InterruptedException
| XmppStringprepException | XMPPException e) {
e.printStackTrace();
}
return results;
}
注意,网上中
getConnection().getServiceName()
是不存在的。
报错:
stackoverflow的说法:
XMPP UserSearch using latest smack 4.1.3 got remote server no found error