DataListener.java

  1. /**
  2.  *
  3.  * Copyright the original author or authors
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.bytestreams.ibb;

  18. import org.jivesoftware.smack.SmackException.NotConnectedException;
  19. import org.jivesoftware.smack.iqrequest.AbstractIqRequestHandler;
  20. import org.jivesoftware.smack.packet.IQ;
  21. import org.jivesoftware.smackx.bytestreams.ibb.packet.Data;
  22. import org.jivesoftware.smackx.bytestreams.ibb.packet.DataPacketExtension;

  23. /**
  24.  * DataListener handles all In-Band Bytestream IQ stanzas containing a data
  25.  * packet extension that don't belong to an existing session.
  26.  * <p>
  27.  * If a data packet is received it looks if a stored In-Band Bytestream session
  28.  * exists. If no session with the given session ID exists an
  29.  * &lt;item-not-found/&gt; error is returned to the sender.
  30.  * <p>
  31.  * Data packets belonging to a running In-Band Bytestream session are processed
  32.  * by more specific listeners registered when an {@link InBandBytestreamSession}
  33.  * is created.
  34.  *
  35.  * @author Henning Staib
  36.  */
  37. class DataListener extends AbstractIqRequestHandler {

  38.     /* manager containing the listeners and the XMPP connection */
  39.     private final InBandBytestreamManager manager;

  40.     /**
  41.      * Constructor.
  42.      *
  43.      * @param manager the In-Band Bytestream manager
  44.      */
  45.     public DataListener(InBandBytestreamManager manager) {
  46.       super(DataPacketExtension.ELEMENT, DataPacketExtension.NAMESPACE, IQ.Type.set, Mode.async);
  47.         this.manager = manager;
  48.     }

  49.     @Override
  50.     public IQ handleIQRequest(IQ iqRequest) {
  51.         Data data = (Data) iqRequest;
  52.         InBandBytestreamSession ibbSession = this.manager.getSessions().get(
  53.                         data.getDataPacketExtension().getSessionID());
  54.         try {
  55.             if (ibbSession == null) {
  56.                 this.manager.replyItemNotFoundPacket(data);
  57.             }
  58.             else {
  59.                 ibbSession.processIQPacket(data);
  60.             }
  61.         }
  62.         catch (NotConnectedException|InterruptedException e) {
  63.             return null;
  64.         }
  65.         return null;
  66.     }

  67. }