/sdk/Domain/PolicyConditions.cs

https://github.com/aliyun/aliyun-oss-csharp-sdk · C# · 300 lines · 173 code · 42 blank · 85 comment · 6 complexity · ab5d2b84812f7ec22497fed78742de79 MD5 · raw file

  1. /*
  2. * Copyright (C) Alibaba Cloud Computing
  3. * All rights reserved.
  4. *
  5. */
  6. using System;
  7. using System.Collections.Generic;
  8. using System.ComponentModel;
  9. using System.Text;
  10. namespace Aliyun.OSS
  11. {
  12. /// <summary>
  13. /// The match mode enum
  14. /// </summary>
  15. public enum MatchMode
  16. {
  17. /// <summary>
  18. /// Unknown
  19. /// </summary>
  20. Unknown,
  21. /// <summary>
  22. /// Exactly match
  23. /// </summary>
  24. Exact,
  25. /// <summary>
  26. /// Match the prefix only
  27. /// </summary>
  28. StartWith,
  29. /// <summary>
  30. /// Match the size range. For example, the policy could be applied the files of size between 1KB to 4KB.
  31. /// </summary>
  32. Range
  33. };
  34. /// <summary>
  35. /// Tuplre type enum.!-- Currently only two tuple {key:value} and three tuple type (tuple1,tuple2,tuple3) are supported.
  36. /// </summary>
  37. internal enum TupleType
  38. {
  39. Unknown,
  40. Two,
  41. Three
  42. };
  43. /// <summary>
  44. /// The abstract Condition Item.
  45. /// </summary>
  46. internal abstract class AbstractConditionItem
  47. {
  48. public string Name { get; set; }
  49. public MatchMode MatchMode { get; set; }
  50. public TupleType TupleType { get; set; }
  51. protected AbstractConditionItem(string name, MatchMode matchMode, TupleType tupleType)
  52. {
  53. Name = name;
  54. MatchMode = matchMode;
  55. TupleType = tupleType;
  56. }
  57. public abstract string Jsonize();
  58. }
  59. /// <summary>
  60. /// EqualConditionItem definition
  61. /// </summary>
  62. internal class EqualConditionItem : AbstractConditionItem
  63. {
  64. public string Value { get; set; }
  65. public EqualConditionItem(string name, string value)
  66. : this(name, value, TupleType.Two)
  67. { }
  68. public EqualConditionItem(string name, string value, TupleType tupleType)
  69. : base(name, MatchMode.Exact, tupleType)
  70. {
  71. Value = value;
  72. }
  73. public override string Jsonize()
  74. {
  75. string jsonizedCond = null;
  76. switch (TupleType)
  77. {
  78. case TupleType.Two:
  79. jsonizedCond = String.Format("{{\"{0}\":\"{1}\"}},", Name, Value);
  80. break;
  81. case TupleType.Three:
  82. jsonizedCond = String.Format("[\"eq\",\"${0}\",\"{1}\"],", Name, Value);
  83. break;
  84. default:
  85. throw new InvalidEnumArgumentException("Invalid tuple type " + TupleType.ToString());
  86. }
  87. return jsonizedCond;
  88. }
  89. }
  90. /// <summary>
  91. /// StartwithConditionItem definition.
  92. /// </summary>
  93. internal class StartWithConditionItem : AbstractConditionItem
  94. {
  95. public string Value { get; set; }
  96. public StartWithConditionItem(string name, string value)
  97. : base(name, MatchMode.StartWith, TupleType.Three)
  98. {
  99. Value = value;
  100. }
  101. public override string Jsonize()
  102. {
  103. return String.Format("[\"starts-with\",\"${0}\",\"{1}\"],", Name, Value);
  104. }
  105. }
  106. /// <summary>
  107. /// Content size's RangeConditionItem definition.
  108. /// </summary>
  109. internal class RangeConditionItem : AbstractConditionItem
  110. {
  111. public long Minimum { get; set; }
  112. public long Maximum { get; set; }
  113. public RangeConditionItem(string name, long min, long max)
  114. : base(name, MatchMode.Range, TupleType.Three)
  115. {
  116. Minimum = min;
  117. Maximum = max;
  118. }
  119. public override string Jsonize()
  120. {
  121. return String.Format("[\"content-length-range\",{0},{1}],", Minimum, Maximum);
  122. }
  123. }
  124. /// <summary>
  125. /// Conditions list. It specifies all valid fields in the post form.
  126. /// </summary>
  127. public class PolicyConditions
  128. {
  129. /// <summary>
  130. /// Content length range
  131. /// </summary>
  132. public const string CondContentLengthRange = "content-length-range";
  133. /// <summary>
  134. /// The cache control behavior for downloading files
  135. /// </summary>
  136. public const string CondCacheControl = "Cache-Control";
  137. /// <summary>
  138. /// Content types defined in RFC2616
  139. /// </summary>
  140. public const string CondContentType = "Content-Type";
  141. /// <summary>
  142. /// Content disposition behavior
  143. /// </summary>
  144. public const string CondContentDisposition = "Content-Disposition";
  145. /// <summary>
  146. /// The content encoding
  147. /// </summary>
  148. public const string CondContentEncoding = "Content-Encoding";
  149. /// <summary>
  150. /// Expiration time
  151. /// </summary>
  152. public const string CondExpires = "Expires";
  153. /// <summary>
  154. /// object key
  155. /// </summary>
  156. public const string CondKey = "key";
  157. /// <summary>
  158. /// redirect upon success
  159. /// </summary>
  160. public const string CondSuccessActionRedirect = "success_action_redirect";
  161. /// <summary>
  162. /// The action status upon success
  163. /// </summary>
  164. public const string CondSuccessActionStatus = "success_action_status";
  165. /// <summary>
  166. /// The custom metadata prefix
  167. /// </summary>
  168. public const string CondXOssMetaPrefix = "x-oss-meta-";
  169. private IList<AbstractConditionItem> _conds = new List<AbstractConditionItem>();
  170. /// <summary>
  171. /// Adds a condition item with exact MatchMode
  172. /// </summary>
  173. /// <param name="name">Condition name</param>
  174. /// <param name="value">Condition value</param>
  175. public void AddConditionItem(string name, string value)
  176. {
  177. MatchRuleChecker.Check(MatchMode.Exact, name);
  178. _conds.Add(new EqualConditionItem(name, value));
  179. }
  180. /// <summary>
  181. /// Adds a condition item with specified MatchMode
  182. /// </summary>
  183. /// <param name="matchMode">Conditions match mode</param>
  184. /// <param name="name">Condition name</param>
  185. /// <param name="value">Condition value</param>
  186. public void AddConditionItem(MatchMode matchMode, string name, string value)
  187. {
  188. MatchRuleChecker.Check(matchMode, name);
  189. switch (matchMode)
  190. {
  191. case MatchMode.Exact:
  192. _conds.Add(new EqualConditionItem(name, value, TupleType.Three));
  193. break;
  194. case MatchMode.StartWith:
  195. _conds.Add(new StartWithConditionItem(name, value));
  196. break;
  197. default:
  198. throw new InvalidEnumArgumentException("Unsupported match mode " +
  199. matchMode);
  200. }
  201. }
  202. /// <summary>
  203. /// Adds a condition with range match mode.
  204. /// </summary>
  205. /// <param name="name">Condition name</param>
  206. /// <param name="min">Range's low end</param>
  207. /// <param name="max">Range's high end</param>
  208. public void AddConditionItem(string name, long min, long max)
  209. {
  210. if (min > max)
  211. throw new ArgumentException(String.Format("Invalid range [{0}, {1}].", min, max));
  212. _conds.Add(new RangeConditionItem(name, min, max));
  213. }
  214. internal string Jsonize()
  215. {
  216. var jsonizedConds = new StringBuilder();
  217. jsonizedConds.Append("\"conditions\":[");
  218. foreach (var cond in _conds)
  219. jsonizedConds.Append(cond.Jsonize());
  220. if (_conds.Count > 0)
  221. jsonizedConds.Remove(jsonizedConds.Length - 1, 1);
  222. jsonizedConds.Append("]");
  223. return jsonizedConds.ToString();
  224. }
  225. }
  226. internal static class MatchRuleChecker
  227. {
  228. private static IDictionary<string, IList<MatchMode>> _supportedMatchRules
  229. = new Dictionary<string, IList<MatchMode>>();
  230. static MatchRuleChecker()
  231. {
  232. var ordinaryMatchModes = new List<MatchMode> {MatchMode.Exact, MatchMode.StartWith};
  233. var specialMatchModes = new List<MatchMode> {MatchMode.Range};
  234. _supportedMatchRules.Add(PolicyConditions.CondContentLengthRange, specialMatchModes);
  235. _supportedMatchRules.Add(PolicyConditions.CondCacheControl, ordinaryMatchModes);
  236. _supportedMatchRules.Add(PolicyConditions.CondContentType, ordinaryMatchModes);
  237. _supportedMatchRules.Add(PolicyConditions.CondContentDisposition, ordinaryMatchModes);
  238. _supportedMatchRules.Add(PolicyConditions.CondContentEncoding, ordinaryMatchModes);
  239. _supportedMatchRules.Add(PolicyConditions.CondExpires, ordinaryMatchModes);
  240. _supportedMatchRules.Add(PolicyConditions.CondKey, ordinaryMatchModes);
  241. _supportedMatchRules.Add(PolicyConditions.CondSuccessActionRedirect, ordinaryMatchModes);
  242. _supportedMatchRules.Add(PolicyConditions.CondSuccessActionStatus, ordinaryMatchModes);
  243. _supportedMatchRules.Add(PolicyConditions.CondXOssMetaPrefix, ordinaryMatchModes);
  244. }
  245. public static void Check(MatchMode matchMode, string condName)
  246. {
  247. if (_supportedMatchRules.ContainsKey(condName))
  248. {
  249. var mms = _supportedMatchRules[condName];
  250. if (!mms.Contains(matchMode))
  251. {
  252. throw new ArgumentException(
  253. String.Format("Unsupported match mode for condition item {0}", condName));
  254. }
  255. }
  256. }
  257. }
  258. }