Exemplo n.º 1
0
        public ReaderResult Read(string resource, ILogger logger) {
            var result = new ReaderResult { Source = Source.Url };

            try {
                var uri = new Uri(resource);
                if (!string.IsNullOrEmpty(uri.Query)) {
                    result.Parameters = HttpUtility.ParseQueryString(uri.Query.Substring(1));
                }
                var request = (HttpWebRequest)WebRequest.Create(uri);
                request.Method = "GET";

                using (var response = (HttpWebResponse)request.GetResponse()) {
                    using (var responseStream = response.GetResponseStream()) {
                        if (responseStream == null) {

                        } else {
                            if (response.StatusCode == HttpStatusCode.OK) {
                                var reader = new StreamReader(responseStream);
                                result.Content = reader.ReadToEnd();
                            } else {
                                logger.Error("Response code was {0}. {1}", response.StatusCode, response.StatusDescription);
                                result.Source = Source.Error;
                            }
                        }
                    }
                }
            } catch (Exception ex) {
                logger.Error("Can not read url. {0}", ex.Message);
                result.Source = Source.Error;
            }

            return result;
        }
Exemplo n.º 2
0
        public ReaderResult Read(string resource, ILogger logger) {
            var result = new ReaderResult { Source = Source.File };

            var queryStringIndex = resource.IndexOf('?');
            if (queryStringIndex > 0) {
                result.Parameters = HttpUtility.ParseQueryString(resource.Substring(queryStringIndex+1));
                resource = resource.Substring(0, queryStringIndex);
            }

            if (Path.HasExtension(resource)) {

                if (!Path.IsPathRooted(resource)) {
                    resource = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, resource);
                }

                var fileInfo = new FileInfo(resource);
                try {
                    result.Content = File.ReadAllText(fileInfo.FullName);
                } catch (Exception ex) {
                    logger.Error("Can not read file. {0}", ex.Message);
                    result.Source = Source.Error;
                }
            } else {
                logger.Error("Invalid file name: {0}.  File must have an extension (e.g. xml, json, etc)", resource);
                result.Source = Source.Error;
            }
            return result;
        }
Exemplo n.º 3
0
 public ReaderResult Read(string resource, ILogger logger) {
     var result = new ReaderResult { Source = Source.Url };
     for (var i = 0; i < _attempts; i++) {
         result = _reader.Read(resource, logger);
         if (result.Source != Source.Error)
             break;
     }
     return result;
 }
Exemplo n.º 4
0
 public ReaderResult Read(string resource, ILogger logger) {
     var result = new ReaderResult() { Source = _sourceDetector.Detect(resource, logger) };
     switch (result.Source) {
         case Source.File:
             return _fileReader.Read(resource, logger);
         case Source.Url:
             return _webReader.Read(resource, logger);
         default:
             result.Content = resource;
             break;
     }
     return result;
 }