All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.microsoft.azure.storage.SharedAccessAccountPermissions Maven / Gradle / Ivy

/**
 * Copyright Microsoft Corporation
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.microsoft.azure.storage;

import java.util.EnumSet;

import com.microsoft.azure.storage.core.SR;

/**
 * Specifies the set of possible permissions for a shared access account policy.
 */
public enum SharedAccessAccountPermissions {
    /**
     * Permission to read resources and list queues and tables granted.
     */
    READ('r'),

    /**
     * Permission to add messages, table entities, and append to blobs granted.
     */
    ADD('a'),

    /**
     * Permission to create blobs and files granted.
     */
    CREATE('c'),

    /**
     * Permission to write resources granted.
     */
    WRITE('w'),

    /**
     * Permission to delete resources granted.
     */
    DELETE('d'),

    /**
     * Permission to list blob containers, blobs, shares, directories, and files granted.
     */
    LIST('l'),

    /**
     * Permissions to update messages and table entities granted.
     */
    UPDATE('u'),

    /**
     * Permission to get and delete messages granted.
     */
    PROCESS_MESSAGES('p');
    
    final private char value;
    
    /**
     * Create a SharedAccessAccountPermissions.
     * 
     * @param c
     *            The char which represents this permission.
     */
    private SharedAccessAccountPermissions(char c) {
        this.value = c;
    }
    
    /**
     * Converts the given permissions to a String.
     * 
     * @param permissions
     *            The permissions to convert to a String.
     *            
     * @return A String which represents the SharedAccessAccountPermissions.
     */
    static String permissionsToString(EnumSet permissions) {
        if (permissions == null) {
            return Constants.EMPTY_STRING;
        }
        
        StringBuilder value = new StringBuilder();
        
        for (SharedAccessAccountPermissions perm : permissions) {
            value.append(perm.value);
        }
        
        return value.toString();
    }
    
    /**
     * Creates an {@link EnumSet} from the specified permissions string.
     * 
     * @param permString
     *            A String which represents the SharedAccessAccountPermissions.
     * @return A {@link EnumSet} generated from the given String.
     */
    static EnumSet permissionsFromString(String permString) {
        EnumSet permissions = EnumSet.noneOf(SharedAccessAccountPermissions.class);

        for (final char c : permString.toLowerCase().toCharArray()) {
            boolean invalidCharacter = true;
            
            for (SharedAccessAccountPermissions perm : SharedAccessAccountPermissions.values()) {
                if (c == perm.value) {
                    permissions.add(perm);
                    invalidCharacter = false;
                    break;
                }
                
            }
            
            if (invalidCharacter) {
                throw new IllegalArgumentException(
                        String.format(SR.ENUM_COULD_NOT_BE_PARSED, "Permissions", permString));
            }
        }
        
        return permissions;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy