zoukankan      html  css  js  c++  java
  • (转)openfire插件开发(三)通过http方式向openfire客户端发信息

    转:http://blog.csdn.net/hzaccp3/article/details/19964655

    需求:
      通过http方式,向openfire客户端发信息(非XMPP协议)
    openfire发送信息(只发信息)通常使用smack等XMPP客户端来实现,但有时有些客户端不想使用smack,如android、web或非java客户端。此时可以给openfire服务器开一个接口,并且能通过http或service方式调用,这样就可以不用考虑客户端的类型了。当然,这只是做最简单的发送信息
     实现方式:
      给openfire服务器添加一个插件,并注册为servlet和Component,在servlet中使用ComponentManager给客户端发送信息。
        
     目录结构:
        
     实现步骤:
      1:在openfire的srcplugins目录下创建sendmsg目录,在sendmsg目录下创建左上图文件结构,并将srcpluginssendmsgsrcjava应用为源目录,如右上图所示。
      2:实现SendMsgPlugin,继承Plugin类,并重写initializePlugin()及destroyPlugin(),方法体为空。
      3:在plugin.xml文件中添加以下代码,注册插件

    [html] view plain copy
     
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <plugin>  
    3.     <name>sendmsgPlugin</name>  
    4.     <class>com.hzaccp.sendmsg.plugin.SendMsgPlugin</class>  
    5.     <description></description>  
    6. </plugin>  

      4:实现SendMsgServlet类,继承HttpServlet类及实现Component接口。
        • 重写init(ServletConfig config)方法[不是init()方法],在方法体中初始化

    [java] view plain copy
     
    1. AuthCheckFilter.addExclude(SERVICE_NAME);//给验证器添加排除的路径  
    2. componentManager = ComponentManagerFactory.getComponentManager();//注册组件  
    3. componentManager.addComponent(COMPONENTNAME, this);  

        •重写doGet()方法,在方法体中处理业务,这里发送信息

    [java] view plain copy
     
    1. //在些之前应该做验证逻辑  
    2. Message msg = new Message();  
    3. msg.setBody("mess body");  
    4. msg.setFrom("admin" + domain);//发信人  
    5. msg.setTo("service" + domain);//接收人  
    6. msg.setType(Message.Type.chat);//为聊天信息  
    7. componentManager.sendPacket(this, msg);//发送  

        •重写destroy()方法,释放内存

    [java] view plain copy
     
    1. componentManager.removeComponent(COMPONENTNAME);  
    2. componentManager = null;  
    3. AuthCheckFilter.addExclude(SERVICE_NAME);  

      5:在web-custom.xml文件中注册servlet

    [html] view plain copy
     
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">  
    3. <web-app>  
    4.     <servlet>  
    5.         <servlet-name>sendmsgservlet</servlet-name>  
    6.         <servlet-class>com.hzaccp.sendmsg.plugin.SendMsgServlet</servlet-class>  
    7.     </servlet>  
    8.     <servlet-mapping>  
    9.         <servlet-name>sendmsgservlet</servlet-name>  
    10.         <url-pattern>/sendservlet</url-pattern>  
    11.     </servlet-mapping>  
    12. </web-app>  

      6:修改srcwebWEB-INFdecorators.xml文件,添加排除过滤路径

    [html] view plain copy
     
    1. <pattern>/plugins/sendmsg/sendservlet*</pattern>  

      7:发布后访问http://127.0.0.1:9090/plugins/sendmsg/sendservlet
     关键点:
      1:目录结构,这关系到插件能否部署成功
      2:SendMsgServlet类初始化时,需要给验证器添加排除的路径[AuthCheckFilter.addExclude(SERVICE_NAME);],否则访问时需要登录。
      3:如果想在servlet中只输出自已的内容,那第6点是很有必要的,否则会将你的内容嵌入到openfire自带的框架中。如果已在SendMsgServlet类初始化时添加排除路径,而没完成上述第6点,则会出空指针。
      4:在SendMsgServlet的doGet()或doPost()中,应该加上验证,否则任何人都可以向任何人发信息。

           5:最后ant build一下,生成插件
     下载路径:
    http://download.csdn.net/detail/hzaccp3/6962851

  • 相关阅读:
    NativeXml (1):下载、安装、测试
    NativeXml (7):添加属性
    NativeXml (9):读取
    NativeXml (2):对象建立
    NativeXml (3):保存
    NativeXml (6):添加节点
    NativeXml (10):编辑
    NativeXml (5):事件
    博客园现代化建设—用 Entity Framework 与 Json.NET 实现数据的按需更新
    Entity Framework 小知识分享
  • 原文地址:https://www.cnblogs.com/wangle1001986/p/7246346.html
Copyright © 2011-2022 走看看