| 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.predicates.validators; |
| 20 | |
| 21 | import static com.google.common.base.CharMatcher.is; |
| 22 | |
| 23 | import javax.inject.Inject; |
| 24 | |
| 25 | import org.jclouds.predicates.validators.DnsNameValidator; |
| 26 | |
| 27 | import com.google.common.base.CharMatcher; |
| 28 | import com.google.inject.Singleton; |
| 29 | |
| 30 | /** |
| 31 | * Validates name for S3 buckets. The complete requirements are listed at: |
| 32 | * http://docs.amazonwebservices.com/AmazonS3/latest/index.html?BucketRestrictions.html |
| 33 | * |
| 34 | * @see org.jclouds.rest.InputParamValidator |
| 35 | * @see org.jclouds.predicates.Validator |
| 36 | * |
| 37 | * @author Adrian Cole |
| 38 | */ |
| 39 | @Singleton |
| 40 | public class BucketNameValidator extends DnsNameValidator { |
| 41 | |
| 42 | @Inject |
| 43 | BucketNameValidator() { |
| 44 | super(3, 63); |
| 45 | } |
| 46 | |
| 47 | public void validate(String containerName) { |
| 48 | super.validate(containerName); |
| 49 | if (containerName.indexOf("..") != -1) |
| 50 | throw exception(containerName, "Bucket names cannot contain two, adjacent periods"); |
| 51 | if (containerName.endsWith("-")) |
| 52 | throw exception(containerName, "Bucket names should not end with a dash"); |
| 53 | |
| 54 | if (containerName.indexOf("-.") != -1 || containerName.indexOf(".-") != -1) |
| 55 | throw exception( |
| 56 | containerName, |
| 57 | "Bucket names cannot contain dashes next to periods (e.g., \"my-.bucket.com\" and \"my.-bucket\" are invalid)"); |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | protected IllegalArgumentException exception(String containerName, String reason) { |
| 62 | return new IllegalArgumentException( |
| 63 | String |
| 64 | .format( |
| 65 | "Object '%s' doesn't match S3 bucket virtual host naming convention. " |
| 66 | + "Reason: %s. For more info, please refer to http://docs.amazonwebservices.com/AmazonS3/latest/index.html?BucketRestrictions.html.", |
| 67 | containerName, reason)); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Amazon also permits periods in the dns name. |
| 72 | * It also permits underscores, although they aren't recommended. |
| 73 | */ |
| 74 | @Override |
| 75 | protected CharMatcher getAcceptableRange() { |
| 76 | return super.getAcceptableRange().or(is('.')).or(is('_')); |
| 77 | } |
| 78 | } |