1/*
2* Copyright 2010-2017 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
16#include <aws/s3/model/CORSRule.h>
17#include <aws/core/utils/xml/XmlSerializer.h>
18#include <aws/core/utils/StringUtils.h>
19#include <aws/core/utils/memory/stl/AWSStringStream.h>
20
21#include <utility>
22
23using namespace Aws::Utils::Xml;
24using namespace Aws::Utils;
25
26namespace Aws
27{
28namespace S3
29{
30namespace Model
31{
32
33CORSRule::CORSRule() :
34 m_allowedHeadersHasBeenSet(false),
35 m_allowedMethodsHasBeenSet(false),
36 m_allowedOriginsHasBeenSet(false),
37 m_exposeHeadersHasBeenSet(false),
38 m_maxAgeSeconds(0),
39 m_maxAgeSecondsHasBeenSet(false)
40{
41}
42
43CORSRule::CORSRule(const XmlNode& xmlNode) :
44 m_allowedHeadersHasBeenSet(false),
45 m_allowedMethodsHasBeenSet(false),
46 m_allowedOriginsHasBeenSet(false),
47 m_exposeHeadersHasBeenSet(false),
48 m_maxAgeSeconds(0),
49 m_maxAgeSecondsHasBeenSet(false)
50{
51 *this = xmlNode;
52}
53
54CORSRule& CORSRule::operator =(const XmlNode& xmlNode)
55{
56 XmlNode resultNode = xmlNode;
57
58 if(!resultNode.IsNull())
59 {
60 XmlNode allowedHeadersNode = resultNode.FirstChild("AllowedHeader");
61 if(!allowedHeadersNode.IsNull())
62 {
63 XmlNode allowedHeaderMember = allowedHeadersNode;
64 while(!allowedHeaderMember.IsNull())
65 {
66 m_allowedHeaders.push_back(allowedHeaderMember.GetText());
67 allowedHeaderMember = allowedHeaderMember.NextNode("AllowedHeader");
68 }
69
70 m_allowedHeadersHasBeenSet = true;
71 }
72 XmlNode allowedMethodsNode = resultNode.FirstChild("AllowedMethod");
73 if(!allowedMethodsNode.IsNull())
74 {
75 XmlNode allowedMethodMember = allowedMethodsNode;
76 while(!allowedMethodMember.IsNull())
77 {
78 m_allowedMethods.push_back(allowedMethodMember.GetText());
79 allowedMethodMember = allowedMethodMember.NextNode("AllowedMethod");
80 }
81
82 m_allowedMethodsHasBeenSet = true;
83 }
84 XmlNode allowedOriginsNode = resultNode.FirstChild("AllowedOrigin");
85 if(!allowedOriginsNode.IsNull())
86 {
87 XmlNode allowedOriginMember = allowedOriginsNode;
88 while(!allowedOriginMember.IsNull())
89 {
90 m_allowedOrigins.push_back(allowedOriginMember.GetText());
91 allowedOriginMember = allowedOriginMember.NextNode("AllowedOrigin");
92 }
93
94 m_allowedOriginsHasBeenSet = true;
95 }
96 XmlNode exposeHeadersNode = resultNode.FirstChild("ExposeHeader");
97 if(!exposeHeadersNode.IsNull())
98 {
99 XmlNode exposeHeaderMember = exposeHeadersNode;
100 while(!exposeHeaderMember.IsNull())
101 {
102 m_exposeHeaders.push_back(exposeHeaderMember.GetText());
103 exposeHeaderMember = exposeHeaderMember.NextNode("ExposeHeader");
104 }
105
106 m_exposeHeadersHasBeenSet = true;
107 }
108 XmlNode maxAgeSecondsNode = resultNode.FirstChild("MaxAgeSeconds");
109 if(!maxAgeSecondsNode.IsNull())
110 {
111 m_maxAgeSeconds = StringUtils::ConvertToInt32(StringUtils::Trim(Aws::Utils::Xml::DecodeEscapedXmlText(maxAgeSecondsNode.GetText()).c_str()).c_str());
112 m_maxAgeSecondsHasBeenSet = true;
113 }
114 }
115
116 return *this;
117}
118
119void CORSRule::AddToNode(XmlNode& parentNode) const
120{
121 Aws::StringStream ss;
122 if(m_allowedHeadersHasBeenSet)
123 {
124 for(const auto& item : m_allowedHeaders)
125 {
126 XmlNode allowedHeadersNode = parentNode.CreateChildElement("AllowedHeader");
127 allowedHeadersNode.SetText(item);
128 }
129 }
130
131 if(m_allowedMethodsHasBeenSet)
132 {
133 for(const auto& item : m_allowedMethods)
134 {
135 XmlNode allowedMethodsNode = parentNode.CreateChildElement("AllowedMethod");
136 allowedMethodsNode.SetText(item);
137 }
138 }
139
140 if(m_allowedOriginsHasBeenSet)
141 {
142 for(const auto& item : m_allowedOrigins)
143 {
144 XmlNode allowedOriginsNode = parentNode.CreateChildElement("AllowedOrigin");
145 allowedOriginsNode.SetText(item);
146 }
147 }
148
149 if(m_exposeHeadersHasBeenSet)
150 {
151 for(const auto& item : m_exposeHeaders)
152 {
153 XmlNode exposeHeadersNode = parentNode.CreateChildElement("ExposeHeader");
154 exposeHeadersNode.SetText(item);
155 }
156 }
157
158 if(m_maxAgeSecondsHasBeenSet)
159 {
160 XmlNode maxAgeSecondsNode = parentNode.CreateChildElement("MaxAgeSeconds");
161 ss << m_maxAgeSeconds;
162 maxAgeSecondsNode.SetText(ss.str());
163 ss.str("");
164 }
165
166}
167
168} // namespace Model
169} // namespace S3
170} // namespace Aws
171