/** * 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); } }
/** * 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); } }