2014-02-17 23:58:40 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Copyright the original author or authors
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
package org.jivesoftware.smackx.bytestreams.ibb;
|
|
|
|
|
2015-01-08 11:01:35 +01:00
|
|
|
import org.jivesoftware.smack.iqrequest.AbstractIqRequestHandler;
|
|
|
|
import org.jivesoftware.smack.packet.IQ;
|
2020-07-03 21:55:36 +02:00
|
|
|
import org.jivesoftware.smack.packet.StanzaError;
|
2017-06-14 17:12:43 +02:00
|
|
|
|
2014-02-17 23:58:40 +01:00
|
|
|
import org.jivesoftware.smackx.bytestreams.BytestreamListener;
|
|
|
|
import org.jivesoftware.smackx.bytestreams.ibb.packet.Open;
|
2015-02-27 10:02:48 +01:00
|
|
|
import org.jivesoftware.smackx.filetransfer.StreamNegotiator;
|
2014-02-17 23:58:40 +01:00
|
|
|
|
2014-03-19 14:22:20 +01:00
|
|
|
|
2014-02-17 23:58:40 +01:00
|
|
|
/**
|
|
|
|
* InitiationListener handles all incoming In-Band Bytestream open requests. If there are no
|
|
|
|
* listeners for a In-Band Bytestream request InitiationListener will always refuse the request and
|
|
|
|
* reply with a <not-acceptable/> error (<a
|
|
|
|
* href="http://xmpp.org/extensions/xep-0047.html#example-5" >XEP-0047</a> Section 2.1).
|
|
|
|
* <p>
|
|
|
|
* All In-Band Bytestream request having a block size greater than the maximum allowed block size
|
|
|
|
* for this connection are rejected with an <resource-constraint/> error. The maximum block
|
|
|
|
* size can be set by invoking {@link InBandBytestreamManager#setMaximumBlockSize(int)}.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2014-02-17 23:58:40 +01:00
|
|
|
* @author Henning Staib
|
|
|
|
*/
|
2015-01-08 11:01:35 +01:00
|
|
|
class InitiationListener extends AbstractIqRequestHandler {
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
/* manager containing the listeners and the XMPP connection */
|
|
|
|
private final InBandBytestreamManager manager;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2014-02-17 23:58:40 +01:00
|
|
|
* @param manager the In-Band Bytestream manager
|
|
|
|
*/
|
|
|
|
protected InitiationListener(InBandBytestreamManager manager) {
|
2015-01-08 11:01:35 +01:00
|
|
|
super(Open.ELEMENT, Open.NAMESPACE, IQ.Type.set, Mode.async);
|
2014-02-17 23:58:40 +01:00
|
|
|
this.manager = manager;
|
2015-01-08 11:01:35 +01:00
|
|
|
}
|
2014-02-17 23:58:40 +01:00
|
|
|
|
2015-01-08 11:01:35 +01:00
|
|
|
@Override
|
2020-07-03 21:55:36 +02:00
|
|
|
public IQ handleIQRequest(final IQ iqRequest) {
|
|
|
|
Open ibbRequest = (Open) iqRequest;
|
2014-02-17 23:58:40 +01:00
|
|
|
|
2020-07-03 21:55:36 +02:00
|
|
|
int blockSize = ibbRequest.getBlockSize();
|
|
|
|
int maximumBlockSize = manager.getMaximumBlockSize();
|
2014-02-17 23:58:40 +01:00
|
|
|
// validate that block size is within allowed range
|
2020-07-03 21:55:36 +02:00
|
|
|
if (blockSize > maximumBlockSize) {
|
|
|
|
StanzaError error = StanzaError.getBuilder().setCondition(StanzaError.Condition.resource_constraint)
|
|
|
|
.setDescriptiveEnText("Requests block size of " + blockSize + " exceeds maximum block size of "
|
|
|
|
+ maximumBlockSize)
|
|
|
|
.build();
|
|
|
|
return IQ.createErrorResponse(iqRequest, error);
|
2014-02-17 23:58:40 +01:00
|
|
|
}
|
|
|
|
|
2015-03-04 22:42:36 +01:00
|
|
|
StreamNegotiator.signal(ibbRequest.getFrom().toString() + '\t' + ibbRequest.getSessionID(), ibbRequest);
|
2015-02-27 10:02:48 +01:00
|
|
|
|
2014-02-17 23:58:40 +01:00
|
|
|
// ignore request if in ignore list
|
2020-07-03 21:55:36 +02:00
|
|
|
if (this.manager.getIgnoredBytestreamRequests().remove(ibbRequest.getSessionID())) {
|
|
|
|
return null;
|
|
|
|
}
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
// build bytestream request from packet
|
|
|
|
InBandBytestreamRequest request = new InBandBytestreamRequest(this.manager, ibbRequest);
|
|
|
|
|
|
|
|
// notify listeners for bytestream initiation from a specific user
|
|
|
|
BytestreamListener userListener = this.manager.getUserListener(ibbRequest.getFrom());
|
|
|
|
if (userListener != null) {
|
|
|
|
userListener.incomingBytestreamRequest(request);
|
|
|
|
}
|
|
|
|
else if (!this.manager.getAllRequestListeners().isEmpty()) {
|
|
|
|
/*
|
|
|
|
* if there is no user specific listener inform listeners for all initiation requests
|
|
|
|
*/
|
|
|
|
for (BytestreamListener listener : this.manager.getAllRequestListeners()) {
|
|
|
|
listener.incomingBytestreamRequest(request);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2020-07-03 21:55:36 +02:00
|
|
|
StanzaError error = StanzaError.getBuilder()
|
|
|
|
.setCondition(StanzaError.Condition.not_acceptable)
|
|
|
|
.setDescriptiveEnText("No file-transfer listeners registered")
|
|
|
|
.build();
|
|
|
|
return IQ.createErrorResponse(iqRequest, error);
|
2014-02-17 23:58:40 +01:00
|
|
|
}
|
|
|
|
|
2020-07-03 21:55:36 +02:00
|
|
|
return null;
|
2014-02-17 23:58:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|