001/** 002 * 003 * Copyright 2005-2007 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 */ 017package org.jivesoftware.smackx.commands; 018 019import org.jivesoftware.smack.SmackException.NoResponseException; 020import org.jivesoftware.smack.SmackException.NotConnectedException; 021import org.jivesoftware.smack.XMPPConnection; 022import org.jivesoftware.smack.XMPPException.XMPPErrorException; 023import org.jivesoftware.smack.packet.IQ; 024 025import org.jivesoftware.smackx.commands.packet.AdHocCommandData; 026import org.jivesoftware.smackx.xdata.form.FillableForm; 027import org.jivesoftware.smackx.xdata.packet.DataForm; 028 029import org.jxmpp.jid.Jid; 030 031/** 032 * Represents a command that is in a remote location. Invoking one of the 033 * {@link AdHocCommand.Action#execute execute}, {@link AdHocCommand.Action#next next}, 034 * {@link AdHocCommand.Action#prev prev}, {@link AdHocCommand.Action#cancel cancel} or 035 * {@link AdHocCommand.Action#complete complete} actions results in executing that 036 * action in the remote location. In response to that action the internal state 037 * of the this command instance will change. For example, if the command is a 038 * single stage command, then invoking the execute action will execute this 039 * action in the remote location. After that the local instance will have a 040 * state of "completed" and a form or notes that applies. 041 * 042 * @author Gabriel Guardincerri 043 * 044 */ 045public class RemoteCommand extends AdHocCommand { 046 047 /** 048 * The connection that is used to execute this command 049 */ 050 private final XMPPConnection connection; 051 052 /** 053 * The full JID of the command host 054 */ 055 private final Jid jid; 056 057 /** 058 * The session ID of this execution. 059 */ 060 private String sessionID; 061 062 /** 063 * Creates a new RemoteCommand that uses an specific connection to execute a 064 * command identified by <code>node</code> in the host identified by 065 * <code>jid</code> 066 * 067 * @param connection the connection to use for the execution. 068 * @param node the identifier of the command. 069 * @param jid the JID of the host. 070 */ 071 protected RemoteCommand(XMPPConnection connection, String node, Jid jid) { 072 super(); 073 this.connection = connection; 074 this.jid = jid; 075 this.setNode(node); 076 } 077 078 @Override 079 public void cancel() throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { 080 executeAction(Action.cancel); 081 } 082 083 @Override 084 public void complete(FillableForm form) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { 085 executeAction(Action.complete, form.getDataFormToSubmit()); 086 } 087 088 @Override 089 public void execute() throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { 090 executeAction(Action.execute); 091 } 092 093 /** 094 * Executes the default action of the command with the information provided 095 * in the Form. This form must be the answer form of the previous stage. If 096 * there is a problem executing the command it throws an XMPPException. 097 * 098 * @param form the form answer of the previous stage. 099 * @throws XMPPErrorException if an error occurs. 100 * @throws NoResponseException if there was no response from the server. 101 * @throws NotConnectedException if the XMPP connection is not connected. 102 * @throws InterruptedException if the calling thread was interrupted. 103 */ 104 public void execute(FillableForm form) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { 105 executeAction(Action.execute, form.getDataFormToSubmit()); 106 } 107 108 @Override 109 public void next(FillableForm form) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { 110 executeAction(Action.next, form.getDataFormToSubmit()); 111 } 112 113 @Override 114 public void prev() throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { 115 executeAction(Action.prev); 116 } 117 118 private void executeAction(Action action) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { 119 executeAction(action, null); 120 } 121 122 /** 123 * Executes the <code>action</code> with the <code>form</code>. 124 * The action could be any of the available actions. The form must 125 * be the answer of the previous stage. It can be <code>null</code> if it is the first stage. 126 * 127 * @param action the action to execute. 128 * @param form the form with the information. 129 * @throws XMPPErrorException if there is a problem executing the command. 130 * @throws NoResponseException if there was no response from the server. 131 * @throws NotConnectedException if the XMPP connection is not connected. 132 * @throws InterruptedException if the calling thread was interrupted. 133 */ 134 private void executeAction(Action action, DataForm form) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { 135 // TODO: Check that all the required fields of the form were filled, if 136 // TODO: not throw the corresponding exception. This will make a faster response, 137 // TODO: since the request is stopped before it's sent. 138 AdHocCommandData data = new AdHocCommandData(); 139 data.setType(IQ.Type.set); 140 data.setTo(getOwnerJID()); 141 data.setNode(getNode()); 142 data.setSessionID(sessionID); 143 data.setAction(action); 144 data.setForm(form); 145 146 AdHocCommandData responseData = null; 147 try { 148 responseData = connection.createStanzaCollectorAndSend(data).nextResultOrThrow(); 149 } 150 finally { 151 // We set the response data in a 'finally' block, so that it also gets set even if an error IQ was returned. 152 if (responseData != null) { 153 this.sessionID = responseData.getSessionID(); 154 super.setData(responseData); 155 } 156 } 157 158 } 159 160 @Override 161 public Jid getOwnerJID() { 162 return jid; 163 } 164}