1 /*
2 * Copyright 2009-2017 Alibaba Cloud 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 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <sstream>
18 #include <alibabacloud/oss/model/ListPartsResult.h>
19 #include <external/tinyxml2/tinyxml2.h>
20 #include "utils/Utils.h"
21
22 using namespace AlibabaCloud::OSS;
23 using namespace tinyxml2;
24 using std::stringstream;
25
ListPartsResult()26 ListPartsResult::ListPartsResult():
27 OssResult(),
28 maxParts_(0),
29 partNumberMarker_(0),
30 nextPartNumberMarker_(0),
31 isTruncated_(false)
32 {
33 }
34
ListPartsResult(const std::string & result)35 ListPartsResult::ListPartsResult(const std::string& result):
36 ListPartsResult()
37 {
38 *this = result;
39 }
40
ListPartsResult(const std::shared_ptr<std::iostream> & result)41 ListPartsResult::ListPartsResult(const std::shared_ptr<std::iostream>& result):
42 ListPartsResult()
43 {
44 std::istreambuf_iterator<char> isb(*result.get()), end;
45 std::string str(isb, end);
46 *this = str;
47 }
48
operator =(const std::string & result)49 ListPartsResult& ListPartsResult::operator =(const std::string& result)
50 {
51 XMLDocument doc;
52 XMLError xml_err;
53 if ((xml_err = doc.Parse(result.c_str(), result.size())) == XML_SUCCESS) {
54 XMLElement* root =doc.RootElement();
55 if (root && !std::strncmp("ListPartsResult", root->Name(), 15)) {
56 XMLElement *node;
57 node = root->FirstChildElement("EncodingType");
58 if (node && node->GetText()) encodingType_ = node->GetText();
59
60 //Detect encode type
61 bool useUrlDecode = !ToLower(encodingType_.c_str()).compare(0, 3, "url", 3);
62
63 node = root->FirstChildElement("Bucket");
64 if (node && node->GetText()) bucket_ = node->GetText();
65
66 node = root->FirstChildElement("Key");
67 if (node && node->GetText()) key_ = useUrlDecode ? UrlDecode(node->GetText()) : node->GetText();
68
69 node = root->FirstChildElement("UploadId");
70 if (node && node->GetText()) uploadId_ = node->GetText();
71
72 node = root->FirstChildElement("PartNumberMarker");
73 if(node && node->GetText())
74 {
75 partNumberMarker_ = std::strtoul(node->GetText(), nullptr, 10);
76 }
77
78 node = root->FirstChildElement("NextPartNumberMarker");
79 if(node && node->GetText())
80 {
81 nextPartNumberMarker_ = std::strtoul(node->GetText(), nullptr, 10);
82 }
83
84 node = root->FirstChildElement("MaxParts");
85 if (node && node->GetText())
86 {
87 maxParts_ = std::strtoul(node->GetText(), nullptr, 10);
88 }
89
90 node = root->FirstChildElement("IsTruncated");
91 if (node && node->GetText()) isTruncated_ = node->BoolText();
92
93 XMLElement * partNode = root->FirstChildElement("Part");
94 for( ; partNode ; partNode = partNode->NextSiblingElement("Part"))
95 {
96 Part part;
97 node = partNode->FirstChildElement("PartNumber");
98 if(node && node->GetText())
99 {
100 part.partNumber_ = std::atoi(node->GetText());
101 }
102
103 node = partNode->FirstChildElement("LastModified");
104 if(node && node->GetText()) part.lastModified_ = node->GetText();
105
106 node = partNode->FirstChildElement("ETag");
107 if (node && node->GetText()) part.eTag_ = TrimQuotes(node->GetText());
108
109 node = partNode->FirstChildElement("Size");
110 if(node && node->GetText())
111 {
112 part.size_ = std::strtoll(node->GetText(), nullptr, 10);
113 }
114
115 node = partNode->FirstChildElement("HashCrc64ecma");
116 if(node && node->GetText())
117 {
118 part.cRC64_ = std::strtoull(node->GetText(), nullptr, 10);
119 }
120 partList_.push_back(part);
121 }
122 }
123 //TODO check the result and the parse flag;
124 parseDone_ = true;
125 }
126 return *this;
127 }
128
UploadId() const129 const std::string& ListPartsResult::UploadId() const
130 {
131 return uploadId_;
132 }
133
Key() const134 const std::string& ListPartsResult::Key() const
135 {
136 return key_;
137 }
Bucket() const138 const std::string& ListPartsResult::Bucket() const
139 {
140 return bucket_;
141 }
EncodingType() const142 const std::string& ListPartsResult::EncodingType() const
143 {
144 return encodingType_;
145 }
146
MaxParts() const147 uint32_t ListPartsResult::MaxParts() const
148 {
149 return maxParts_;
150 }
151
PartNumberMarker() const152 uint32_t ListPartsResult::PartNumberMarker() const
153 {
154 return partNumberMarker_;
155 }
156
NextPartNumberMarker() const157 uint32_t ListPartsResult::NextPartNumberMarker() const
158 {
159 return nextPartNumberMarker_;
160 }
161
PartList() const162 const PartList& ListPartsResult::PartList() const
163 {
164 return partList_;
165 }
166
IsTruncated() const167 bool ListPartsResult::IsTruncated() const
168 {
169 return isTruncated_;
170 }
171