分享

Tigase插件编写

 WindySky 2016-04-10

Xmpp协议有XEP-0055 :Jabber Search协议。不过这个只能单个用户搜索。然而我们的需求是要进行批量搜索,然后返回有效的Jid。为了实现这个功能,需要开发自己的一套iq包。


  1. import java.util.Arrays;  
  2. import java.util.HashSet;  
  3. import java.util.List;  
  4. import java.util.Map;  
  5. import java.util.Queue;  
  6. import java.util.Set;  
  7. import java.util.logging.Level;  
  8. import java.util.logging.Logger;  
  9.   
  10. import tigase.db.DBInitException;  
  11. import tigase.db.NonAuthUserRepository;  
  12. import tigase.db.RepositoryFactory;  
  13. import tigase.db.UserRepository;  
  14. import tigase.server.Iq;  
  15. import tigase.server.Packet;  
  16. import tigase.util.TigaseStringprepException;  
  17. import tigase.xml.Element;  
  18. import tigase.xmpp.Authorization;  
  19. import tigase.xmpp.BareJID;  
  20. import tigase.xmpp.NotAuthorizedException;  
  21. import tigase.xmpp.PacketErrorTypeException;  
  22. import tigase.xmpp.StanzaType;  
  23. import tigase.xmpp.XMPPException;  
  24. import tigase.xmpp.XMPPPreprocessorIfc;  
  25. import tigase.xmpp.XMPPProcessor;  
  26. import tigase.xmpp.XMPPProcessorIfc;  
  27. import tigase.xmpp.XMPPResourceConnection;  
  28. import tigase.xmpp.impl.roster.RosterAbstract;  
  29.   
  30. public class TalkKingRoster extends XMPPProcessor implements XMPPProcessorIfc, XMPPPreprocessorIfc{  
  31.     private static Logger log = Logger.getLogger(TalkKingRoster.class.getName());  
  32.     public static final String ID = "talkKing:" + RosterAbstract.XMLNS;  
  33.     public static final String XMLNS = "talkKing:roster";  
  34.     private static final String[] XMLNSS = {XMLNS};  
  35.     private static final String[][] ELEMENTS = {Iq.IQ_QUERY_PATH};  
  36.      private static final Element[] FEATURES = { new Element("roster", new String[] { "xmlns" }, new String[] { XMLNS }) };  
  37.   
  38.     @Override  
  39.     public String id() {  
  40.         // TODO Auto-generated method stub  
  41.         return ID;  
  42.     }  
  43.   
  44.     @Override  
  45.     public boolean preProcess(Packet packet, XMPPResourceConnection session,  
  46.             NonAuthUserRepository repo, Queue<Packet> results,  
  47.             Map<String, Object> settings) {  
  48.         // TODO Auto-generated method stub  
  49.         return false;  
  50.     }  
  51.   
  52.     @Override  
  53.     public void process(Packet packet, XMPPResourceConnection session,  
  54.             NonAuthUserRepository repo, Queue<Packet> results,  
  55.             Map<String, Object> settings) throws XMPPException {  
  56.         // TODO Auto-generated method stub  
  57.         if (log.isLoggable(Level.FINEST)) {  
  58.             log.finest("Processing packet: " + packet.toString());  
  59.         }  
  60.         if(session==null){  
  61.             if(log.isLoggable(Level.FINE)){  
  62.                 log.log(Level.FINE,"Session is null, ignoring packet: {0}",packet);  
  63.                 return;  
  64.             }  
  65.         }  
  66.         if (packet.getStanzaFrom()!= null && session.isUserId(packet.getStanzaFrom().getBareJID())&& !session.isAuthorized()) {  
  67.             if ( log.isLoggable( Level.FINE ) ){  
  68.                 log.log( Level.FINE, "Session is not authorized, ignoring packet: {0}", packet );  
  69.             }  
  70.             return;  
  71.         }  
  72.           
  73.         try {  
  74.             if (!session.isServerSession() && (packet.getStanzaFrom() != null ) && !session.isUserId(packet.getStanzaFrom().getBareJID())) {  
  75.                 // RFC says: ignore such request  
  76.                 log.log( Level.WARNING, "Roster request ''from'' attribute doesn't match "  
  77.                     + "session: {0}, request: {1}", new Object[] { session, packet } );  
  78.                 return;  
  79.             }  
  80.               
  81.             StanzaType type = packet.getType();  
  82.             String xmlns = packet.getElement().getXMLNSStaticStr( Iq.IQ_QUERY_PATH );  
  83.               
  84.             if (xmlns == XMLNS && type == StanzaType.get){  
  85.                 List<Element> items = packet.getElemChildrenStaticStr(Iq.IQ_QUERY_PATH);  
  86.                   
  87.                 if (items!=null) {  
  88.                     String uri = System.getProperty( "user-db-uri" );  
  89.                     UserRepository userRepository = RepositoryFactory.getUserRepository( null, uri, null );  
  90.                     String serverDomain = session.getDomainAsJID().getDomain();  
  91.                     Set<BareJID> found = new HashSet<BareJID>();  
  92.                     for (Element item : items) {  
  93.                         if (!item.getName().equals("item")) {  
  94.                             continue;  
  95.                         }  
  96.                           
  97.                         BareJID jid = BareJID.bareJIDInstance(item.getAttributeStaticStr("jid"));  
  98.                         String domain = jid.getDomain();  
  99.                         BareJID localJid = BareJID.bareJIDInstance(jid.getLocalpart(),serverDomain);  
  100.                           
  101.                           
  102.                         boolean isLocalJid = domain.equals(serverDomain);  
  103.                         if (isLocalJid) {  
  104.                             if (userRepository.userExists(localJid)) {  
  105.                                 found.add(jid);  
  106.                             }  
  107.                         }  
  108.                     }  
  109.                       
  110.                     Element query = new Element(Iq.QUERY_NAME);  
  111.                     query.setXMLNS(XMLNS);  
  112.                       
  113.                     for (BareJID bareJID : found) {  
  114.                         Element item = new Element("item");  
  115.                         item.setAttribute("jid", bareJID.toString());  
  116.                         query.addChild(item);  
  117.                     }  
  118.                       
  119.                     results.offer(packet.okResult(query, 0));  
  120.                       
  121.                     packet.processedBy(ID);  
  122.                 }  
  123.             }  
  124.         } catch (NotAuthorizedException e){  
  125.              log.log( Level.WARNING, "Received roster request but user session is not authorized yet: {0}", packet );  
  126.                 try {  
  127.                     results.offer( Authorization.NOT_AUTHORIZED.getResponseMessage( packet,  
  128.                         "You must authorize session first.", true ) );  
  129.                 }  
  130.                 catch (PacketErrorTypeException pe) {  
  131.                     // ignored  
  132.                 }  
  133.                   
  134.         } catch (TigaseStringprepException e) {  
  135.             // TODO Auto-generated catch block  
  136.               
  137.         } catch (DBInitException e) {  
  138.             // TODO Auto-generated catch block  
  139.             log.log( Level.WARNING, "Database problem, please contact admin:", e );  
  140.             try {  
  141.                 results.offer( Authorization.INTERNAL_SERVER_ERROR.getResponseMessage( packet,  
  142.                     "Database access problem, please contact administrator.", true ) );  
  143.             }  
  144.             catch (PacketErrorTypeException pe) {  
  145.                 // ignored  
  146.             }  
  147.         } catch (ClassNotFoundException e) {  
  148.             // TODO Auto-generated catch block  
  149.             e.printStackTrace();  
  150.         } catch (InstantiationException e) {  
  151.             // TODO Auto-generated catch block  
  152.             e.printStackTrace();  
  153.         } catch (IllegalAccessException e) {  
  154.             // TODO Auto-generated catch block  
  155.             e.printStackTrace();  
  156.         }   
  157.     }  
  158.       
  159.     @Override  
  160.     public Set<StanzaType> supTypes() {  
  161.         return new HashSet<StanzaType>(Arrays.asList(StanzaType.get));  
  162.     }  
  163.   
  164.     @Override  
  165.     public String[][] supElementNamePaths() {  
  166.         // TODO Auto-generated method stub  
  167.         return ELEMENTS;  
  168.     }  
  169.   
  170.     @Override  
  171.     public String[] supNamespaces() {  
  172.         // TODO Auto-generated method stub  
  173.         return XMLNSS;  
  174.     }  
  175.   
  176.     @Override  
  177.     public Element[] supStreamFeatures(XMPPResourceConnection session) {  
  178.         if (log.isLoggable(Level.FINEST) && (session != null)) {  
  179.             log.finest("VHostItem: " + session.getDomain());  
  180.         }  
  181.         if (session != null) {  
  182.             return FEATURES;  
  183.         }  
  184.         else {  
  185.             return null;  
  186.         }  
  187.     }  
  188.       
  189.       
  190. }  


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多