コード例 #1
0
        public HttpResponseMessage SendToUser()
        {
            /*
             *      SqlConnection Conn = new SqlConnection(KeyWords.DBconnectionString);
             *      await Conn.OpenAsync();
             *      SqlCommand Cmd = Conn.CreateCommand();
             *      Cmd.CommandText = this.query;
             *      //Cmd.CommandTimeout = KeyWords.DatabaseSearchTimeout == null || KeyWords.DatabaseSearchTimeout == "" ? 600 : Int32.Parse(KeyWords.DatabaseSearchTimeout);
             *      Cmd.CommandTimeout = Int32.Parse(KeyWords.DatabaseSearchTimeout);
             *      //SqlDataReader reader = await Cmd.ExecuteReaderAsync();
             *      var Adapter = new SqlDataAdapter(Cmd);
             *      Adapter.Fill(ResultsDataSet);
             *      Conn.Close();
             */
            RunQuery();

            //BinaryFormatter fmt = new BinaryFormatter();
            Action <Stream, HttpContent, TransportContext> WriteToStream = null;
            BinaryFormatter fmt;

            // do not add the SQL query as a second table in vo services and csv/txt/fits formats.
            if (!format.Contains("csv") && !format.Contains("txt") && !format.Contains("text/plain") && !format.Contains("fits") && queryType != KeyWords.SDSSFields && queryType != KeyWords.ConeSearchQuery && queryType != KeyWords.SIAP)
            {
                AddQueryTable(ResultsDataSet);// this adds to "ResultsDataSet" a new Table that shows the sql command.
            }

            string FileType = "";

            ExtraInfo.TryGetValue("FormatFromUser", out FileType);
            string SaveResult = "";

            ExtraInfo.TryGetValue("SaveResult", out SaveResult);

            switch (format)
            {
            case "csv":
            case "txt":
            case "text/plain":
                ResultsDataSet.RemotingFormat = SerializationFormat.Xml;
                WriteToStream    = (stream, foo, bar) => { OutputUtils.writeCSV(ResultsDataSet, stream); stream.Close(); };
                response.Content = new PushStreamContent(WriteToStream, new MediaTypeHeaderValue((KeyWords.contentCSV)));
                if (FileType == "csv")
                {
                    FileType = ".csv";
                }
                else
                {
                    FileType = ".txt";
                }
                if (SaveResult == "true")
                {
                    response.Content.Headers.Add("Content-Disposition", "attachment;filename=\"result" + FileType + "\"");
                }
                break;

            case "fits":
            case "application/fits":
                ResultsDataSet.RemotingFormat = SerializationFormat.Binary;
                WriteToStream    = (stream, foo, bar) => { OutputUtils.WriteFits(ResultsDataSet, stream); stream.Close(); };
                response.Content = new PushStreamContent(WriteToStream, new MediaTypeHeaderValue((KeyWords.contentFITS)));
                if (SaveResult == "true")
                {
                    response.Content.Headers.Add("Content-Disposition", "attachment;filename=\"result.fits\"");
                }
                break;

            case "votable":
            case "application/x-votable+xml":
                ResultsDataSet.RemotingFormat = SerializationFormat.Xml;
                WriteToStream    = (stream, foo, bar) => { OutputUtils.WriteVOTable(ResultsDataSet, stream); stream.Close(); };
                response.Content = new PushStreamContent(WriteToStream, new MediaTypeHeaderValue((KeyWords.contentVOTable)));
                if (SaveResult == "true")
                {
                    response.Content.Headers.Add("Content-Disposition", "attachment;filename=\"result.votable.xml\"");
                }
                break;

            case "xml":
            case "application/xml":
                ResultsDataSet.RemotingFormat = SerializationFormat.Xml;
                WriteToStream    = (stream, foo, bar) => { OutputUtils.WriteXml(ResultsDataSet, stream); stream.Close(); };
                response.Content = new PushStreamContent(WriteToStream, new MediaTypeHeaderValue((KeyWords.contentXML)));
                if (SaveResult == "true")
                {
                    response.Content.Headers.Add("Content-Disposition", "attachment;filename=\"result.xml\"");
                }
                break;

            case "json":
            case "application/json":
                ResultsDataSet.RemotingFormat = SerializationFormat.Xml;
                WriteToStream    = (stream, foo, bar) => { OutputUtils.WriteJson(ResultsDataSet, stream); stream.Close(); };
                response.Content = new PushStreamContent(WriteToStream, new MediaTypeHeaderValue((KeyWords.contentJson)));
                if (SaveResult == "true")
                {
                    response.Content.Headers.Add("Content-Disposition", "attachment;filename=\"result.json\"");
                }
                break;

            case "html":
            case "dataset":
            case "application/x-dataset":
                ProcessDataSet proc = new ProcessDataSet(query, format, TaskName, ExtraInfo, null, true, positionType, queryType, null, null);
                response.Content = proc.GetContent(ResultsDataSet);
                if (ExtraInfo.ContainsKey("FormatFromUser"))
                {
                    if (ExtraInfo["FormatFromUser"] == "html")
                    {
                        response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/html");
                    }
                }
                //ResultsDataSet.RemotingFormat = SerializationFormat.Binary;
                //fmt = new BinaryFormatter();
                //WriteToStream = (stream, foo, bar) => { fmt.Serialize(stream, ResultsDataSet); stream.Close(); };
                //response.Content = new PushStreamContent(WriteToStream, new MediaTypeHeaderValue(KeyWords.contentDataset));
                break;

            default:
                ResultsDataSet.RemotingFormat = SerializationFormat.Binary;
                fmt              = new BinaryFormatter();
                WriteToStream    = (stream, foo, bar) => { fmt.Serialize(stream, ResultsDataSet); stream.Close(); };
                response.Content = new PushStreamContent(WriteToStream, new MediaTypeHeaderValue((KeyWords.contentDataset)));
                break;
            }
            //reader.Close();
            //response.Content = new StringContent(ClientIP);
            logger.LogActivity(ActivityInfo, "SkyserverMessage");
            return(response);
        }
コード例 #2
0
        public HttpResponseMessage SendToMyDB()
        {
            HttpResponseMessage respMessage = null;
            string ResponseResult           = "";
            string requestUri = "";

            if (logger.IsValidUser)
            {
                string TableName = ExtraInfo["TableName"];

                if (!string.IsNullOrEmpty(TableName))
                {
                    Regex rg = new Regex("[^a-zA-Z0-9]");
                    if (rg.IsMatch(TableName))
                    {
                        throw (new Exception("String TableName may only contain letters or numbers."));
                    }

                    /*
                     * string ForbiddenChar = ",./-?\!";
                     * for (int i = 0; i < ForbiddenChar.Length;i++)
                     * {
                     *  if (TableName.Contains(ForbiddenChar.Substring(i,1)))
                     *      throw (new Exception("TableName may not contain characters like " + ForbiddenChar ));
                     * }
                     */
                    requestUri = ConfigurationManager.AppSettings["CASJobsREST"] + "contexts/MyDB/tables/";
                    HttpClient client = new HttpClient();
                    client.Timeout     = new TimeSpan(0, 0, 0, KeyWords.TimeoutCASJobs);// default is 100000ms
                    client.BaseAddress = new Uri(requestUri);
                    client.DefaultRequestHeaders.Add("X-Auth-Token", logger.Token);
                    respMessage    = client.GetAsync(requestUri).Result;
                    ResponseResult = respMessage.Content.ReadAsStringAsync().Result;
                    if (!respMessage.IsSuccessStatusCode)
                    {
                        if (ExtraInfo["DoReturnHtml"].ToLower() == "false")
                        {
                            CreateErrorResponseMessageJSON(ResponseResult);
                        }
                        else
                        {
                            CreateErrorResponseMessageHTML(ResponseResult);
                        }

                        logger.LogActivity(ActivityInfo, "SkyserverMessage");
                        return(response);
                    }

                    Dictionary <string, string> values;
                    Newtonsoft.Json.Linq.JArray array = (Newtonsoft.Json.Linq.JArray)JsonConvert.DeserializeObject(ResponseResult);//ResponseResult comes in json format
                    for (int i = 0; i < array.Count; i++)
                    {
                        values = JsonConvert.DeserializeObject <Dictionary <string, string> >(array[i].ToString());
                        if (values["Name"] == TableName)
                        {
                            if (ExtraInfo["DoReturnHtml"].ToLower() == "false")
                            {
                                throw (new Exception("Table \"" + TableName + "\" already exists in MyDB. Try changing the table name or see it in MyDB"));
                            }
                            else
                            {
                                ProcessDataSet proc         = new ProcessDataSet(query, format, TaskName, ExtraInfo, null, true, positionType, queryType, null, null);
                                string         queryResult1 = proc.getTableReSubmitHTMLresult(TableName, logger.Token);
                                response.Content = new StringContent(queryResult1, tCode, KeyWords.contentHTML);
                                response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/html");
                            }
                            return(response);
                        }
                        //throw (new Exception("Table \"" + TableName + "\" already exists in MyDB. Try changing the table name or  <a target=INFO href=\"" + ConfigurationManager.AppSettings["CASJobs"] + "MyDB.aspx" + "\">LINK</a> "));
                    }
                }
                else// create a table name
                {
                    //DateTime2 now = DateTime2.Now;
                    TableName = "Table_" + DateTime.Now.ToString("yyyyMMdd_HHmmss_fff");
                }

/*
 *              if (!(ExtraInfo["EntryPoint"].ToLower().Contains("sqlsearch") || ExtraInfo["EntryPoint"].ToLower().Contains("crossid") || ExtraInfo["EntryPoint"].ToLower().Contains("proximity")))// sending query as a job
 *              {
 *                  string queryResult = "";
 *                  StringBuilder strbldr = new StringBuilder();
 *                  StringWriter sw = new StringWriter(strbldr);
 *                  using (JsonWriter writer = new JsonTextWriter(sw))
 *                  {
 *                      writer.WriteStartObject();
 *                      writer.WritePropertyName("query");
 *                      writer.WriteValue(ExtraInfo["QueryForUserDisplay"]);
 *                      writer.WritePropertyName("TaskName");
 *                      writer.WriteValue("SkyserverWS.SendToMyDB");
 *                      writer.WritePropertyName("CreateTable");
 *                      writer.WriteValue("true");
 *                      writer.WritePropertyName("TableName");
 *                      writer.WriteValue(TableName);
 *                  }
 *
 *                  StringContent content = new StringContent(strbldr.ToString());
 *                  content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
 *                  content.Headers.Add("X-Auth-Token", logger.Token);
 *
 *                  //posting the request and getting the result back.
 *                  HttpClient client2 = new HttpClient();
 *                  client2.Timeout = new TimeSpan(0, 0, 0, KeyWords.TimeoutCASJobs);// default is 100000ms
 *                  requestUri = ConfigurationManager.AppSettings["CASJobsREST"] + "contexts/" + KeyWords.DataRelease + "/jobs";
 *                  client2.BaseAddress = new Uri(requestUri);
 *                  respMessage = client2.PutAsync(requestUri, content).Result;
 *                  string JobID = "UNKNOWN";
 *                  if (respMessage.IsSuccessStatusCode)
 *                  {
 *                      JobID = respMessage.Content.ReadAsStringAsync().Result;
 *                      if (ExtraInfo["DoReturnHtml"].ToLower() == "false")
 *                      {
 *                          queryResult = "[{\"JobID\": \"" + JobID + "\", \"TableName\"= \"" + TableName + "\"}]";
 *                          response.Content = new StringContent(queryResult, tCode, KeyWords.contentJson);
 *                          response.Content.Headers.ContentType = new MediaTypeHeaderValue(KeyWords.contentJson);
 *                      }
 *                      else
 *                      {
 *                          ProcessDataSet proc = new ProcessDataSet(query, format, TaskName, ExtraInfo, null, true, positionType, queryType, null, null);
 *                          queryResult = proc.getCasJobsSubmitHTMLresult(JobID, TableName, logger.Token);
 *                          response.Content = new StringContent(queryResult, tCode, KeyWords.contentHTML);
 *                          response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/html");
 *                      }
 *                  }
 *                  else
 *                  {
 *                      string ErrorMessage = respMessage.Content.ReadAsStringAsync().Result;
 *                      if (ExtraInfo["DoReturnHtml"].ToLower() == "false")
 *                          CreateErrorResponseMessageJSON(ErrorMessage);
 *                      else
 *                          CreateErrorResponseMessageHTML(ErrorMessage);
 *                  }
 *
 *                  logger.LogActivity(ActivityInfo, "SkyserverMessage");
 *                  return response;
 *
 *              }
 *              else // sending query results as a csv
 *              {
 *              }
 */

                //sending query result as a csv:
                string queryResult = "";
                RunQuery();
                Action <Stream, HttpContent, TransportContext> WriteToStream = null;
                DataSet ds = new DataSet();
                ds.Tables.Add(ResultsDataSet.Tables[0].Copy());
                WriteToStream = (stream, foo, bar) => { OutputUtils.writeCSV(ds, stream, false); stream.Close(); };
                HttpContent content = new PushStreamContent(WriteToStream, new MediaTypeHeaderValue((KeyWords.contentCSV)));
                //content.Headers.ContentType = new MediaTypeHeaderValue("text/plain");
                content.Headers.Add("X-Auth-Token", logger.Token);
                requestUri = ConfigurationManager.AppSettings["CASJobsREST"] + "contexts/MyDB/tables/" + TableName;
                HttpClient client2 = new HttpClient();
                client2.Timeout     = new TimeSpan(0, 0, 0, KeyWords.TimeoutCASJobs);// default is 100000ms
                client2.BaseAddress = new Uri(requestUri);
                respMessage         = client2.PostAsync(requestUri, content).Result;
                if (respMessage.IsSuccessStatusCode)
                {
                    if (ExtraInfo["DoReturnHtml"].ToLower() == "false")
                    {
                        queryResult      = "[{\"IsSuccessStatusCode\": \"true\", \"TableName\"= \"" + TableName + "\"}]";
                        response.Content = new StringContent(queryResult, tCode, KeyWords.contentJson);
                        response.Content.Headers.ContentType = new MediaTypeHeaderValue(KeyWords.contentJson);
                    }
                    else
                    {
                        ProcessDataSet proc = new ProcessDataSet(query, format, TaskName, ExtraInfo, null, true, positionType, queryType, null, null);
                        queryResult      = proc.getTableSubmitHTMLresult(TableName, logger.Token);
                        response.Content = new StringContent(queryResult, tCode, KeyWords.contentHTML);
                        response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/html");
                    }
                }
                else
                {
                    string ErrorMessage = respMessage.Content.ReadAsStringAsync().Result;
                    if (ExtraInfo["DoReturnHtml"].ToLower() == "false")
                    {
                        CreateErrorResponseMessageJSON(ErrorMessage);
                    }
                    else
                    {
                        CreateErrorResponseMessageHTML(ErrorMessage);
                    }
                }

                logger.LogActivity(ActivityInfo, "SkyserverMessage");
                return(response);
            }
            else
            {
                throw (new UnauthorizedAccessException("You are not logged-in with SciServer. Please log-in and try again."));
            }
        }