Home | History | Annotate | Download | only in packet
      1 /**
      2  * $RCSfile$
      3  * $Revision$
      4  * $Date$
      5  *
      6  * Copyright 2005-2008 Jive Software.
      7  *
      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
     11  *
     12  *     http://www.apache.org/licenses/LICENSE-2.0
     13  *
     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.
     19  */
     20 
     21 package org.jivesoftware.smackx.packet;
     22 
     23 import org.jivesoftware.smack.packet.IQ;
     24 import org.jivesoftware.smack.packet.PacketExtension;
     25 import org.jivesoftware.smackx.commands.AdHocCommand;
     26 import org.jivesoftware.smackx.commands.AdHocCommand.Action;
     27 import org.jivesoftware.smackx.commands.AdHocCommand.SpecificErrorCondition;
     28 import org.jivesoftware.smackx.commands.AdHocCommandNote;
     29 
     30 import java.util.ArrayList;
     31 import java.util.List;
     32 
     33 /**
     34  * Represents the state and the request of the execution of an adhoc command.
     35  *
     36  * @author Gabriel Guardincerri
     37  */
     38 public class AdHocCommandData extends IQ {
     39 
     40     /* JID of the command host */
     41     private String id;
     42 
     43     /* Command name */
     44     private String name;
     45 
     46     /* Command identifier */
     47     private String node;
     48 
     49     /* Unique ID of the execution */
     50     private String sessionID;
     51 
     52     private List<AdHocCommandNote> notes = new ArrayList<AdHocCommandNote>();
     53 
     54     private DataForm form;
     55 
     56     /* Action request to be executed */
     57     private AdHocCommand.Action action;
     58 
     59     /* Current execution status */
     60     private AdHocCommand.Status status;
     61 
     62     private ArrayList<AdHocCommand.Action> actions = new ArrayList<AdHocCommand.Action>();
     63 
     64     private AdHocCommand.Action executeAction;
     65 
     66     private String lang;
     67 
     68     public AdHocCommandData() {
     69     }
     70 
     71     @Override
     72     public String getChildElementXML() {
     73         StringBuilder buf = new StringBuilder();
     74         buf.append("<command xmlns=\"http://jabber.org/protocol/commands\"");
     75         buf.append(" node=\"").append(node).append("\"");
     76         if (sessionID != null) {
     77             if (!sessionID.equals("")) {
     78                 buf.append(" sessionid=\"").append(sessionID).append("\"");
     79             }
     80         }
     81         if (status != null) {
     82             buf.append(" status=\"").append(status).append("\"");
     83         }
     84         if (action != null) {
     85             buf.append(" action=\"").append(action).append("\"");
     86         }
     87 
     88         if (lang != null) {
     89             if (!lang.equals("")) {
     90                 buf.append(" lang=\"").append(lang).append("\"");
     91             }
     92         }
     93         buf.append(">");
     94 
     95         if (getType() == Type.RESULT) {
     96             buf.append("<actions");
     97 
     98             if (executeAction != null) {
     99                 buf.append(" execute=\"").append(executeAction).append("\"");
    100             }
    101             if (actions.size() == 0) {
    102                 buf.append("/>");
    103             } else {
    104                 buf.append(">");
    105 
    106                 for (AdHocCommand.Action action : actions) {
    107                     buf.append("<").append(action).append("/>");
    108                 }
    109                 buf.append("</actions>");
    110             }
    111         }
    112 
    113         if (form != null) {
    114             buf.append(form.toXML());
    115         }
    116 
    117         for (AdHocCommandNote note : notes) {
    118             buf.append("<note type=\"").append(note.getType().toString()).append("\">");
    119             buf.append(note.getValue());
    120             buf.append("</note>");
    121         }
    122 
    123         // TODO ERRORS
    124 //        if (getError() != null) {
    125 //            buf.append(getError().toXML());
    126 //        }
    127 
    128         buf.append("</command>");
    129         return buf.toString();
    130     }
    131 
    132     /**
    133      * Returns the JID of the command host.
    134      *
    135      * @return the JID of the command host.
    136      */
    137     public String getId() {
    138         return id;
    139     }
    140 
    141     public void setId(String id) {
    142         this.id = id;
    143     }
    144 
    145     /**
    146      * Returns the human name of the command
    147      *
    148      * @return the name of the command.
    149      */
    150     public String getName() {
    151         return name;
    152     }
    153 
    154     public void setName(String name) {
    155         this.name = name;
    156     }
    157 
    158     /**
    159      * Returns the identifier of the command
    160      *
    161      * @return the node.
    162      */
    163     public String getNode() {
    164         return node;
    165     }
    166 
    167     public void setNode(String node) {
    168         this.node = node;
    169     }
    170 
    171     /**
    172      * Returns the list of notes that the command has.
    173      *
    174      * @return the notes.
    175      */
    176     public List<AdHocCommandNote> getNotes() {
    177         return notes;
    178     }
    179 
    180     public void addNote(AdHocCommandNote note) {
    181         this.notes.add(note);
    182     }
    183 
    184     public void remveNote(AdHocCommandNote note) {
    185         this.notes.remove(note);
    186     }
    187 
    188     /**
    189      * Returns the form of the command.
    190      *
    191      * @return the data form associated with the command.
    192      */
    193     public DataForm getForm() {
    194         return form;
    195     }
    196 
    197     public void setForm(DataForm form) {
    198         this.form = form;
    199     }
    200 
    201     /**
    202      * Returns the action to execute. The action is set only on a request.
    203      *
    204      * @return the action to execute.
    205      */
    206     public AdHocCommand.Action getAction() {
    207         return action;
    208     }
    209 
    210     public void setAction(AdHocCommand.Action action) {
    211         this.action = action;
    212     }
    213 
    214     /**
    215      * Returns the status of the execution.
    216      *
    217      * @return the status.
    218      */
    219     public AdHocCommand.Status getStatus() {
    220         return status;
    221     }
    222 
    223     public void setStatus(AdHocCommand.Status status) {
    224         this.status = status;
    225     }
    226 
    227     public List<Action> getActions() {
    228         return actions;
    229     }
    230 
    231     public void addAction(Action action) {
    232         actions.add(action);
    233     }
    234 
    235     public void setExecuteAction(Action executeAction) {
    236         this.executeAction = executeAction;
    237     }
    238 
    239     public Action getExecuteAction() {
    240         return executeAction;
    241     }
    242 
    243     public void setSessionID(String sessionID) {
    244         this.sessionID = sessionID;
    245     }
    246 
    247     public String getSessionID() {
    248         return sessionID;
    249     }
    250 
    251     public static class SpecificError implements PacketExtension {
    252 
    253         public static final String namespace = "http://jabber.org/protocol/commands";
    254 
    255         public SpecificErrorCondition condition;
    256 
    257         public SpecificError(SpecificErrorCondition condition) {
    258             this.condition = condition;
    259         }
    260 
    261         public String getElementName() {
    262             return condition.toString();
    263         }
    264         public String getNamespace() {
    265             return namespace;
    266         }
    267 
    268         public SpecificErrorCondition getCondition() {
    269             return condition;
    270         }
    271 
    272         public String toXML() {
    273             StringBuilder buf = new StringBuilder();
    274             buf.append("<").append(getElementName());
    275             buf.append(" xmlns=\"").append(getNamespace()).append("\"/>");
    276             return buf.toString();
    277         }
    278     }
    279 }