场景:在开发一款xmpp的应用,需要获取好友资料变动并实时刷新。
1.确定问题:获取好友资料变动;
2.工具:spark和adt
3.解决问题:
?
我先用spark开启调试模式登录,在spark端和im移动客户端同时上线之后,im端修改了个人vcard数据保存,在spark端的调试窗口看到了发送过来的vcard包。说明好友更新了vcard之后服务器是有转发的。
?
然后我查看asmack 的api发现vcard需要添加一个provider:
pm.addIQProvider("vCard", "vcard-temp", new VCardProvider());
?
我们跟进去VCardProvider的源码:
?
/**
* $RCSfile$
* $Revision$
* $Date$
*
* Copyright 2003-2007 Jive Software.
*
* All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.provider;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.provider.IQProvider;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smackx.packet.VCard;
import org.w3c.dom.*;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* vCard provider.
*
* @author Gaston Dombiak
* @author Derek DeMoro
*/
public class VCardProvider implements IQProvider {
private static final String PREFERRED_ENCODING = "UTF-8";
public IQ parseIQ(XmlPullParser parser) throws Exception {
final StringBuilder sb = new StringBuilder();
try {
int event = parser.getEventType();
// get the content
while (true) {
switch (event) {
case XmlPullParser.TEXT:
// We must re-escape the xml so that the DOM won‘t throw an exception
sb.append(StringUtils.escapeForXML(parser.getText()));
break;
case XmlPullParser.START_TAG:
sb.append(‘<‘).append(parser.getName()).append(‘>‘);
break;
case XmlPullParser.END_TAG:
sb.append("</").append(parser.getName()).append(‘>‘);
break;
default:
}
if (event == XmlPullParser.END_TAG && "vCard".equals(parser.getName())) break;
event = parser.next();
}
}
catch (XmlPullParserException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
String xmlText = sb.toString();
return createVCardFromXML(xmlText);
}
/**
* Builds a users vCard from xml file.
*
* @param xml the xml representing a users vCard.
* @return the VCard.
* @throws Exception if an exception occurs.
*/
public static VCard createVCardFromXML(String xml) throws Exception {
VCard vCard = new VCard();
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.parse(
new ByteArrayInputStream(xml.getBytes(PREFERRED_ENCODING)));
new VCardReader(vCard, document).initializeFields();
return vCard;
}
private static class VCardReader {
private final VCard vCard;
private final Document document;
VCardReader(VCard vCard, Document document) {
this.vCard = vCard;
this.document = document;
}
public void initializeFields() {
vCard.setFirstName(getTagContents("GIVEN"));
vCard.setLastName(getTagContents("FAMILY"));
vCard.setMiddleName(getTagContents("MIDDLE"));
setupPhoto();
setupEmails();
vCard.setOrganization(getTagContents("ORGNAME"));
vCard.setOrganizationUnit(getTagContents("ORGUNIT"));
setupSimpleFields();
setupPhones();
setupAddresses();
}
private void setupPhoto() {
String binval = null;
String mimetype = null;
NodeList photo = document.getElementsByTagName("PHOTO");
if (photo.getLength() != 1)
return;
Node photoNode = photo.item(0);
NodeList childNodes = photoNode.getChildNodes();
int childNodeCount = childNodes.getLength();
List<Node> nodes = new ArrayList<Node>(childNodeCount);
for (int i = 0; i < childNodeCount; i++)
nodes.add(childNodes.item(i));
String name = null;
String value = null;
for (Node n : nodes) {
name = n.getNodeName();
value = n.getTextContent();
if (name.equals("BINVAL")) {
binval = value;
}
else if (name.equals("TYPE")) {
mimetype = value;
}
}
if (binval == null || mimetype == null)
return;
vCard.setAvatar(binval, mimetype);
}
private void setupEmails() {
NodeList nodes = document.getElementsByTagName("USERID");
if (nodes == null) return;
for (int i = 0; i < nodes.getLength(); i++) {
Element element = (Element) nodes.item(i);
if ("WORK".equals(element.getParentNode().getFirstChild().getNodeName())) {
vCard.setEmailWork(getTextContent(element));
}
else {
vCard.setEmailHome(getTextContent(element));
}
}
}
private void setupPhones() {
NodeList allPhones = document.getElementsByTagName("TEL");
if (allPhones == null) return;
for (int i = 0; i < allPhones.getLength(); i++) {
NodeList nodes = allPhones.item(i).getChildNodes();
String type = null;
String code = null;
String value = null;
for (int j = 0; j < nodes.getLength(); j++) {
Node node = nodes.item(j);
if (node.getNodeType() != Node.ELEMENT_NODE) continue;
String nodeName = node.getNodeName();
if ("NUMBER".equals(nodeName)) {
value = getTextContent(node);
}
else if (isWorkHome(nodeName)) {
type = nodeName;
}
else {
code = nodeName;
}
}
if (code == null || value == null) continue;
if ("HOME".equals(type)) {
vCard.setPhoneHome(code, value);
}
else { // By default, setup work phone
vCard.setPhoneWork(code, value);
}
}
}
private boolean isWorkHome(String nodeName) {
return "HOME".equals(nodeName) || "WORK".equals(nodeName);
}
private void setupAddresses() {
NodeList allAddresses = document.getElementsByTagName("ADR");
if (allAddresses == null) return;
for (int i = 0; i < allAddresses.getLength(); i++) {
Element addressNode = (Element) allAddresses.item(i);
String type = null;
List<String> code = new ArrayList<String>();
List<String> value = new ArrayList<String>();
NodeList childNodes = addressNode.getChildNodes();
for (int j = 0; j < childNodes.getLength(); j++) {
Node node = childNodes.item(j);
if (node.getNodeType() != Node.ELEMENT_NODE) continue;
String nodeName = node.getNodeName();
if (isWorkHome(nodeName)) {
type = nodeName;
}
else {
code.add(nodeName);
value.add(getTextContent(node));
}
}
for (int j = 0; j < value.size(); j++) {
if ("HOME".equals(type)) {
vCard.setAddressFieldHome((String) code.get(j), (String) value.get(j));
}
else { // By default, setup work address
vCard.setAddressFieldWork((String) code.get(j), (String) value.get(j));
}
}
}
}
private String getTagContents(String tag) {
NodeList nodes = document.getElementsByTagName(tag);
if (nodes != null && nodes.getLength() == 1) {
return getTextContent(nodes.item(0));
}
return null;
}
private void setupSimpleFields() {
NodeList childNodes = document.getDocumentElement().getChildNodes();
for (int i = 0; i < childNodes.getLength(); i++) {
Node node = childNodes.item(i);
if (node instanceof Element) {
Element element = (Element) node;
String field = element.getNodeName();
if (element.getChildNodes().getLength() == 0) {
vCard.setField(field, "");
}
else if (element.getChildNodes().getLength() == 1 &&
element.getChildNodes().item(0) instanceof Text) {
vCard.setField(field, getTextContent(element));
}
}
}
}
private String getTextContent(Node node) {
StringBuilder result = new StringBuilder();
appendText(result, node);
return result.toString();
}
private void appendText(StringBuilder result, Node node) {
NodeList childNodes = node.getChildNodes();
for (int i = 0; i < childNodes.getLength(); i++) {
Node nd = childNodes.item(i);
String nodeValue = nd.getNodeValue();
if (nodeValue != null) {
result.append(nodeValue);
}
appendText(result, nd);
}
}
}
}
?
了解provider的人都知道,这是一个iq包的处理器类,这个VCardProvider是api提供的,跟踪到里面的代码发现,这个类主要是获取到了vcard数据然后封装成了vcard对象返回回去。
?
那么问题来了,我们要怎么才能拿到这个vcard数据呢?
?
那么我们回归到这段代码,
pm.addIQProvider("vCard", "vcard-temp", new VCardProvider());
我猜测providerManager一定有一个集合,里面有很多provider,当有iq包过来的时候会把这些iq包通过命名空间过滤然后交给相应的provider处理。现在我们看下源码:
?
?
private Map<String, Object> extensionProviders = new ConcurrentHashMap<String, Object>();
private Map<String, Object> iqProviders = new ConcurrentHashMap<String, Object>();
查看源码我们发现确实是这样的。
?
?
那么在哪里要用到这个provider呢?我们跟踪进去看,发现在packetParseUtil下有一个parseIQ方法专门解析iq包,里面有关键的一段:
?
?
Object provider = ProviderManager.getInstance().getIQProvider(elementName, namespace);
if (provider != null) {
if (provider instanceof IQProvider) {
iqPacket = ((IQProvider)provider).parseIQ(parser);
}
else if (provider instanceof Class) {
iqPacket = (IQ)PacketParserUtils.parseWithIntrospection(elementName,
(Class<?>)provider, parser);
}
}
?
从代码我们很容易看明白,从这里获取到了provider,parseIQ又是谁在调用呢?我们继续跟进,发现是PacketReader的parsePackets(Thread)方法,我们继续跟进:
?
private void processPacket(Packet packet) {
if (packet == null) {
return;
}
// Loop through all collectors and notify the appropriate ones.
for (PacketCollector collector: connection.getPacketCollectors()) {
collector.processPacket(packet);
}
// Deliver the incoming packet to listeners.
listenerExecutor.submit(new ListenerNotification(packet));
}
发现交给了、listenerExecutor处理,发现这里有一个类
?
?
* A runnable to notify all listeners of a packet.
*/
private class ListenerNotification implements Runnable {
private Packet packet;
public ListenerNotification(Packet packet) {
this.packet = packet;
}
public void run() {
for (ListenerWrapper listenerWrapper : connection.recvListeners.values()) {
try {
listenerWrapper.notifyListener(packet);
} catch (Exception e) {
System.err.println("Exception in packet listener: " + e);
e.printStackTrace();
}
}
}
}
这个类有一个重要的方法:
?
?
public void notifyListener(Packet packet) {
if (packetFilter == null || packetFilter.accept(packet)) {
packetListener.processPacket(packet);
}
}
到了这里我们就能明白了,原来iqprovider处理后的数据最终都是交给了packetListener处理,要想处理这个packet就需要制定filter匹配规则,然后我们发现在构造这个filter的时候有一种方式是通过制定类构造的,到了这里很明白了,之所以返回的是vcard对象就是要通过packet拿到的时候能直接转换成vcard对象拿来使用,一下子云开雾散。
?
?
其他的所有的provider的原理都是这样,搞了几个小时才搞出来的,希望大家尊敬我的劳动成果,在转载的时候添加上转载,谢谢。
原文:http://zhonglunshun.iteye.com/blog/2246587