CloseListener.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.Close;

  22. /**
  23.  * CloseListener handles all In-Band Bytestream close requests.
  24.  * <p>
  25.  * If a close request is received it looks if a stored In-Band Bytestream
  26.  * session exists and closes it. If no session with the given session ID exists
  27.  * an &lt;item-not-found/&gt; error is returned to the sender.
  28.  *
  29.  * @author Henning Staib
  30.  */
  31. class CloseListener extends AbstractIqRequestHandler {

  32.     /* manager containing the listeners and the XMPP connection */
  33.     private final InBandBytestreamManager manager;

  34.     /**
  35.      * Constructor.
  36.      *
  37.      * @param manager the In-Band Bytestream manager
  38.      */
  39.     protected CloseListener(InBandBytestreamManager manager) {
  40.         super(Close.ELEMENT, Close.NAMESPACE, IQ.Type.set, Mode.async);
  41.         this.manager = manager;
  42.     }

  43.     @Override
  44.     public IQ handleIQRequest(IQ iqRequest) {
  45.         Close closeRequest = (Close) iqRequest;
  46.         InBandBytestreamSession ibbSession = this.manager.getSessions().get(
  47.                         closeRequest.getSessionID());
  48.         if (ibbSession == null) {
  49.             try {
  50.                 this.manager.replyItemNotFoundPacket(closeRequest);
  51.             }
  52.             catch (InterruptedException | NotConnectedException e) {
  53.                 return null;
  54.             }
  55.         }
  56.         else {
  57.             try {
  58.                 ibbSession.closeByPeer(closeRequest);
  59.             }
  60.             catch (InterruptedException | NotConnectedException e) {
  61.                 return null;
  62.             }
  63.             this.manager.getSessions().remove(closeRequest.getSessionID());
  64.         }
  65.         return null;
  66.     }

  67. }