OpenFire plug-in development (iii) sending messages to OpenFire clients via HTTP

Source: Internet
Author: User
Tags sendmsg

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

Demand:
Send messages to OpenFire clients via HTTP (non-XMPP protocol)
OpenFire send messages (messages only) are typically implemented using XMPP clients such as smack, but sometimes clients do not want to use smack, such as Android, Web, or non-Java clients. At this point, you can open an interface to the OpenFire server, and can be called by HTTP or service, so that you can not consider the type of the client. Of course, this is just the simplest message to send.
Implementation method:
Add a plug-in to the OpenFire server and register as Servlets and component, using ComponentManager to send information to the client in the servlet.
    
Directory structure:
     
Implementation steps:
1: Create the sendmsg directory under the Src\plugins directory in OpenFire, create the left file structure under the Sendmsg directory, and apply the Src\plugins\sendmsg\src\java as the source directory, as shown in the right.
2: Implement Sendmsgplugin, Inherit plugin class, and rewrite Initializeplugin () and Destroyplugin (), method body is empty.
3: Add the following code to the Plugin.xml file to register the plugin

[HTML]View PlainCopy
  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: Implement Sendmsgservlet class, Inherit HttpServlet class and implement component interface.
• Rewrite init (servletconfig config) method [not Init () method], initialize in method body

[Java]View PlainCopy
    1. Authcheckfilter.addexclude (service_name); //Add an excluded path to the validator
    2. ComponentManager = Componentmanagerfactory.getcomponentmanager (); //Register Components
    3. Componentmanager.addcomponent (componentname, this );

• Rewrite the Doget () method to process the business in the method body, where information is sent

[Java]View PlainCopy
    1. You should do the verification logic before
    2. Message msg = new Message ();
    3. Msg.setbody ("mess Body");
    4. Msg.setfrom ("admin" + domain); Sender
    5. Msg.setto ("service" + domain); Receiving person
    6. Msg.settype (Message.Type.chat); //For chat information
    7. Componentmanager.sendpacket (this, msg); Send

• Override the Destroy () method to free up memory

[Java]View PlainCopy
    1. Componentmanager.removecomponent (componentname);
    2. ComponentManager = null;
    3. Authcheckfilter.addexclude (service_name);

5: Registering the servlet in the Web-custom.xml file

[HTML]View PlainCopy
  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: Modify Src\web\web-inf\decorators.xml file, add exclude filter path

[HTML]View PlainCopy
    1. <pattern>/plugins/sendmsg/sendservlet*</pattern>

7: Post-release access to Http://127.0.0.1:9090/plugins/sendmsg/sendservlet
Key points:
1: directory structure, which concerns whether the plug-in can be deployed successfully
2:sendmsgservlet class initialization, you need to add an excluded path to the validator [Authcheckfilter.addexclude (service_name);], otherwise you need to log in when you access it.
3: If you want to lose only the content in the servlet, the 6th is necessary, otherwise it will embed your content in the OpenFire frame. If an exclusion path has been added while the Sendmsgservlet class was initialized, and the above 6th is not completed, a null pointer will be given.
4: In Sendmsgservlet's doget () or Dopost (), authentication should be added, otherwise anyone can send information to anyone.

5: Last ant build, build Plugin
Download path:
http://download.csdn.net/detail/hzaccp3/6962851

(EXT) OpenFire plug-in development (iii) sending messages to OpenFire clients via HTTP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.