/// <summary>
        /// Reads and returns instrument profiles from specified address with a specified basic user and password credentials.
        /// This method recognizes popular data compression formats "zip" and "gzip" by analyzing file name.
        /// If file name ends with ".zip" then all compressed files will be read independently one by one
        /// in their order of appearing and total concatenated list of instrument profiles will be returned.
        /// If file name ends with ".gz" then compressed content will be read and returned.
        /// In other cases file will be considered uncompressed and will be read as is.
        ///
        /// Specified user and password take precedence over authentication information that is supplied to this method
        /// as part of URL user info like "http://*****:*****@host:port/path/file.ipf".
        ///
        /// This operation updates GetLastModified().
        /// </summary>
        /// <param name="address">URL of file to read from.</param>
        /// <param name="user">The user name (may be null).</param>
        /// <param name="password">The password (may be null).</param>
        /// <returns>List of instrument profiles.</returns>
        /// <exception cref="IOException">If an I/O error occurs.</exception>
        /// <exception cref="InstrumentProfileFormatException">If input stream does not conform to the Simple File Format.</exception>
        public IList <InstrumentProfile> ReadFromFile(string address, string user, string password)
        {
            string url = ResolveSourceUrl(address);

            try
            {
                WebRequest webRequest = URLInputStream.OpenConnection(URLInputStream.ResolveUrl(url), user, password);
                webRequest.Headers.Add(Constants.LIVE_PROP_KEY, Constants.LIVE_PROP_REQUEST_NO);
                using (WebResponse response = webRequest.GetResponse())
                {
                    DateTime modificationTime;
                    if (response.GetType() == typeof(FileWebResponse))
                    {
                        Uri fileUri = new Uri(address);
                        modificationTime = File.GetLastWriteTime(fileUri.AbsolutePath);
                    }
                    else
                    {
                        URLInputStream.CheckConnectionResponseCode(response);
                        if (response.GetType() == typeof(FtpWebResponse))
                        {
                            modificationTime = ((FtpWebResponse)response).LastModified;
                        }
                        else
                        {
                            modificationTime = ((HttpWebResponse)response).LastModified;
                        }
                    }

                    IList <InstrumentProfile> list;
                    using (var dataStream = response.GetResponseStream())
                    {
                        list = Read(dataStream, url);
                    }
                    lastModified = modificationTime;
                    return(list);
                }
            }
            catch (InstrumentProfileFormatException)
            {
                throw;
            }
            catch (IOException)
            {
                throw;
            }
            catch (Exception exc)
            {
                throw new IOException("Read profiles from file failed: " + exc);
            }
        }
Пример #2
0
        /// <summary>
        /// Opens (or keeps) connection for receiving instrument profiles from server.
        /// </summary>
        private void Download()
        {
            var webRequest = URLInputStream.OpenConnection(address);

            webRequest.Headers.Add(Constants.LIVE_PROP_KEY, Constants.LIVE_PROP_REQUEST_YES);
            if (LastModified != DateTime.MinValue && !supportsLive &&
                webRequest.GetType() == typeof(HttpWebRequest))
            {
                ((HttpWebRequest)webRequest).IfModifiedSince = lastModified;
            }

            try
            {
                lock (webResponseLocker)
                {
                    webResponse = webRequest.GetResponse();
                }

                try
                {
                    var      isFileStream = webResponse.GetType() == typeof(FileWebResponse);
                    DateTime time;
                    if (isFileStream)
                    {
                        var fileUri = new Uri(address);
                        time = File.GetLastWriteTime(fileUri.AbsolutePath);
                    }
                    else
                    {
                        URLInputStream.CheckConnectionResponseCode(webResponse);
                        time         = ((HttpWebResponse)webResponse).LastModified;
                        supportsLive =
                            Constants.LIVE_PROP_RESPONSE.Equals(webResponse.Headers.Get(Constants.LIVE_PROP_KEY));
                    }

                    if (time == LastModified)
                    {
                        return; // nothing changed
                    }
                    MakeConnected();
                    using (var inputStream = webResponse.GetResponseStream())
                    {
                        var compress = isFileStream
                            ? StreamCompression.DetectCompressionByExtension(new Uri(address))
                            : StreamCompression.DetectCompressionByMimeType(webResponse.ContentType);
                        using (var decompressedIn = compress.Decompress(inputStream))
                        {
                            Process(decompressedIn);
                            // Update timestamp only after first successful processing
                            LastModified = time;
                        }
                    }
                }
                finally
                {
                    lock (webResponseLocker)
                    {
                        webResponse = null;
                    }
                }
            }
            catch (WebException we)
            {
                using (var webResponse = we.Response)
                {
                    if (webResponse != null && webResponse.GetType() == typeof(HttpWebResponse) &&
                        ((HttpWebResponse)webResponse).StatusCode == HttpStatusCode.NotModified)
                    {
                        return; // not modified
                    }
                }
                throw;
            }
        }