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/SetBucketLoggingRequest.h>
18 #include <sstream>
19 #include <utils/Utils.h>
20 #include "ModelError.h"
21 
22 using namespace AlibabaCloud::OSS;
23 
SetBucketLoggingRequest(const std::string & bucket)24 SetBucketLoggingRequest::SetBucketLoggingRequest(const std::string& bucket) :
25     SetBucketLoggingRequest(bucket, "", "")
26 {
27 }
28 
SetBucketLoggingRequest(const std::string & bucket,const std::string & targetBucket,const std::string & targetPrefix)29 SetBucketLoggingRequest::SetBucketLoggingRequest(const std::string& bucket,
30     const std::string& targetBucket, const std::string& targetPrefix) :
31     OssBucketRequest(bucket),
32     targetBucket_(targetBucket),
33     targetPrefix_(targetPrefix)
34 {
35     setFlags(Flags() | REQUEST_FLAG_CONTENTMD5);
36 }
37 
payload() const38 std::string SetBucketLoggingRequest::payload() const
39 {
40     std::stringstream ss;
41     ss << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" << std::endl;
42     ss << "<BucketLoggingStatus>" << std::endl;
43     ss << "<LoggingEnabled>" << std::endl;
44     ss << "<TargetBucket>" << targetBucket_ << "</TargetBucket>" << std::endl;
45     ss << "<TargetPrefix>" << targetPrefix_ << "</TargetPrefix>" << std::endl;
46     ss << "</LoggingEnabled>" << std::endl;
47     ss << "</BucketLoggingStatus>" << std::endl;
48     return ss.str();
49 }
50 
specialParameters() const51 ParameterCollection SetBucketLoggingRequest::specialParameters() const
52 {
53     ParameterCollection parameters;
54     parameters["logging"] = "";
55     return parameters;
56 }
57 
validate() const58 int SetBucketLoggingRequest::validate() const
59 {
60     int ret;
61     if ((ret = OssBucketRequest::validate()) != 0 ) {
62         return ret;
63     }
64 
65     if (!IsValidBucketName(targetBucket_)) {
66         return ARG_ERROR_BUCKET_NAME;
67     }
68 
69     if (!IsValidLoggingPrefix(targetPrefix_)) {
70         return ARG_ERROR_LOGGING_TARGETPREFIX_INVALID;
71     }
72 
73     return 0;
74 }
75