/** * Collects all the informations contained in this object like the target * host, the protocol; the more information contained in * {@link DSRESTCommand} input parameter as the type of request, the method * to call then to return the url to run * * * @param the * specific DSRESTCommand * @return a requested url */ private String BuildRequestURL(DSRESTCommand command) { String LPathPrefix = getUrlPath(); int LPort = getPort(); String LHost = getHost(); String LMethodName = command.getText(); String LProtocol = getProtocol(); if (LProtocol.Equals("")) { LProtocol = "http"; } if (LHost.Equals("")) { LHost = "localhost"; } if (!LPathPrefix.Equals("")) { LPathPrefix = "/" + LPathPrefix; } String LPortString = ""; if (LPort > 0) { LPortString = ":" + Convert.ToInt32(LPort); } if (command.getRequestType() == DSHTTPRequestType.GET || command.getRequestType() == DSHTTPRequestType.DELETE) { LMethodName = LMethodName.Substring(0, LMethodName.IndexOf(".")) + "/" + LMethodName.Substring(LMethodName.IndexOf(".") + 1); LMethodName = LMethodName.Replace("\"", ""); } else { LMethodName = LMethodName.Substring(0, LMethodName.IndexOf(".")) + "/%22" + LMethodName.Substring(LMethodName.IndexOf(".") + 1) + "%22"; LMethodName = LMethodName.Replace("\"", "%22"); } String LContext = getContext(); if (LContext.Equals("")) { LContext = "datasnap/"; } String LUrl = LProtocol + "://" + encodeURIComponent(LHost) + LPortString + LPathPrefix + "/" + LContext + "rest/" + LMethodName + "/"; SessionID = getSessionID(); return(LUrl); }
/** * Collects all the informations contained in this object like the target * host, the protocol; the more information contained in * {@link DSRESTCommand} input parameter as the type of request, the method * to call then to return the url to run * * * @param the * specific DSRESTCommand * @return a requested url */ private String BuildRequestURL(DSRESTCommand command) { String LPathPrefix = getUrlPath(); int LPort = getPort(); String LHost = getHost(); String LMethodName = command.getText(); String LProtocol = getProtocol(); if (LProtocol.Equals("")) LProtocol = "http"; if (LHost.Equals("")) LHost = "localhost"; if (!LPathPrefix.Equals("")) LPathPrefix = "/" + LPathPrefix; String LPortString = ""; if (LPort > 0) LPortString = ":" + Convert.ToInt32(LPort); if (command.getRequestType() == DSHTTPRequestType.GET || command.getRequestType() == DSHTTPRequestType.DELETE) { LMethodName = LMethodName.Substring(0, LMethodName.IndexOf(".")) + "/" + LMethodName.Substring(LMethodName.IndexOf(".") + 1); LMethodName = LMethodName.Replace("\"", ""); } else { LMethodName = LMethodName.Substring(0, LMethodName.IndexOf(".")) + "/%22" + LMethodName.Substring(LMethodName.IndexOf(".") + 1) + "%22"; LMethodName = LMethodName.Replace("\"", "%22"); } String LContext = getContext(); if (LContext.Equals("")) LContext = "datasnap/"; String LUrl = LProtocol + "://" + encodeURIComponent(LHost) + LPortString + LPathPrefix + "/" + LContext + "rest/" + LMethodName + "/"; SessionID = getSessionID(); return LUrl; }
/** * 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); } }