-
Notifications
You must be signed in to change notification settings - Fork 305
Expand file tree
/
Copy pathWebUtilities.cs
More file actions
206 lines (179 loc) · 7.67 KB
/
WebUtilities.cs
File metadata and controls
206 lines (179 loc) · 7.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
using System;
using System.Globalization;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Xml;
namespace SharpMap.Web
{
internal class HttpCacheUtility
{
private static bool _systemWebTested;
private static readonly DateTime NoAbsoluteExpiration = DateTime.MaxValue;
private static readonly object LockContext = new object();
/*
private static Type _cacheDependencyType;
private static PropertyInfo _cachePropertyInfo;
private static PropertyInfo _httpContextPropertyInfo;
*/
private static MethodInfo _insertMethodInfo;
private static MethodInfo _getMethodInfo;
private static object _context;
private static object _cache;
private static void TestSystemWeb()
{
if (_systemWebTested) return;
lock (LockContext)
{
if (_systemWebTested) return;
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
{
if (assembly.GetName().Name != "System.Web") continue;
try
{
var httpContextType =
assembly.GetType("System.Web.HttpContext");
var httpContextPropertyInfo =
httpContextType.GetProperty("Current",
BindingFlags.Public | BindingFlags.Static);
_context = httpContextPropertyInfo.GetValue(null, null);
var cachePropertyInfo =
httpContextType.GetProperty("Cache",
BindingFlags.Public | BindingFlags.Instance);
if (_context != null)
{
_cache = cachePropertyInfo.GetValue(_context, null);
var cacheType = assembly.GetType("System.Web.Caching.Cache");
var cacheDependencyType =
assembly.GetType("System.Web.Caching.CacheDependency");
_insertMethodInfo = cacheType.GetMethod("Insert",
new[]
{
typeof (string), typeof (object),
cacheDependencyType, typeof (DateTime),
typeof (TimeSpan)
});
_getMethodInfo = cacheType.GetMethod("Get", new[] { typeof(string) });
}
//Found it, tested it, continue with useful stuff
break;
}
catch (Exception /*ex*/)
{
}
}
_systemWebTested = true;
}
}
private static object GetCurrentHttpContext()
{
return _context;
/*
if (_httpContextPropertyInfo == null)
return null;
return _httpContextPropertyInfo.GetValue(null, null);
*/
}
private static object GetHttpContextCache(/*object httpContext*/)
{
return _cache;
/*
if (_cachePropertyInfo == null)
return null;
return _cachePropertyInfo.GetValue(httpContext, null);
*/
}
private static object GetCurrentCache()
{
if (!_systemWebTested)
{
lock(LockContext)
{
if (!_systemWebTested)
TestSystemWeb();
}
}
var httpContext = GetCurrentHttpContext();
if (httpContext == null)
return null;
return GetHttpContextCache(/*httpContext*/);
}
[MethodImpl(MethodImplOptions.Synchronized)]
internal static bool TryGetValue<T>(string key, out T instance)
where T: class
{
var cache = GetCurrentCache();
if (cache == null)
{
instance = default(T);
return false;
}
instance = (T)_getMethodInfo.Invoke(cache, new object[] {key});
return instance != null;
}
/// <summary>
/// Gets a value indicating that the code is being run in a web context
/// </summary>
internal static bool IsWebContext { get { return GetCurrentCache() != null; } }
/// <summary>
/// Function that tries to get an object from the WebCache
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <param name="instance"></param>
/// <returns></returns>
internal static bool TryAddValue<T>(string key, T instance)
where T: class
{
return TryAddValue(key, instance, TimeSpan.FromDays(1));
}
[MethodImpl(MethodImplOptions.Synchronized)]
internal static bool TryAddValue<T>(string key, T instance, TimeSpan timeSpan)
where T : class
{
var cache = GetCurrentCache();
if (cache == null)
return false;
_insertMethodInfo.Invoke(cache, new object[] { key, instance, null, NoAbsoluteExpiration, timeSpan });
return true;
}
}
class WebUtilities
{
#region Reusable XML Parsing
public static XmlNode FindEpsgNode(XmlNode bbox)
{
if (bbox == null || bbox.Attributes == null)
throw new ArgumentNullException("bbox");
XmlNode epsgNode = ((bbox.Attributes["srs"] ?? bbox.Attributes["crs"]) ?? bbox.Attributes["SRS"]) ?? bbox.Attributes["CRS"];
return epsgNode;
}
public static bool TryParseNodeAsEpsg(XmlNode node, out int epsg)
{
epsg = default(int);
if (node == null) return false;
string epsgString = node.Value;
if (String.IsNullOrEmpty(epsgString)) return false;
const string prefix = "EPSG:";
int index = epsgString.IndexOf(prefix, StringComparison.InvariantCulture);
if (index < 0) return false;
return (Int32.TryParse(epsgString.Substring(index + prefix.Length), NumberStyles.Any, Map.NumberFormatEnUs, out epsg));
}
public static double ParseNodeAsDouble(XmlNode node, double defaultValue)
{
if (node == null) return defaultValue;
if (String.IsNullOrEmpty(node.InnerText)) return defaultValue;
double value;
if (Double.TryParse(node.InnerText, NumberStyles.Any, Map.NumberFormatEnUs, out value))
return value;
return defaultValue;
}
public static bool TryParseNodeAsDouble(XmlNode node, out double value)
{
value = default(double);
if (node == null) return false;
if (String.IsNullOrEmpty(node.InnerText)) return false;
return Double.TryParse(node.InnerText, NumberStyles.Any, Map.NumberFormatEnUs, out value);
}
#endregion
}
}