Exemple #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);
        }