/
AccountSASPermission.java
252 lines (212 loc) · 6.66 KB
/
AccountSASPermission.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.microsoft.azure.storage.blob;
import java.util.Locale;
/**
* This is a helper class to construct a string representing the permissions granted by an AccountSAS. Setting a value
* to true means that any SAS which uses these permissions will grant permissions for that operation. Once all the
* values are set, this should be serialized with toString and set as the permissions field on an
* {@link AccountSASSignatureValues} object. It is possible to construct the permissions string without this class, but
* the order of the permissions is particular and this class guarantees correctness.
*/
public final class AccountSASPermission {
private boolean read;
private boolean add;
private boolean create;
private boolean write;
private boolean delete;
private boolean list;
private boolean update;
private boolean processMessages;
/**
* Initializes an {@code AccountSASPermission} object with all fields set to false.
*/
public AccountSASPermission() {
}
/**
* Creates an {@code AccountSASPermission} from the specified permissions string. This method will throw an
* {@code IllegalArgumentException} if it encounters a character that does not correspond to a valid permission.
*
* @param permString
* A {@code String} which represents the {@code SharedAccessAccountPermissions}.
*
* @return An {@code AccountSASPermission} object generated from the given {@code String}.
*/
public static AccountSASPermission parse(String permString) {
AccountSASPermission permissions = new AccountSASPermission();
for (int i = 0; i < permString.length(); i++) {
char c = permString.charAt(i);
switch (c) {
case 'r':
permissions.read = true;
break;
case 'w':
permissions.write = true;
break;
case 'd':
permissions.delete = true;
break;
case 'l':
permissions.list = true;
break;
case 'a':
permissions.add = true;
break;
case 'c':
permissions.create = true;
break;
case 'u':
permissions.update = true;
break;
case 'p':
permissions.processMessages = true;
break;
default:
throw new IllegalArgumentException(
String.format(Locale.ROOT, SR.ENUM_COULD_NOT_BE_PARSED_INVALID_VALUE, "Permissions", permString, c));
}
}
return permissions;
}
/**
* Permission to read resources and list queues and tables granted.
*/
public boolean read() {
return read;
}
/**
* Permission to read resources and list queues and tables granted.
*/
public AccountSASPermission withRead(boolean read) {
this.read = read;
return this;
}
/**
* Permission to add messages, table entities, and append to blobs granted.
*/
public boolean add() {
return add;
}
/**
* Permission to add messages, table entities, and append to blobs granted.
*/
public AccountSASPermission withAdd(boolean add) {
this.add = add;
return this;
}
/**
* Permission to create blobs and files granted.
*/
public boolean create() {
return create;
}
/**
* Permission to create blobs and files granted.
*/
public AccountSASPermission withCreate(boolean create) {
this.create = create;
return this;
}
/**
* Permission to write resources granted.
*/
public boolean write() {
return write;
}
/**
* Permission to write resources granted.
*/
public AccountSASPermission withWrite(boolean write) {
this.write = write;
return this;
}
/**
* Permission to delete resources granted.
*/
public boolean delete() {
return delete;
}
/**
* Permission to delete resources granted.
*/
public AccountSASPermission withDelete(boolean delete) {
this.delete = delete;
return this;
}
/**
* Permission to list blob containers, blobs, shares, directories, and files granted.
*/
public boolean list() {
return list;
}
/**
* Permission to list blob containers, blobs, shares, directories, and files granted.
*/
public AccountSASPermission withList(boolean list) {
this.list = list;
return this;
}
/**
* Permissions to update messages and table entities granted.
*/
public boolean update() {
return update;
}
/**
* Permissions to update messages and table entities granted.
*/
public AccountSASPermission withUpdate(boolean update) {
this.update = update;
return this;
}
/**
* Permission to get and delete messages granted.
*/
public boolean processMessages() {
return processMessages;
}
/**
* Permission to get and delete messages granted.
*/
public AccountSASPermission withProcessMessages(boolean processMessages) {
this.processMessages = processMessages;
return this;
}
/**
* Converts the given permissions to a {@code String}. Using this method will guarantee the permissions are in an
* order accepted by the service.
*
* @return A {@code String} which represents the {@code AccountSASPermissions}.
*/
@Override
public String toString() {
// The order of the characters should be as specified here to ensure correctness:
// https://docs.microsoft.com/en-us/rest/api/storageservices/constructing-an-account-sas
final StringBuilder builder = new StringBuilder();
if (this.read) {
builder.append('r');
}
if (this.write) {
builder.append('w');
}
if (this.delete) {
builder.append('d');
}
if (this.list) {
builder.append('l');
}
if (this.add) {
builder.append('a');
}
if (this.create) {
builder.append('c');
}
if (this.update) {
builder.append('u');
}
if (this.processMessages) {
builder.append('p');
}
return builder.toString();
}
}