6 * Copyright 2005-2007 Jive Software.
8 * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
20 package org
.jivesoftware
.smackx
.commands
;
22 import org
.jivesoftware
.smack
.XMPPException
;
23 import org
.jivesoftware
.smack
.packet
.XMPPError
;
24 import org
.jivesoftware
.smackx
.Form
;
25 import org
.jivesoftware
.smackx
.packet
.AdHocCommandData
;
27 import java
.util
.List
;
30 * An ad-hoc command is responsible for executing the provided service and
31 * storing the result of the execution. Each new request will create a new
32 * instance of the command, allowing information related to executions to be
33 * stored in it. For example suppose that a command that retrieves the list of
34 * users on a server is implemented. When the command is executed it gets that
35 * list and the result is stored as a form in the command instance, i.e. the
36 * <code>getForm</code> method retrieves a form with all the users.
38 * Each command has a <tt>node</tt> that should be unique within a given JID.
40 * Commands may have zero or more stages. Each stage is usually used for
41 * gathering information required for the command execution. Users are able to
42 * move forward or backward across the different stages. Commands may not be
43 * cancelled while they are being executed. However, users may request the
44 * "cancel" action when submitting a stage response indicating that the command
45 * execution should be aborted. Thus, releasing any collected information.
46 * Commands that require user interaction (i.e. have more than one stage) will
47 * have to provide the data forms the user must complete in each stage and the
48 * allowed actions the user might perform during each stage (e.g. go to the
49 * previous stage or go to the next stage).
51 * All the actions may throw an XMPPException if there is a problem executing
52 * them. The <code>XMPPError</code> of that exception may have some specific
53 * information about the problem. The possible extensions are:
55 * <li><i>malformed-action</i>. Extension of a <i>bad-request</i> error.</li>
56 * <li><i>bad-action</i>. Extension of a <i>bad-request</i> error.</li>
57 * <li><i>bad-locale</i>. Extension of a <i>bad-request</i> error.</li>
58 * <li><i>bad-payload</i>. Extension of a <i>bad-request</i> error.</li>
59 * <li><i>bad-sessionid</i>. Extension of a <i>bad-request</i> error.</li>
60 * <li><i>session-expired</i>. Extension of a <i>not-allowed</i> error.</li>
62 * See the <code>SpecificErrorCondition</code> class for detailed description
65 * Use the <code>getSpecificErrorConditionFrom</code> to obtain the specific
66 * information from an <code>XMPPError</code>.
68 * @author Gabriel Guardincerri
71 public abstract class AdHocCommand
{
72 // TODO: Analyze the redesign of command by having an ExecutionResponse as a
73 // TODO: result to the execution of every action. That result should have all the
74 // TODO: information related to the execution, e.g. the form to fill. Maybe this
75 // TODO: design is more intuitive and simpler than the current one that has all in
78 private AdHocCommandData data
;
80 public AdHocCommand() {
82 data
= new AdHocCommandData();
86 * Returns the specific condition of the <code>error</code> or <tt>null</tt> if the
87 * error doesn't have any.
89 * @param error the error the get the specific condition from.
90 * @return the specific condition of this error, or null if it doesn't have
93 public static SpecificErrorCondition
getSpecificErrorCondition(XMPPError error
) {
94 // This method is implemented to provide an easy way of getting a packet
95 // extension of the XMPPError.
96 for (SpecificErrorCondition condition
: SpecificErrorCondition
.values()) {
97 if (error
.getExtension(condition
.toString(),
98 AdHocCommandData
.SpecificError
.namespace
) != null) {
106 * Set the the human readable name of the command, usually used for
107 * displaying in a UI.
109 * @param name the name.
111 public void setName(String name
) {
116 * Returns the human readable name of the command.
118 * @return the human readable name of the command
120 public String
getName() {
121 return data
.getName();
125 * Sets the unique identifier of the command. This value must be unique for
126 * the <code>OwnerJID</code>.
128 * @param node the unique identifier of the command.
130 public void setNode(String node
) {
135 * Returns the unique identifier of the command. It is unique for the
136 * <code>OwnerJID</code>.
138 * @return the unique identifier of the command.
140 public String
getNode() {
141 return data
.getNode();
145 * Returns the full JID of the owner of this command. This JID is the "to" of a
148 * @return the owner JID.
150 public abstract String
getOwnerJID();
153 * Returns the notes that the command has at the current stage.
155 * @return a list of notes.
157 public List
<AdHocCommandNote
> getNotes() {
158 return data
.getNotes();
162 * Adds a note to the current stage. This should be used when setting a
163 * response to the execution of an action. All the notes added here are
164 * returned by the {@link #getNotes} method during the current stage.
165 * Once the stage changes all the notes are discarded.
167 * @param note the note.
169 protected void addNote(AdHocCommandNote note
) {
173 public String
getRaw() {
174 return data
.getChildElementXML();
178 * Returns the form of the current stage. Usually it is the form that must
179 * be answered to execute the next action. If that is the case it should be
180 * used by the requester to fill all the information that the executor needs
181 * to continue to the next stage. It can also be the result of the
184 * @return the form of the current stage to fill out or the result of the
187 public Form
getForm() {
188 if (data
.getForm() == null) {
192 return new Form(data
.getForm());
197 * Sets the form of the current stage. This should be used when setting a
198 * response. It could be a form to fill out the information needed to go to
199 * the next stage or the result of an execution.
201 * @param form the form of the current stage to fill out or the result of the
204 protected void setForm(Form form
) {
205 data
.setForm(form
.getDataFormToSend());
209 * Executes the command. This is invoked only on the first stage of the
210 * command. It is invoked on every command. If there is a problem executing
211 * the command it throws an XMPPException.
213 * @throws XMPPException if there is an error executing the command.
215 public abstract void execute() throws XMPPException
;
218 * Executes the command, waiting up to the <tt>timeout</tt> for a reply.
219 * This is invoked only on the first stage of the
220 * command. It is invoked on every command. If there is a problem executing
221 * the command it throws an XMPPException.
223 * @param timeout the length of time in ms to wait for a reply.
224 * @throws XMPPException if there is an error executing the command.
226 public abstract void execute(long timeout
) throws XMPPException
;
229 * Executes the next action of the command with the information provided in
230 * the <code>response</code>. This form must be the answer form of the
231 * previous stage. This method will be only invoked for commands that have one
232 * or more stages. If there is a problem executing the command it throws an
235 * @param response the form answer of the previous stage.
236 * @throws XMPPException if there is a problem executing the command.
238 public abstract void next(Form response
) throws XMPPException
;
241 * Completes the command execution with the information provided in the
242 * <code>response</code>. This form must be the answer form of the
243 * previous stage. This method will be only invoked for commands that have one
244 * or more stages. If there is a problem executing the command it throws an
247 * @param response the form answer of the previous stage.
248 * @throws XMPPException if there is a problem executing the command.
250 public abstract void complete(Form response
) throws XMPPException
;
253 * Goes to the previous stage. The requester is asking to re-send the
254 * information of the previous stage. The command must change it state to
255 * the previous one. If there is a problem executing the command it throws
258 * @throws XMPPException if there is a problem executing the command.
260 public abstract void prev() throws XMPPException
;
263 * Cancels the execution of the command. This can be invoked on any stage of
264 * the execution. If there is a problem executing the command it throws an
267 * @throws XMPPException if there is a problem executing the command.
269 public abstract void cancel() throws XMPPException
;
272 * Returns a collection with the allowed actions based on the current stage.
273 * Possible actions are: {@link Action#prev prev}, {@link Action#next next} and
274 * {@link Action#complete complete}. This method will be only invoked for commands that
275 * have one or more stages.
277 * @return a collection with the allowed actions based on the current stage
278 * as defined in the SessionData.
280 protected List
<Action
> getActions() {
281 return data
.getActions();
285 * Add an action to the current stage available actions. This should be used
286 * when creating a response.
288 * @param action the action.
290 protected void addActionAvailable(Action action
) {
291 data
.addAction(action
);
295 * Returns the action available for the current stage which is
296 * considered the equivalent to "execute". When the requester sends his
297 * reply, if no action was defined in the command then the action will be
298 * assumed "execute" thus assuming the action returned by this method. This
299 * method will never be invoked for commands that have no stages.
301 * @return the action available for the current stage which is considered
302 * the equivalent to "execute".
304 protected Action
getExecuteAction() {
305 return data
.getExecuteAction();
309 * Sets which of the actions available for the current stage is
310 * considered the equivalent to "execute". This should be used when setting
311 * a response. When the requester sends his reply, if no action was defined
312 * in the command then the action will be assumed "execute" thus assuming
313 * the action returned by this method.
315 * @param action the action.
317 protected void setExecuteAction(Action action
) {
318 data
.setExecuteAction(action
);
322 * Returns the status of the current stage.
324 * @return the current status.
326 public Status
getStatus() {
327 return data
.getStatus();
331 * Sets the data of the current stage. This should not used.
333 * @param data the data.
335 void setData(AdHocCommandData data
) {
340 * Gets the data of the current stage. This should not used.
344 AdHocCommandData
getData() {
349 * Returns true if the <code>action</code> is available in the current stage.
350 * The {@link Action#cancel cancel} action is always allowed. To define the
351 * available actions use the <code>addActionAvailable</code> method.
354 * The action to check if it is available.
355 * @return True if the action is available for the current stage.
357 protected boolean isValidAction(Action action
) {
358 return getActions().contains(action
) || Action
.cancel
.equals(action
);
362 * The status of the stage in the adhoc command.
367 * The command is being executed.
372 * The command has completed. The command session has ended.
377 * The command has been canceled. The command session has ended.
385 * The command should be executed or continue to be executed. This is
391 * The command should be canceled.
396 * The command should be digress to the previous stage of execution.
401 * The command should progress to the next stage of execution.
406 * The command should be completed (if possible).
411 * The action is unknow. This is used when a recieved message has an
412 * unknown action. It must not be used to send an execution request.
417 public enum SpecificErrorCondition
{
420 * The responding JID cannot accept the specified action.
422 badAction("bad-action"),
425 * The responding JID does not understand the specified action.
427 malformedAction("malformed-action"),
430 * The responding JID cannot accept the specified language/locale.
432 badLocale("bad-locale"),
435 * The responding JID cannot accept the specified payload (e.g. the data
436 * form did not provide one or more required fields).
438 badPayload("bad-payload"),
441 * The responding JID cannot accept the specified sessionid.
443 badSessionid("bad-sessionid"),
446 * The requesting JID specified a sessionid that is no longer active
447 * (either because it was completed, canceled, or timed out).
449 sessionExpired("session-expired");
451 private String value
;
453 SpecificErrorCondition(String value
) {
457 public String
toString() {