| 1 | /** |
| 2 | * |
| 3 | * Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com> |
| 4 | * |
| 5 | * ==================================================================== |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * 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, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | * ==================================================================== |
| 18 | */ |
| 19 | package org.jclouds.azureblob.predicates.validators; |
| 20 | |
| 21 | import javax.inject.Inject; |
| 22 | |
| 23 | import org.jclouds.predicates.validators.DnsNameValidator; |
| 24 | |
| 25 | import com.google.inject.Singleton; |
| 26 | |
| 27 | /** |
| 28 | * Validates name for Azure container. The complete requirements are listed at: |
| 29 | * http://weblogs.asp.net |
| 30 | * /vblasberg/archive/2009/02/17/azure-details-and-limitations-blobs-tables-and-queues.aspx |
| 31 | * |
| 32 | * @see org.jclouds.rest.InputParamValidator |
| 33 | * @see org.jclouds.predicates.Validator |
| 34 | * |
| 35 | * @author Oleksiy Yarmula |
| 36 | */ |
| 37 | @Singleton |
| 38 | public class ContainerNameValidator extends DnsNameValidator { |
| 39 | |
| 40 | @Inject |
| 41 | ContainerNameValidator() { |
| 42 | super(3,63); |
| 43 | } |
| 44 | |
| 45 | public void validate(String containerName) { |
| 46 | super.validate(containerName); |
| 47 | if (containerName.contains("--")) |
| 48 | throw exception(containerName, "Every dash must be followed by letter or number"); |
| 49 | if (containerName.endsWith("-")) |
| 50 | throw exception(containerName, "Shouldn't end with a dash"); |
| 51 | } |
| 52 | |
| 53 | @Override |
| 54 | protected IllegalArgumentException exception(String containerName, String reason) { |
| 55 | return new IllegalArgumentException( |
| 56 | String |
| 57 | .format( |
| 58 | "Object '%s' doesn't match Azure container naming convention. " |
| 59 | + "Reason: %s. For more info, please refer to http://weblogs.asp.net/vblasberg/archive/2009/02/17/" |
| 60 | + "azure-details-and-limitations-blobs-tables-and-queues.aspx.", |
| 61 | containerName, reason)); |
| 62 | } |
| 63 | |
| 64 | } |