|
9 | 9 | import org.buddycloud.channelserver.packetprocessor.iq.namespace.pubsub.JabberPubsub; |
10 | 10 | import org.buddycloud.channelserver.packetprocessor.iq.namespace.pubsub.PubSubElementProcessorAbstract; |
11 | 11 | import org.buddycloud.channelserver.pubsub.model.NodeMembership; |
| 12 | +import org.buddycloud.channelserver.pubsub.subscription.Subscriptions; |
12 | 13 | import org.buddycloud.channelserver.utils.XMLConstants; |
13 | 14 | import org.buddycloud.channelserver.utils.node.item.payload.Buddycloud; |
14 | 15 | import org.dom4j.Element; |
|
19 | 20 |
|
20 | 21 | public class AffiliationsGet extends PubSubElementProcessorAbstract { |
21 | 22 |
|
22 | | - private final BlockingQueue<Packet> outQueue; |
23 | | - private final ChannelManager channelManager; |
| 23 | + private static final Logger LOGGER = Logger.getLogger(AffiliationsGet.class); |
24 | 24 |
|
25 | | - private IQ requestIq; |
26 | | - private String node; |
27 | | - private JID actorJid; |
28 | | - private IQ result; |
29 | | - private String firstItem; |
| 25 | + public AffiliationsGet(BlockingQueue<Packet> outQueue, ChannelManager channelManager) { |
| 26 | + this.outQueue = outQueue; |
| 27 | + this.channelManager = channelManager; |
| 28 | + } |
30 | 29 |
|
31 | | - private static final Logger LOGGER = Logger.getLogger(AffiliationsGet.class); |
| 30 | + @Override |
| 31 | + public void process(Element elm, JID actorJID, IQ reqIQ, Element rsm) throws Exception { |
| 32 | + response = IQ.createResultIQ(reqIQ); |
| 33 | + request = reqIQ; |
| 34 | + actor = actorJID; |
32 | 35 |
|
| 36 | + node = request.getChildElement().element("affiliations").attributeValue(XMLConstants.NODE_ATTR); |
| 37 | + if (!Configuration.getInstance().isLocalJID(request.getFrom())) { |
| 38 | + response.getElement().addAttribute(XMLConstants.REMOTE_SERVER_DISCOVER_ATTR, |
| 39 | + Boolean.FALSE.toString()); |
33 | 40 |
|
34 | | - public AffiliationsGet(BlockingQueue<Packet> outQueue, ChannelManager channelManager) { |
35 | | - this.outQueue = outQueue; |
36 | | - this.channelManager = channelManager; |
37 | 41 | } |
38 | | - |
39 | | - @Override |
40 | | - public void process(Element elm, JID actorJID, IQ reqIQ, Element rsm) throws Exception { |
41 | | - result = IQ.createResultIQ(reqIQ); |
42 | | - requestIq = reqIQ; |
43 | | - actorJid = actorJID; |
44 | | - |
45 | | - node = elm.attributeValue(XMLConstants.NODE_ATTR); |
46 | | - if (!Configuration.getInstance().isLocalJID(requestIq.getFrom())) { |
47 | | - result.getElement().addAttribute(XMLConstants.REMOTE_SERVER_DISCOVER_ATTR, Boolean.FALSE.toString()); |
48 | | - |
49 | | - } |
50 | | - String namespace = JabberPubsub.NS_PUBSUB_OWNER; |
51 | | - if (node == null) { |
52 | | - namespace = JabberPubsub.NAMESPACE_URI; |
53 | | - } |
54 | | - |
55 | | - |
56 | | - Element pubsub = result.setChildElement(XMLConstants.PUBSUB_ELEM, namespace); |
57 | | - Element affiliations = pubsub.addElement(XMLConstants.AFFILIATION_ELEM); |
58 | | - |
59 | | - if (actorJid == null) { |
60 | | - actorJid = requestIq.getFrom(); |
61 | | - } |
62 | | - |
63 | | - boolean isProcessedLocally = true; |
64 | | - if (node == null) { |
65 | | - isProcessedLocally = getUserMemberships(affiliations); |
66 | | - } else { |
67 | | - isProcessedLocally = getNodeAffiliations(affiliations); |
68 | | - } |
69 | | - if (!isProcessedLocally) { |
70 | | - return; |
71 | | - } |
72 | | - |
73 | | - outQueue.put(result); |
| 42 | + String namespace = JabberPubsub.NAMESPACE_URI; |
| 43 | + if (node == null) { |
| 44 | + namespace = JabberPubsub.NS_PUBSUB_OWNER; |
74 | 45 | } |
75 | 46 |
|
76 | | - private boolean getNodeAffiliations(Element affiliations) throws NodeStoreException, InterruptedException { |
77 | | - if (!Configuration.getInstance().isLocalNode(node) && (!channelManager.isCachedNode(node))) { |
78 | 47 |
|
79 | | - makeRemoteRequest(node.split("/")[2]); |
80 | | - return false; |
81 | | - } |
82 | | - ResultSet<NodeMembership> nodeMemberships; |
83 | | - nodeMemberships = channelManager.getNodeMemberships(node); |
| 48 | + Element pubsub = response.setChildElement(XMLConstants.PUBSUB_ELEM, namespace); |
| 49 | + Element affiliations = pubsub.addElement(XMLConstants.AFFILIATIONS_ELEM); |
84 | 50 |
|
85 | | - if ((!nodeMemberships.isEmpty()) && (!Configuration.getInstance().isLocalNode(node))) { |
86 | | - makeRemoteRequest(node.split("/")[2]); |
87 | | - return false; |
88 | | - } |
| 51 | + if (actor == null) { |
| 52 | + actor = request.getFrom(); |
| 53 | + } |
89 | 54 |
|
| 55 | + boolean isProcessedLocally = |
| 56 | + (node == null) ? getUserMemberships(affiliations) : getNodeAffiliations(affiliations); |
| 57 | + if (!isProcessedLocally) { |
| 58 | + return; |
| 59 | + } |
90 | 60 |
|
91 | | - for (NodeMembership nodeMembership : nodeMemberships) { |
| 61 | + outQueue.put(response); |
| 62 | + } |
92 | 63 |
|
93 | | - if (actorJid.toBareJID().equals(nodeMembership.getUser().toBareJID())) { |
94 | | - if (null == firstItem) { |
95 | | - firstItem = nodeMembership.getUser().toString(); |
96 | | - } |
| 64 | + private boolean getNodeAffiliations(Element affiliations) throws NodeStoreException, |
| 65 | + InterruptedException { |
97 | 66 |
|
98 | | - affiliations.addElement(XMLConstants.AFFILIATION_ELEM).addAttribute(XMLConstants.NODE_ATTR, nodeMembership.getNodeId()) |
99 | | - .addAttribute(XMLConstants.AFFILIATION_ELEM, nodeMembership.getAffiliation().toString()) |
100 | | - .addAttribute(XMLConstants.JID_ATTR, nodeMembership.getUser().toString()); |
101 | | - } |
102 | | - } |
| 67 | + if (!Configuration.getInstance().isLocalNode(node) && (!channelManager.isCachedNode(node))) { |
| 68 | + |
| 69 | + makeRemoteRequest(node.split("/")[2]); |
| 70 | + return false; |
| 71 | + } |
| 72 | + ResultSet<NodeMembership> nodeMemberships; |
| 73 | + nodeMemberships = channelManager.getNodeMemberships(node); |
103 | 74 |
|
104 | | - return true; |
| 75 | + if ((nodeMemberships.isEmpty()) && (!Configuration.getInstance().isLocalNode(node))) { |
| 76 | + makeRemoteRequest(node.split("/")[2]); |
| 77 | + return false; |
105 | 78 | } |
106 | 79 |
|
107 | | - private boolean isOwnerModerator() throws NodeStoreException { |
108 | | - return channelManager.getNodeMembership(node, actorJid).getAffiliation().canAuthorize(); |
| 80 | + |
| 81 | + for (NodeMembership nodeMembership : nodeMemberships) { |
| 82 | + if (!nodeMembership.getSubscription().equals(Subscriptions.subscribed) |
| 83 | + && !actor.toBareJID().equals(nodeMembership.getUser().toBareJID()) && !isOwnerModerator()) { |
| 84 | + continue; |
| 85 | + } |
| 86 | + if (nodeMembership.getSubscription().equals(Subscriptions.none)) { |
| 87 | + continue; |
| 88 | + } |
| 89 | + Element affiliation = affiliations.addElement(XMLConstants.AFFILIATION_ELEM); |
| 90 | + affiliation.addAttribute(XMLConstants.NODE_ATTR, nodeMembership.getNodeId()); |
| 91 | + affiliation.addAttribute(XMLConstants.AFFILIATION_ELEM, nodeMembership.getAffiliation() |
| 92 | + .toString()); |
| 93 | + affiliation.addAttribute(XMLConstants.JID_ATTR, nodeMembership.getUser().toString()); |
| 94 | + if ((actor.toBareJID().equals(nodeMembership.getUser().toBareJID()) || isOwnerModerator()) |
| 95 | + && (null != nodeMembership.getInvitedBy())) { |
| 96 | + affiliation.addAttribute(XMLConstants.INVITED_BY_ATTR, nodeMembership.getInvitedBy() |
| 97 | + .toBareJID()); |
| 98 | + } |
109 | 99 | } |
110 | 100 |
|
111 | | - private boolean getUserMemberships(Element affiliations) throws NodeStoreException, InterruptedException { |
| 101 | + return true; |
| 102 | + } |
112 | 103 |
|
113 | | - if (!Configuration.getInstance().isLocalJID(actorJid) && (!channelManager.isCachedJID(requestIq.getFrom()))) { |
114 | | - makeRemoteRequest(actorJid.getDomain()); |
115 | | - return false; |
116 | | - } |
| 104 | + private boolean isOwnerModerator() throws NodeStoreException { |
| 105 | + return channelManager.getNodeMembership(node, actor).getAffiliation().canAuthorize(); |
| 106 | + } |
117 | 107 |
|
118 | | - ResultSet<NodeMembership> memberships = channelManager.getUserMemberships(actorJid); |
| 108 | + private boolean getUserMemberships(Element affiliations) throws NodeStoreException, |
| 109 | + InterruptedException { |
119 | 110 |
|
120 | | - for (NodeMembership membership : memberships) { |
| 111 | + if (!channelManager.isCachedJID(request.getFrom()) |
| 112 | + && !Configuration.getInstance().isLocalJID(actor)) { |
| 113 | + makeRemoteRequest(actor.getDomain()); |
| 114 | + return false; |
| 115 | + } |
121 | 116 |
|
122 | | - if (actorJid.toBareJID().equals(membership.getUser().toBareJID())) { |
123 | | - LOGGER.trace("Adding affiliation for " + membership.getUser() + " affiliation " + membership.getAffiliation() + " (no node provided)"); |
| 117 | + ResultSet<NodeMembership> memberships = channelManager.getUserMemberships(actor); |
124 | 118 |
|
125 | | - if (null == firstItem) { |
126 | | - firstItem = membership.getNodeId(); |
127 | | - } |
| 119 | + for (NodeMembership membership : memberships) { |
128 | 120 |
|
129 | | - affiliations.addElement(XMLConstants.AFFILIATION_ELEM).addAttribute(XMLConstants.NODE_ATTR, membership.getNodeId()) |
130 | | - .addAttribute(XMLConstants.AFFILIATION_ELEM, membership.getAffiliation().toString()) |
131 | | - .addAttribute(XMLConstants.JID_ATTR, membership.getUser().toBareJID()); |
132 | | - } |
| 121 | + if (membership.getSubscription().equals(Subscriptions.none)) { |
| 122 | + continue; |
| 123 | + } |
| 124 | + Element affiliation = affiliations.addElement(XMLConstants.AFFILIATION_ELEM); |
| 125 | + affiliation.addAttribute(XMLConstants.NODE_ATTR, membership.getNodeId()); |
| 126 | + affiliation.addAttribute(XMLConstants.AFFILIATION_ELEM, membership.getAffiliation() |
| 127 | + .toString()); |
| 128 | + affiliation.addAttribute(XMLConstants.JID_ATTR, membership.getUser().toBareJID()); |
133 | 129 |
|
134 | | - } |
135 | | - return true; |
136 | | - } |
| 130 | + if (membership.getSubscription().equals(Subscriptions.invited) |
| 131 | + && (null != membership.getInvitedBy())) { |
| 132 | + affiliation.addAttribute(XMLConstants.INVITED_BY_ATTR, membership.getInvitedBy() |
| 133 | + .toBareJID()); |
| 134 | + } |
137 | 135 |
|
138 | | - private void makeRemoteRequest(String node) throws InterruptedException { |
139 | | - LOGGER.info("Going federated for <affiliations />"); |
140 | | - requestIq.setTo(new JID(node).getDomain()); |
141 | | - if (null == requestIq.getElement().element("pubsub").element("actor")) { |
142 | | - Element actor = requestIq.getElement().element("pubsub").addElement("actor", Buddycloud.NS); |
143 | | - actor.addText(requestIq.getFrom().toBareJID()); |
144 | | - } |
145 | | - outQueue.put(requestIq); |
146 | 136 | } |
147 | | - |
148 | | - @Override |
149 | | - public boolean accept(Element elm) { |
150 | | - return XMLConstants.AFFILIATION_ELEM.equals(elm.getName()); |
| 137 | + return true; |
| 138 | + } |
| 139 | + |
| 140 | + private void makeRemoteRequest(String node) throws InterruptedException { |
| 141 | + LOGGER.info("Going federated for <affiliations />"); |
| 142 | + request.setTo(new JID(node).getDomain()); |
| 143 | + if (null == request.getElement().element("pubsub").element("actor")) { |
| 144 | + Element actor = request.getElement().element("pubsub").addElement("actor", Buddycloud.NS); |
| 145 | + actor.addText(request.getFrom().toBareJID()); |
151 | 146 | } |
| 147 | + outQueue.put(request); |
| 148 | + } |
| 149 | + |
| 150 | + @Override |
| 151 | + public boolean accept(Element elm) { |
| 152 | + return XMLConstants.AFFILIATIONS_ELEM.equals(elm.getName()); |
| 153 | + } |
152 | 154 | } |
0 commit comments