/**
  * Allows you to prepare and execute a request of type REST.
  *
  * Use the properties and methods of DSRESTCommand to: <br>
  * &nbsp;&nbsp;&nbsp;&nbsp;- Set and get the request type. <br>
  * &nbsp;&nbsp;&nbsp;&nbsp;- Set and get the specific method to call. <br>
  * &nbsp;&nbsp;&nbsp;&nbsp;- Prepare the command from a metadata input
  * parameter. <br>
  * &nbsp;&nbsp;&nbsp;&nbsp;- Get all the parameters contained. <br>
  * &nbsp;&nbsp;&nbsp;&nbsp;- Get a parameter by index or name.
  *
  */
 public DSRESTCommand(DSRESTConnection Connection)
     : base()
 {
     this.Connection = Connection;
     Admin = new DSAdmin(Connection, (ex) =>
     {
     throw ex;
     });
 }
 public DSCallbackChannelManager(DSRESTConnection Connection,
         String ChannelName)
     : base()
 {
     locker = new Object();
     this.ChannelName = ChannelName;
     this.ManagerID = getNewManagerID();
     this.Connection = Connection;
     this.dsadmin = new DSAdmin(this.Connection, ExCallback);
     Random random = new Random();
     this.SecurityToken = Convert.ToString(random.Next(100000)) + "." + Convert.ToString(random.Next(100000));
 }
            public HTTPPOSTExecutor(DSRESTConnection connection, HttpWebRequest Client, DSRESTCommand command, DSAdmin Sender, Delegate callback, Delegate EXCallback, TJSONArray parameters)
                : base(connection, Client, command, Sender, callback, EXCallback)
            {
                this._parameters = parameters;

                Client.Method = "POST";
                SetUpHeaders(Client);
            }
 public HTTPGETExecutor(DSRESTConnection connection, HttpWebRequest Client, DSRESTCommand command, DSAdmin Sender, Delegate callback, Delegate EXCallback)
     : base(connection, Client, command, Sender, callback, EXCallback)
 {
     Client.Method = "GET";
     SetUpHeaders(Client);
 }
            protected HTTPExecutor(DSRESTConnection connection, HttpWebRequest Client, DSRESTCommand command, DSAdmin Sender, Delegate callback, Delegate EXCallback)
            {
                this.connection = connection;
                this.Client = Client;
                this.command = command;
                this.Sender = Sender;
                this.callback = callback;
                this.EXCallback = EXCallback;
                this._TimedOut = false;
                this._Timer = null;

                //don't enable timeout if the request is for a heavyweight callback. Heavyweight callbacks should be timed out
                //with custom code, which uses a call to close the channel with the server when the timeout happens.
                if (connection.getConnectionTimeout() > 0 && !isHeavyweightCallbackRequest(Client))
                {
                    connection.syncContext.Send(new SendOrPostCallback(x => initTimer()), null);
                }
            }
        /**
         * Execute the request from a specific {@link DSRESTCommand} input, that
         * will contain useful information to construct the URL as the type of
         * request, the method to execute and the parameters to be passed. This
         * information be added to those contained in this object as protocol,
         * target host, context... They form the complete request to execute. This
         * method is need to pass parameters correctly or under the parameter
         * direction, it will be append on the url string or written in the body of
         * the request. Upon receipt of the response will have to check the
         * correctness of the received parameters and set them in the
         * {@link DSRESTCommand}.
         *
         * @param command the specific {@link DSRESTCommand}
         * @param Sender DSAdmin
         * @param callback Delegate
         * @param EXCallback Delegate
         */
        public void execute(DSRESTCommand command, DSAdmin Sender, Delegate callback, Delegate EXCallback = null)
        {
            TJSONArray _parameters = null;
            String URL = BuildRequestURL(command);
            LinkedList<DSRESTParameter> ParametersToSend = new LinkedList<DSRESTParameter>();
            if (command.getParameters().Count > 0)
                foreach (DSRESTParameter parameter in command.getParameters())
                {
                    if (parameter.Direction == DSRESTParamDirection.Input ||
                            parameter.Direction == DSRESTParamDirection.InputOutput)
                        ParametersToSend.AddLast(parameter);
                }
            if (command.getRequestType() == DSHTTPRequestType.GET ||
                    command.getRequestType() == DSHTTPRequestType.DELETE)
            {
                foreach (DSRESTParameter parameter in ParametersToSend)
                    URL += encodeURIComponent(parameter) + '/';
            }
            else // POST or PUT
            {
                bool CanAddParamsToUrl = true;
                _parameters = new TJSONArray();
                foreach (DSRESTParameter parameter in ParametersToSend)
                    if (CanAddParamsToUrl && isURLParameter(parameter))
                        URL += encodeURIComponent(parameter) + '/';
                    else // add the json rapresentation in the body
                    {
                        CanAddParamsToUrl = false;
                        parameter.getValue().appendTo(_parameters);
                    }
            }
            HttpWebRequest Client = (HttpWebRequest)WebRequest.Create(URL + "?" + DateTime.Now.Ticks.ToString());

            HTTPExecutor _executor = null;
            try
            {
                switch (command.getRequestType())
                {
                    case DSHTTPRequestType.GET:
                        {
                            _executor = new HTTPGETExecutor(this, Client, command, Sender, callback, EXCallback);
                            break;
                        }
                    case DSHTTPRequestType.DELETE:
                        {
                            _executor = new HTTPDELETEExecutor(this, Client, command, Sender, callback, EXCallback);
                            break;
                        }
                    case DSHTTPRequestType.POST:
                        {
                            _executor = new HTTPPOSTExecutor(this, Client, command, Sender, callback, EXCallback, _parameters);
                            break;
                        }
                    case DSHTTPRequestType.PUT:
                        {
                            _executor = new HTTPPUTExecutor(this, Client, command, Sender, callback, EXCallback, _parameters);
                            break;
                        }
                    default: { break; }
                }

                if (_executor != null)
                {
                    try
                    {
                        _executor.execute();
                    }
                    catch (Exception ex)
                    {
                        _executor.stop();
                        throw new DBXException(ex.Message);
                    }
                }
            }
            catch (DBXException e)
            {
                throw new DBXException(e.Message);
            }
        }
 public WorkerThread(DSRESTConnection connection,
         DSCallbackChannelManager mngr)
     : base()
 {
     this.dsadmin = new DSAdmin(connection, mngr.ExCallback);
     this.mngr = mngr;
     callbacks = new Dictionary<string, DBXCallback>();
 }
 /**
  * Registering another callback with the client channel
  *
  * @param CallbackId
  * @throws Exception
  */
 private void registerClientCallback(String CallbackId,
         DSAdmin.RegisterClientCallbackServerCallback RegisterClientCallbackServerCallback = null)
 {
     dsadmin.RegisterClientCallbackServer(getManagerID(), CallbackId,
             ChannelName, getSecurityToken(),
             RegisterClientCallbackServerCallback,
             (Exception ex) =>
             {
                 if (EventListener != null)
                     EventListener.OnException(ex);
             });
 }
 /**
  * Removing a callback from a Client Channel
  *
  * @param CallbackId
  * @throws Exception
  */
 public void unregisterCallback(String CallbackId, DSAdmin.UnregisterClientCallbackCallback Callback = null)
 {
     lockIt();
     try
     {
         dsadmin.UnregisterClientCallback(ChannelName,
                 CallbackId, getSecurityToken(), Callback,
                 (Exception ex) => { if (EventListener != null) EventListener.OnException(ex); });
     }
     finally
     {
         unlockIt();
     }
 }
 public void NotifyCallback(String ClientId, String CallbackId, TJSONValue Msg, DSAdmin.NotifyCallbackCallback callback = null, DSAdmin.ExceptionCallback ExCal = null)
 {
     dsadmin.NotifyCallback(ClientId, CallbackId, Msg, callback, ExCal);
 }
 public void BroadcastToChannel(String ChannelName, TJSONValue Msg, DSAdmin.BroadcastToChannelCallback callback = null, DSAdmin.ExceptionCallback ExCal = null)
 {
     dsadmin.BroadcastToChannel(ChannelName, Msg, callback, ExCal);
 }