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.resource; 17 18 19 import java.util.Optional; 20 import software.amazon.awssdk.annotations.SdkInternalApi; 21 import software.amazon.awssdk.arns.Arn; 22 import software.amazon.awssdk.arns.ArnResource; 23 import software.amazon.awssdk.utils.StringUtils; 24 25 @SdkInternalApi 26 public class S3ArnUtils { 27 private static final int OUTPOST_ID_START_INDEX = "outpost".length() + 1; 28 S3ArnUtils()29 private S3ArnUtils() { 30 } 31 parseS3AccessPointArn(Arn arn)32 public static S3AccessPointResource parseS3AccessPointArn(Arn arn) { 33 return S3AccessPointResource.builder() 34 .partition(arn.partition()) 35 .region(arn.region().orElse(null)) 36 .accountId(arn.accountId().orElse(null)) 37 .accessPointName(arn.resource().resource()) 38 .build(); 39 } 40 parseOutpostArn(Arn arn)41 public static IntermediateOutpostResource parseOutpostArn(Arn arn) { 42 String resource = arn.resourceAsString(); 43 44 Integer outpostIdEndIndex = null; 45 46 for (int i = OUTPOST_ID_START_INDEX; i < resource.length(); ++i) { 47 char ch = resource.charAt(i); 48 49 if (ch == ':' || ch == '/') { 50 outpostIdEndIndex = i; 51 break; 52 } 53 } 54 55 if (outpostIdEndIndex == null) { 56 throw new IllegalArgumentException("Invalid format for S3 outpost ARN, missing outpostId"); 57 } 58 59 String outpostId = resource.substring(OUTPOST_ID_START_INDEX, outpostIdEndIndex); 60 61 if (StringUtils.isEmpty(outpostId)) { 62 throw new IllegalArgumentException("Invalid format for S3 outpost ARN, missing outpostId"); 63 } 64 65 String subresource = resource.substring(outpostIdEndIndex + 1); 66 67 if (StringUtils.isEmpty(subresource)) { 68 throw new IllegalArgumentException("Invalid format for S3 outpost ARN"); 69 } 70 71 return IntermediateOutpostResource.builder() 72 .outpostId(outpostId) 73 .outpostSubresource(ArnResource.fromString(subresource)) 74 .build(); 75 } 76 getArnType(String arnString)77 public static Optional<S3ResourceType> getArnType(String arnString) { 78 try { 79 Arn arn = Arn.fromString(arnString); 80 String resourceType = arn.resource().resourceType().get(); 81 S3ResourceType s3ResourceType = S3ResourceType.fromValue(resourceType); 82 return Optional.of(s3ResourceType); 83 } catch (Exception ignored) { 84 return Optional.empty(); 85 } 86 } 87 } 88