2008-05-28 20:59:52 +02:00
|
|
|
/**
|
|
|
|
* $RCSfile$
|
|
|
|
* $Revision: $
|
|
|
|
* $Date: $
|
|
|
|
*
|
|
|
|
* Copyright 2005-2007 Jive Software.
|
|
|
|
*
|
|
|
|
* All rights reserved. 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;
|
|
|
|
|
|
|
|
import org.jivesoftware.smack.XMPPException;
|
|
|
|
import org.jivesoftware.smack.packet.XMPPError;
|
|
|
|
import org.jivesoftware.smackx.Form;
|
|
|
|
import org.jivesoftware.smackx.packet.AdHocCommandData;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An ad-hoc command is responsible for executing the provided service and
|
|
|
|
* storing the result of the execution. Each new request will create a new
|
|
|
|
* instance of the command, allowing information related to executions to be
|
2008-11-13 19:34:03 +01:00
|
|
|
* stored in it. For example suppose that a command that retrieves the list of
|
|
|
|
* users on a server is implemented. When the command is executed it gets that
|
2008-05-28 20:59:52 +02:00
|
|
|
* list and the result is stored as a form in the command instance, i.e. the
|
|
|
|
* <code>getForm</code> method retrieves a form with all the users.
|
|
|
|
* <p>
|
|
|
|
* Each command has a <tt>node</tt> that should be unique within a given JID.
|
|
|
|
* <p>
|
|
|
|
* Commands may have zero or more stages. Each stage is usually used for
|
|
|
|
* gathering information required for the command execution. Users are able to
|
|
|
|
* move forward or backward across the different stages. Commands may not be
|
|
|
|
* cancelled while they are being executed. However, users may request the
|
|
|
|
* "cancel" action when submitting a stage response indicating that the command
|
|
|
|
* execution should be aborted. Thus, releasing any collected information.
|
|
|
|
* Commands that require user interaction (i.e. have more than one stage) will
|
|
|
|
* have to provide the data forms the user must complete in each stage and the
|
|
|
|
* allowed actions the user might perform during each stage (e.g. go to the
|
|
|
|
* previous stage or go to the next stage).
|
|
|
|
* <p>
|
|
|
|
* All the actions may throw an XMPPException if there is a problem executing
|
|
|
|
* them. The <code>XMPPError</code> of that exception may have some specific
|
|
|
|
* information about the problem. The possible extensions are:
|
|
|
|
*
|
|
|
|
* <li><i>malformed-action</i>. Extension of a <i>bad-request</i> error.</li>
|
|
|
|
* <li><i>bad-action</i>. Extension of a <i>bad-request</i> error.</li>
|
|
|
|
* <li><i>bad-locale</i>. Extension of a <i>bad-request</i> error.</li>
|
|
|
|
* <li><i>bad-payload</i>. Extension of a <i>bad-request</i> error.</li>
|
|
|
|
* <li><i>bad-sessionid</i>. Extension of a <i>bad-request</i> error.</li>
|
|
|
|
* <li><i>session-expired</i>. Extension of a <i>not-allowed</i> error.</li>
|
|
|
|
* <p>
|
|
|
|
* See the <code>SpecificErrorCondition</code> class for detailed description
|
|
|
|
* of each one.
|
|
|
|
* <p>
|
|
|
|
* Use the <code>getSpecificErrorConditionFrom</code> to obtain the specific
|
|
|
|
* information from an <code>XMPPError</code>.
|
|
|
|
*
|
|
|
|
* @author Gabriel Guardincerri
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public abstract class AdHocCommand {
|
2008-11-13 19:34:03 +01:00
|
|
|
// TODO: Analyze the redesign of command by having an ExecutionResponse as a
|
2008-11-03 17:28:57 +01:00
|
|
|
// TODO: result to the execution of every action. That result should have all the
|
|
|
|
// TODO: information related to the execution, e.g. the form to fill. Maybe this
|
|
|
|
// TODO: design is more intuitive and simpler than the current one that has all in
|
|
|
|
// TODO: one class.
|
2008-05-28 20:59:52 +02:00
|
|
|
|
|
|
|
private AdHocCommandData data;
|
|
|
|
|
|
|
|
public AdHocCommand() {
|
|
|
|
super();
|
|
|
|
data = new AdHocCommandData();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-11-03 17:28:57 +01:00
|
|
|
* Returns the specific condition of the <code>error</code> or <tt>null</tt> if the
|
2008-05-28 20:59:52 +02:00
|
|
|
* error doesn't have any.
|
|
|
|
*
|
2008-11-03 17:28:57 +01:00
|
|
|
* @param error the error the get the specific condition from.
|
2008-05-28 20:59:52 +02:00
|
|
|
* @return the specific condition of this error, or null if it doesn't have
|
|
|
|
* any.
|
|
|
|
*/
|
2008-11-03 17:28:57 +01:00
|
|
|
public static SpecificErrorCondition getSpecificErrorCondition(XMPPError error) {
|
|
|
|
// This method is implemented to provide an easy way of getting a packet
|
2008-05-28 20:59:52 +02:00
|
|
|
// extension of the XMPPError.
|
|
|
|
for (SpecificErrorCondition condition : SpecificErrorCondition.values()) {
|
|
|
|
if (error.getExtension(condition.toString(),
|
|
|
|
AdHocCommandData.SpecificError.namespace) != null) {
|
|
|
|
return condition;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the the human readable name of the command, usually used for
|
|
|
|
* displaying in a UI.
|
|
|
|
*
|
2008-10-31 05:04:15 +01:00
|
|
|
* @param name the name.
|
2008-05-28 20:59:52 +02:00
|
|
|
*/
|
|
|
|
public void setName(String name) {
|
|
|
|
data.setName(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the human readable name of the command.
|
|
|
|
*
|
|
|
|
* @return the human readable name of the command
|
|
|
|
*/
|
|
|
|
public String getName() {
|
|
|
|
return data.getName();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the unique identifier of the command. This value must be unique for
|
|
|
|
* the <code>OwnerJID</code>.
|
|
|
|
*
|
2008-11-03 17:28:57 +01:00
|
|
|
* @param node the unique identifier of the command.
|
2008-05-28 20:59:52 +02:00
|
|
|
*/
|
|
|
|
public void setNode(String node) {
|
|
|
|
data.setNode(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the unique identifier of the command. It is unique for the
|
|
|
|
* <code>OwnerJID</code>.
|
|
|
|
*
|
2008-11-03 17:28:57 +01:00
|
|
|
* @return the unique identifier of the command.
|
2008-05-28 20:59:52 +02:00
|
|
|
*/
|
|
|
|
public String getNode() {
|
|
|
|
return data.getNode();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-11-03 17:28:57 +01:00
|
|
|
* Returns the full JID of the owner of this command. This JID is the "to" of a
|
2008-05-28 20:59:52 +02:00
|
|
|
* execution request.
|
|
|
|
*
|
2008-10-31 05:04:15 +01:00
|
|
|
* @return the owner JID.
|
2008-05-28 20:59:52 +02:00
|
|
|
*/
|
|
|
|
public abstract String getOwnerJID();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the notes that the command has at the current stage.
|
|
|
|
*
|
|
|
|
* @return a list of notes.
|
|
|
|
*/
|
|
|
|
public List<AdHocCommandNote> getNotes() {
|
|
|
|
return data.getNotes();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a note to the current stage. This should be used when setting a
|
|
|
|
* response to the execution of an action. All the notes added here are
|
2008-11-03 17:28:57 +01:00
|
|
|
* returned by the {@link #getNotes} method during the current stage.
|
2008-05-28 20:59:52 +02:00
|
|
|
* Once the stage changes all the notes are discarded.
|
|
|
|
*
|
2008-10-31 05:04:15 +01:00
|
|
|
* @param note the note.
|
2008-05-28 20:59:52 +02:00
|
|
|
*/
|
|
|
|
protected void addNote(AdHocCommandNote note) {
|
|
|
|
data.addNote(note);
|
|
|
|
}
|
|
|
|
|
2008-12-16 23:33:57 +01:00
|
|
|
public String getRaw() {
|
|
|
|
return data.getChildElementXML();
|
|
|
|
}
|
|
|
|
|
2008-05-28 20:59:52 +02:00
|
|
|
/**
|
|
|
|
* Returns the form of the current stage. Usually it is the form that must
|
|
|
|
* be answered to execute the next action. If that is the case it should be
|
|
|
|
* used by the requester to fill all the information that the executor needs
|
|
|
|
* to continue to the next stage. It can also be the result of the
|
|
|
|
* execution.
|
|
|
|
*
|
|
|
|
* @return the form of the current stage to fill out or the result of the
|
|
|
|
* execution.
|
|
|
|
*/
|
|
|
|
public Form getForm() {
|
|
|
|
if (data.getForm() == null) {
|
|
|
|
return null;
|
2008-11-03 17:28:57 +01:00
|
|
|
}
|
|
|
|
else {
|
2008-05-28 20:59:52 +02:00
|
|
|
return new Form(data.getForm());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the form of the current stage. This should be used when setting a
|
|
|
|
* response. It could be a form to fill out the information needed to go to
|
|
|
|
* the next stage or the result of an execution.
|
|
|
|
*
|
2008-11-03 17:28:57 +01:00
|
|
|
* @param form the form of the current stage to fill out or the result of the
|
|
|
|
* execution.
|
2008-05-28 20:59:52 +02:00
|
|
|
*/
|
|
|
|
protected void setForm(Form form) {
|
|
|
|
data.setForm(form.getDataFormToSend());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Executes the command. This is invoked only on the first stage of the
|
|
|
|
* command. It is invoked on every command. If there is a problem executing
|
|
|
|
* the command it throws an XMPPException.
|
|
|
|
*
|
2008-12-16 23:33:57 +01:00
|
|
|
* @throws XMPPException if there is an error executing the command.
|
2008-05-28 20:59:52 +02:00
|
|
|
*/
|
|
|
|
public abstract void execute() throws XMPPException;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Executes the next action of the command with the information provided in
|
|
|
|
* the <code>response</code>. This form must be the answer form of the
|
2008-11-03 17:28:57 +01:00
|
|
|
* previous stage. This method will be only invoked for commands that have one
|
2008-05-28 20:59:52 +02:00
|
|
|
* or more stages. If there is a problem executing the command it throws an
|
|
|
|
* XMPPException.
|
|
|
|
*
|
2008-11-03 17:28:57 +01:00
|
|
|
* @param response the form answer of the previous stage.
|
|
|
|
* @throws XMPPException if there is a problem executing the command.
|
2008-05-28 20:59:52 +02:00
|
|
|
*/
|
|
|
|
public abstract void next(Form response) throws XMPPException;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Completes the command execution with the information provided in the
|
|
|
|
* <code>response</code>. This form must be the answer form of the
|
2008-11-13 19:34:03 +01:00
|
|
|
* previous stage. This method will be only invoked for commands that have one
|
2008-05-28 20:59:52 +02:00
|
|
|
* or more stages. If there is a problem executing the command it throws an
|
|
|
|
* XMPPException.
|
|
|
|
*
|
2008-11-03 17:28:57 +01:00
|
|
|
* @param response the form answer of the previous stage.
|
|
|
|
* @throws XMPPException if there is a problem executing the command.
|
2008-05-28 20:59:52 +02:00
|
|
|
*/
|
|
|
|
public abstract void complete(Form response) throws XMPPException;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Goes to the previous stage. The requester is asking to re-send the
|
|
|
|
* information of the previous stage. The command must change it state to
|
|
|
|
* the previous one. If there is a problem executing the command it throws
|
|
|
|
* an XMPPException.
|
|
|
|
*
|
2008-11-03 17:28:57 +01:00
|
|
|
* @throws XMPPException if there is a problem executing the command.
|
2008-05-28 20:59:52 +02:00
|
|
|
*/
|
|
|
|
public abstract void prev() throws XMPPException;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cancels the execution of the command. This can be invoked on any stage of
|
|
|
|
* the execution. If there is a problem executing the command it throws an
|
|
|
|
* XMPPException.
|
|
|
|
*
|
2008-11-03 17:28:57 +01:00
|
|
|
* @throws XMPPException if there is a problem executing the command.
|
2008-05-28 20:59:52 +02:00
|
|
|
*/
|
|
|
|
public abstract void cancel() throws XMPPException;
|
|
|
|
|
|
|
|
/**
|
2008-11-03 17:28:57 +01:00
|
|
|
* Returns a collection with the allowed actions based on the current stage.
|
|
|
|
* Possible actions are: {@link Action#prev prev}, {@link Action#next next} and
|
|
|
|
* {@link Action#complete complete}. This method will be only invoked for commands that
|
|
|
|
* have one or more stages.
|
2008-05-28 20:59:52 +02:00
|
|
|
*
|
|
|
|
* @return a collection with the allowed actions based on the current stage
|
2008-11-03 17:28:57 +01:00
|
|
|
* as defined in the SessionData.
|
2008-05-28 20:59:52 +02:00
|
|
|
*/
|
|
|
|
protected List<Action> getActions() {
|
|
|
|
return data.getActions();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add an action to the current stage available actions. This should be used
|
|
|
|
* when creating a response.
|
|
|
|
*
|
2008-10-31 05:04:15 +01:00
|
|
|
* @param action the action.
|
2008-05-28 20:59:52 +02:00
|
|
|
*/
|
|
|
|
protected void addActionAvailable(Action action) {
|
|
|
|
data.addAction(action);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-11-03 17:28:57 +01:00
|
|
|
* Returns the action available for the current stage which is
|
2008-05-28 20:59:52 +02:00
|
|
|
* considered the equivalent to "execute". When the requester sends his
|
|
|
|
* reply, if no action was defined in the command then the action will be
|
|
|
|
* assumed "execute" thus assuming the action returned by this method. This
|
|
|
|
* method will never be invoked for commands that have no stages.
|
|
|
|
*
|
2008-11-03 17:28:57 +01:00
|
|
|
* @return the action available for the current stage which is considered
|
|
|
|
* the equivalent to "execute".
|
2008-05-28 20:59:52 +02:00
|
|
|
*/
|
|
|
|
protected Action getExecuteAction() {
|
|
|
|
return data.getExecuteAction();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-11-13 19:34:03 +01:00
|
|
|
* Sets which of the actions available for the current stage is
|
2008-05-28 20:59:52 +02:00
|
|
|
* considered the equivalent to "execute". This should be used when setting
|
|
|
|
* a response. When the requester sends his reply, if no action was defined
|
|
|
|
* in the command then the action will be assumed "execute" thus assuming
|
|
|
|
* the action returned by this method.
|
|
|
|
*
|
2008-10-31 05:04:15 +01:00
|
|
|
* @param action the action.
|
2008-05-28 20:59:52 +02:00
|
|
|
*/
|
|
|
|
protected void setExecuteAction(Action action) {
|
|
|
|
data.setExecuteAction(action);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the status of the current stage.
|
|
|
|
*
|
2008-10-31 05:04:15 +01:00
|
|
|
* @return the current status.
|
2008-05-28 20:59:52 +02:00
|
|
|
*/
|
|
|
|
public Status getStatus() {
|
|
|
|
return data.getStatus();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the data of the current stage. This should not used.
|
|
|
|
*
|
2008-10-31 05:04:15 +01:00
|
|
|
* @param data the data.
|
2008-05-28 20:59:52 +02:00
|
|
|
*/
|
|
|
|
void setData(AdHocCommandData data) {
|
|
|
|
this.data = data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the data of the current stage. This should not used.
|
2008-10-31 05:04:15 +01:00
|
|
|
*
|
|
|
|
* @return the data.
|
2008-05-28 20:59:52 +02:00
|
|
|
*/
|
|
|
|
AdHocCommandData getData() {
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-11-03 17:28:57 +01:00
|
|
|
* Returns true if the <code>action</code> is available in the current stage.
|
|
|
|
* The {@link Action#cancel cancel} action is always allowed. To define the
|
2008-05-28 20:59:52 +02:00
|
|
|
* available actions use the <code>addActionAvailable</code> method.
|
|
|
|
*
|
|
|
|
* @param action
|
|
|
|
* The action to check if it is available.
|
|
|
|
* @return True if the action is available for the current stage.
|
|
|
|
*/
|
|
|
|
protected boolean isValidAction(Action action) {
|
|
|
|
return getActions().contains(action) || Action.cancel.equals(action);
|
|
|
|
}
|
|
|
|
|
2008-11-03 17:28:57 +01:00
|
|
|
/**
|
|
|
|
* The status of the stage in the adhoc command.
|
|
|
|
*/
|
2008-05-28 20:59:52 +02:00
|
|
|
public enum Status {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The command is being executed.
|
|
|
|
*/
|
|
|
|
executing,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The command has completed. The command session has ended.
|
|
|
|
*/
|
|
|
|
completed,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The command has been canceled. The command session has ended.
|
|
|
|
*/
|
|
|
|
canceled
|
|
|
|
}
|
|
|
|
|
|
|
|
public enum Action {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The command should be executed or continue to be executed. This is
|
|
|
|
* the default value.
|
|
|
|
*/
|
|
|
|
execute,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The command should be canceled.
|
|
|
|
*/
|
|
|
|
cancel,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The command should be digress to the previous stage of execution.
|
|
|
|
*/
|
|
|
|
prev,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The command should progress to the next stage of execution.
|
|
|
|
*/
|
|
|
|
next,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The command should be completed (if possible).
|
|
|
|
*/
|
|
|
|
complete,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The action is unknow. This is used when a recieved message has an
|
|
|
|
* unknown action. It must not be used to send an execution request.
|
|
|
|
*/
|
2008-10-31 05:04:15 +01:00
|
|
|
unknown
|
2008-05-28 20:59:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public enum SpecificErrorCondition {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The responding JID cannot accept the specified action.
|
|
|
|
*/
|
2008-11-03 17:28:57 +01:00
|
|
|
badAction("bad-action"),
|
2008-05-28 20:59:52 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The responding JID does not understand the specified action.
|
|
|
|
*/
|
2008-11-03 17:28:57 +01:00
|
|
|
malformedAction("malformed-action"),
|
2008-05-28 20:59:52 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The responding JID cannot accept the specified language/locale.
|
|
|
|
*/
|
2008-11-03 17:28:57 +01:00
|
|
|
badLocale("bad-locale"),
|
2008-05-28 20:59:52 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The responding JID cannot accept the specified payload (e.g. the data
|
|
|
|
* form did not provide one or more required fields).
|
|
|
|
*/
|
2008-11-03 17:28:57 +01:00
|
|
|
badPayload("bad-payload"),
|
2008-05-28 20:59:52 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The responding JID cannot accept the specified sessionid.
|
|
|
|
*/
|
2008-11-03 17:28:57 +01:00
|
|
|
badSessionid("bad-sessionid"),
|
2008-05-28 20:59:52 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The requesting JID specified a sessionid that is no longer active
|
|
|
|
* (either because it was completed, canceled, or timed out).
|
|
|
|
*/
|
2008-11-03 17:28:57 +01:00
|
|
|
sessionExpired("session-expired");
|
|
|
|
|
|
|
|
private String value;
|
|
|
|
|
|
|
|
SpecificErrorCondition(String value) {
|
|
|
|
this.value = value;
|
|
|
|
}
|
2008-05-28 20:59:52 +02:00
|
|
|
|
|
|
|
public String toString() {
|
2008-11-03 17:28:57 +01:00
|
|
|
return value;
|
2008-05-28 20:59:52 +02:00
|
|
|
}
|
|
|
|
}
|
2008-11-03 17:28:57 +01:00
|
|
|
}
|