001/** 002 * 003 * Copyright 2005-2008 Jive Software. 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018package org.jivesoftware.smackx.commands.packet; 019 020import java.util.ArrayList; 021import java.util.List; 022 023import org.jivesoftware.smack.packet.ExtensionElement; 024import org.jivesoftware.smack.packet.IQ; 025 026import org.jivesoftware.smackx.commands.AdHocCommand; 027import org.jivesoftware.smackx.commands.AdHocCommand.Action; 028import org.jivesoftware.smackx.commands.AdHocCommand.SpecificErrorCondition; 029import org.jivesoftware.smackx.commands.AdHocCommandNote; 030import org.jivesoftware.smackx.xdata.packet.DataForm; 031 032import org.jxmpp.jid.Jid; 033 034/** 035 * Represents the state and the request of the execution of an adhoc command. 036 * 037 * @author Gabriel Guardincerri 038 */ 039public class AdHocCommandData extends IQ { 040 041 public static final String ELEMENT = "command"; 042 public static final String NAMESPACE = "http://jabber.org/protocol/commands"; 043 044 /* JID of the command host */ 045 private Jid id; 046 047 /* Command name */ 048 private String name; 049 050 /* Command identifier */ 051 private String node; 052 053 /* Unique ID of the execution */ 054 private String sessionID; 055 056 private final List<AdHocCommandNote> notes = new ArrayList<>(); 057 058 private DataForm form; 059 060 /* Action request to be executed */ 061 private AdHocCommand.Action action; 062 063 /* Current execution status */ 064 private AdHocCommand.Status status; 065 066 private final ArrayList<AdHocCommand.Action> actions = new ArrayList<>(); 067 068 private AdHocCommand.Action executeAction; 069 070 public AdHocCommandData() { 071 super(ELEMENT, NAMESPACE); 072 } 073 074 @Override 075 protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) { 076 xml.attribute("node", node); 077 xml.optAttribute("sessionid", sessionID); 078 xml.optAttribute("status", status); 079 xml.optAttribute("action", action); 080 xml.rightAngleBracket(); 081 082 if (getType() == Type.result) { 083 xml.halfOpenElement("actions"); 084 xml.optAttribute("execute", executeAction); 085 if (actions.size() == 0) { 086 xml.closeEmptyElement(); 087 } else { 088 xml.rightAngleBracket(); 089 090 for (AdHocCommand.Action action : actions) { 091 xml.emptyElement(action); 092 } 093 xml.closeElement("actions"); 094 } 095 } 096 097 if (form != null) { 098 xml.append(form.toXML()); 099 } 100 101 for (AdHocCommandNote note : notes) { 102 xml.halfOpenElement("note").attribute("type", note.getType().toString()).rightAngleBracket(); 103 xml.append(note.getValue()); 104 xml.closeElement("note"); 105 } 106 107 // TODO ERRORS 108// if (getError() != null) { 109// buf.append(getError().toXML()); 110// } 111 112 return xml; 113 } 114 115 /** 116 * Returns the JID of the command host. 117 * 118 * @return the JID of the command host. 119 */ 120 public Jid getId() { 121 return id; 122 } 123 124 public void setId(Jid id) { 125 this.id = id; 126 } 127 128 /** 129 * Returns the human name of the command. 130 * 131 * @return the name of the command. 132 */ 133 public String getName() { 134 return name; 135 } 136 137 public void setName(String name) { 138 this.name = name; 139 } 140 141 /** 142 * Returns the identifier of the command. 143 * 144 * @return the node. 145 */ 146 public String getNode() { 147 return node; 148 } 149 150 public void setNode(String node) { 151 this.node = node; 152 } 153 154 /** 155 * Returns the list of notes that the command has. 156 * 157 * @return the notes. 158 */ 159 public List<AdHocCommandNote> getNotes() { 160 return notes; 161 } 162 163 public void addNote(AdHocCommandNote note) { 164 this.notes.add(note); 165 } 166 167 public void removeNote(AdHocCommandNote note) { 168 this.notes.remove(note); 169 } 170 171 /** 172 * Returns the form of the command. 173 * 174 * @return the data form associated with the command. 175 */ 176 public DataForm getForm() { 177 return form; 178 } 179 180 public void setForm(DataForm form) { 181 this.form = form; 182 } 183 184 /** 185 * Returns the action to execute. The action is set only on a request. 186 * 187 * @return the action to execute. 188 */ 189 public AdHocCommand.Action getAction() { 190 return action; 191 } 192 193 public void setAction(AdHocCommand.Action action) { 194 this.action = action; 195 } 196 197 /** 198 * Returns the status of the execution. 199 * 200 * @return the status. 201 */ 202 public AdHocCommand.Status getStatus() { 203 return status; 204 } 205 206 public void setStatus(AdHocCommand.Status status) { 207 this.status = status; 208 } 209 210 public List<Action> getActions() { 211 return actions; 212 } 213 214 public void addAction(Action action) { 215 actions.add(action); 216 } 217 218 public void setExecuteAction(Action executeAction) { 219 this.executeAction = executeAction; 220 } 221 222 public Action getExecuteAction() { 223 return executeAction; 224 } 225 226 /** 227 * Set the 'sessionid' attribute of the command. 228 * <p> 229 * This value can be null or empty for the first command, but MUST be set for subsequent commands. See also <a 230 * href="http://xmpp.org/extensions/xep-0050.html#impl-session">XEP-0050 ยง 3.3 Session Lifetime</a>. 231 * </p> 232 * 233 * @param sessionID TODO javadoc me please 234 */ 235 public void setSessionID(String sessionID) { 236 this.sessionID = sessionID; 237 } 238 239 public String getSessionID() { 240 return sessionID; 241 } 242 243 public static class SpecificError implements ExtensionElement { 244 245 public static final String namespace = "http://jabber.org/protocol/commands"; 246 247 public SpecificErrorCondition condition; 248 249 public SpecificError(SpecificErrorCondition condition) { 250 this.condition = condition; 251 } 252 253 @Override 254 public String getElementName() { 255 return condition.toString(); 256 } 257 @Override 258 public String getNamespace() { 259 return namespace; 260 } 261 262 public SpecificErrorCondition getCondition() { 263 return condition; 264 } 265 266 @Override 267 public String toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) { 268 StringBuilder buf = new StringBuilder(); 269 buf.append('<').append(getElementName()); 270 buf.append(" xmlns=\"").append(getNamespace()).append("\"/>"); 271 return buf.toString(); 272 } 273 } 274}