AdHocCommand.java

  1. /**
  2.  *
  3.  * Copyright 2005-2007 Jive Software.
  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.commands;

  18. import org.jivesoftware.smack.SmackException.NoResponseException;
  19. import org.jivesoftware.smack.SmackException.NotConnectedException;
  20. import org.jivesoftware.smack.XMPPException.XMPPErrorException;
  21. import org.jivesoftware.smack.packet.XMPPError;
  22. import org.jivesoftware.smackx.commands.packet.AdHocCommandData;
  23. import org.jivesoftware.smackx.xdata.Form;
  24. import org.jxmpp.jid.Jid;

  25. import java.util.List;

  26. /**
  27.  * An ad-hoc command is responsible for executing the provided service and
  28.  * storing the result of the execution. Each new request will create a new
  29.  * instance of the command, allowing information related to executions to be
  30.  * stored in it. For example suppose that a command that retrieves the list of
  31.  * users on a server is implemented. When the command is executed it gets that
  32.  * list and the result is stored as a form in the command instance, i.e. the
  33.  * <code>getForm</code> method retrieves a form with all the users.
  34.  * <p>
  35.  * Each command has a <tt>node</tt> that should be unique within a given JID.
  36.  * <p>
  37.  * Commands may have zero or more stages. Each stage is usually used for
  38.  * gathering information required for the command execution. Users are able to
  39.  * move forward or backward across the different stages. Commands may not be
  40.  * cancelled while they are being executed. However, users may request the
  41.  * "cancel" action when submitting a stage response indicating that the command
  42.  * execution should be aborted. Thus, releasing any collected information.
  43.  * Commands that require user interaction (i.e. have more than one stage) will
  44.  * have to provide the data forms the user must complete in each stage and the
  45.  * allowed actions the user might perform during each stage (e.g. go to the
  46.  * previous stage or go to the next stage).
  47.  * <p>
  48.  * All the actions may throw an XMPPException if there is a problem executing
  49.  * them. The <code>XMPPError</code> of that exception may have some specific
  50.  * information about the problem. The possible extensions are:
  51.  *
  52.  * <li><i>malformed-action</i>. Extension of a <i>bad-request</i> error.</li>
  53.  * <li><i>bad-action</i>. Extension of a <i>bad-request</i> error.</li>
  54.  * <li><i>bad-locale</i>. Extension of a <i>bad-request</i> error.</li>
  55.  * <li><i>bad-payload</i>. Extension of a <i>bad-request</i> error.</li>
  56.  * <li><i>bad-sessionid</i>. Extension of a <i>bad-request</i> error.</li>
  57.  * <li><i>session-expired</i>. Extension of a <i>not-allowed</i> error.</li>
  58.  * <p>
  59.  * See the <code>SpecificErrorCondition</code> class for detailed description
  60.  * of each one.
  61.  * <p>
  62.  * Use the <code>getSpecificErrorConditionFrom</code> to obtain the specific
  63.  * information from an <code>XMPPError</code>.
  64.  *
  65.  * @author Gabriel Guardincerri
  66.  *
  67.  */
  68. public abstract class AdHocCommand {
  69.     // TODO: Analyze the redesign of command by having an ExecutionResponse as a
  70.     // TODO: result to the execution of every action. That result should have all the
  71.     // TODO: information related to the execution, e.g. the form to fill. Maybe this
  72.     // TODO: design is more intuitive and simpler than the current one that has all in
  73.     // TODO: one class.

  74.     private AdHocCommandData data;

  75.     public AdHocCommand() {
  76.         super();
  77.         data = new AdHocCommandData();
  78.     }

  79.     /**
  80.      * Returns the specific condition of the <code>error</code> or <tt>null</tt> if the
  81.      * error doesn't have any.
  82.      *
  83.      * @param error the error the get the specific condition from.
  84.      * @return the specific condition of this error, or null if it doesn't have
  85.      *         any.
  86.      */
  87.     public static SpecificErrorCondition getSpecificErrorCondition(XMPPError error) {
  88.         // This method is implemented to provide an easy way of getting a packet
  89.         // extension of the XMPPError.
  90.         for (SpecificErrorCondition condition : SpecificErrorCondition.values()) {
  91.             if (error.getExtension(condition.toString(),
  92.                     AdHocCommandData.SpecificError.namespace) != null) {
  93.                 return condition;
  94.             }
  95.         }
  96.         return null;
  97.     }

  98.     /**
  99.      * Set the the human readable name of the command, usually used for
  100.      * displaying in a UI.
  101.      *
  102.      * @param name the name.
  103.      */
  104.     public void setName(String name) {
  105.         data.setName(name);
  106.     }

  107.     /**
  108.      * Returns the human readable name of the command.
  109.      *
  110.      * @return the human readable name of the command
  111.      */
  112.     public String getName() {
  113.         return data.getName();
  114.     }

  115.     /**
  116.      * Sets the unique identifier of the command. This value must be unique for
  117.      * the <code>OwnerJID</code>.
  118.      *
  119.      * @param node the unique identifier of the command.
  120.      */
  121.     public void setNode(String node) {
  122.         data.setNode(node);
  123.     }

  124.     /**
  125.      * Returns the unique identifier of the command. It is unique for the
  126.      * <code>OwnerJID</code>.
  127.      *
  128.      * @return the unique identifier of the command.
  129.      */
  130.     public String getNode() {
  131.         return data.getNode();
  132.     }

  133.     /**
  134.      * Returns the full JID of the owner of this command. This JID is the "to" of a
  135.      * execution request.
  136.      *
  137.      * @return the owner JID.
  138.      */
  139.     public abstract Jid getOwnerJID();

  140.     /**
  141.      * Returns the notes that the command has at the current stage.
  142.      *
  143.      * @return a list of notes.
  144.      */
  145.     public List<AdHocCommandNote> getNotes() {
  146.         return data.getNotes();
  147.     }

  148.     /**
  149.      * Adds a note to the current stage. This should be used when setting a
  150.      * response to the execution of an action. All the notes added here are
  151.      * returned by the {@link #getNotes} method during the current stage.
  152.      * Once the stage changes all the notes are discarded.
  153.      *
  154.      * @param note the note.
  155.      */
  156.     protected void addNote(AdHocCommandNote note) {
  157.         data.addNote(note);
  158.     }

  159.     public String getRaw() {
  160.         return data.getChildElementXML().toString();
  161.     }

  162.     /**
  163.      * Returns the form of the current stage. Usually it is the form that must
  164.      * be answered to execute the next action. If that is the case it should be
  165.      * used by the requester to fill all the information that the executor needs
  166.      * to continue to the next stage. It can also be the result of the
  167.      * execution.
  168.      *
  169.      * @return the form of the current stage to fill out or the result of the
  170.      *         execution.
  171.      */
  172.     public Form getForm() {
  173.         if (data.getForm() == null) {
  174.             return null;
  175.         }
  176.         else {
  177.             return new Form(data.getForm());
  178.         }
  179.     }

  180.     /**
  181.      * Sets the form of the current stage. This should be used when setting a
  182.      * response. It could be a form to fill out the information needed to go to
  183.      * the next stage or the result of an execution.
  184.      *
  185.      * @param form the form of the current stage to fill out or the result of the
  186.      *      execution.
  187.      */
  188.     protected void setForm(Form form) {
  189.         data.setForm(form.getDataFormToSend());
  190.     }

  191.     /**
  192.      * Executes the command. This is invoked only on the first stage of the
  193.      * command. It is invoked on every command. If there is a problem executing
  194.      * the command it throws an XMPPException.
  195.      *
  196.      * @throws XMPPErrorException if there is an error executing the command.
  197.      * @throws NotConnectedException
  198.      * @throws InterruptedException
  199.      */
  200.     public abstract void execute() throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException;

  201.     /**
  202.      * Executes the next action of the command with the information provided in
  203.      * the <code>response</code>. This form must be the answer form of the
  204.      * previous stage. This method will be only invoked for commands that have one
  205.      * or more stages. If there is a problem executing the command it throws an
  206.      * XMPPException.
  207.      *
  208.      * @param response the form answer of the previous stage.
  209.      * @throws XMPPErrorException if there is a problem executing the command.
  210.      * @throws NotConnectedException
  211.      * @throws InterruptedException
  212.      */
  213.     public abstract void next(Form response) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException;

  214.     /**
  215.      * Completes the command execution with the information provided in the
  216.      * <code>response</code>. This form must be the answer form of the
  217.      * previous stage. This method will be only invoked for commands that have one
  218.      * or more stages. If there is a problem executing the command it throws an
  219.      * XMPPException.
  220.      *
  221.      * @param response the form answer of the previous stage.
  222.      * @throws XMPPErrorException if there is a problem executing the command.
  223.      * @throws NotConnectedException
  224.      * @throws InterruptedException
  225.      */
  226.     public abstract void complete(Form response) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException;

  227.     /**
  228.      * Goes to the previous stage. The requester is asking to re-send the
  229.      * information of the previous stage. The command must change it state to
  230.      * the previous one. If there is a problem executing the command it throws
  231.      * an XMPPException.
  232.      *
  233.      * @throws XMPPErrorException if there is a problem executing the command.
  234.      * @throws NotConnectedException
  235.      * @throws InterruptedException
  236.      */
  237.     public abstract void prev() throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException;

  238.     /**
  239.      * Cancels the execution of the command. This can be invoked on any stage of
  240.      * the execution. If there is a problem executing the command it throws an
  241.      * XMPPException.
  242.      *
  243.      * @throws XMPPErrorException if there is a problem executing the command.
  244.      * @throws NotConnectedException
  245.      * @throws InterruptedException
  246.      */
  247.     public abstract void cancel() throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException;

  248.     /**
  249.      * Returns a collection with the allowed actions based on the current stage.
  250.      * Possible actions are: {@link Action#prev prev}, {@link Action#next next} and
  251.      * {@link Action#complete complete}. This method will be only invoked for commands that
  252.      * have one or more stages.
  253.      *
  254.      * @return a collection with the allowed actions based on the current stage
  255.      *      as defined in the SessionData.
  256.      */
  257.     protected List<Action> getActions() {
  258.         return data.getActions();
  259.     }

  260.     /**
  261.      * Add an action to the current stage available actions. This should be used
  262.      * when creating a response.
  263.      *
  264.      * @param action the action.
  265.      */
  266.     protected void addActionAvailable(Action action) {
  267.         data.addAction(action);
  268.     }

  269.     /**
  270.      * Returns the action available for the current stage which is
  271.      * considered the equivalent to "execute". When the requester sends his
  272.      * reply, if no action was defined in the command then the action will be
  273.      * assumed "execute" thus assuming the action returned by this method. This
  274.      * method will never be invoked for commands that have no stages.
  275.      *
  276.      * @return the action available for the current stage which is considered
  277.      *      the equivalent to "execute".
  278.      */
  279.     protected Action getExecuteAction() {
  280.         return data.getExecuteAction();
  281.     }

  282.     /**
  283.      * Sets which of the actions available for the current stage is
  284.      * considered the equivalent to "execute". This should be used when setting
  285.      * a response. When the requester sends his reply, if no action was defined
  286.      * in the command then the action will be assumed "execute" thus assuming
  287.      * the action returned by this method.
  288.      *
  289.      * @param action the action.
  290.      */
  291.     protected void setExecuteAction(Action action) {
  292.         data.setExecuteAction(action);
  293.     }

  294.     /**
  295.      * Returns the status of the current stage.
  296.      *
  297.      * @return the current status.
  298.      */
  299.     public Status getStatus() {
  300.         return data.getStatus();
  301.     }

  302.     /**
  303.      * Sets the data of the current stage. This should not used.
  304.      *
  305.      * @param data the data.
  306.      */
  307.     void setData(AdHocCommandData data) {
  308.         this.data = data;
  309.     }

  310.     /**
  311.      * Gets the data of the current stage. This should not used.
  312.      *
  313.      * @return the data.
  314.      */
  315.     AdHocCommandData getData() {
  316.         return data;
  317.     }

  318.     /**
  319.      * Returns true if the <code>action</code> is available in the current stage.
  320.      * The {@link Action#cancel cancel} action is always allowed. To define the
  321.      * available actions use the <code>addActionAvailable</code> method.
  322.      *
  323.      * @param action
  324.      *            The action to check if it is available.
  325.      * @return True if the action is available for the current stage.
  326.      */
  327.     protected boolean isValidAction(Action action) {
  328.         return getActions().contains(action) || Action.cancel.equals(action);
  329.     }

  330.     /**
  331.      * The status of the stage in the adhoc command.
  332.      */
  333.     public enum Status {

  334.         /**
  335.          * The command is being executed.
  336.          */
  337.         executing,

  338.         /**
  339.          * The command has completed. The command session has ended.
  340.          */
  341.         completed,

  342.         /**
  343.          * The command has been canceled. The command session has ended.
  344.          */
  345.         canceled
  346.     }

  347.     public enum Action {

  348.         /**
  349.          * The command should be executed or continue to be executed. This is
  350.          * the default value.
  351.          */
  352.         execute,

  353.         /**
  354.          * The command should be canceled.
  355.          */
  356.         cancel,

  357.         /**
  358.          * The command should be digress to the previous stage of execution.
  359.          */
  360.         prev,

  361.         /**
  362.          * The command should progress to the next stage of execution.
  363.          */
  364.         next,

  365.         /**
  366.          * The command should be completed (if possible).
  367.          */
  368.         complete,

  369.         /**
  370.          * The action is unknow. This is used when a recieved message has an
  371.          * unknown action. It must not be used to send an execution request.
  372.          */
  373.         unknown
  374.     }

  375.     public enum SpecificErrorCondition {

  376.         /**
  377.          * The responding JID cannot accept the specified action.
  378.          */
  379.         badAction("bad-action"),

  380.         /**
  381.          * The responding JID does not understand the specified action.
  382.          */
  383.         malformedAction("malformed-action"),

  384.         /**
  385.          * The responding JID cannot accept the specified language/locale.
  386.          */
  387.         badLocale("bad-locale"),

  388.         /**
  389.          * The responding JID cannot accept the specified payload (e.g. the data
  390.          * form did not provide one or more required fields).
  391.          */
  392.         badPayload("bad-payload"),

  393.         /**
  394.          * The responding JID cannot accept the specified sessionid.
  395.          */
  396.         badSessionid("bad-sessionid"),

  397.         /**
  398.          * The requesting JID specified a sessionid that is no longer active
  399.          * (either because it was completed, canceled, or timed out).
  400.          */
  401.         sessionExpired("session-expired");

  402.         private String value;

  403.         SpecificErrorCondition(String value) {
  404.             this.value = value;
  405.         }

  406.         public String toString() {
  407.             return value;
  408.         }
  409.     }
  410. }