| 1 | /** |
| 2 | * Licensed to jclouds, Inc. (jclouds) under one or more |
| 3 | * contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. jclouds licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | package org.jclouds.s3.util; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkArgument; |
| 22 | import static com.google.common.base.Preconditions.checkNotNull; |
| 23 | |
| 24 | import java.util.regex.Pattern; |
| 25 | |
| 26 | import org.jclouds.s3.S3Client; |
| 27 | import org.jclouds.util.Patterns; |
| 28 | |
| 29 | /** |
| 30 | * Encryption, Hashing, and IO Utilities needed to sign and verify S3 requests and responses. |
| 31 | * |
| 32 | * @author Adrian Cole |
| 33 | */ |
| 34 | public class S3Utils { |
| 35 | |
| 36 | public static final Pattern BUCKET_NAME_PATTERN = Pattern.compile("^[a-z0-9][-_.a-z0-9]+"); |
| 37 | |
| 38 | // TODO add validatorparam so that this is actually used |
| 39 | public static String validateBucketName(String bucketName) { |
| 40 | checkNotNull(bucketName, "bucketName"); |
| 41 | checkArgument( |
| 42 | BUCKET_NAME_PATTERN.matcher(bucketName).matches(), |
| 43 | "bucketName name must start with a number or letter and can only contain lowercase letters, numbers, periods (.), underscores (_), and dashes (-)"); |
| 44 | checkArgument(bucketName.length() > 2 && bucketName.length() < 256, |
| 45 | "bucketName name must be between 3 and 255 characters long"); |
| 46 | checkArgument(!Patterns.IP_PATTERN.matcher(bucketName).matches(), |
| 47 | "bucketName name cannot be ip address style"); |
| 48 | return bucketName; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * This implementation invokes {@link S3Client#deleteBucketIfEmpty} followed by |
| 53 | * {@link S3Client#bucketExists} until it is true. |
| 54 | */ |
| 55 | public static boolean deleteAndVerifyContainerGone(S3Client sync, String container) { |
| 56 | sync.deleteBucketIfEmpty(container); |
| 57 | return sync.bucketExists(container); |
| 58 | } |
| 59 | } |