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 <alibabacloud/oss/model/SetBucketWebsiteRequest.h>
18 #include <sstream>
19 #include <utils/Utils.h>
20 #include "ModelError.h"
21 
22 using namespace AlibabaCloud::OSS;
23 
SetBucketWebsiteRequest(const std::string & bucket)24 SetBucketWebsiteRequest::SetBucketWebsiteRequest(const std::string& bucket) :
25     OssBucketRequest(bucket),
26     indexDocumentIsSet_(false),
27     errorDocumentIsSet_(false)
28 {
29     setFlags(Flags() | REQUEST_FLAG_CONTENTMD5);
30 }
31 
payload() const32 std::string SetBucketWebsiteRequest::payload() const
33 {
34     std::stringstream ss;
35     ss << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" << std::endl;
36     ss << "<WebsiteConfiguration>" << std::endl;
37     ss << "  <IndexDocument>" << std::endl;
38     ss << "    <Suffix>" << indexDocument_ << "</Suffix>" << std::endl;
39     ss << "  </IndexDocument>" << std::endl;
40     if (errorDocumentIsSet_) {
41         ss << "  <ErrorDocument>" << std::endl;
42         ss << "    <Key>" << errorDocument_ << "</Key>" << std::endl;
43         ss << "  </ErrorDocument>" << std::endl;
44     }
45     ss << "</WebsiteConfiguration>" << std::endl;
46     return ss.str();
47 }
48 
specialParameters() const49 ParameterCollection SetBucketWebsiteRequest::specialParameters() const
50 {
51     ParameterCollection parameters;
52     parameters["website"] = "";
53     return parameters;
54 }
55 
IsValidWebpage(const std::string & webpage)56 static bool IsValidWebpage(const std::string &webpage)
57 {
58     const std::string pageSuffix = ".html";
59     return (webpage.size() > pageSuffix.size()) &&
60         (webpage.substr(webpage.size() - 5).compare(".html") == 0);
61 }
62 
validate() const63 int SetBucketWebsiteRequest::validate() const
64 {
65     int ret = OssBucketRequest::validate();
66     if (ret != 0)
67         return ret;
68 
69     if (indexDocument_.empty())
70         return ARG_ERROR_WEBSITE_INDEX_DOCCUMENT_EMPTY;
71 
72     if (!IsValidWebpage(indexDocument_))
73         return ARG_ERROR_WEBSITE_INDEX_DOCCUMENT_NAME_INVALID;
74 
75     if (errorDocumentIsSet_ && !IsValidWebpage(errorDocument_))
76         return ARG_ERROR_WEBSITE_ERROR_DOCCUMENT_NAME_INVALID;
77 
78     return 0;
79 }
80 
81