OssCommand.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Copyright (C) Alibaba Cloud Computing
  3. * All rights reserved.
  4. *
  5. * 版权所有 (C)阿里云计算有限公司
  6. */
  7. using System;
  8. using System.IO;
  9. using System.Collections.Generic;
  10. using Aliyun.OSS.Common.Communication;
  11. using Aliyun.OSS.Util;
  12. using Aliyun.OSS.Transform;
  13. namespace Aliyun.OSS.Commands
  14. {
  15. internal abstract class OssCommand
  16. {
  17. protected ExecutionContext Context { get; private set; }
  18. private IServiceClient Client { get; set; }
  19. private Uri Endpoint { get; set; }
  20. protected virtual bool LeaveRequestOpen
  21. {
  22. get { return false; }
  23. }
  24. protected virtual HttpMethod Method
  25. {
  26. get { return HttpMethod.Get; }
  27. }
  28. protected virtual String Bucket
  29. {
  30. get { return null; }
  31. }
  32. protected virtual String Key
  33. {
  34. get { return null; }
  35. }
  36. protected virtual IDictionary<String, String> Headers
  37. {
  38. get { return new Dictionary<String, String>(); }
  39. }
  40. protected virtual IDictionary<String, String> Parameters
  41. {
  42. get { return new Dictionary<String, String>(); }
  43. }
  44. protected virtual Stream Content
  45. {
  46. get { return null; }
  47. }
  48. protected OssCommand(IServiceClient client, Uri endpoint, ExecutionContext context)
  49. {
  50. Client = client;
  51. Endpoint = endpoint;
  52. Context = context;
  53. }
  54. public ServiceResponse Execute()
  55. {
  56. var request = BuildRequest();
  57. try
  58. {
  59. return Client.Send(request, Context);
  60. }
  61. finally
  62. {
  63. if (!LeaveRequestOpen)
  64. request.Dispose();
  65. }
  66. }
  67. public IAsyncResult AsyncExecute(AsyncCallback callback, Object state)
  68. {
  69. var request = BuildRequest();
  70. return Client.BeginSend(request, Context, callback, state);
  71. }
  72. private ServiceRequest BuildRequest()
  73. {
  74. var conf = OssUtils.GetClientConfiguration(Client);
  75. var request = new ServiceRequest
  76. {
  77. Method = Method,
  78. Endpoint = OssUtils.MakeBucketEndpoint(Endpoint, Bucket, conf),
  79. ResourcePath = OssUtils.MakeResourcePath(Endpoint, Bucket, Key)
  80. };
  81. foreach (var p in Parameters)
  82. request.Parameters.Add(p.Key, p.Value);
  83. var adjustedTime = DateTime.UtcNow.AddSeconds(conf.TickOffset);
  84. request.Headers[HttpHeaders.Date] = DateUtils.FormatRfc822Date(adjustedTime);
  85. if (!Headers.ContainsKey(HttpHeaders.ContentType))
  86. request.Headers[HttpHeaders.ContentType] = string.Empty;
  87. foreach(var h in Headers)
  88. request.Headers.Add(h.Key, h.Value);
  89. if (Context.Credentials.UseToken)
  90. request.Headers[HttpHeaders.SecurityToken] = Context.Credentials.SecurityToken;
  91. request.Content = Content;
  92. return request;
  93. }
  94. }
  95. internal abstract class OssCommand<T> : OssCommand
  96. {
  97. private readonly IDeserializer<ServiceResponse, T> _deserializer;
  98. protected virtual bool LeaveResponseOpen { get { return false; } }
  99. protected OssCommand(IServiceClient client, Uri endpoint, ExecutionContext context,
  100. IDeserializer<ServiceResponse, T> deserializer)
  101. : base(client, endpoint, context)
  102. {
  103. _deserializer = deserializer;
  104. Context.Command = this;
  105. }
  106. public new T Execute()
  107. {
  108. var response = base.Execute();
  109. return DeserializeResponse(response);
  110. }
  111. public T DeserializeResponse(ServiceResponse response)
  112. {
  113. try
  114. {
  115. return _deserializer.Deserialize(response);
  116. }
  117. catch (ResponseDeserializationException ex)
  118. {
  119. throw ExceptionFactory.CreateInvalidResponseException(ex);
  120. }
  121. finally
  122. {
  123. if (!LeaveResponseOpen)
  124. response.Dispose();
  125. }
  126. }
  127. }
  128. }