@@ -84,42 +84,43 @@ private NodeViewAcl getNodeViewAcl() {
8484 @ Override
8585 public void process (Element elm , JID actorJID , IQ reqIQ , Element rsm )
8686 throws Exception {
87- node = elm .attributeValue ("node" );
88- requestIq = reqIQ ;
89- reply = IQ .createResultIQ (reqIQ );
90- element = elm ;
87+ node = elm .attributeValue ("node" );
88+ requestIq = reqIQ ;
89+ reply = IQ .createResultIQ (reqIQ );
90+ element = elm ;
9191 resultSetManagement = rsm ;
9292
9393 if (false == channelManager .isLocalJID (requestIq .getFrom ())) {
94- reply .getElement ().addAttribute ("remote-server-discover" , "false" );
95- }
94+ reply .getElement ().addAttribute ("remote-server-discover" , "false" );
95+ }
96+
97+ isSubscriptionsNode = node .substring (node .length () - 13 ).equals (
98+ "subscriptions" );
9699
97- isSubscriptionsNode = node .substring (node .length () - 13 ).equals ("subscriptions" );
98-
99100 boolean isCached = false ;
100101 if (true == isSubscriptionsNode ) {
101102 isCached = channelManager .nodeHasSubscriptions (node );
102103 } else {
103104 isCached = channelManager .isCachedNode (node );
104105 }
105-
106+
106107 fetchersJid = requestIq .getFrom ();
107108
108- if (false == channelManager .isLocalNode (node )
109- && ( false == isCached )) {
110- logger . debug ( "Node " + node + " is remote and not cached, off to get some data" );
109+ if (false == channelManager .isLocalNode (node ) && ( false == isCached )) {
110+ logger . debug ( "Node " + node
111+ + " is remote and not cached, off to get some data" );
111112 makeRemoteRequest ();
112- return ;
113+ return ;
113114 }
114-
115+
115116 try {
116117 if (false == nodeExists ()) {
117118 setErrorCondition (PacketError .Type .cancel ,
118119 PacketError .Condition .item_not_found );
119120 outQueue .put (reply );
120121 return ;
121122 }
122-
123+
123124 if (actorJID != null ) {
124125 fetchersJid = actorJID ;
125126 }
@@ -128,22 +129,47 @@ public void process(Element elm, JID actorJID, IQ reqIQ, Element rsm)
128129 outQueue .put (reply );
129130 return ;
130131 }
131- getItems ();
132+ xmlReader = new SAXReader ();
133+ if (null == element .element ("item" )) {
134+ getItems ();
135+ } else {
136+ if (false == getItem ())
137+ return ;
138+ }
132139 } catch (NodeStoreException e ) {
133140 setErrorCondition (PacketError .Type .wait ,
134141 PacketError .Condition .internal_server_error );
135142 }
136143 outQueue .put (reply );
137144 }
138145
146+ private boolean getItem () throws Exception {
147+ NodeItem nodeItem = channelManager .getNodeItem (node ,
148+ element .element ("item" ).attributeValue ("id" ));
149+ if (null == nodeItem ) {
150+ if (false == channelManager .isLocalNode (node )) {
151+ makeRemoteRequest ();
152+ return false ;
153+ }
154+ setErrorCondition (PacketError .Type .cancel ,
155+ PacketError .Condition .item_not_found );
156+ return true ;
157+ }
158+ Element pubsub = reply .getElement ().addElement ("pubsub" );
159+ pubsub .addNamespace ("" , JabberPubsub .NAMESPACE_URI );
160+ Element items = pubsub .addElement ("items" ).addAttribute ("node" , node );
161+ addItemToResponse (nodeItem , items );
162+ return true ;
163+ }
164+
139165 private void makeRemoteRequest () throws InterruptedException {
140166 requestIq .setTo (new JID (node .split ("/" )[2 ]).getDomain ());
141167 if (null == requestIq .getElement ().element ("pubsub" ).element ("actor" )) {
142- Element actor = requestIq .getElement ().element ("pubsub" )
143- .addElement ("actor" , JabberPubsub .NS_BUDDYCLOUD );
144- actor .addText (requestIq .getFrom ().toBareJID ());
168+ Element actor = requestIq .getElement ().element ("pubsub" )
169+ .addElement ("actor" , JabberPubsub .NS_BUDDYCLOUD );
170+ actor .addText (requestIq .getFrom ().toBareJID ());
145171 }
146- outQueue .put (requestIq );
172+ outQueue .put (requestIq );
147173 }
148174
149175 private boolean nodeExists () throws NodeStoreException {
@@ -184,15 +210,20 @@ private void getItems() throws Exception {
184210 if (after != null ) {
185211 try {
186212 // Try and parse it as a global item id
187- GlobalItemID afterGlobalItemID = GlobalItemIDImpl .fromString (after .getTextTrim ());
213+ GlobalItemID afterGlobalItemID = GlobalItemIDImpl
214+ .fromString (after .getTextTrim ());
188215 afterItemId = afterGlobalItemID .getItemID ();
189-
216+
190217 // Check it's for the correct node
191- if (!afterGlobalItemID .getNodeID ().equals (node )) {
192- createExtendedErrorReply (Type .modify , Condition .item_not_found , "RSM 'after' specifies an unexpected NodeID: " + afterGlobalItemID .getNodeID ());
218+ if (!afterGlobalItemID .getNodeID ().equals (node )) {
219+ createExtendedErrorReply (Type .modify ,
220+ Condition .item_not_found ,
221+ "RSM 'after' specifies an unexpected NodeID: "
222+ + afterGlobalItemID .getNodeID ());
193223 }
194- } catch (IllegalArgumentException e ) {
195- // If the after isn't a valid 'tag:...' then it might just be a straight ItemID
224+ } catch (IllegalArgumentException e ) {
225+ // If the after isn't a valid 'tag:...' then it might just
226+ // be a straight ItemID
196227 afterItemId = after .getTextTrim ();
197228 }
198229 }
@@ -201,7 +232,6 @@ private void getItems() throws Exception {
201232 Element items = pubsub .addElement ("items" );
202233 items .addAttribute ("node" , node );
203234
204- xmlReader = new SAXReader ();
205235 entry = null ;
206236 int totalEntriesCount = 0 ;
207237
@@ -214,12 +244,12 @@ private void getItems() throws Exception {
214244 }
215245
216246 if ((false == channelManager .isLocalNode (node ))
217- && (0 == rsmEntriesCount )) {
247+ && (0 == rsmEntriesCount )) {
218248 logger .debug ("No results in cache for remote node, so "
219- + "we're going federated to get more" );
220- makeRemoteRequest ();
221- return ;
222- }
249+ + "we're going federated to get more" );
250+ makeRemoteRequest ();
251+ return ;
252+ }
223253
224254 if ((resultSetManagement != null )
225255 || (totalEntriesCount > maxItemsToReturn )) {
@@ -296,33 +326,34 @@ private int getNodeItems(Element items, int maxItemsToReturn,
296326 while (itemIt .hasNext ()) {
297327 ++rsmEntriesCount ;
298328 NodeItem nodeItem = itemIt .next ();
299-
329+
300330 if (firstItem == null ) {
301331 firstItem = nodeItem .getId ();
302332 }
303- try {
304- entry = xmlReader .read (
305- new StringReader (nodeItem .getPayload ()))
306- .getRootElement ();
307- Element item = items .addElement ("item" );
308- item .addAttribute ("id" , nodeItem .getId ());
309- item .add (entry );
310- lastItem = nodeItem .getId ();
311- } catch (DocumentException e ) {
312- logger .error ("Error parsing a node entry, ignoring. "
313- + nodeItem );
314- }
315-
333+ addItemToResponse (nodeItem , items );
334+ lastItem = nodeItem .getId ();
316335 }
317- logger .debug ("Including RSM there are " + rsmEntriesCount
318- + " items for node " + node );
336+ logger .debug ("Including RSM there are " + rsmEntriesCount
337+ + " items for node " + node );
319338 return channelManager .countNodeItems (node );
320339 } finally {
321340 if (itemIt != null )
322341 itemIt .close ();
323342 }
324343 }
325344
345+ private void addItemToResponse (NodeItem nodeItem , Element parent ) {
346+ try {
347+ entry = xmlReader .read (new StringReader (nodeItem .getPayload ()))
348+ .getRootElement ();
349+ Element item = parent .addElement ("item" );
350+ item .addAttribute ("id" , nodeItem .getId ());
351+ item .add (entry );
352+ } catch (DocumentException e ) {
353+ logger .error ("Error parsing a node entry, ignoring. " + nodeItem );
354+ }
355+ }
356+
326357 /**
327358 * Get items for the /subscriptions node
328359 */
@@ -339,12 +370,12 @@ private int getSubscriptionItems(Element items, int maxItemsToReturn,
339370 Element query ;
340371
341372 for (NodeSubscription subscriber : subscribers ) {
342-
373+
343374 jidItem = items .addElement ("item" );
344375 jidItem .addAttribute ("id" , subscriber .getUser ().toString ());
345376 query = jidItem .addElement ("query" );
346377 query .addNamespace ("" , JabberPubsub .NS_DISCO_ITEMS );
347-
378+
348379 if (firstItem == null ) {
349380 firstItem = subscriber .getUser ().toString ();
350381 }
@@ -360,18 +391,19 @@ private void addSubscriptionItems(Element query, JID subscriber)
360391
361392 ResultSet <NodeSubscription > subscriptions = channelManager
362393 .getUserSubscriptions (subscriber );
363-
394+
364395 if ((null == subscriptions ) || (0 == subscriptions .size ())) {
365396 return ;
366397 }
367398 Element item ;
368399 Namespace ns1 = new Namespace ("ns1" , JabberPubsub .NAMESPACE_URI );
369400 Namespace ns2 = new Namespace ("ns2" , JabberPubsub .NAMESPACE_URI );
370- // TODO: This whole section of code is very inefficient
401+ // TODO: This whole section of code is very inefficient
371402 for (NodeSubscription subscription : subscriptions ) {
372- ////if (false == subscription.getNodeId().contains(fetchersJid.toBareJID())) {
373- // continue;
374- //}
403+ // //if (false ==
404+ // subscription.getNodeId().contains(fetchersJid.toBareJID())) {
405+ // continue;
406+ // }
375407 NodeAffiliation affiliation = channelManager .getUserAffiliation (
376408 subscription .getNodeId (), subscription .getUser ());
377409 item = query .addElement ("item" );
@@ -381,10 +413,10 @@ private void addSubscriptionItems(Element query, JID subscriber)
381413 item .addAttribute ("node" , subscription .getNodeId ());
382414 QName affiliationAttribute = new QName ("affiliation" , ns1 );
383415 QName subscriptionAttribute = new QName ("subscription" , ns2 );
384- item .addAttribute (affiliationAttribute , affiliation . getAffiliation ()
385- .toString ());
386- item .addAttribute (subscriptionAttribute , subscription . getSubscription ()
387- .toString ());
416+ item .addAttribute (affiliationAttribute , affiliation
417+ .getAffiliation (). toString ());
418+ item .addAttribute (subscriptionAttribute , subscription
419+ .getSubscription (). toString ());
388420 }
389421 }
390422
0 commit comments