扩展Smack Message

XMPP是一种基于标准通用标记语言的子集XML的协议,它继承了在XML环境中灵活的发展性。

Smack是一个开源的用 java 写的XMPP(jabber)客户端代码库

 

因为XMPP是XML,所以进行扩展十分方便。而Smack也提供了对Message扩展的支持。

 

Smack对Message扩展主要需要两个接口

org.jivesoftware.smack.packet.PacketExtension
org.jivesoftware.smack.provider.PacketExtensionProvider

 

假设我们需要扩展一个消息回执,需要在Message中定义一个Received节点:

 

package org.jivesoftware.smack.packet;

import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smack.util.StringUtils;

public class Received implements PacketExtension {

	public static final String NAME = "received";
	public static final String NAME_SPACE = "urn:xmpp:receipts";

	private String id = "";
	private Integer status = 0;

	/**
	 * @return the id
	 */
	public String getId() {
		return id;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(String id) {
		this.id = id;
	}

	/**
	 * @return the status
	 */
	public Integer getStatus() {
		return status;
	}

	/**
	 * @param status the status to set
	 */
	public void setStatus(Integer status) {
		this.status = status;
	}

	@Override
	public String getElementName() {
		return "received";
	}

	@Override
	public String getNamespace() {
		return "urn:xmpp:receipts";
	}

	@Override
	public String toXML() {
		StringBuilder buffer = new StringBuilder();
		buffer.append("<received xmlns=\"urn:xmpp:receipts\"");
		buffer.append(" id=\"").append(StringUtils.escapeForXML(id)).append("\"");
		buffer.append(" status=\"").append(status).append("\"");
		buffer.append("/>");
		return buffer.toString();
	}
}

  

 然后我们需要定义一个Received的解析器

package org.jivesoftware.smack.provider;

import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smack.packet.Received;
import org.xmlpull.v1.XmlPullParser;

public class ReceivedProvider implements PacketExtensionProvider {

	@Override
	public PacketExtension parseExtension(XmlPullParser parser) throws Exception {
		boolean done = false;
		Received received = new Received();
		while (!done) {
			int eventType = parser.next();
			String name = parser.getName();//XML Tab标签
			if (eventType == XmlPullParser.START_TAG) {
				if (name.equals("id")) {
					received.setId(parser.nextText());
				}
				if (name.equals("status")) {
					received.setStatus(Integer.parseInt(parser.nextText().trim()));
				}
			}
			if (eventType == XmlPullParser.END_TAG) {
				if (Received.NAME.equals(name)) {
					done = true;
				}
			}
		}
		return received;
	}

}

 

我们在使用Smack的时候需要将扩展的Provider添加到ProviderManager

ProviderManager.getInstance().addExtensionProvider(Received.NAME, Received.NAME_SPACE, new ReceivedProvider());

 

我们想message添加received

Message message = new Message();
message.setTo("to");//目标
message.setFrom("from");//来源
Received received = new Received();
received.setId("CJq74-0");//回执的消息ID
received.setStatus(1);//回执状态
message.addExtension(received);
System.out.println(message.toXML());//查看XML

  

XML的格式

<message id="CJq74-1" to="to" from="from"><received xmlns="urn:xmpp:receipts" id="CJq74-0" status="1"/></message>

 

我们可以这样获得received

Received received=(Received)message.getExtension(Received.NAME, Received.NAME_SPACE);

  

 

posted @ 2015-03-16 11:00  relucent  阅读(1282)  评论(1编辑  收藏  举报