Example #1
0
        /**
         *
         * @return a JArray with the ByteArray of Stream
         */
        protected TJSONArray StreamToJson()
        {
            checkCurrentDBXType(DBXDataTypes.BinaryBlobType);

            try {
                byte[]     b     = DBXTools.streamToByteArray(streamValue);
                TJSONArray jsArr = new TJSONArray();
                for (int i = 0; i < b.Length; i++)
                {
                    jsArr.add(new TJSONNumber(Convert.ToInt32(b[i])));
                }
                return(jsArr);
            } catch (Exception e) {
                throw new DBXException(e.Message);
            }
        }
            public override void execute()
            {
                if (_parameters == null)
                {
                    throw new DBXException(
                              "Parameters cannot be null in a POST request");
                }
                TJSONObject body = new TJSONObject();

                body.addPairs("_parameters", _parameters);
                Client.BeginGetRequestStream((IAsyncResult asynchronousResult) =>
                {
                    Stream postStream = Client.EndGetRequestStream(asynchronousResult);
                    byte[] postBytes  = Encoding.UTF8.GetBytes(body.ToString());
                    postStream.Write(postBytes, 0, postBytes.Length);
                    postStream.Close();
                    Client.BeginGetResponse((IAsyncResult asynchResult) =>
                    {
                        HttpWebRequest request   = (HttpWebRequest)asynchResult.AsyncState;
                        HttpWebResponse response = null;
                        try
                        {
                            response = (HttpWebResponse)request.EndGetResponse(asynchResult);
                        }
                        catch (WebException e)
                        {
                            try
                            {
                                Stream s       = ((HttpWebResponse)e.Response).GetResponseStream();
                                TextReader txt = new StreamReader(s);
                                if (s.Length > 0)
                                {
                                    connection.throwExceptionIfNeeded(JObject.Parse(txt.ReadToEnd()));
                                }
                                else
                                {
                                    throw new DBXException(e.Message);
                                }
                            }
                            catch (Exception ex)
                            {
                                if (EXCallback != null)
                                {
                                    connection.syncContext.Send(new SendOrPostCallback(x => EXCallback.DynamicInvoke(ex)), null);
                                }
                                else
                                if (Sender.BaseExCal != null)
                                {
                                    connection.syncContext.Send(new SendOrPostCallback(x => Sender.BaseExCal.DynamicInvoke(ex)), null);
                                }
                                else
                                {
                                    throw ex;
                                }
                                return;
                            }
                        }
                        finally
                        {
                            syncStop();
                        }
                        connection.setSessionIdentifier(response);
                        connection.throwExceptionIfNeeded(response);
                        if (!isThereOnlyOneStreamInOutput(command.getParameters()))
                        {
                            string resultString = null;
                            using (StreamReader rdr = new StreamReader(response.GetResponseStream()))
                            {
                                resultString = rdr.ReadToEnd();
                                rdr.Close();
                            }
                            response.Close();
                            try
                            {
                                JObject obj        = JObject.Parse(resultString);
                                JArray arr         = obj.Value <JArray>("result");
                                int returnParIndex = 0;
                                foreach (DSRESTParameter param in command.getParameters())
                                {
                                    if ((param.Direction == DSRESTParamDirection.ReturnValue) ||
                                        (param.Direction == DSRESTParamDirection.InputOutput) ||
                                        (param.Direction == DSRESTParamDirection.Output))
                                    {
                                        DBXJSONTools.JSONtoDBX(arr[returnParIndex],
                                                               param.getValue(), param.TypeName);
                                        returnParIndex++;
                                    } // if
                                }     // for
                            }
                            catch (DBXException e)
                            {
                                throw new DBXException(e.Message);
                            }
                            if (!(_TimedOut))
                            {
                                connection.syncContext.Send(new SendOrPostCallback(x => callback.DynamicInvoke()), null);
                            }
                        }
                        else
                        {
                            Stream inputstream = response.GetResponseStream();
                            byte[] b1          = DBXTools.streamToByteArray(inputstream);
                            inputstream.Close();
                            response.Close();
                            TStream ins = new TStream(b1);
                            foreach (DSRESTParameter param in command.getParameters())
                            {
                                if ((param.Direction == DSRESTParamDirection.ReturnValue) ||
                                    (param.Direction == DSRESTParamDirection.InputOutput) ||
                                    (param.Direction == DSRESTParamDirection.Output))
                                {
                                    if (param.TypeName.StartsWith("TDBX") && param.TypeName.EndsWith("Value"))
                                    {
                                        param.getValue().GetAsDBXValue().SetAsStream(ins);
                                    }
                                    else
                                    {
                                        param.getValue().SetAsStream(ins);
                                    }
                                    if (!(_TimedOut))
                                    {
                                        connection.syncContext.Send(new SendOrPostCallback(x => callback.DynamicInvoke()), null);
                                    }
                                }
                            }
                        };
                    }, Client);
                }, Client);
                start();
            }