GetBucketLifecycleDeserializer.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. * Copyright (C) Alibaba Cloud Computing
  3. * All rights reserved.
  4. *
  5. * 版权所有 (C)阿里云计算有限公司
  6. */
  7. using System;
  8. using System.Collections.Generic;
  9. using System.ComponentModel;
  10. using System.IO;
  11. using Aliyun.OSS.Common.Communication;
  12. using Aliyun.OSS.Model;
  13. namespace Aliyun.OSS.Transform
  14. {
  15. internal class GetBucketLifecycleDeserializer
  16. : ResponseDeserializer<IList<LifecycleRule>, LifecycleConfiguration>
  17. {
  18. public GetBucketLifecycleDeserializer(IDeserializer<Stream, LifecycleConfiguration> contentDeserializer)
  19. : base(contentDeserializer)
  20. { }
  21. public override IList<LifecycleRule> Deserialize(ServiceResponse xmlStream)
  22. {
  23. var lifecycleConfig = ContentDeserializer.Deserialize(xmlStream.Content);
  24. var rules = new List<LifecycleRule>();
  25. foreach (var lcc in lifecycleConfig.LifecycleRules)
  26. {
  27. var rule = new LifecycleRule
  28. {
  29. ID = lcc.ID,
  30. Prefix = lcc.Prefix
  31. };
  32. RuleStatus status;
  33. if (TryParseEnum(lcc.Status, out status))
  34. rule.Status = status;
  35. else
  36. throw new InvalidEnumArgumentException(@"Unsupported rule status " + lcc.Status);
  37. if (lcc.Expiration.IsSetDays())
  38. rule.ExpriationDays = lcc.Expiration.Days;
  39. else if (lcc.Expiration.IsSetDate())
  40. rule.ExpirationTime = DateTime.Parse(lcc.Expiration.Date);
  41. rules.Add(rule);
  42. }
  43. return rules;
  44. }
  45. /// <summary>
  46. /// 为了兼容.NET2.0,实现了一个类似.NET4.0中Enum.TryParse的方法
  47. /// </summary>
  48. /// <param name="value">转换的枚举名称或基础值的字符串表示形式。</param>
  49. /// <param name="status">此方法在返回时包含一个类型为 TEnum 的一个对象,其值由 value 表示。该参数未经初始化即被传递。</param>
  50. /// <returns>是否解析成功</returns>
  51. private bool TryParseEnum(string value, out RuleStatus status)
  52. {
  53. if (!Enum.IsDefined(typeof(RuleStatus), value))
  54. {
  55. status = RuleStatus.Disabled;
  56. return false;
  57. }
  58. status = (RuleStatus)Enum.Parse(typeof(RuleStatus), value);
  59. return true;
  60. }
  61. }
  62. }