Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2161388
  • 博文数量: 556
  • 博客积分: 11457
  • 博客等级: 上将
  • 技术积分: 5973
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-24 22:33
文章分类

全部博文(556)

文章存档

2013年(22)

2012年(74)

2011年(460)

分类: 系统运维

2011-09-08 21:24:08

第一部分ChatEncryptorPlugin 类,过滤消息事件

在这里为所有的发送出去的消息与收到的消息,添加自己的处理动作

package papaya.guava.mango;

import java.io.IOException;

import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.spark.plugin.Plugin;
import org.jivesoftware.spark.SparkManager;
import org.jivesoftware.spark.ChatManager;
import org.jivesoftware.spark.ui.ChatRoom;
import org.jivesoftware.spark.ui.ChatRoomListenerAdapter;
import org.jivesoftware.spark.ui.MessageEventListener;
import org.jivesoftware.spark.ui.MessageFilter;
import org.jivesoftware.spark.ui.rooms.ChatRoomImpl;

/**
* Implements the Spark Plugin framework to display the different possibilities
* using Spark.
*/

public class ChatEncryptorPlugin implements Plugin {

/**
* Called after Spark is loaded to initialize the new plugin.
*/
public void initialize() {
   /*
   * 到
   * 下载 Sparkplug Kit,回来之后。
   *
   * 1. 把sparkplugs\builder\build\build.xml 的
   *      ant buildfile文件导入自己工程修改好。
   *
   * 2. 按照sparkplug_dev_guide.html 导入spark的包,
   *      和配置好启动的class, 还有debug configure 的
   *      vm 参数加上下面两句之际spark的目录和自己插件的xml文件
   *      -Dappdir="C:\\Documents and Settings\\dddddd\\Desktop\\sparkplugs\\spark"
                -Dplugin="E:\Eclipse_workspace\ChatEncryptor\plugin.xml"
   *     
   * 3、直接运行会出现错误MAIN_IMAGE not found 错误
   *   需要 手工打开 spark.jar包
   *   把 \org\jivesoftware\resource 目录下
   *   spark.properties 和Default.class两个文件里面的
   *   MAIN_IMAGE 值修改成一样的,同时确保文件名在 spark.jar里面的
   *   images\ 文件夹下可以找到。
   *   我自己用java decompiler 反编译,然后调试了一下他的
   *   src/java/org/jivesoftware/resource/Default.java 文件才找出问题
   *  
   *   不知道是不是他们故意设置一个这样的门槛,呵呵
   *  
   * 4. 然后就可以调试插件了
   * */
  
   System.out.println("我的chat encryptor 插件开始运行了哦...");
   installMessageFilter();
   installMessageEventListenerForAllChatRoom();
}

/**
* Called when Spark is shutting down to allow for persistence of
* information or releasing of resources.
*/
public void shutdown() {

}

/**
* Return true if the Spark can shutdown on users request.
*
* @return true if Spark can shutdown on users request.
*/
public boolean canShutDown() {
   return true;
}

/**
* Is called when a user explicitly asked to uninstall this plugin. The
* plugin owner is responsible to clean up any resources and remove any
* components install in Spark.
*/
public void uninstall() {
   // Remove all resources belonging to this plugin.
}

/**
* Installs a new MessageFilter.
*/
private void installMessageFilter() {
   // Retrieve the ChatManager from SparkManager
   ChatManager chatManager = SparkManager.getChatManager();

   MessageFilter messageFilter = new MessageFilter() {
    /*
    * 查看这个源代码,这个在事件在添加到 聊天窗体之前 和接受之后触发,是比较合适的处理位置。
    */
    public void filterIncoming(ChatRoom room, Message message) {
     // 现在不支持群组消息

     boolean broadcast = message.getProperty("broadcast") != null;
     // If this is a group chat message, discard
     if (message.getType() == Message.Type.groupchat || broadcast
       || message.getType() == Message.Type.normal
       || message.getType() == Message.Type.headline) {
      return;
     }

     // Do not accept Administrative messages.
     final String host = SparkManager.getSessionManager()
       .getServerAddress();
     if (host.equals(message.getFrom())) {
      return;
     }

     if (message.getType() != Message.Type.chat) {
      return;
     }

     if (!(room instanceof ChatRoomImpl)) {
      return;
     }

     E2EProtocal.MessageIn(message);
    }

    /*
    * 查看这个源代码,这个是消息添加到聊天窗体之前触发, 不是合适的处理位置。加密应该在显示到聊天窗体之后才合适
    */
    public void filterOutgoing(ChatRoom room, Message message) {

    }

   };

   chatManager.addMessageFilter(messageFilter);

   // Just remember to remove your filter if need be.
}

private void installMessageEventListenerForAllChatRoom() {
   // Retrieve ChatManager from the SparkManager
   ChatManager chatManager = SparkManager.getChatManager();

   // Add to a new ChatRoom when the ChatRoom opens.
   chatManager.addChatRoomListener(new ChatRoomListenerAdapter() {
    public void chatRoomOpened(ChatRoom room) {
     // only do the translation for single chat
     if (room instanceof ChatRoomImpl) {
      final ChatRoomImpl roomImpl = (ChatRoomImpl) room;
      //
      // // Create a new ChatRoomButton.
      // final JComboBox translatorBox = new
      // JComboBox(TranslatorUtil.TranslationType.getTypes());
      //
      // translatorBox.addActionListener(new ActionListener() {
      // public void actionPerformed(ActionEvent e) {
      // // Set the focus back to the message box.
      // roomImpl.getChatInputEditor().requestFocusInWindow();
      // }
      // });
      //
      // roomImpl.getEditorBar().add(translatorBox);

      final MessageEventListener messageListener = new MessageEventListener() {

       /*
       * 这个在收到消息,添加到聊天记录之后,才触发, 不是的合适的处理点,解密应该在显示到聊天窗体之前做才合适
       */
       public void receivingMessage(Message message) {
       
       }

       /*
       * 这个事件在消息添加到 聊天记录之后,发送之前触发 比较适合的处理点 加密应该在显示到聊天窗体之后才合适
       */
       public void sendingMessage(Message message) {

        E2EProtocal.MessageOut(message);
       }

      };
      roomImpl.addMessageEventListener(messageListener);

     }
    }
   });

}

}

转自:http://gmd20.blog.163.com/blog/static/1684392320106175449529/?   fromdm&fromSearch&isFromSearchEngine=yes

阅读(3057) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~