InitiationListener.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.iqrequest.AbstractIqRequestHandler;
  19. import org.jivesoftware.smack.packet.IQ;
  20. import org.jivesoftware.smack.packet.StanzaError;

  21. import org.jivesoftware.smackx.bytestreams.BytestreamListener;
  22. import org.jivesoftware.smackx.bytestreams.ibb.packet.Open;
  23. import org.jivesoftware.smackx.filetransfer.StreamNegotiator;


  24. /**
  25.  * InitiationListener handles all incoming In-Band Bytestream open requests. If there are no
  26.  * listeners for a In-Band Bytestream request InitiationListener will always refuse the request and
  27.  * reply with a &lt;not-acceptable/&gt; error (<a
  28.  * href="http://xmpp.org/extensions/xep-0047.html#example-5" >XEP-0047</a> Section 2.1).
  29.  * <p>
  30.  * All In-Band Bytestream request having a block size greater than the maximum allowed block size
  31.  * for this connection are rejected with an &lt;resource-constraint/&gt; error. The maximum block
  32.  * size can be set by invoking {@link InBandBytestreamManager#setMaximumBlockSize(int)}.
  33.  *
  34.  * @author Henning Staib
  35.  */
  36. class InitiationListener extends AbstractIqRequestHandler {

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

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

  48.     @Override
  49.     public IQ handleIQRequest(final IQ iqRequest) {
  50.         Open ibbRequest = (Open) iqRequest;

  51.         int blockSize = ibbRequest.getBlockSize();
  52.         int maximumBlockSize = manager.getMaximumBlockSize();
  53.         // validate that block size is within allowed range
  54.         if (blockSize > maximumBlockSize) {
  55.             StanzaError error = StanzaError.getBuilder().setCondition(StanzaError.Condition.resource_constraint)
  56.                             .setDescriptiveEnText("Requests block size of " + blockSize + " exceeds maximum block size of "
  57.                                                             + maximumBlockSize)
  58.                             .build();
  59.             return IQ.createErrorResponse(iqRequest, error);
  60.         }

  61.         StreamNegotiator.signal(ibbRequest.getFrom().toString() + '\t' + ibbRequest.getSessionID(), ibbRequest);

  62.         // ignore request if in ignore list
  63.         if (this.manager.getIgnoredBytestreamRequests().remove(ibbRequest.getSessionID())) {
  64.             return null;
  65.         }

  66.         // build bytestream request from packet
  67.         InBandBytestreamRequest request = new InBandBytestreamRequest(this.manager, ibbRequest);

  68.         // notify listeners for bytestream initiation from a specific user
  69.         BytestreamListener userListener = this.manager.getUserListener(ibbRequest.getFrom());
  70.         if (userListener != null) {
  71.             userListener.incomingBytestreamRequest(request);
  72.         }
  73.         else if (!this.manager.getAllRequestListeners().isEmpty()) {
  74.             /*
  75.              * if there is no user specific listener inform listeners for all initiation requests
  76.              */
  77.             for (BytestreamListener listener : this.manager.getAllRequestListeners()) {
  78.                 listener.incomingBytestreamRequest(request);
  79.             }
  80.         }
  81.         else {
  82.             StanzaError error = StanzaError.getBuilder()
  83.                             .setCondition(StanzaError.Condition.not_acceptable)
  84.                             .setDescriptiveEnText("No file-transfer listeners registered")
  85.                             .build();
  86.             return IQ.createErrorResponse(iqRequest, error);
  87.         }

  88.         return null;
  89.     }

  90. }