root / trunk / hammock / src / net35 / Hammock / Extensions / WebExtensions.cs @ 0eea575a
History | View | Annotate | Download (3.8 kB)
1 |
using System; |
---|---|
2 |
using System.Collections.Generic; |
3 |
using System.Reflection; |
4 |
using System.Text; |
5 |
using Hammock.Attributes; |
6 |
using Hammock.Attributes.Validation; |
7 |
using Hammock.Web; |
8 |
|
9 |
namespace Hammock.Extensions |
10 |
{ |
11 |
internal static class WebExtensions |
12 |
{ |
13 |
public static void ParseNamedAttributes<T>(this IWebQueryInfo info, |
14 |
IEnumerable<PropertyInfo> properties, |
15 |
IDictionary<string, string> transforms, |
16 |
IDictionary<string, string> collection) |
17 |
where T : Attribute, INamedAttribute |
18 |
{ |
19 |
foreach (var property in properties) |
20 |
{ |
21 |
var attributes = property.GetCustomAttributes<T>(true); |
22 |
|
23 |
foreach (var attribute in attributes) |
24 |
{ |
25 |
var value = transforms.ContainsKey(property.Name) |
26 |
? transforms[property.Name] |
27 |
: property.GetValue(info, null); |
28 |
|
29 |
if (value == null) |
30 |
{ |
31 |
continue; |
32 |
} |
33 |
|
34 |
var header = value.ToString(); |
35 |
if (!header.IsNullOrBlank()) |
36 |
{ |
37 |
collection.Add(attribute.Name, header); |
38 |
} |
39 |
} |
40 |
} |
41 |
} |
42 |
|
43 |
public static Uri UriMinusQuery(this Uri uri, out WebParameterCollection parameters) |
44 |
{ |
45 |
var sb = new StringBuilder(); |
46 |
|
47 |
parameters = new WebParameterCollection(); |
48 |
var query = uri.Query.ParseQueryString(); |
49 |
foreach(var key in query.Keys) |
50 |
{ |
51 |
parameters.Add(key, query[key].UrlDecode()); |
52 |
} |
53 |
|
54 |
var port = uri.Scheme.Equals("http") && uri.Port != 80 || |
55 |
uri.Scheme.Equals("https") && uri.Port != 443 ? |
56 |
":" + uri.Port : ""; |
57 |
|
58 |
sb.Append(uri.Scheme) |
59 |
.Append("://") |
60 |
.Append(uri.Host) |
61 |
.Append(port) |
62 |
.Append(uri.AbsolutePath); |
63 |
|
64 |
return new Uri(sb.ToString()); |
65 |
} |
66 |
|
67 |
public static string ToBasicAuthorizationHeader(string username, string password) |
68 |
{ |
69 |
var token = "{0}:{1}".FormatWith(username, password).GetBytes().ToBase64String(); |
70 |
return "Basic {0}".FormatWith(token); |
71 |
} |
72 |
|
73 |
public static void ParseValidationAttributes(this IWebQueryInfo info, |
74 |
IEnumerable<PropertyInfo> properties, |
75 |
IDictionary<string, string> collection) |
76 |
{ |
77 |
foreach (var property in properties) |
78 |
{ |
79 |
var attributes = property.GetCustomAttributes<ValidationAttribute>(true); |
80 |
|
81 |
foreach (var attribute in attributes) |
82 |
{ |
83 |
// Support multiple transforms |
84 |
var value = collection.ContainsKey(property.Name) |
85 |
? collection[property.Name] |
86 |
: property.GetValue(info, null); |
87 |
|
88 |
var transformed = attribute.TransformValue(property, value); |
89 |
if (transformed.IsNullOrBlank()) |
90 |
{ |
91 |
continue; |
92 |
} |
93 |
|
94 |
if(collection.ContainsKey(property.Name)) |
95 |
{ |
96 |
collection[property.Name] = transformed; |
97 |
} |
98 |
else |
99 |
{ |
100 |
collection.Add(property.Name, transformed); |
101 |
} |
102 |
} |
103 |
} |
104 |
} |
105 |
} |
106 |
} |