2014-02-17 23:58:40 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Copyright 2005-2008 Jive Software.
|
|
|
|
*
|
|
|
|
* 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.commands;
|
|
|
|
|
2017-02-11 16:16:41 +01:00
|
|
|
import java.lang.reflect.InvocationTargetException;
|
2014-08-15 23:16:18 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.WeakHashMap;
|
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
2015-01-05 20:18:35 +01:00
|
|
|
import java.util.logging.Level;
|
|
|
|
import java.util.logging.Logger;
|
2014-08-15 23:16:18 +02:00
|
|
|
|
|
|
|
import org.jivesoftware.smack.ConnectionCreationListener;
|
|
|
|
import org.jivesoftware.smack.Manager;
|
|
|
|
import org.jivesoftware.smack.SmackException;
|
2015-01-05 20:18:35 +01:00
|
|
|
import org.jivesoftware.smack.SmackException.NoResponseException;
|
2014-03-19 14:22:20 +01:00
|
|
|
import org.jivesoftware.smack.SmackException.NotConnectedException;
|
2014-08-15 23:16:18 +02:00
|
|
|
import org.jivesoftware.smack.XMPPConnection;
|
|
|
|
import org.jivesoftware.smack.XMPPConnectionRegistry;
|
|
|
|
import org.jivesoftware.smack.XMPPException;
|
2014-03-12 11:50:05 +01:00
|
|
|
import org.jivesoftware.smack.XMPPException.XMPPErrorException;
|
2015-01-08 11:01:35 +01:00
|
|
|
import org.jivesoftware.smack.iqrequest.AbstractIqRequestHandler;
|
|
|
|
import org.jivesoftware.smack.iqrequest.IQRequestHandler.Mode;
|
2014-02-17 23:58:40 +01:00
|
|
|
import org.jivesoftware.smack.packet.IQ;
|
2018-04-07 21:25:40 +02:00
|
|
|
import org.jivesoftware.smack.packet.StanzaError;
|
2014-02-17 23:58:40 +01:00
|
|
|
import org.jivesoftware.smack.util.StringUtils;
|
2017-06-14 17:12:43 +02:00
|
|
|
|
2014-02-17 23:58:40 +01:00
|
|
|
import org.jivesoftware.smackx.commands.AdHocCommand.Action;
|
|
|
|
import org.jivesoftware.smackx.commands.AdHocCommand.Status;
|
|
|
|
import org.jivesoftware.smackx.commands.packet.AdHocCommandData;
|
2014-10-13 22:35:45 +02:00
|
|
|
import org.jivesoftware.smackx.disco.AbstractNodeInformationProvider;
|
2014-02-17 23:58:40 +01:00
|
|
|
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
|
|
|
|
import org.jivesoftware.smackx.disco.packet.DiscoverInfo;
|
2014-08-15 23:16:18 +02:00
|
|
|
import org.jivesoftware.smackx.disco.packet.DiscoverItems;
|
2014-02-17 23:58:40 +01:00
|
|
|
import org.jivesoftware.smackx.xdata.Form;
|
2017-06-14 17:12:43 +02:00
|
|
|
|
2015-02-14 17:15:02 +01:00
|
|
|
import org.jxmpp.jid.Jid;
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* An AdHocCommandManager is responsible for keeping the list of available
|
|
|
|
* commands offered by a service and for processing commands requests.
|
|
|
|
*
|
2015-02-12 12:13:19 +01:00
|
|
|
* Pass in an XMPPConnection instance to
|
2016-05-18 15:58:08 +02:00
|
|
|
* {@link #getAddHocCommandsManager(XMPPConnection)} in order to
|
2014-02-17 23:58:40 +01:00
|
|
|
* get an instance of this class.
|
|
|
|
*
|
|
|
|
* @author Gabriel Guardincerri
|
|
|
|
*/
|
2015-04-06 10:45:12 +02:00
|
|
|
public final class AdHocCommandManager extends Manager {
|
2014-03-09 11:19:44 +01:00
|
|
|
public static final String NAMESPACE = "http://jabber.org/protocol/commands";
|
2014-02-17 23:58:40 +01:00
|
|
|
|
2015-01-05 20:18:35 +01:00
|
|
|
private static final Logger LOGGER = Logger.getLogger(AdHocCommandManager.class.getName());
|
|
|
|
|
2014-02-17 23:58:40 +01:00
|
|
|
/**
|
|
|
|
* The session time out in seconds.
|
|
|
|
*/
|
|
|
|
private static final int SESSION_TIMEOUT = 2 * 60;
|
|
|
|
|
|
|
|
/**
|
2015-02-12 12:13:19 +01:00
|
|
|
* Map an XMPPConnection with it AdHocCommandManager. This map have a key-value
|
2014-02-17 23:58:40 +01:00
|
|
|
* pair for every active connection.
|
|
|
|
*/
|
2017-12-13 23:10:11 +01:00
|
|
|
private static final Map<XMPPConnection, AdHocCommandManager> instances = new WeakHashMap<>();
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Register the listener for all the connection creations. When a new
|
|
|
|
* connection is created a new AdHocCommandManager is also created and
|
|
|
|
* related to that connection.
|
|
|
|
*/
|
|
|
|
static {
|
2014-05-25 12:28:08 +02:00
|
|
|
XMPPConnectionRegistry.addConnectionCreationListener(new ConnectionCreationListener() {
|
2017-02-11 16:16:41 +01:00
|
|
|
@Override
|
2014-03-10 09:45:50 +01:00
|
|
|
public void connectionCreated(XMPPConnection connection) {
|
2014-02-17 23:58:40 +01:00
|
|
|
getAddHocCommandsManager(connection);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the <code>AdHocCommandManager</code> related to the
|
|
|
|
* <code>connection</code>.
|
|
|
|
*
|
|
|
|
* @param connection the XMPP connection.
|
|
|
|
* @return the AdHocCommandManager associated with the connection.
|
|
|
|
*/
|
2014-03-10 09:45:50 +01:00
|
|
|
public static synchronized AdHocCommandManager getAddHocCommandsManager(XMPPConnection connection) {
|
2014-02-17 23:58:40 +01:00
|
|
|
AdHocCommandManager ahcm = instances.get(connection);
|
2014-12-27 13:37:02 +01:00
|
|
|
if (ahcm == null) {
|
|
|
|
ahcm = new AdHocCommandManager(connection);
|
|
|
|
instances.put(connection, ahcm);
|
|
|
|
}
|
2014-02-17 23:58:40 +01:00
|
|
|
return ahcm;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Map a command node with its AdHocCommandInfo. Note: Key=command node,
|
|
|
|
* Value=command. Command node matches the node attribute sent by command
|
|
|
|
* requesters.
|
|
|
|
*/
|
2017-12-13 23:10:11 +01:00
|
|
|
private final Map<String, AdHocCommandInfo> commands = new ConcurrentHashMap<>();
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Map a command session ID with the instance LocalCommand. The LocalCommand
|
|
|
|
* is the an objects that has all the information of the current state of
|
|
|
|
* the command execution. Note: Key=session ID, Value=LocalCommand. Session
|
|
|
|
* ID matches the sessionid attribute sent by command responders.
|
|
|
|
*/
|
2017-12-13 23:10:11 +01:00
|
|
|
private final Map<String, LocalCommand> executingCommands = new ConcurrentHashMap<>();
|
2015-03-17 11:33:02 +01:00
|
|
|
|
2014-02-17 23:58:40 +01:00
|
|
|
private final ServiceDiscoveryManager serviceDiscoveryManager;
|
2015-03-17 11:33:02 +01:00
|
|
|
|
2014-02-17 23:58:40 +01:00
|
|
|
/**
|
|
|
|
* Thread that reaps stale sessions.
|
|
|
|
*/
|
2015-01-05 20:17:51 +01:00
|
|
|
// FIXME The session sweeping is horrible implemented. The thread will never stop running. A different approach must
|
|
|
|
// be implemented. For example one that does stop reaping sessions and the thread if there are no more, and restarts
|
|
|
|
// the reaping process on demand. Or for every command a scheduled task should be created that removes the session
|
|
|
|
// if it's timed out. See SMACK-624.
|
2014-02-17 23:58:40 +01:00
|
|
|
private Thread sessionsSweeper;
|
|
|
|
|
2014-03-10 09:45:50 +01:00
|
|
|
private AdHocCommandManager(XMPPConnection connection) {
|
2014-03-09 11:19:44 +01:00
|
|
|
super(connection);
|
2014-02-17 23:58:40 +01:00
|
|
|
this.serviceDiscoveryManager = ServiceDiscoveryManager.getInstanceFor(connection);
|
|
|
|
|
|
|
|
// Add the feature to the service discovery manage to show that this
|
|
|
|
// connection supports the AdHoc-Commands protocol.
|
|
|
|
// This information will be used when another client tries to
|
|
|
|
// discover whether this client supports AdHoc-Commands or not.
|
|
|
|
ServiceDiscoveryManager.getInstanceFor(connection).addFeature(
|
2014-03-09 11:19:44 +01:00
|
|
|
NAMESPACE);
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
// Set the NodeInformationProvider that will provide information about
|
|
|
|
// which AdHoc-Commands are registered, whenever a disco request is
|
|
|
|
// received
|
|
|
|
ServiceDiscoveryManager.getInstanceFor(connection)
|
2014-03-09 11:19:44 +01:00
|
|
|
.setNodeInformationProvider(NAMESPACE,
|
2014-10-13 22:35:45 +02:00
|
|
|
new AbstractNodeInformationProvider() {
|
|
|
|
@Override
|
2014-02-17 23:58:40 +01:00
|
|
|
public List<DiscoverItems.Item> getNodeItems() {
|
|
|
|
|
2017-12-13 23:10:11 +01:00
|
|
|
List<DiscoverItems.Item> answer = new ArrayList<>();
|
2014-02-17 23:58:40 +01:00
|
|
|
Collection<AdHocCommandInfo> commandsList = getRegisteredCommands();
|
|
|
|
|
|
|
|
for (AdHocCommandInfo info : commandsList) {
|
|
|
|
DiscoverItems.Item item = new DiscoverItems.Item(
|
|
|
|
info.getOwnerJID());
|
|
|
|
item.setName(info.getName());
|
|
|
|
item.setNode(info.getNode());
|
|
|
|
answer.add(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
return answer;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// The packet listener and the filter for processing some AdHoc Commands
|
|
|
|
// Packets
|
2015-01-08 11:01:35 +01:00
|
|
|
connection.registerIQRequestHandler(new AbstractIqRequestHandler(AdHocCommandData.ELEMENT,
|
|
|
|
AdHocCommandData.NAMESPACE, IQ.Type.set, Mode.async) {
|
|
|
|
@Override
|
|
|
|
public IQ handleIQRequest(IQ iqRequest) {
|
|
|
|
AdHocCommandData requestData = (AdHocCommandData) iqRequest;
|
2014-03-12 11:50:05 +01:00
|
|
|
try {
|
2015-01-08 11:01:35 +01:00
|
|
|
return processAdHocCommand(requestData);
|
2014-03-12 11:50:05 +01:00
|
|
|
}
|
2015-02-14 09:43:44 +01:00
|
|
|
catch (InterruptedException | NoResponseException | NotConnectedException e) {
|
2017-12-13 23:10:11 +01:00
|
|
|
LOGGER.log(Level.INFO, "processAdHocCommand threw exception", e);
|
2015-01-08 11:01:35 +01:00
|
|
|
return null;
|
2014-03-12 11:50:05 +01:00
|
|
|
}
|
2014-02-17 23:58:40 +01:00
|
|
|
}
|
2015-01-08 11:01:35 +01:00
|
|
|
});
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
sessionsSweeper = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Registers a new command with this command manager, which is related to a
|
|
|
|
* connection. The <tt>node</tt> is an unique identifier of that command for
|
|
|
|
* the connection related to this command manager. The <tt>name</tt> is the
|
|
|
|
* human readable name of the command. The <tt>class</tt> is the class of
|
|
|
|
* the command, which must extend {@link LocalCommand} and have a default
|
|
|
|
* constructor.
|
|
|
|
*
|
|
|
|
* @param node the unique identifier of the command.
|
|
|
|
* @param name the human readable name of the command.
|
|
|
|
* @param clazz the class of the command, which must extend {@link LocalCommand}.
|
|
|
|
*/
|
|
|
|
public void registerCommand(String node, String name, final Class<? extends LocalCommand> clazz) {
|
|
|
|
registerCommand(node, name, new LocalCommandFactory() {
|
2017-02-11 16:16:41 +01:00
|
|
|
@Override
|
2017-08-16 14:31:46 +02:00
|
|
|
public LocalCommand getInstance() throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
|
|
|
|
return clazz.getConstructor().newInstance();
|
2014-02-17 23:58:40 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Registers a new command with this command manager, which is related to a
|
|
|
|
* connection. The <tt>node</tt> is an unique identifier of that
|
|
|
|
* command for the connection related to this command manager. The <tt>name</tt>
|
2017-12-13 23:10:11 +01:00
|
|
|
* is the human readable name of the command. The <tt>factory</tt> generates
|
2014-02-17 23:58:40 +01:00
|
|
|
* new instances of the command.
|
|
|
|
*
|
|
|
|
* @param node the unique identifier of the command.
|
|
|
|
* @param name the human readable name of the command.
|
|
|
|
* @param factory a factory to create new instances of the command.
|
|
|
|
*/
|
|
|
|
public void registerCommand(String node, final String name, LocalCommandFactory factory) {
|
2014-03-09 11:19:44 +01:00
|
|
|
AdHocCommandInfo commandInfo = new AdHocCommandInfo(node, name, connection().getUser(), factory);
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
commands.put(node, commandInfo);
|
|
|
|
// Set the NodeInformationProvider that will provide information about
|
|
|
|
// the added command
|
|
|
|
serviceDiscoveryManager.setNodeInformationProvider(node,
|
2014-10-13 22:35:45 +02:00
|
|
|
new AbstractNodeInformationProvider() {
|
|
|
|
@Override
|
2014-02-17 23:58:40 +01:00
|
|
|
public List<String> getNodeFeatures() {
|
2017-12-13 23:10:11 +01:00
|
|
|
List<String> answer = new ArrayList<>();
|
2014-03-09 11:19:44 +01:00
|
|
|
answer.add(NAMESPACE);
|
2014-02-17 23:58:40 +01:00
|
|
|
// TODO: check if this service is provided by the
|
|
|
|
// TODO: current connection.
|
|
|
|
answer.add("jabber:x:data");
|
|
|
|
return answer;
|
|
|
|
}
|
2014-10-13 22:35:45 +02:00
|
|
|
@Override
|
2014-02-17 23:58:40 +01:00
|
|
|
public List<DiscoverInfo.Identity> getNodeIdentities() {
|
2017-12-13 23:10:11 +01:00
|
|
|
List<DiscoverInfo.Identity> answer = new ArrayList<>();
|
2014-02-17 23:58:40 +01:00
|
|
|
DiscoverInfo.Identity identity = new DiscoverInfo.Identity(
|
|
|
|
"automation", name, "command-node");
|
|
|
|
answer.add(identity);
|
|
|
|
return answer;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Discover the commands of an specific JID. The <code>jid</code> is a
|
|
|
|
* full JID.
|
|
|
|
*
|
|
|
|
* @param jid the full JID to retrieve the commands for.
|
|
|
|
* @return the discovered items.
|
|
|
|
* @throws XMPPException if the operation failed for some reason.
|
2014-03-12 11:50:05 +01:00
|
|
|
* @throws SmackException if there was no response from the server.
|
2015-02-14 09:43:44 +01:00
|
|
|
* @throws InterruptedException
|
2014-02-17 23:58:40 +01:00
|
|
|
*/
|
2015-02-14 17:15:02 +01:00
|
|
|
public DiscoverItems discoverCommands(Jid jid) throws XMPPException, SmackException, InterruptedException {
|
2014-03-09 11:19:44 +01:00
|
|
|
return serviceDiscoveryManager.discoverItems(jid, NAMESPACE);
|
2014-02-17 23:58:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Publish the commands to an specific JID.
|
|
|
|
*
|
|
|
|
* @param jid the full JID to publish the commands to.
|
|
|
|
* @throws XMPPException if the operation failed for some reason.
|
2014-03-12 11:50:05 +01:00
|
|
|
* @throws SmackException if there was no response from the server.
|
2015-02-14 09:43:44 +01:00
|
|
|
* @throws InterruptedException
|
2018-02-21 20:25:44 +01:00
|
|
|
* @deprecated This method uses no longer existent XEP-0030 features and will be removed.
|
2014-02-17 23:58:40 +01:00
|
|
|
*/
|
2018-02-21 20:25:44 +01:00
|
|
|
@SuppressWarnings("deprecation")
|
|
|
|
@Deprecated
|
|
|
|
// TODO: Remove in Smack 4.4.
|
2015-02-14 17:15:02 +01:00
|
|
|
public void publishCommands(Jid jid) throws XMPPException, SmackException, InterruptedException {
|
2014-02-17 23:58:40 +01:00
|
|
|
// Collects the commands to publish as items
|
|
|
|
DiscoverItems discoverItems = new DiscoverItems();
|
|
|
|
Collection<AdHocCommandInfo> xCommandsList = getRegisteredCommands();
|
|
|
|
|
|
|
|
for (AdHocCommandInfo info : xCommandsList) {
|
|
|
|
DiscoverItems.Item item = new DiscoverItems.Item(info.getOwnerJID());
|
|
|
|
item.setName(info.getName());
|
|
|
|
item.setNode(info.getNode());
|
|
|
|
discoverItems.addItem(item);
|
|
|
|
}
|
|
|
|
|
2014-03-09 11:19:44 +01:00
|
|
|
serviceDiscoveryManager.publishItems(jid, NAMESPACE, discoverItems);
|
2014-02-17 23:58:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a command that represents an instance of a command in a remote
|
|
|
|
* host. It is used to execute remote commands. The concept is similar to
|
|
|
|
* RMI. Every invocation on this command is equivalent to an invocation in
|
|
|
|
* the remote command.
|
|
|
|
*
|
|
|
|
* @param jid the full JID of the host of the remote command
|
|
|
|
* @param node the identifier of the command
|
|
|
|
* @return a local instance equivalent to the remote command.
|
|
|
|
*/
|
2015-02-14 17:15:02 +01:00
|
|
|
public RemoteCommand getRemoteCommand(Jid jid, String node) {
|
2014-03-09 11:19:44 +01:00
|
|
|
return new RemoteCommand(connection(), node, jid);
|
2014-02-17 23:58:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-03-31 14:17:30 +02:00
|
|
|
* Process the AdHoc-Command stanza that request the execution of some
|
2014-02-17 23:58:40 +01:00
|
|
|
* action of a command. If this is the first request, this method checks,
|
|
|
|
* before executing the command, if:
|
|
|
|
* <ul>
|
|
|
|
* <li>The requested command exists</li>
|
|
|
|
* <li>The requester has permissions to execute it</li>
|
|
|
|
* <li>The command has more than one stage, if so, it saves the command and
|
|
|
|
* session ID for further use</li>
|
|
|
|
* </ul>
|
|
|
|
*
|
|
|
|
* <br>
|
|
|
|
* <br>
|
|
|
|
* If this is not the first request, this method checks, before executing
|
|
|
|
* the command, if:
|
|
|
|
* <ul>
|
|
|
|
* <li>The session ID of the request was stored</li>
|
|
|
|
* <li>The session life do not exceed the time out</li>
|
|
|
|
* <li>The action to execute is one of the available actions</li>
|
|
|
|
* </ul>
|
|
|
|
*
|
|
|
|
* @param requestData
|
2018-03-31 14:17:30 +02:00
|
|
|
* the stanza to process.
|
2015-01-05 20:18:35 +01:00
|
|
|
* @throws NotConnectedException
|
|
|
|
* @throws NoResponseException
|
2015-02-14 09:43:44 +01:00
|
|
|
* @throws InterruptedException
|
2014-02-17 23:58:40 +01:00
|
|
|
*/
|
2015-02-14 09:43:44 +01:00
|
|
|
private IQ processAdHocCommand(AdHocCommandData requestData) throws NoResponseException, NotConnectedException, InterruptedException {
|
2014-02-17 23:58:40 +01:00
|
|
|
// Creates the response with the corresponding data
|
|
|
|
AdHocCommandData response = new AdHocCommandData();
|
|
|
|
response.setTo(requestData.getFrom());
|
2015-02-05 11:17:27 +01:00
|
|
|
response.setStanzaId(requestData.getStanzaId());
|
2014-02-17 23:58:40 +01:00
|
|
|
response.setNode(requestData.getNode());
|
|
|
|
response.setId(requestData.getTo());
|
|
|
|
|
|
|
|
String sessionId = requestData.getSessionID();
|
|
|
|
String commandNode = requestData.getNode();
|
|
|
|
|
|
|
|
if (sessionId == null) {
|
|
|
|
// A new execution request has been received. Check that the
|
|
|
|
// command exists
|
|
|
|
if (!commands.containsKey(commandNode)) {
|
|
|
|
// Requested command does not exist so return
|
|
|
|
// item_not_found error.
|
2018-04-07 21:25:40 +02:00
|
|
|
return respondError(response, StanzaError.Condition.item_not_found);
|
2014-02-17 23:58:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create new session ID
|
|
|
|
sessionId = StringUtils.randomString(15);
|
|
|
|
|
|
|
|
try {
|
|
|
|
// Create a new instance of the command with the
|
2017-12-13 23:10:11 +01:00
|
|
|
// corresponding sessionid
|
2017-08-16 14:31:46 +02:00
|
|
|
LocalCommand command;
|
|
|
|
try {
|
|
|
|
command = newInstanceOfCmd(commandNode, sessionId);
|
|
|
|
}
|
|
|
|
catch (InstantiationException | IllegalAccessException | IllegalArgumentException
|
|
|
|
| InvocationTargetException | NoSuchMethodException | SecurityException e) {
|
2018-04-07 21:25:40 +02:00
|
|
|
StanzaError.Builder xmppError = StanzaError.getBuilder().setCondition(StanzaError.Condition.internal_server_error).setDescriptiveEnText(e.getMessage());
|
2017-08-16 14:31:46 +02:00
|
|
|
return respondError(response, xmppError);
|
|
|
|
}
|
2014-02-17 23:58:40 +01:00
|
|
|
|
2014-06-06 02:20:45 +02:00
|
|
|
response.setType(IQ.Type.result);
|
2014-02-17 23:58:40 +01:00
|
|
|
command.setData(response);
|
|
|
|
|
|
|
|
// Check that the requester has enough permission.
|
|
|
|
// Answer forbidden error if requester permissions are not
|
|
|
|
// enough to execute the requested command
|
|
|
|
if (!command.hasPermission(requestData.getFrom())) {
|
2018-04-07 21:25:40 +02:00
|
|
|
return respondError(response, StanzaError.Condition.forbidden);
|
2014-02-17 23:58:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Action action = requestData.getAction();
|
|
|
|
|
|
|
|
// If the action is unknown then respond an error.
|
|
|
|
if (action != null && action.equals(Action.unknown)) {
|
2018-04-07 21:25:40 +02:00
|
|
|
return respondError(response, StanzaError.Condition.bad_request,
|
2014-02-17 23:58:40 +01:00
|
|
|
AdHocCommand.SpecificErrorCondition.malformedAction);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the action is not execute, then it is an invalid action.
|
|
|
|
if (action != null && !action.equals(Action.execute)) {
|
2018-04-07 21:25:40 +02:00
|
|
|
return respondError(response, StanzaError.Condition.bad_request,
|
2014-02-17 23:58:40 +01:00
|
|
|
AdHocCommand.SpecificErrorCondition.badAction);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Increase the state number, so the command knows in witch
|
|
|
|
// stage it is
|
|
|
|
command.incrementStage();
|
|
|
|
// Executes the command
|
|
|
|
command.execute();
|
|
|
|
|
|
|
|
if (command.isLastStage()) {
|
|
|
|
// If there is only one stage then the command is completed
|
|
|
|
response.setStatus(Status.completed);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Else it is still executing, and is registered to be
|
|
|
|
// available for the next call
|
|
|
|
response.setStatus(Status.executing);
|
|
|
|
executingCommands.put(sessionId, command);
|
|
|
|
// See if the session reaping thread is started. If not, start it.
|
|
|
|
if (sessionsSweeper == null) {
|
|
|
|
sessionsSweeper = new Thread(new Runnable() {
|
2017-02-11 16:16:41 +01:00
|
|
|
@Override
|
2014-02-17 23:58:40 +01:00
|
|
|
public void run() {
|
|
|
|
while (true) {
|
|
|
|
for (String sessionId : executingCommands.keySet()) {
|
|
|
|
LocalCommand command = executingCommands.get(sessionId);
|
|
|
|
// Since the command could be removed in the meanwhile
|
|
|
|
// of getting the key and getting the value - by a
|
|
|
|
// processed packet. We must check if it still in the
|
|
|
|
// map.
|
|
|
|
if (command != null) {
|
|
|
|
long creationStamp = command.getCreationDate();
|
|
|
|
// Check if the Session data has expired (default is
|
|
|
|
// 10 minutes)
|
|
|
|
// To remove it from the session list it waits for
|
|
|
|
// the double of the of time out time. This is to
|
|
|
|
// let
|
|
|
|
// the requester know why his execution request is
|
|
|
|
// not accepted. If the session is removed just
|
|
|
|
// after the time out, then whe the user request to
|
2017-12-13 23:10:11 +01:00
|
|
|
// continue the execution he will received an
|
2014-02-17 23:58:40 +01:00
|
|
|
// invalid session error and not a time out error.
|
|
|
|
if (System.currentTimeMillis() - creationStamp > SESSION_TIMEOUT * 1000 * 2) {
|
|
|
|
// Remove the expired session
|
|
|
|
executingCommands.remove(sessionId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
Thread.sleep(1000);
|
|
|
|
}
|
|
|
|
catch (InterruptedException ie) {
|
|
|
|
// Ignore.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
sessionsSweeper.setDaemon(true);
|
|
|
|
sessionsSweeper.start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sends the response packet
|
2015-01-08 11:01:35 +01:00
|
|
|
return response;
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
}
|
2014-03-12 11:50:05 +01:00
|
|
|
catch (XMPPErrorException e) {
|
2014-02-17 23:58:40 +01:00
|
|
|
// If there is an exception caused by the next, complete,
|
|
|
|
// prev or cancel method, then that error is returned to the
|
|
|
|
// requester.
|
2018-04-07 21:25:40 +02:00
|
|
|
StanzaError error = e.getXMPPError();
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
// If the error type is cancel, then the execution is
|
|
|
|
// canceled therefore the status must show that, and the
|
|
|
|
// command be removed from the executing list.
|
2018-04-07 21:25:40 +02:00
|
|
|
if (StanzaError.Type.CANCEL.equals(error.getType())) {
|
2014-02-17 23:58:40 +01:00
|
|
|
response.setStatus(Status.canceled);
|
|
|
|
executingCommands.remove(sessionId);
|
|
|
|
}
|
2018-04-07 21:25:40 +02:00
|
|
|
return respondError(response, StanzaError.getBuilder(error));
|
2014-02-17 23:58:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
LocalCommand command = executingCommands.get(sessionId);
|
|
|
|
|
|
|
|
// Check that a command exists for the specified sessionID
|
|
|
|
// This also handles if the command was removed in the meanwhile
|
|
|
|
// of getting the key and the value of the map.
|
|
|
|
if (command == null) {
|
2018-04-07 21:25:40 +02:00
|
|
|
return respondError(response, StanzaError.Condition.bad_request,
|
2014-02-17 23:58:40 +01:00
|
|
|
AdHocCommand.SpecificErrorCondition.badSessionid);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the Session data has expired (default is 10 minutes)
|
|
|
|
long creationStamp = command.getCreationDate();
|
|
|
|
if (System.currentTimeMillis() - creationStamp > SESSION_TIMEOUT * 1000) {
|
|
|
|
// Remove the expired session
|
|
|
|
executingCommands.remove(sessionId);
|
|
|
|
|
|
|
|
// Answer a not_allowed error (session-expired)
|
2018-04-07 21:25:40 +02:00
|
|
|
return respondError(response, StanzaError.Condition.not_allowed,
|
2014-02-17 23:58:40 +01:00
|
|
|
AdHocCommand.SpecificErrorCondition.sessionExpired);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Since the requester could send two requests for the same
|
|
|
|
* executing command i.e. the same session id, all the execution of
|
|
|
|
* the action must be synchronized to avoid inconsistencies.
|
|
|
|
*/
|
|
|
|
synchronized (command) {
|
|
|
|
Action action = requestData.getAction();
|
|
|
|
|
|
|
|
// If the action is unknown the respond an error
|
|
|
|
if (action != null && action.equals(Action.unknown)) {
|
2018-04-07 21:25:40 +02:00
|
|
|
return respondError(response, StanzaError.Condition.bad_request,
|
2014-02-17 23:58:40 +01:00
|
|
|
AdHocCommand.SpecificErrorCondition.malformedAction);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the user didn't specify an action or specify the execute
|
|
|
|
// action then follow the actual default execute action
|
|
|
|
if (action == null || Action.execute.equals(action)) {
|
|
|
|
action = command.getExecuteAction();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the specified action was previously
|
|
|
|
// offered
|
|
|
|
if (!command.isValidAction(action)) {
|
2018-04-07 21:25:40 +02:00
|
|
|
return respondError(response, StanzaError.Condition.bad_request,
|
2014-02-17 23:58:40 +01:00
|
|
|
AdHocCommand.SpecificErrorCondition.badAction);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
// TODO: Check that all the required fields of the form are
|
|
|
|
// TODO: filled, if not throw an exception. This will simplify the
|
|
|
|
// TODO: construction of new commands
|
|
|
|
|
|
|
|
// Since all errors were passed, the response is now a
|
|
|
|
// result
|
2014-06-06 02:20:45 +02:00
|
|
|
response.setType(IQ.Type.result);
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
// Set the new data to the command.
|
|
|
|
command.setData(response);
|
|
|
|
|
|
|
|
if (Action.next.equals(action)) {
|
|
|
|
command.incrementStage();
|
|
|
|
command.next(new Form(requestData.getForm()));
|
|
|
|
if (command.isLastStage()) {
|
|
|
|
// If it is the last stage then the command is
|
|
|
|
// completed
|
|
|
|
response.setStatus(Status.completed);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Otherwise it is still executing
|
|
|
|
response.setStatus(Status.executing);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (Action.complete.equals(action)) {
|
|
|
|
command.incrementStage();
|
|
|
|
command.complete(new Form(requestData.getForm()));
|
|
|
|
response.setStatus(Status.completed);
|
|
|
|
// Remove the completed session
|
|
|
|
executingCommands.remove(sessionId);
|
|
|
|
}
|
|
|
|
else if (Action.prev.equals(action)) {
|
|
|
|
command.decrementStage();
|
|
|
|
command.prev();
|
|
|
|
}
|
|
|
|
else if (Action.cancel.equals(action)) {
|
|
|
|
command.cancel();
|
|
|
|
response.setStatus(Status.canceled);
|
|
|
|
// Remove the canceled session
|
|
|
|
executingCommands.remove(sessionId);
|
|
|
|
}
|
|
|
|
|
2015-01-08 11:01:35 +01:00
|
|
|
return response;
|
2014-02-17 23:58:40 +01:00
|
|
|
}
|
2014-03-12 11:50:05 +01:00
|
|
|
catch (XMPPErrorException e) {
|
2014-02-17 23:58:40 +01:00
|
|
|
// If there is an exception caused by the next, complete,
|
|
|
|
// prev or cancel method, then that error is returned to the
|
|
|
|
// requester.
|
2018-04-07 21:25:40 +02:00
|
|
|
StanzaError error = e.getXMPPError();
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
// If the error type is cancel, then the execution is
|
|
|
|
// canceled therefore the status must show that, and the
|
|
|
|
// command be removed from the executing list.
|
2018-04-07 21:25:40 +02:00
|
|
|
if (StanzaError.Type.CANCEL.equals(error.getType())) {
|
2014-02-17 23:58:40 +01:00
|
|
|
response.setStatus(Status.canceled);
|
|
|
|
executingCommands.remove(sessionId);
|
|
|
|
}
|
2018-04-07 21:25:40 +02:00
|
|
|
return respondError(response, StanzaError.getBuilder(error));
|
2014-02-17 23:58:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Responds an error with an specific condition.
|
|
|
|
*
|
|
|
|
* @param response the response to send.
|
|
|
|
* @param condition the condition of the error.
|
2014-03-19 14:22:20 +01:00
|
|
|
* @throws NotConnectedException
|
2014-02-17 23:58:40 +01:00
|
|
|
*/
|
2015-03-24 18:33:47 +01:00
|
|
|
private static IQ respondError(AdHocCommandData response,
|
2018-04-07 21:25:40 +02:00
|
|
|
StanzaError.Condition condition) {
|
|
|
|
return respondError(response, StanzaError.getBuilder(condition));
|
2014-02-17 23:58:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Responds an error with an specific condition.
|
|
|
|
*
|
|
|
|
* @param response the response to send.
|
|
|
|
* @param condition the condition of the error.
|
|
|
|
* @param specificCondition the adhoc command error condition.
|
2014-03-19 14:22:20 +01:00
|
|
|
* @throws NotConnectedException
|
2014-02-17 23:58:40 +01:00
|
|
|
*/
|
2018-04-07 21:25:40 +02:00
|
|
|
private static IQ respondError(AdHocCommandData response, StanzaError.Condition condition,
|
2018-04-06 10:21:46 +02:00
|
|
|
AdHocCommand.SpecificErrorCondition specificCondition) {
|
2018-04-07 21:25:40 +02:00
|
|
|
StanzaError.Builder error = StanzaError.getBuilder(condition).addExtension(new AdHocCommandData.SpecificError(specificCondition));
|
2015-01-08 11:01:35 +01:00
|
|
|
return respondError(response, error);
|
2014-02-17 23:58:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Responds an error with an specific error.
|
|
|
|
*
|
|
|
|
* @param response the response to send.
|
|
|
|
* @param error the error to send.
|
2014-03-19 14:22:20 +01:00
|
|
|
* @throws NotConnectedException
|
2014-02-17 23:58:40 +01:00
|
|
|
*/
|
2018-04-07 21:25:40 +02:00
|
|
|
private static IQ respondError(AdHocCommandData response, StanzaError.Builder error) {
|
2014-06-06 02:20:45 +02:00
|
|
|
response.setType(IQ.Type.error);
|
2014-02-17 23:58:40 +01:00
|
|
|
response.setError(error);
|
2015-01-08 11:01:35 +01:00
|
|
|
return response;
|
2014-02-17 23:58:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new instance of a command to be used by a new execution request
|
|
|
|
*
|
|
|
|
* @param commandNode the command node that identifies it.
|
|
|
|
* @param sessionID the session id of this execution.
|
|
|
|
* @return the command instance to execute.
|
2014-03-12 11:50:05 +01:00
|
|
|
* @throws XMPPErrorException if there is problem creating the new instance.
|
2017-08-16 14:31:46 +02:00
|
|
|
* @throws SecurityException
|
|
|
|
* @throws NoSuchMethodException
|
|
|
|
* @throws InvocationTargetException
|
|
|
|
* @throws IllegalArgumentException
|
|
|
|
* @throws IllegalAccessException
|
|
|
|
* @throws InstantiationException
|
2014-02-17 23:58:40 +01:00
|
|
|
*/
|
2017-08-16 14:31:46 +02:00
|
|
|
private LocalCommand newInstanceOfCmd(String commandNode, String sessionID)
|
|
|
|
throws XMPPErrorException, InstantiationException, IllegalAccessException, IllegalArgumentException,
|
|
|
|
InvocationTargetException, NoSuchMethodException, SecurityException {
|
2014-02-17 23:58:40 +01:00
|
|
|
AdHocCommandInfo commandInfo = commands.get(commandNode);
|
2017-08-16 14:31:46 +02:00
|
|
|
LocalCommand command = commandInfo.getCommandInstance();
|
|
|
|
command.setSessionID(sessionID);
|
|
|
|
command.setName(commandInfo.getName());
|
|
|
|
command.setNode(commandInfo.getNode());
|
|
|
|
|
2014-02-17 23:58:40 +01:00
|
|
|
return command;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the registered commands of this command manager, which is related
|
|
|
|
* to a connection.
|
|
|
|
*
|
|
|
|
* @return the registered commands.
|
|
|
|
*/
|
|
|
|
private Collection<AdHocCommandInfo> getRegisteredCommands() {
|
|
|
|
return commands.values();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stores ad-hoc command information.
|
|
|
|
*/
|
2018-03-28 14:02:21 +02:00
|
|
|
private static final class AdHocCommandInfo {
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
private String node;
|
|
|
|
private String name;
|
2015-02-14 17:15:02 +01:00
|
|
|
private final Jid ownerJID;
|
2014-02-17 23:58:40 +01:00
|
|
|
private LocalCommandFactory factory;
|
|
|
|
|
2018-03-28 14:02:21 +02:00
|
|
|
private AdHocCommandInfo(String node, String name, Jid ownerJID,
|
2018-04-06 10:21:46 +02:00
|
|
|
LocalCommandFactory factory) {
|
2014-02-17 23:58:40 +01:00
|
|
|
this.node = node;
|
|
|
|
this.name = name;
|
|
|
|
this.ownerJID = ownerJID;
|
|
|
|
this.factory = factory;
|
|
|
|
}
|
|
|
|
|
|
|
|
public LocalCommand getCommandInstance() throws InstantiationException,
|
2018-04-06 10:21:46 +02:00
|
|
|
IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
|
2014-02-17 23:58:40 +01:00
|
|
|
return factory.getInstance();
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getName() {
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getNode() {
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2015-02-14 17:15:02 +01:00
|
|
|
public Jid getOwnerJID() {
|
2014-02-17 23:58:40 +01:00
|
|
|
return ownerJID;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|