openfire vcard的创建,更新过程源码分析

来源:互联网 发布:ubuntu怎么连电信 编辑:程序博客网 时间:2024/05/24 06:02

1:  终端请求:

客户端请求更新vcard,走的是IQ消息,通过IQRouter.java路由找到IQvCardHandler.java.

IQvCardHandler.java通过handleIQ解析,得知是需要获取vcard信息:

else if (type.equals(IQ.Type.get)) {        //获取vcard信息            JID recipient = packet.getTo();            // If no TO was specified then get the vCard of the sender of the packet            if (recipient == null) {                recipient = packet.getFrom();            }            // By default return an empty vCard            result.setChildElement("vCard", "vcard-temp");            // Only try to get the vCard values of non-anonymous users            if (recipient != null) {                if (recipient.getNode() != null && server.isLocal(recipient)) {                    VCardManager vManager = VCardManager.getInstance();                    //通过VCardManager进行获取vcard信息                    Element userVCard = vManager.getVCard(recipient.getNode());                    if (userVCard != null) {                        // Check if the requester wants to ignore some vCard's fields                        Element filter = packet.getChildElement()                                .element(QName.get("filter", "vcard-temp-filter"));                        if (filter != null) {                            // Create a copy so we don't modify the original vCard                            userVCard = userVCard.createCopy();                            // Ignore fields requested by the user                            for (Iterator toFilter = filter.elementIterator(); toFilter.hasNext();)                            {                                Element field = (Element) toFilter.next();                                Element fieldToRemove = userVCard.element(field.getName());                                if (fieldToRemove != null) {                                    fieldToRemove.detach();                                }                            }                        }                        //发送信息                        result.setChildElement(userVCard);                    }                }                else {                    result = IQ.createResultIQ(packet);                    result.setChildElement(packet.getChildElement().createCopy());                    result.setError(PacketError.Condition.item_not_found);                }            } else {                result = IQ.createResultIQ(packet);                result.setChildElement(packet.getChildElement().createCopy());                result.setError(PacketError.Condition.item_not_found);            }        }

VCardProvider.java从缓存中获取vcard信息:

public Element getVCard(String username) {        Element vCardElement = getOrLoadVCard(username);        return vCardElement == null ? null : vCardElement.createCopy();    }    private Element getOrLoadVCard(String username) {        Element vCardElement = vcardCache.get(username);        if (vCardElement == null) {            vCardElement = provider.loadVCard(username);            if (vCardElement != null) {                vcardCache.put(username, vCardElement);            }        }        return vCardElement;    }

2:更新vcard信息:

IQvCardHandler.java:

if (type.equals(IQ.Type.set)) {        //保存,更新vcard信息            try {                User user = userManager.getUser(packet.getFrom().getNode());                Element vcard = packet.getChildElement();                if (vcard != null) {                    VCardManager.getInstance().setVCard(user.getUsername(), vcard);                }            }            catch (UserNotFoundException e) {                result = IQ.createResultIQ(packet);                result.setChildElement(packet.getChildElement().createCopy());                result.setError(PacketError.Condition.item_not_found);            }            catch (Exception e) {                Log.error(e.getMessage(), e);                result.setError(PacketError.Condition.internal_server_error);            }        }

VCardManager.java:

public void setVCard(String username, Element vCardElement) throws Exception {        boolean created = false;        boolean updated = false;        if (provider.isReadOnly()) {            throw new UnsupportedOperationException("VCard provider is read-only.");        }        //缓存中获取VCard        Element oldVCard = getOrLoadVCard(username);        Element newvCard = null;        // See if we need to update the vCard or insert a new one.        if (oldVCard != null) {            // Only update the vCard in the database if the vCard has changed.            if (!oldVCard.equals(vCardElement)) {                try {                //缓存中获取如果存在则更新VCard并存入缓存                    newvCard = provider.updateVCard(username, vCardElement);                    vcardCache.put(username, newvCard);                    updated = true;                }                catch (NotFoundException e) {                //不存在则创建新的VCard并存入缓存                    Log.warn("Tried to update a vCard that does not exist", e);                    newvCard = provider.createVCard(username, vCardElement);                    vcardCache.put(username, newvCard);                    created = true;                }            }        }        else {            try {            //创建新的VCard并存入缓存                newvCard = provider.createVCard(username, vCardElement);                vcardCache.put(username, newvCard);                created = true;            }            catch (AlreadyExistsException e) {            //如果存在则更新                Log.warn("Tried to create a vCard when one already exist", e);                newvCard = provider.updateVCard(username, vCardElement);                vcardCache.put(username, newvCard);                updated = true;            }        }        // Dispatch vCard events        if (created) {            // Alert listeners that a new vCard has been created            VCardEventDispatcher.dispatchVCardCreated(username, newvCard);        } else if (updated) {            // Alert listeners that a vCard has been updated            VCardEventDispatcher.dispatchVCardUpdated(username, newvCard);        }    }

DefaultVCardProvider.java:

//保存vcard    public Element createVCard(String username, Element vCardElement) throws AlreadyExistsException {        if (loadVCard(username) != null) {            // The user already has a vCard            throw new AlreadyExistsException("Username " + username + " already has a vCard");        }        Connection con = null;        PreparedStatement pstmt = null;        try {            con = DbConnectionManager.getConnection();            pstmt = con.prepareStatement(INSERT_PROPERTY);            pstmt.setString(1, username);            pstmt.setString(2, vCardElement.asXML());            pstmt.executeUpdate();        }        catch (SQLException e) {            Log.error("Error creating vCard for username: " + username, e);        }        finally {            DbConnectionManager.closeConnection(pstmt, con);        }        return vCardElement;    }    //更新vcard    public Element updateVCard(String username, Element vCardElement) throws NotFoundException {        if (loadVCard(username) == null) {            // The user already has a vCard            throw new NotFoundException("Username " + username + " does not have a vCard");        }        Connection con = null;        PreparedStatement pstmt = null;        try {            con = DbConnectionManager.getConnection();            pstmt = con.prepareStatement(UPDATE_PROPERTIES);            pstmt.setString(1, vCardElement.asXML());            pstmt.setString(2, username);            pstmt.executeUpdate();        }        catch (SQLException e) {            Log.error("Error updating vCard of username: " + username, e);        }        finally {            DbConnectionManager.closeConnection(pstmt, con);        }        return vCardElement;    }    //删除vcard    public void deleteVCard(String username) {        Connection con = null;        PreparedStatement pstmt = null;        try {            con = DbConnectionManager.getConnection();            pstmt = con.prepareStatement(DELETE_PROPERTIES);            pstmt.setString(1, username);            pstmt.executeUpdate();        }        catch (SQLException e) {            Log.error("Error deleting vCard of username: " + username, e);        }        finally {            DbConnectionManager.closeConnection(pstmt, con);        }    }



0 0
原创粉丝点击