Exemple #1
0
        /**
         * Create a JSONObject from a DBXReader
         * @param dbxReader
         * @return JSONObject
         */
        public static TJSONObject DBXReaderToJSONObject(TDBXReader dbxReader)
        {
            TJSONObject json = new TJSONObject();
            TJSONArray  arr2;
            TParams     columns = dbxReader.getColumns();

            try
            {
                arr2 = new TJSONArray();
                for (int i = 0; i < columns.size(); i++)
                {
                    arr2.add(columns.getParameter(i).tojson());
                    // Create the empty JArray for the data. Will be filled after
                    json.addPairs(columns.getParameter(i).getName(), new TJSONArray());
                }

                while (dbxReader.next())
                {
                    for (int c = 0; c < columns.size(); c++)
                    {
                        dbxReader.getColumns().getParameter(c).getValue().appendTo(
                            json.getJSONArray(columns.getParameter(c)
                                              .getName()));
                    }
                }
                json.addPairs("table", arr2);
            }
            catch (Exception)
            {
            }
            return(json);
        }
        /**
         * Create a JSONObject from a DBXParameters
         * @param dbxParameters
         * @return JSONObject
         */
        public static TJSONObject DBXParametersToJSONObject(TParams dbxParameters)
        {
            TJSONObject json = new TJSONObject();
            TJSONObject jsonobj = new TJSONObject();

            for (int i = 0; i < dbxParameters.size(); i++)
            {
                TJSONArray arr3;
                try
                {
                    arr3 = new TJSONArray();
                    arr3.add(new TJSONString(dbxParameters.getParameter(i).getValue().ToString()));
                    json.addPairs(dbxParameters.getParameter(i).getName(), arr3);
                }
                catch (Exception)
                {

                }
            }

            TJSONArray arr2;
            arr2 = new TJSONArray();
            for (int i = 0; i < dbxParameters.size(); i++)
            {
                TJSONArray arr;
                arr = new TJSONArray();
                arr.add(new TJSONString(dbxParameters.getParameter(i).getName()));
                arr.add(new TJSONNumber(dbxParameters.getParameter(i).getDataType()));
                arr.add(new TJSONNumber(dbxParameters.getParameter(i).getOrdinal()));
                arr.add(new TJSONNumber(dbxParameters.getParameter(i).getSubType()));
                arr.add(new TJSONNumber(dbxParameters.getParameter(i).getScale()));
                arr.add(new TJSONNumber(dbxParameters.getParameter(i).getSize()));
                arr.add(new TJSONNumber(dbxParameters.getParameter(i).getPrecision()));
                arr.add(new TJSONNumber(dbxParameters.getParameter(i).getChildPosition()));
                if (dbxParameters.getParameter(i).getNullable()) arr.add(new TJSONTrue());
                else arr.add(new TJSONFalse());
                if (dbxParameters.getParameter(i).getHidden()) arr.add(new TJSONTrue());
                else arr.add(new TJSONFalse());
                arr.add(new TJSONNumber(dbxParameters.getParameter(i).getParameterDirection()));
                if (dbxParameters.getParameter(i).getValueParameter()) arr.add(new TJSONTrue());
                else arr.add(new TJSONFalse());
                if (dbxParameters.getParameter(i).getLiteral()) arr.add(new TJSONTrue());
                else arr.add(new TJSONFalse());
                arr2.add(arr);
            }
            try
            {
                json.addPairs("table", arr2);
            }
            catch (Exception)
            {

            }
            return json;
        }
        /**
          	 * Create a JSONObject from a DBXReader
          	 * @param dbxReader
          	 * @return JSONObject
          	 */
        public static TJSONObject DBXReaderToJSONObject(TDBXReader dbxReader)
        {
            TJSONObject json = new TJSONObject();
            TJSONArray arr2;
            TParams columns = dbxReader.getColumns();
            try
            {
                arr2 = new TJSONArray();
                for (int i = 0; i < columns.size(); i++)
                {
                    arr2.add(columns.getParameter(i).tojson());
                    // Create the empty JArray for the data. Will be filled after
                    json.addPairs(columns.getParameter(i).getName(), new TJSONArray());
                }

                while (dbxReader.next())
                {
                    for (int c = 0; c < columns.size(); c++)
                        dbxReader.getColumns().getParameter(c).getValue().appendTo(
                                json.getJSONArray(columns.getParameter(c)
                                                .getName()));
                }
                json.addPairs("table", arr2);
            }
            catch (Exception)
            {

            }
            return json;
        }
 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();
 }
Exemple #5
0
        /**
         * Create a JSONObject from a DBXParameters
         * @param dbxParameters
         * @return JSONObject
         */
        public static TJSONObject DBXParametersToJSONObject(TParams dbxParameters)
        {
            TJSONObject json    = new TJSONObject();
            TJSONObject jsonobj = new TJSONObject();

            for (int i = 0; i < dbxParameters.size(); i++)
            {
                TJSONArray arr3;
                try
                {
                    arr3 = new TJSONArray();
                    arr3.add(new TJSONString(dbxParameters.getParameter(i).getValue().ToString()));
                    json.addPairs(dbxParameters.getParameter(i).getName(), arr3);
                }
                catch (Exception)
                {
                }
            }

            TJSONArray arr2;

            arr2 = new TJSONArray();
            for (int i = 0; i < dbxParameters.size(); i++)
            {
                TJSONArray arr;
                arr = new TJSONArray();
                arr.add(new TJSONString(dbxParameters.getParameter(i).getName()));
                arr.add(new TJSONNumber(dbxParameters.getParameter(i).getDataType()));
                arr.add(new TJSONNumber(dbxParameters.getParameter(i).getOrdinal()));
                arr.add(new TJSONNumber(dbxParameters.getParameter(i).getSubType()));
                arr.add(new TJSONNumber(dbxParameters.getParameter(i).getScale()));
                arr.add(new TJSONNumber(dbxParameters.getParameter(i).getSize()));
                arr.add(new TJSONNumber(dbxParameters.getParameter(i).getPrecision()));
                arr.add(new TJSONNumber(dbxParameters.getParameter(i).getChildPosition()));
                if (dbxParameters.getParameter(i).getNullable())
                {
                    arr.add(new TJSONTrue());
                }
                else
                {
                    arr.add(new TJSONFalse());
                }
                if (dbxParameters.getParameter(i).getHidden())
                {
                    arr.add(new TJSONTrue());
                }
                else
                {
                    arr.add(new TJSONFalse());
                }
                arr.add(new TJSONNumber(dbxParameters.getParameter(i).getParameterDirection()));
                if (dbxParameters.getParameter(i).getValueParameter())
                {
                    arr.add(new TJSONTrue());
                }
                else
                {
                    arr.add(new TJSONFalse());
                }
                if (dbxParameters.getParameter(i).getLiteral())
                {
                    arr.add(new TJSONTrue());
                }
                else
                {
                    arr.add(new TJSONFalse());
                }
                arr2.add(arr);
            }
            try
            {
                json.addPairs("table", arr2);
            }
            catch (Exception)
            {
            }
            return(json);
        }
            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();
            }