PageRenderTime 71ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/sdk/src/Services/S3/Custom/Model/Internal/MarshallTransformations/PutLifecycleConfigurationRequestMarshaller.cs

https://gitlab.com/vectorci/aws-sdk-net
C# | 197 lines | 162 code | 18 blank | 17 comment | 38 complexity | b8d65736d425b8ba7829872fe720974a MD5 | raw file
  1. /*
  2. * Copyright 2010-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License").
  5. * You may not use this file except in compliance with the License.
  6. * A copy of the License is located at
  7. *
  8. * http://aws.amazon.com/apache2.0
  9. *
  10. * or in the "license" file accompanying this file. This file is distributed
  11. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  12. * express or implied. See the License for the specific language governing
  13. * permissions and limitations under the License.
  14. */
  15. using System.IO;
  16. using System.Xml;
  17. using System.Text;
  18. using Amazon.S3.Util;
  19. using Amazon.Runtime;
  20. using Amazon.Runtime.Internal;
  21. using Amazon.Runtime.Internal.Transform;
  22. using Amazon.Util;
  23. #pragma warning disable 1591
  24. namespace Amazon.S3.Model.Internal.MarshallTransformations
  25. {
  26. /// <summary>
  27. /// Put Bucket Lifecycle Request Marshaller
  28. /// </summary>
  29. public class PutLifecycleConfigurationRequestMarshaller : IMarshaller<IRequest, PutLifecycleConfigurationRequest> ,IMarshaller<IRequest,Amazon.Runtime.AmazonWebServiceRequest>
  30. {
  31. public IRequest Marshall(Amazon.Runtime.AmazonWebServiceRequest input)
  32. {
  33. return this.Marshall((PutLifecycleConfigurationRequest)input);
  34. }
  35. public IRequest Marshall(PutLifecycleConfigurationRequest putLifecycleConfigurationRequest)
  36. {
  37. IRequest request = new DefaultRequest(putLifecycleConfigurationRequest, "AmazonS3");
  38. request.HttpMethod = "PUT";
  39. request.ResourcePath = string.Concat("/", S3Transforms.ToStringValue(putLifecycleConfigurationRequest.BucketName));
  40. request.AddSubResource("lifecycle");
  41. var stringWriter = new StringWriter(System.Globalization.CultureInfo.InvariantCulture);
  42. using (var xmlWriter = XmlWriter.Create(stringWriter, new XmlWriterSettings(){Encoding = Encoding.UTF8, OmitXmlDeclaration = true}))
  43. {
  44. var lifecycleConfigurationLifecycleConfiguration = putLifecycleConfigurationRequest.Configuration;
  45. if (lifecycleConfigurationLifecycleConfiguration != null)
  46. {
  47. xmlWriter.WriteStartElement("LifecycleConfiguration", "");
  48. if (lifecycleConfigurationLifecycleConfiguration != null)
  49. {
  50. var lifecycleConfigurationLifecycleConfigurationrulesList = lifecycleConfigurationLifecycleConfiguration.Rules;
  51. if (lifecycleConfigurationLifecycleConfigurationrulesList != null && lifecycleConfigurationLifecycleConfigurationrulesList.Count > 0)
  52. {
  53. foreach (var lifecycleConfigurationLifecycleConfigurationrulesListValue in lifecycleConfigurationLifecycleConfigurationrulesList)
  54. {
  55. xmlWriter.WriteStartElement("Rule", "");
  56. if (lifecycleConfigurationLifecycleConfigurationrulesListValue != null)
  57. {
  58. var expiration = lifecycleConfigurationLifecycleConfigurationrulesListValue.Expiration;
  59. if (expiration != null)
  60. {
  61. xmlWriter.WriteStartElement("Expiration", "");
  62. if (expiration.IsSetDate())
  63. {
  64. xmlWriter.WriteElementString("Date", "", S3Transforms.ToXmlStringValue(expiration.Date));
  65. }
  66. if (expiration.IsSetDays())
  67. {
  68. xmlWriter.WriteElementString("Days", "", S3Transforms.ToXmlStringValue(expiration.Days));
  69. }
  70. if (expiration.IsSetExpiredObjectDeleteMarker())
  71. {
  72. xmlWriter.WriteElementString("ExpiredObjectDeleteMarker", "", expiration.ExpiredObjectDeleteMarker.ToString().ToLowerInvariant());
  73. }
  74. xmlWriter.WriteEndElement();
  75. }
  76. var transitions = lifecycleConfigurationLifecycleConfigurationrulesListValue.Transitions;
  77. if (transitions != null && transitions.Count > 0)
  78. {
  79. foreach (var transition in transitions)
  80. {
  81. if (transition != null)
  82. {
  83. xmlWriter.WriteStartElement("Transition", "");
  84. if (transition.IsSetDate())
  85. {
  86. xmlWriter.WriteElementString("Date", "", S3Transforms.ToXmlStringValue(transition.Date));
  87. }
  88. if (transition.IsSetDays())
  89. {
  90. xmlWriter.WriteElementString("Days", "", S3Transforms.ToXmlStringValue(transition.Days));
  91. }
  92. if (transition.IsSetStorageClass())
  93. {
  94. xmlWriter.WriteElementString("StorageClass", "", S3Transforms.ToXmlStringValue(transition.StorageClass));
  95. }
  96. xmlWriter.WriteEndElement();
  97. }
  98. }
  99. }
  100. var noncurrentVersionExpiration = lifecycleConfigurationLifecycleConfigurationrulesListValue.NoncurrentVersionExpiration;
  101. if (noncurrentVersionExpiration != null)
  102. {
  103. xmlWriter.WriteStartElement("NoncurrentVersionExpiration", "");
  104. if (noncurrentVersionExpiration.IsSetNoncurrentDays())
  105. {
  106. xmlWriter.WriteElementString("NoncurrentDays", "", S3Transforms.ToXmlStringValue(noncurrentVersionExpiration.NoncurrentDays));
  107. }
  108. xmlWriter.WriteEndElement();
  109. }
  110. var noncurrentVersionTransitions = lifecycleConfigurationLifecycleConfigurationrulesListValue.NoncurrentVersionTransitions;
  111. if (noncurrentVersionTransitions != null && noncurrentVersionTransitions.Count > 0)
  112. {
  113. foreach (var noncurrentVersionTransition in noncurrentVersionTransitions)
  114. {
  115. if (noncurrentVersionTransition != null)
  116. {
  117. xmlWriter.WriteStartElement("NoncurrentVersionTransition", "");
  118. if (noncurrentVersionTransition.IsSetNoncurrentDays())
  119. {
  120. xmlWriter.WriteElementString("NoncurrentDays", "", S3Transforms.ToXmlStringValue(noncurrentVersionTransition.NoncurrentDays));
  121. }
  122. if (noncurrentVersionTransition.IsSetStorageClass())
  123. {
  124. xmlWriter.WriteElementString("StorageClass", "", S3Transforms.ToXmlStringValue(noncurrentVersionTransition.StorageClass));
  125. }
  126. xmlWriter.WriteEndElement();
  127. }
  128. }
  129. }
  130. var abortIncompleteMultipartUpload = lifecycleConfigurationLifecycleConfigurationrulesListValue.AbortIncompleteMultipartUpload;
  131. if (abortIncompleteMultipartUpload != null)
  132. {
  133. xmlWriter.WriteStartElement("AbortIncompleteMultipartUpload", "");
  134. if (abortIncompleteMultipartUpload.IsSetDaysAfterInitiation())
  135. {
  136. xmlWriter.WriteElementString("DaysAfterInitiation", "", S3Transforms.ToXmlStringValue(abortIncompleteMultipartUpload.DaysAfterInitiation));
  137. }
  138. xmlWriter.WriteEndElement();
  139. }
  140. }
  141. if (lifecycleConfigurationLifecycleConfigurationrulesListValue.IsSetId())
  142. {
  143. xmlWriter.WriteElementString("ID", "", S3Transforms.ToXmlStringValue(lifecycleConfigurationLifecycleConfigurationrulesListValue.Id));
  144. }
  145. if (lifecycleConfigurationLifecycleConfigurationrulesListValue.IsSetPrefix())
  146. {
  147. xmlWriter.WriteElementString("Prefix", "", S3Transforms.ToXmlStringValue(lifecycleConfigurationLifecycleConfigurationrulesListValue.Prefix));
  148. }
  149. if (lifecycleConfigurationLifecycleConfigurationrulesListValue.IsSetStatus())
  150. {
  151. xmlWriter.WriteElementString("Status", "", S3Transforms.ToXmlStringValue(lifecycleConfigurationLifecycleConfigurationrulesListValue.Status));
  152. }
  153. else
  154. {
  155. xmlWriter.WriteElementString("Status", "", "Disabled");
  156. }
  157. xmlWriter.WriteEndElement();
  158. }
  159. }
  160. }
  161. xmlWriter.WriteEndElement();
  162. }
  163. }
  164. try
  165. {
  166. var content = stringWriter.ToString();
  167. request.Content = Encoding.UTF8.GetBytes(content);
  168. request.Headers[HeaderKeys.ContentTypeHeader] = "application/xml";
  169. var checksum = AmazonS3Util.GenerateChecksumForContent(content, true);
  170. request.Headers[HeaderKeys.ContentMD5Header] = checksum;
  171. }
  172. catch (EncoderFallbackException e)
  173. {
  174. throw new AmazonServiceException("Unable to marshall request to XML", e);
  175. }
  176. return request;
  177. }
  178. }
  179. }