All Downloads are FREE. Search and download functionalities are using the official Maven repository.

software.amazon.awssdk.services.s3.transform.PutBucketAclRequestMarshaller Maven / Gradle / Ivy

Go to download

The AWS Java SDK for Amazon S3 module holds the client classes that are used for communicating with Amazon Simple Storage Service

There is a newer version: 2.29.39
Show newest version
/*
 * Copyright 2012-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
 * the License. A copy of the License is located at
 * 
 * http://aws.amazon.com/apache2.0
 * 
 * or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
 * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
 * and limitations under the License.
 */

package software.amazon.awssdk.services.s3.transform;

import static software.amazon.awssdk.util.StringUtils.UTF8;

import java.io.StringWriter;

import java.util.List;

import javax.annotation.Generated;

import software.amazon.awssdk.SdkClientException;
import software.amazon.awssdk.Request;
import software.amazon.awssdk.DefaultRequest;
import software.amazon.awssdk.http.HttpMethodName;
import software.amazon.awssdk.services.s3.model.*;
import software.amazon.awssdk.runtime.transform.Marshaller;

import software.amazon.awssdk.util.StringInputStream;
import software.amazon.awssdk.util.StringUtils;

import software.amazon.awssdk.util.Md5Utils;
import software.amazon.awssdk.util.XmlWriter;

/**
 * PutBucketAclRequest Marshaller
 */

@Generated("software.amazon.awssdk:aws-java-sdk-code-generator")
public class PutBucketAclRequestMarshaller implements Marshaller, PutBucketAclRequest> {

    public Request marshall(PutBucketAclRequest putBucketAclRequest) {

        if (putBucketAclRequest == null) {
            throw new SdkClientException("Invalid argument passed to marshall(...)");
        }

        Request request = new DefaultRequest(putBucketAclRequest, "S3Client");

        request.setHttpMethod(HttpMethodName.PUT);

        if (putBucketAclRequest.acl() != null) {
            request.addHeader("x-amz-acl", StringUtils.fromString(putBucketAclRequest.acl()));
        }

        if (putBucketAclRequest.contentMD5() != null) {
            request.addHeader("Content-MD5", StringUtils.fromString(putBucketAclRequest.contentMD5()));
        }

        if (putBucketAclRequest.grantFullControl() != null) {
            request.addHeader("x-amz-grant-full-control", StringUtils.fromString(putBucketAclRequest.grantFullControl()));
        }

        if (putBucketAclRequest.grantRead() != null) {
            request.addHeader("x-amz-grant-read", StringUtils.fromString(putBucketAclRequest.grantRead()));
        }

        if (putBucketAclRequest.grantReadACP() != null) {
            request.addHeader("x-amz-grant-read-acp", StringUtils.fromString(putBucketAclRequest.grantReadACP()));
        }

        if (putBucketAclRequest.grantWrite() != null) {
            request.addHeader("x-amz-grant-write", StringUtils.fromString(putBucketAclRequest.grantWrite()));
        }

        if (putBucketAclRequest.grantWriteACP() != null) {
            request.addHeader("x-amz-grant-write-acp", StringUtils.fromString(putBucketAclRequest.grantWriteACP()));
        }

        String uriResourcePath = "/{Bucket}?acl";

        uriResourcePath = software.amazon.awssdk.util.UriResourcePathUtils.addStaticQueryParamtersToRequest(request,
                uriResourcePath);

        uriResourcePath = software.amazon.awssdk.runtime.transform.PathMarshallers.NON_GREEDY.marshall(uriResourcePath, "Bucket",
                putBucketAclRequest.bucket());
        request.setResourcePath(uriResourcePath);

        try {
            StringWriter stringWriter = null;
            AccessControlPolicy accessControlPolicy = putBucketAclRequest.accessControlPolicy();
            if (accessControlPolicy != null) {
                stringWriter = new StringWriter();
                XmlWriter xmlWriter = new XmlWriter(stringWriter, "http://s3.amazonaws.com/doc/2006-03-01/");
                xmlWriter.startElement("AccessControlPolicy");

                java.util.List accessControlPolicyGrantsList = accessControlPolicy.grants();
                if (accessControlPolicyGrantsList != null) {
                    xmlWriter.startElement("AccessControlList");

                    for (Grant accessControlPolicyGrantsListValue : accessControlPolicyGrantsList) {
                        xmlWriter.startElement("Grant");

                        Grantee grantee = accessControlPolicyGrantsListValue.grantee();
                        if (grantee != null) {
                            xmlWriter.startElement("Grantee");

                            if (grantee.displayName() != null) {
                                xmlWriter.startElement("DisplayName").value(grantee.displayName()).endElement();
                            }

                            if (grantee.emailAddress() != null) {
                                xmlWriter.startElement("EmailAddress").value(grantee.emailAddress()).endElement();
                            }

                            if (grantee.id() != null) {
                                xmlWriter.startElement("ID").value(grantee.id()).endElement();
                            }

                            if (grantee.type() != null) {
                                xmlWriter.startElement("xsi:type").value(grantee.type()).endElement();
                            }

                            if (grantee.uri() != null) {
                                xmlWriter.startElement("URI").value(grantee.uri()).endElement();
                            }
                            xmlWriter.endElement();
                        }

                        if (accessControlPolicyGrantsListValue.permission() != null) {
                            xmlWriter.startElement("Permission").value(accessControlPolicyGrantsListValue.permission())
                                    .endElement();
                        }
                        xmlWriter.endElement();
                    }
                    xmlWriter.endElement();
                }

                Owner owner = accessControlPolicy.owner();
                if (owner != null) {
                    xmlWriter.startElement("Owner");

                    if (owner.displayName() != null) {
                        xmlWriter.startElement("DisplayName").value(owner.displayName()).endElement();
                    }

                    if (owner.id() != null) {
                        xmlWriter.startElement("ID").value(owner.id()).endElement();
                    }
                    xmlWriter.endElement();
                }
                xmlWriter.endElement();
            }

            if (stringWriter != null) {
                if (!request.getHeaders().containsKey("Content-MD5")) {
                    request.addHeader("Content-MD5", Md5Utils.md5AsBase64(stringWriter.getBuffer().toString().getBytes(UTF8)));
                }
                request.setContent(new StringInputStream(stringWriter.getBuffer().toString()));
                request.addHeader("Content-Length", Integer.toString(stringWriter.getBuffer().toString().getBytes(UTF8).length));
            }
            if (!request.getHeaders().containsKey("Content-Type")) {
                request.addHeader("Content-Type", "application/xml");
            }
        } catch (Throwable t) {
            throw new SdkClientException("Unable to marshall request to XML: " + t.getMessage(), t);
        }

        return request;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy