1 /* 2 * Copyright Amazon.com, Inc. or its affiliates. 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 * A copy of the License is located at 7 * 8 * http://aws.amazon.com/apache2.0 9 * 10 * or in the "license" file accompanying this file. This file is distributed 11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 12 * express or implied. See the License for the specific language governing 13 * permissions and limitations under the License. 14 */ 15 16 package software.amazon.awssdk.services.s3.internal; 17 18 import software.amazon.awssdk.annotations.SdkInternalApi; 19 import software.amazon.awssdk.core.adapter.TypeAdapter; 20 import software.amazon.awssdk.services.s3.model.Tag; 21 import software.amazon.awssdk.services.s3.model.Tagging; 22 import software.amazon.awssdk.utils.http.SdkHttpUtils; 23 24 /** 25 * {@link TypeAdapter} that converts the {@link Tagging} modeled object into a 26 * URL encoded map of key to values. Used for Put and Copy object operations 27 * which models the Tagging as a string. 28 */ 29 @SdkInternalApi 30 public final class TaggingAdapter implements TypeAdapter<Tagging, String> { 31 32 private static final TaggingAdapter INSTANCE = new TaggingAdapter(); 33 34 TaggingAdapter()35 private TaggingAdapter() { 36 } 37 38 @Override adapt(Tagging tagging)39 public String adapt(Tagging tagging) { 40 StringBuilder tagBuilder = new StringBuilder(); 41 42 if (tagging != null && !tagging.tagSet().isEmpty()) { 43 Tagging taggingClone = tagging.toBuilder().build(); 44 45 Tag firstTag = taggingClone.tagSet().get(0); 46 tagBuilder.append(SdkHttpUtils.urlEncode(firstTag.key())); 47 tagBuilder.append("="); 48 tagBuilder.append(SdkHttpUtils.urlEncode(firstTag.value())); 49 50 for (int i = 1; i < taggingClone.tagSet().size(); i++) { 51 Tag t = taggingClone.tagSet().get(i); 52 tagBuilder.append("&"); 53 tagBuilder.append(SdkHttpUtils.urlEncode(t.key())); 54 tagBuilder.append("="); 55 tagBuilder.append(SdkHttpUtils.urlEncode(t.value())); 56 } 57 } 58 59 return tagBuilder.toString(); 60 } 61 62 /** 63 * @return Singleton instance of {@link TaggingAdapter}. 64 */ instance()65 public static TaggingAdapter instance() { 66 return INSTANCE; 67 } 68 } 69