AdHocCommandData.java

  1. /**
  2.  *
  3.  * Copyright 2005-2008 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.packet;

  18. import org.jivesoftware.smack.packet.IQ;
  19. import org.jivesoftware.smack.packet.ExtensionElement;
  20. import org.jivesoftware.smackx.commands.AdHocCommand;
  21. import org.jivesoftware.smackx.commands.AdHocCommand.Action;
  22. import org.jivesoftware.smackx.commands.AdHocCommand.SpecificErrorCondition;
  23. import org.jivesoftware.smackx.commands.AdHocCommandNote;
  24. import org.jivesoftware.smackx.xdata.packet.DataForm;
  25. import org.jxmpp.jid.Jid;

  26. import java.util.ArrayList;
  27. import java.util.List;

  28. /**
  29.  * Represents the state and the request of the execution of an adhoc command.
  30.  *
  31.  * @author Gabriel Guardincerri
  32.  */
  33. public class AdHocCommandData extends IQ {

  34.     public static final String ELEMENT = "command";
  35.     public static final String NAMESPACE = "http://jabber.org/protocol/commands";

  36.     /* JID of the command host */
  37.     private Jid id;

  38.     /* Command name */
  39.     private String name;

  40.     /* Command identifier */
  41.     private String node;

  42.     /* Unique ID of the execution */
  43.     private String sessionID;

  44.     private List<AdHocCommandNote> notes = new ArrayList<AdHocCommandNote>();

  45.     private DataForm form;

  46.     /* Action request to be executed */
  47.     private AdHocCommand.Action action;

  48.     /* Current execution status */
  49.     private AdHocCommand.Status status;

  50.     private ArrayList<AdHocCommand.Action> actions = new ArrayList<AdHocCommand.Action>();

  51.     private AdHocCommand.Action executeAction;

  52.     public AdHocCommandData() {
  53.         super(ELEMENT, NAMESPACE);
  54.     }

  55.     @Override
  56.     protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
  57.         xml.attribute("node", node);
  58.         xml.optAttribute("sessionid", sessionID);
  59.         xml.optAttribute("status", status);
  60.         xml.optAttribute("action", action);
  61.         xml.rightAngleBracket();

  62.         if (getType() == Type.result) {
  63.             xml.halfOpenElement("actions");
  64.             xml.optAttribute("execute", executeAction);
  65.             if (actions.size() == 0) {
  66.                 xml.closeEmptyElement();
  67.             } else {
  68.                 xml.rightAngleBracket();

  69.                 for (AdHocCommand.Action action : actions) {
  70.                     xml.emptyElement(action);
  71.                 }
  72.                 xml.closeElement("actions");
  73.             }
  74.         }

  75.         if (form != null) {
  76.             xml.append(form.toXML());
  77.         }

  78.         for (AdHocCommandNote note : notes) {
  79.             xml.halfOpenElement("note").attribute("type", note.getType().toString()).rightAngleBracket();
  80.             xml.append(note.getValue());
  81.             xml.closeElement("note");
  82.         }

  83.         // TODO ERRORS
  84. //        if (getError() != null) {
  85. //            buf.append(getError().toXML());
  86. //        }

  87.         return xml;
  88.     }

  89.     /**
  90.      * Returns the JID of the command host.
  91.      *
  92.      * @return the JID of the command host.
  93.      */
  94.     public Jid getId() {
  95.         return id;
  96.     }

  97.     public void setId(Jid id) {
  98.         this.id = id;
  99.     }

  100.     /**
  101.      * Returns the human name of the command
  102.      *
  103.      * @return the name of the command.
  104.      */
  105.     public String getName() {
  106.         return name;
  107.     }

  108.     public void setName(String name) {
  109.         this.name = name;
  110.     }

  111.     /**
  112.      * Returns the identifier of the command
  113.      *
  114.      * @return the node.
  115.      */
  116.     public String getNode() {
  117.         return node;
  118.     }

  119.     public void setNode(String node) {
  120.         this.node = node;
  121.     }

  122.     /**
  123.      * Returns the list of notes that the command has.
  124.      *
  125.      * @return the notes.
  126.      */
  127.     public List<AdHocCommandNote> getNotes() {
  128.         return notes;
  129.     }

  130.     public void addNote(AdHocCommandNote note) {
  131.         this.notes.add(note);
  132.     }

  133.     public void remveNote(AdHocCommandNote note) {
  134.         this.notes.remove(note);
  135.     }

  136.     /**
  137.      * Returns the form of the command.
  138.      *
  139.      * @return the data form associated with the command.
  140.      */
  141.     public DataForm getForm() {
  142.         return form;
  143.     }

  144.     public void setForm(DataForm form) {
  145.         this.form = form;
  146.     }

  147.     /**
  148.      * Returns the action to execute. The action is set only on a request.
  149.      *
  150.      * @return the action to execute.
  151.      */
  152.     public AdHocCommand.Action getAction() {
  153.         return action;
  154.     }

  155.     public void setAction(AdHocCommand.Action action) {
  156.         this.action = action;
  157.     }

  158.     /**
  159.      * Returns the status of the execution.
  160.      *
  161.      * @return the status.
  162.      */
  163.     public AdHocCommand.Status getStatus() {
  164.         return status;
  165.     }

  166.     public void setStatus(AdHocCommand.Status status) {
  167.         this.status = status;
  168.     }

  169.     public List<Action> getActions() {
  170.         return actions;
  171.     }

  172.     public void addAction(Action action) {
  173.         actions.add(action);
  174.     }

  175.     public void setExecuteAction(Action executeAction) {
  176.         this.executeAction = executeAction;
  177.     }

  178.     public Action getExecuteAction() {
  179.         return executeAction;
  180.     }

  181.     /**
  182.      * Set the 'sessionid' attribute of the command.
  183.      * <p>
  184.      * This value can be null or empty for the first command, but MUST be set for subsequent commands. See also <a
  185.      * href="http://xmpp.org/extensions/xep-0050.html#impl-session">XEP-0050 ยง 3.3 Session Lifetime</a>.
  186.      * </p>
  187.      *
  188.      * @param sessionID
  189.      */
  190.     public void setSessionID(String sessionID) {
  191.         this.sessionID = sessionID;
  192.     }

  193.     public String getSessionID() {
  194.         return sessionID;
  195.     }

  196.     public static class SpecificError implements ExtensionElement {

  197.         public static final String namespace = "http://jabber.org/protocol/commands";
  198.        
  199.         public SpecificErrorCondition condition;
  200.        
  201.         public SpecificError(SpecificErrorCondition condition) {
  202.             this.condition = condition;
  203.         }
  204.        
  205.         public String getElementName() {
  206.             return condition.toString();
  207.         }
  208.         public String getNamespace() {
  209.             return namespace;
  210.         }

  211.         public SpecificErrorCondition getCondition() {
  212.             return condition;
  213.         }
  214.        
  215.         public String toXML() {
  216.             StringBuilder buf = new StringBuilder();
  217.             buf.append("<").append(getElementName());
  218.             buf.append(" xmlns=\"").append(getNamespace()).append("\"/>");
  219.             return buf.toString();
  220.         }
  221.     }
  222. }