NTP Analyzer  0.8.2
Analyze the operation of time servers
TimeServerWebAdapter.cs
Go to the documentation of this file.
1 //
2 // Copyright (c) 2013-2017 Carsten Sonne Larsen <cs@innolan.net>
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 // THE SOFTWARE.
21 
22 using System;
23 using System.IO;
24 using System.Net;
25 using Ntp.Analyzer.Data.Log;
26 using Ntp.Analyzer.Objects;
27 using Ntp.Common.Log;
28 
29 namespace Ntp.Analyzer.Data.Import
30 {
31  public abstract class TimeServerWebAdapter
32  {
34  {
35  Log = log;
36  }
37 
38  private static bool enabled;
39 
40  protected readonly LogBase Log;
41 
42  protected abstract string Provider { get; }
43 
44  public static TimeServerWebAdapter Create(LogBase log)
45  {
46  // Use the ExactlyAdapter for now
47  return enabled ? new ExactlyAdapter(log) : null;
48  }
49 
50  public abstract TimeServer Import(int orgId);
51 
52  public static void Initialize(bool enable)
53  {
54  enabled = enable;
55  }
56 
57  protected string FetchHtml(string url, int orgId)
58  {
59  if (orgId >= 5000)
60  {
61  Log.TimeServerMaxId(Provider);
62  return null;
63  }
64 
65  string html = Download(url, orgId);
66  if (html != null)
67  return html;
68 
69  Log.TimeServerNotReceived(orgId);
70  return null;
71  }
72 
73  private string Download(string url, int orgId)
74  {
75  try
76  {
77  var client = new WebClient();
78  // TODO: Set timeout
79  client.Headers.Add(
80  "user-agent",
81  "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2;)"
82  );
83 
84  var stream = client.OpenRead(url);
85  Log.TimeServerDownload(Provider, url, orgId);
86 
87  if (stream != null)
88  {
89  var reader = new StreamReader(stream);
90  string html = reader.ReadToEnd();
91  reader.Close();
92  return html;
93  }
94  }
95  catch (WebException e)
96  {
97  var response = e.Response as HttpWebResponse;
98  if (response != null && response.StatusCode == HttpStatusCode.NotFound)
99  {
100  Log.TimeServerNotFound(Provider, orgId);
101  }
102  else
103  {
104  Log.TimeServerError(Provider, orgId);
105  }
106 
107  Log.WriteLine(e);
108  }
109  catch (Exception e)
110  {
111  Log.TimeServerError(Provider, orgId);
112  Log.WriteLine(e);
113  }
114 
115  return null;
116  }
117  }
118 }
var e
Definition: bootstrap.min.js:6
static TimeServerWebAdapter Create(LogBase log)