@@ -42,17 +42,19 @@ public class NodeItemsGet extends PubSubElementProcessorAbstract {
4242 private String lastItem ;
4343 private SAXReader xmlReader ;
4444 private Element entry ;
45- private IQ requestIq ;
4645 private IQ reply ;
4746 private Element resultSetManagement ;
4847 private Element element ;
48+ private boolean parentOnly = false ;
4949
5050 private Map <String , String > nodeDetails ;
5151
5252 private int rsmEntriesCount ;
5353
5454 private JID actor ;
5555
56+ private Element items ;
57+
5658 public NodeItemsGet (BlockingQueue <Packet > outQueue , ChannelManager channelManager ) {
5759 setOutQueue (outQueue );
5860 setChannelManager (channelManager );
@@ -66,31 +68,37 @@ public void setChannelManager(ChannelManager ds) {
6668
6769 @ Override
6870 public void process (Element elm , JID actorJID , IQ reqIQ , Element rsm ) throws Exception {
69- node = elm . attributeValue ( XMLConstants . NODE_ATTR ) ;
70- requestIq = reqIQ ;
71+ request = reqIQ ;
72+
7173 reply = IQ .createResultIQ (reqIQ );
7274 element = elm ;
7375 resultSetManagement = rsm ;
7476
75- if (!Configuration .getInstance ().isLocalJID (requestIq .getFrom ())) {
77+ if (!Configuration .getInstance ().isLocalJID (request .getFrom ())) {
7678 reply .getElement ().addAttribute (XMLConstants .REMOTE_SERVER_DISCOVER_ATTR , Boolean .FALSE .toString ());
7779 }
7880
79- boolean isCached = channelManager .isCachedNode (node );
80-
81- this .actor = actorJID ;
82- if (null == this .actor ) {
83- this .actor = requestIq .getFrom ();
84- }
85-
86- if (!Configuration .getInstance ().isLocalNode (node ) && !isCached ) {
87- LOGGER .debug ("Node " + node + " is remote and not cached, off to get some data" );
88-
89- makeRemoteRequest ();
90- return ;
91- }
92-
9381 try {
82+
83+ if (!isValidStanza ()) {
84+ outQueue .put (reply );
85+ return ;
86+ }
87+
88+ boolean isCached = channelManager .isCachedNode (node );
89+
90+ this .actor = actorJID ;
91+ if (null == this .actor ) {
92+ this .actor = request .getFrom ();
93+ }
94+
95+ if (!Configuration .getInstance ().isLocalNode (node ) && !isCached ) {
96+ LOGGER .debug ("Node " + node + " is remote and not cached, off to get some data" );
97+
98+ makeRemoteRequest ();
99+ return ;
100+ }
101+
94102 if (!checkNodeExists ()) {
95103 setErrorCondition (PacketError .Type .cancel , PacketError .Condition .item_not_found );
96104 outQueue .put (reply );
@@ -102,7 +110,7 @@ public void process(Element elm, JID actorJID, IQ reqIQ, Element rsm) throws Exc
102110 return ;
103111 }
104112 xmlReader = new SAXReader ();
105- if (element .element (XMLConstants .ITEM_ELEM ) == null ) {
113+ if (null == items .element (XMLConstants .ITEM_ELEM )) {
106114 getItems ();
107115 } else {
108116 if (!getItem ()) {
@@ -112,13 +120,22 @@ public void process(Element elm, JID actorJID, IQ reqIQ, Element rsm) throws Exc
112120 } catch (NodeStoreException e ) {
113121 LOGGER .error (e );
114122 setErrorCondition (PacketError .Type .wait , PacketError .Condition .internal_server_error );
123+ } catch (NullPointerException e ) {
124+ LOGGER .error (e );
125+ setErrorCondition (PacketError .Type .modify , PacketError .Condition .bad_request );
115126 }
116127 outQueue .put (reply );
117128 }
129+
130+ protected boolean isValidStanza () {
131+ Element pubsub = request .getElement ().element (XMLConstants .PUBSUB_ELEM );
132+ items = pubsub .element (XMLConstants .ITEMS_ELEM );
133+ node = items .attributeValue (XMLConstants .NODE_ATTR );
134+ return true ;
135+ }
118136
119137 private boolean getItem () throws Exception {
120- NodeItem nodeItem = channelManager .getNodeItem (node , element .element (XMLConstants .ITEM_ELEM ).attributeValue (XMLConstants .ID_ATTR ));
121-
138+ NodeItem nodeItem = channelManager .getNodeItem (node , items .element (XMLConstants .ITEM_ELEM ).attributeValue (XMLConstants .ID_ATTR ));
122139 if (nodeItem == null ) {
123140 if (!Configuration .getInstance ().isLocalNode (node )) {
124141 makeRemoteRequest ();
@@ -137,12 +154,12 @@ private boolean getItem() throws Exception {
137154
138155 @ Override
139156 protected void makeRemoteRequest () throws InterruptedException {
140- requestIq .setTo (new JID (node .split ("/" )[2 ]).getDomain ());
141- if (null == requestIq .getElement ().element (XMLConstants .PUBSUB_ELEM ).element (XMLConstants .ACTOR_ELEM )) {
142- Element actor = requestIq .getElement ().element (XMLConstants .PUBSUB_ELEM ).addElement (XMLConstants .ACTOR_ELEM , Buddycloud .NS );
143- actor .addText (requestIq .getFrom ().toBareJID ());
157+ request .setTo (new JID (node .split ("/" )[2 ]).getDomain ());
158+ if (null == request .getElement ().element (XMLConstants .PUBSUB_ELEM ).element (XMLConstants .ACTOR_ELEM )) {
159+ Element actor = request .getElement ().element (XMLConstants .PUBSUB_ELEM ).addElement (XMLConstants .ACTOR_ELEM , Buddycloud .NS );
160+ actor .addText (request .getFrom ().toBareJID ());
144161 }
145- outQueue .put (requestIq );
162+ outQueue .put (request );
146163 }
147164
148165 @ Override
@@ -169,7 +186,12 @@ private void getItems() throws Exception {
169186 int maxItemsToReturn = MAX_ITEMS_TO_RETURN ;
170187 String afterItemId = null ;
171188
172- String maxItems = element .attributeValue (XMLConstants .MAX_ITEMS_ATTR );
189+ String parentOnlyAttribute = request .getChildElement ().element (XMLConstants .ITEMS_ELEM ).attributeValue (XMLConstants .PARENT_ONLY_ATTR );
190+ if ((null != parentOnlyAttribute ) && ((Boolean .TRUE .toString ().equals (parentOnlyAttribute )) || ("1" .equals (parentOnlyAttribute )))) {
191+ parentOnly = true ;
192+ }
193+
194+ String maxItems = items .attributeValue (XMLConstants .MAX_ITEMS_ATTR );
173195
174196 if (maxItems != null ) {
175197 maxItemsToReturn = Integer .parseInt (maxItems );
@@ -205,7 +227,7 @@ private void getItems() throws Exception {
205227 items .addAttribute (XMLConstants .NODE_ATTR , node );
206228
207229 entry = null ;
208- int totalEntriesCount = getNodeItems (items , maxItemsToReturn , afterItemId );
230+ int totalEntriesCount = getNodeItems (items , maxItemsToReturn , afterItemId , parentOnly );
209231
210232 if ((false == Configuration .getInstance ().isLocalNode (node )) && (0 == rsmEntriesCount )) {
211233 LOGGER .debug ("No results in cache for remote node, so " + "we're going federated to get more" );
@@ -254,9 +276,9 @@ private AccessModels getNodeAccessModel() {
254276 /**
255277 * Get items nodes
256278 */
257- private int getNodeItems (Element items , int maxItemsToReturn , String afterItemId ) throws NodeStoreException {
279+ private int getNodeItems (Element items , int maxItemsToReturn , String afterItemId , boolean parentOnly ) throws NodeStoreException {
258280
259- CloseableIterator <NodeItem > itemIt = channelManager .getNodeItems (node , afterItemId , maxItemsToReturn );
281+ CloseableIterator <NodeItem > itemIt = channelManager .getNodeItems (node , afterItemId , maxItemsToReturn , parentOnly );
260282 rsmEntriesCount = 0 ;
261283 if (itemIt == null ) {
262284 return 0 ;
@@ -273,7 +295,7 @@ private int getNodeItems(Element items, int maxItemsToReturn, String afterItemId
273295 lastItem = nodeItem .getId ();
274296 }
275297 LOGGER .debug ("Including RSM there are " + rsmEntriesCount + " items for node " + node );
276- return channelManager .countNodeItems (node );
298+ return channelManager .countNodeItems (node , parentOnly );
277299 } finally {
278300 itemIt .close ();
279301 }
0 commit comments