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 
18 #include <alibabacloud/oss/model/ListObjectsResult.h>
19 #include <external/tinyxml2/tinyxml2.h>
20 #include <alibabacloud/oss/model/Owner.h>
21 #include "utils/Utils.h"
22 using namespace AlibabaCloud::OSS;
23 using namespace tinyxml2;
24 
25 
ListObjectsResult()26 ListObjectsResult::ListObjectsResult() :
27     OssResult(),
28     name_(),
29     prefix_(),
30     marker_(),
31     delimiter_(),
32     nextMarker_(),
33     isTruncated_(false),
34     maxKeys_(),
35     commonPrefixes_(),
36     objectSummarys_()
37 {
38 }
39 
ListObjectsResult(const std::string & result)40 ListObjectsResult::ListObjectsResult(const std::string& result):
41     ListObjectsResult()
42 {
43     *this = result;
44 }
45 
ListObjectsResult(const std::shared_ptr<std::iostream> & result)46 ListObjectsResult::ListObjectsResult(const std::shared_ptr<std::iostream>& result):
47     ListObjectsResult()
48 {
49     std::istreambuf_iterator<char> isb(*result.get()), end;
50     std::string str(isb, end);
51     *this = str;
52 }
53 
operator =(const std::string & result)54 ListObjectsResult& ListObjectsResult::operator =(const std::string& result)
55 {
56     XMLDocument doc;
57     XMLError xml_err;
58     if ((xml_err = doc.Parse(result.c_str(), result.size())) == XML_SUCCESS) {
59         XMLElement* root =doc.RootElement();
60         if (root && !std::strncmp("ListBucketResult", root->Name(), 16)) {
61             XMLElement *node;
62             node = root->FirstChildElement("Name");
63             if (node && node->GetText()) name_ = node->GetText();
64 
65             node = root->FirstChildElement("Prefix");
66             if (node && node->GetText()) prefix_ = node->GetText();
67 
68             node = root->FirstChildElement("Marker");
69             if (node && node->GetText()) marker_ = node->GetText();
70 
71             node = root->FirstChildElement("Delimiter");
72             if (node && node->GetText()) delimiter_ = node->GetText();
73 
74             node = root->FirstChildElement("MaxKeys");
75             if (node && node->GetText()) maxKeys_ = atoi(node->GetText());
76 
77             node = root->FirstChildElement("IsTruncated");
78             if (node && node->GetText()) isTruncated_ = !std::strncmp("true", node->GetText(), 4);
79 
80             node = root->FirstChildElement("NextMarker");
81             if (node && node->GetText()) nextMarker_ = node->GetText();
82 
83             node = root->FirstChildElement("EncodingType");
84             if (node && node->GetText()) encodingType_ = node->GetText();
85 
86 
87             //Detect encode type
88             bool useUrlDecode = !ToLower(encodingType_.c_str()).compare(0, 3, "url", 3);
89 
90             //CommonPrefixes
91             node = root->FirstChildElement("CommonPrefixes");
92             for (; node; node = node->NextSiblingElement("CommonPrefixes")) {
93                 XMLElement *prefix_node = node->FirstChildElement("Prefix");
94                 if (prefix_node && prefix_node->GetText()) commonPrefixes_.push_back(
95                     (useUrlDecode ? UrlDecode(prefix_node->GetText()) : prefix_node->GetText()));
96             }
97 
98             //Contents
99             XMLElement *contents_node = root->FirstChildElement("Contents");
100             for (; contents_node; contents_node = contents_node->NextSiblingElement("Contents")) {
101                 ObjectSummary content;
102                 node = contents_node->FirstChildElement("Key");
103                 if (node && node->GetText()) content.key_ = useUrlDecode ? UrlDecode(node->GetText()) : node->GetText();
104 
105                 node = contents_node->FirstChildElement("LastModified");
106                 if (node && node->GetText()) content.lastModified_ = node->GetText();
107 
108                 node = contents_node->FirstChildElement("ETag");
109                 if (node && node->GetText()) content.eTag_ = TrimQuotes(node->GetText());
110 
111                 node = contents_node->FirstChildElement("Size");
112                 if (node && node->GetText()) content.size_ = std::atoll(node->GetText());
113 
114                 node = contents_node->FirstChildElement("StorageClass");
115                 if (node && node->GetText()) content.storageClass_ = ToStorageClassType(node->GetText());
116 
117                 node = contents_node->FirstChildElement("Type");
118                 if (node && node->GetText()) content.type_ = node->GetText();
119 
120                 node = contents_node->FirstChildElement("Owner");
121                 std::string owner_ID, owner_DisplayName;
122                 if (node) {
123                     XMLElement *sub_node;
124                     sub_node = node->FirstChildElement("ID");
125                     if (sub_node && sub_node->GetText()) owner_ID = sub_node->GetText();
126 
127                     sub_node = node->FirstChildElement("DisplayName");
128                     if (sub_node && sub_node->GetText()) owner_DisplayName = sub_node->GetText();
129                 }
130 
131                 content.owner_ = Owner(owner_ID, owner_DisplayName);
132                 objectSummarys_.push_back(content);
133             }
134 
135             //EncodingType
136             if (useUrlDecode) {
137                 delimiter_  = UrlDecode(delimiter_);
138                 marker_     = UrlDecode(marker_);
139                 nextMarker_ = UrlDecode(nextMarker_);
140                 prefix_     = UrlDecode(prefix_);
141             }
142         }
143 
144         //TODO check the result and the parse flag;
145         parseDone_ = true;
146     }
147 
148     return *this;
149 }
150 
151