public Deferred execute(final GwtCommand command, final Deferred deferred) {
		if (!deferreds.contains(deferred)) {
			deferreds.add(deferred);
		}

		command.setLocale(locale);
		command.setUserToken(userToken);

		// shortcut, no need to invoke the server if we know the token has expired
		if (null != userToken && userToken.length() > 0 && afterLoginCommands.containsKey(userToken)) {
			afterLogin(command, deferred);
			return deferred;
		}

		incrementDispatched();

		service.execute(command, new AsyncCallback<CommandResponse>() {

			public void onFailure(Throwable error) {
				try {
					boolean errorHandled = false;
					for (CommandCallback<?> callback : deferred.getCallbacks()) {
						if (callback instanceof CommunicationExceptionCallback) {
							try {
								((CommunicationExceptionCallback) callback).onCommunicationException(error);
							} catch (Throwable t) {
								Log.logError("Command failed on error callback", t);
							}
							errorHandled = true;
						}
					}
					if (!errorHandled && deferred.isLogCommunicationExceptions()) {
						onCommunicationException(error);
					}
				} catch (Throwable t) {
					if (deferred.isLogCommunicationExceptions()) {
						Log.logError("Command failed on error callback", t);
					}
				} finally {
					decrementDispatched();
					deferreds.remove(deferred);
				}
			}

			public void onSuccess(CommandResponse response) {
				try {
					if (response.isError()) {
						handleError(response);
					} else {
						if (!deferred.isCancelled()) {
							for (CommandCallback callback : deferred.getCallbacks()) {
								try {
									callback.execute(response);
								} catch (Throwable t) {
									Log.logError("Command failed on success callback", t);
								}
							}
						}
					}
				} catch (Throwable t) {
					Log.logError("Command failed on success callback", t);
				} finally {
					decrementDispatched();
					deferreds.remove(deferred);
				}
			}

			private void handleError(CommandResponse response) {
				// Security exceptions which indicate that the token is invalid cause a token request.
				// Other security exceptions are normally reported, except when then undefined (null or empty)
				// token is used. For restricted access for the anonymous user, this should also be assigned
				// a proper token (can be a constant).
				// This assumes that the undefined token is only used either in combination with the allow all
				// security service or in a state between logout and login.
				boolean authenticationFailed = false;
				for (ExceptionDto exception : response.getExceptions()) {
					authenticationFailed |= SECURITY_EXCEPTION_CLASS_NAME.equals(exception.getClassName())
							&& (ExceptionCode.CREDENTIALS_MISSING_OR_INVALID == exception.getExceptionCode()
								|| isUndefinedToken(command.getUserToken()));
				}
				if (authenticationFailed && null != tokenRequestHandler) {
					handleLogin(command, deferred);
				} else {
					// normal error handling...

					boolean errorHandled = false;
					for (CommandCallback callback : deferred.getCallbacks()) {
						if (callback instanceof CommandExceptionCallback) {
							try {
								((CommandExceptionCallback) callback).onCommandException(response);
								errorHandled = true;
							} catch (Throwable t) {
								Log.logError("Command failed on error callback", t);
							}
						}
					}
					// fallback to the default behaviour
					if (!errorHandled) {
						try {
							onCommandException(response);
						} catch (Throwable t) {
							Log.logError("Command failed on error callback", t);
						}
					}
				}
			}
		});
		return deferred;
	}
public Deferred execute ( final GwtCommand command , final Deferred deferred ) { if ( ! deferreds . contains ( deferred ) ) { deferreds . add ( deferred ) ; } command . setLocale ( locale ) ; command . setUserToken ( userToken ) ; if ( null != userToken && userToken . length ( ) > 0 && afterLoginCommands . containsKey ( userToken ) ) { afterLogin ( command , deferred ) ; return deferred ; } incrementDispatched ( ) ; service . execute ( command , new AsyncCallback < CommandResponse > ( ) { public void onFailure ( Throwable error ) { try { boolean errorHandled = false ; for ( CommandCallback < ? > callback : deferred . getCallbacks ( ) ) { if ( callback instanceof CommunicationExceptionCallback ) { try { ( ( CommunicationExceptionCallback ) callback ) . onCommunicationException ( error ) ; } catch ( Throwable t ) { Log . logError ( "Command failed on error callback" , t ) ; } errorHandled = true ; } } if ( ! errorHandled && deferred . isLogCommunicationExceptions ( ) ) { onCommunicationException ( error ) ; } } catch ( Throwable t ) { if ( deferred . isLogCommunicationExceptions ( ) ) { Log . logError ( "Command failed on error callback" , t ) ; } } finally { decrementDispatched ( ) ; deferreds . remove ( deferred ) ; } } public void onSuccess ( CommandResponse response ) { try { if ( response . isError ( ) ) { handleError ( response ) ; } else { if ( ! deferred . isCancelled ( ) ) { for ( CommandCallback callback : deferred . getCallbacks ( ) ) { try { callback . execute ( response ) ; } catch ( Throwable t ) { Log . logError ( "Command failed on success callback" , t ) ; } } } } } catch ( Throwable t ) { Log . logError ( "Command failed on success callback" , t ) ; } finally { decrementDispatched ( ) ; deferreds . remove ( deferred ) ; } } private void handleError ( CommandResponse response ) { boolean authenticationFailed = false ; for ( ExceptionDto exception : response . getExceptions ( ) ) { authenticationFailed |= SECURITY_EXCEPTION_CLASS_NAME . equals ( exception . getClassName ( ) ) && ( ExceptionCode . CREDENTIALS_MISSING_OR_INVALID == exception . getExceptionCode ( ) || isUndefinedToken ( command . getUserToken ( ) ) ) ; } if ( authenticationFailed && null != tokenRequestHandler ) { handleLogin ( command , deferred ) ; } else { boolean errorHandled = false ; for ( CommandCallback callback : deferred . getCallbacks ( ) ) { if ( callback instanceof CommandExceptionCallback ) { try { ( ( CommandExceptionCallback ) callback ) . onCommandException ( response ) ; errorHandled = true ; } catch ( Throwable t ) { Log . logError ( "Command failed on error callback" , t ) ; } } } if ( ! errorHandled ) { try { onCommandException ( response ) ; } catch ( Throwable t ) { Log . logError ( "Command failed on error callback" , t ) ; } } } } } ) ; return deferred ; }
The execution function. Executes a server side command.

@param command
The command to be executed. This command is a wrapper around the actual request object.
@param deferred
list of callbacks for the command
@return original deferred object as passed as parameter