-
Notifications
You must be signed in to change notification settings - Fork 26
/
SmsCollectionProvider.java
550 lines (510 loc) · 28.5 KB
/
SmsCollectionProvider.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
/*
* The contents of this file are subject to the terms of the Common Development and
* Distribution License (the License). You may not use this file except in compliance with the
* License.
*
* You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
* specific language governing permission and limitations under the License.
*
* When distributing Covered Software, include this CDDL Header Notice in each file and include
* the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
* Header, with the fields enclosed by brackets [] replaced by your own identifying
* information: "Portions copyright [year] [name of copyright owner]".
*
* Copyright 2015-2016 ForgeRock AS.
* Portions Copyrighted 2015 Nomura Research Institute, Ltd.
* Portions Copyrighted 2022-2023 Wren Security
*/
package org.forgerock.openam.core.rest.sms;
import static com.sun.identity.authentication.config.AMAuthenticationManager.getAuthenticationServiceNames;
import static org.forgerock.api.enums.CreateMode.ID_FROM_CLIENT;
import static org.forgerock.api.enums.ParameterSource.PATH;
import static org.forgerock.api.models.Action.action;
import static org.forgerock.api.models.Create.create;
import static org.forgerock.api.models.Delete.delete;
import static org.forgerock.api.models.Items.items;
import static org.forgerock.api.models.Parameter.parameter;
import static org.forgerock.api.models.Query.query;
import static org.forgerock.api.models.Read.read;
import static org.forgerock.api.models.Update.update;
import static org.forgerock.json.JsonValue.array;
import static org.forgerock.json.JsonValue.field;
import static org.forgerock.json.JsonValue.json;
import static org.forgerock.json.JsonValue.object;
import static org.forgerock.json.resource.Responses.newActionResponse;
import static org.forgerock.json.resource.Responses.newQueryResponse;
import static org.forgerock.json.resource.Responses.newResourceResponse;
import static org.forgerock.json.resource.http.HttpUtils.PROTOCOL_VERSION_1;
import static org.forgerock.openam.i18n.apidescriptor.ApiDescriptorConstants.SMS_RESOURCE_PROVIDER;
import static org.forgerock.openam.rest.RestUtils.crestProtocolVersion;
import static org.forgerock.openam.rest.RestUtils.isContractConformantUserProvidedIdCreate;
import static org.forgerock.openam.utils.Time.currentTimeMillis;
import static org.forgerock.util.promise.Promises.newResultPromise;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.regex.Pattern;
import javax.inject.Inject;
import javax.inject.Named;
import org.forgerock.api.annotations.Action;
import org.forgerock.api.annotations.CollectionProvider;
import org.forgerock.api.annotations.Create;
import org.forgerock.api.annotations.Delete;
import org.forgerock.api.annotations.Handler;
import org.forgerock.api.annotations.Operation;
import org.forgerock.api.annotations.Query;
import org.forgerock.api.annotations.Read;
import org.forgerock.api.annotations.Update;
import org.forgerock.api.enums.QueryType;
import org.forgerock.api.models.ApiDescription;
import org.forgerock.api.models.Paths;
import org.forgerock.api.models.Resource;
import org.forgerock.api.models.Schema;
import org.forgerock.api.models.VersionedPath;
import org.wrensecurity.guava.common.base.Optional;
import org.forgerock.http.ApiProducer;
import org.forgerock.http.routing.ApiVersionRouterContext;
import org.forgerock.http.routing.UriRouterContext;
import org.forgerock.http.routing.Version;
import org.forgerock.json.JsonValue;
import org.forgerock.json.resource.ActionRequest;
import org.forgerock.json.resource.ActionResponse;
import org.forgerock.json.resource.BadRequestException;
import org.forgerock.json.resource.ConflictException;
import org.forgerock.json.resource.CreateRequest;
import org.forgerock.json.resource.InternalServerErrorException;
import org.forgerock.json.resource.NotFoundException;
import org.forgerock.json.resource.NotSupportedException;
import org.forgerock.json.resource.PreconditionFailedException;
import org.forgerock.json.resource.QueryRequest;
import org.forgerock.json.resource.QueryResourceHandler;
import org.forgerock.json.resource.QueryResponse;
import org.forgerock.json.resource.Request;
import org.forgerock.json.resource.ResourceException;
import org.forgerock.json.resource.ResourceResponse;
import org.forgerock.json.resource.UpdateRequest;
import org.forgerock.json.resource.http.HttpContext;
import org.forgerock.services.context.Context;
import org.forgerock.util.Function;
import org.forgerock.util.Reject;
import org.forgerock.util.i18n.LocalizableString;
import org.forgerock.util.promise.ExceptionHandler;
import org.forgerock.util.promise.Promise;
import org.forgerock.util.promise.PromiseImpl;
import org.forgerock.util.promise.ResultHandler;
import com.google.inject.assistedinject.Assisted;
import com.iplanet.sso.SSOException;
import com.sun.identity.shared.debug.Debug;
import com.sun.identity.shared.locale.AMResourceBundleCache;
import com.sun.identity.sm.SMSException;
import com.sun.identity.sm.SchemaType;
import com.sun.identity.sm.ServiceAlreadyExistsException;
import com.sun.identity.sm.ServiceConfig;
import com.sun.identity.sm.ServiceConfigManager;
import com.sun.identity.sm.ServiceNotFoundException;
import com.sun.identity.sm.ServiceSchema;
/**
* A CREST collection provider for SMS schema config.
* @since 13.0.0
*/
@CollectionProvider(details = @Handler(mvccSupported = false))
public class SmsCollectionProvider extends SmsResourceProvider {
private static final Collection<Pattern> UNCREATABLE_TYPES =
Collections.singleton(Pattern.compile("services\\/scripting\\/contexts$"));
private static final Collection<Pattern> UNDELETABLE_TYPES =
Collections.singleton(Pattern.compile("services\\/scripting\\/contexts\\/[\\w]+$"));
private static final LocalizableString QUERY_DESCRIPTION =
new LocalizableString(SMS_RESOURCE_PROVIDER + "collection.query.description");
private final boolean autoCreatedAuthModule;
private final String authModuleResourceName;
private final ApiDescription description;
@Inject
SmsCollectionProvider(@Assisted SmsJsonConverter converter, @Assisted ServiceSchema schema,
@Assisted SchemaType type, @Assisted List<ServiceSchema> subSchemaPath, @Assisted String uriPath,
@Assisted boolean serviceHasInstanceName, @Named("frRest") Debug debug,
@Named("AMResourceBundleCache") AMResourceBundleCache resourceBundleCache,
@Named("DefaultLocale") Locale defaultLocale) {
super(schema, type, subSchemaPath, uriPath, serviceHasInstanceName, converter, debug, resourceBundleCache,
defaultLocale);
Reject.ifTrue(type != SchemaType.GLOBAL && type != SchemaType.ORGANIZATION, "Unsupported type: " + type);
Reject.ifTrue(subSchemaPath.isEmpty(), "Root schemas do not support multiple instances");
autoCreatedAuthModule = subSchemaPath.size() == 1 && getAuthenticationServiceNames().contains(serviceName) &&
super.uriPath.size() == 1 && AUTO_CREATED_AUTHENTICATION_MODULES.containsValue(super.uriPath.get(0));
authModuleResourceName = autoCreatedAuthModule ? super.uriPath.get(0) : null;
description = ApiDescription.apiDescription().id("fake").version("v")
.paths(Paths.paths().put("", VersionedPath.versionedPath()
.put(VersionedPath.UNVERSIONED, Resource.resource()
.title(getI18NName())
.description(getSchemaDescription(schema.getI18NKey()))
.mvccSupported(false)
.items(items().pathParameter(parameter().name("id").type("string").source(PATH).build())
.read(read().build())
.update(update().build())
.delete(delete().build())
.create(create().mode(ID_FROM_CLIENT).build())
.build())
.resourceSchema(Schema.schema().schema(
createSchema(Optional.<Context>absent())).build())
.resourceSchema(Schema.schema().schema(createSchema(Optional.<Context>absent())).build())
.query(query().type(QueryType.FILTER)
.description(QUERY_DESCRIPTION).queryableFields().build())
.create(create().mode(ID_FROM_CLIENT).build())
.action(action().name("schema").description(SCHEMA_DESCRIPTION).build())
.action(action().name("template").description(TEMPLATE_DESCRIPTION).build())
.build()).build()
).build()).build();
}
@Action(operationDescription = @Operation, name = "nextdescendents")
public Promise<ActionResponse, ResourceException> nextDescendentsAction(Context context, String resourceId,
ActionRequest request) {
try {
ServiceConfigManager scm = getServiceConfigManager(context);
ServiceConfig config = parentSubConfigFor(context, scm);
ServiceConfig contextConfig = config.getSubConfig(getUriTemplateVariables(context).get("id"));
JsonValue result = json(object(field("result", array())));
for (String subConfigName : contextConfig.getSubConfigNames("*")) {
ServiceConfig subConfig = contextConfig.getSubConfig(subConfigName);
ServiceSchema subSchema = schema.getSubSchema(subConfigName);
JsonValue value = new SmsCollectionProvider(new SmsJsonConverter(subSchema), subSchema,
subSchema.getServiceType(), subSchemaPath, null, false, debug, resourceBundleCache,
defaultLocale).getJsonValue(null, subConfig, context);
result.get("result").add(value.getObject());
}
return newActionResponse(result).asPromise();
} catch (SMSException e) {
debug.warning("::SmsCollectionProvider:: SMSException on nextdescendents action", e);
return new InternalServerErrorException("Unable to perform nextdescendents action SMS config: "
+ e.getMessage()).asPromise();
} catch (SSOException e) {
debug.warning("::SmsCollectionProvider:: SSOException on nextdescendents action", e);
return new InternalServerErrorException("Unable to perform nextdescendents action SMS config: "
+ e.getMessage()).asPromise();
} catch (ResourceException e) {
return e.asPromise();
}
}
@Action(operationDescription = @Operation, name = "getCreatableTypes")
public Promise<ActionResponse, ResourceException> getCreatableTypes(Context context, String actionId, ActionRequest request) {
return newActionResponse(json(array())).asPromise();
}
/**
* Creates a new child instance of config. The parent config referenced by the request path is found, and
* new config is created using the provided name property.
* {@inheritDoc}
*/
@Create(operationDescription = @Operation)
public Promise<ResourceResponse, ResourceException> createInstance(final Context context, CreateRequest request) {
if (matchesPattern(UNCREATABLE_TYPES, context.asContext(UriRouterContext.class).getBaseUri())) {
return new NotSupportedException().asPromise();
}
JsonValue content = request.getContent();
final String realm = realmFor(context);
try {
Map<String, Set<String>> attrs = converter.fromJson(realm, content);
ServiceConfigManager scm = getServiceConfigManager(context);
ServiceConfig config = parentSubConfigFor(context, scm);
String name = content.get("_id").asString();
if (name == null) {
name = request.getNewResourceId();
} else if (request.getNewResourceId() != null && !name.equals(request.getNewResourceId())) {
return new BadRequestException("name and URI's resource ID do not match").asPromise();
}
if (name == null) {
return new BadRequestException("Invalid name").asPromise();
}
config.addSubConfig(name, lastSchemaNodeName(), 0, attrs);
final ServiceConfig created = checkedInstanceSubConfig(context, name, config);
return awaitCreation(context, name)
.then(new Function<Void, ResourceResponse, ResourceException>() {
@Override
public ResourceResponse apply(Void aVoid) {
JsonValue result = null;
try {
result = getJsonValue(realm, created, context, authModuleResourceName,
autoCreatedAuthModule);
} catch (InternalServerErrorException e) {
debug.warning("Error creating JsonValue", e);
}
return newResourceResponse(created.getName(), String.valueOf(result.hashCode()), result);
}
});
} catch (ServiceAlreadyExistsException e) {
debug.warning("::SmsCollectionProvider:: ServiceAlreadyExistsException on create", e);
if (isContractConformantUserProvidedIdCreate(context, request)) {
return new PreconditionFailedException("Unable to create SMS config: " + e.getMessage()).asPromise();
} else {
return new ConflictException("Unable to create SMS config: " + e.getMessage()).asPromise();
}
} catch (SMSException e) {
debug.warning("::SmsCollectionProvider:: SMSException on create", e);
return new InternalServerErrorException("Unable to create SMS config: " + e.getMessage()).asPromise();
} catch (SSOException e) {
debug.warning("::SmsCollectionProvider:: SSOException on create", e);
return new InternalServerErrorException("Unable to create SMS config: " + e.getMessage()).asPromise();
} catch (ResourceException e) {
return e.asPromise();
}
}
private boolean matchesPattern(Collection<Pattern> patterns, String baseUri) {
for (Pattern pattern : patterns) {
if (pattern.matcher(baseUri).find()) {
return true;
}
}
return false;
}
/**
* Deletes a child instance of config. The parent config referenced by the request path is found, and
* the config is deleted using the resourceId.
* {@inheritDoc}
*/
@Delete(operationDescription = @Operation)
public Promise<ResourceResponse, ResourceException> deleteInstance(Context context, final String resourceId) {
if (matchesPattern(UNDELETABLE_TYPES, context.asContext(UriRouterContext.class).getBaseUri())) {
return new NotSupportedException().asPromise();
}
try {
ServiceConfigManager scm = getServiceConfigManager(context);
ServiceConfig config = parentSubConfigFor(context, scm);
checkedInstanceSubConfig(context, resourceId, config);
if (isDefaultCreatedAuthModule(context, resourceId) && config.getSubConfig(resourceId) == null) {
Map<?, ?> attrs = config.getAttributesWithoutDefaults();
if (attrs != null) {
config.removeAttributes(attrs.keySet());
}
} else {
config.removeSubConfig(resourceId);
}
return awaitDeletion(context, resourceId)
.then(new Function<Void, ResourceResponse, ResourceException>() {
@Override
public ResourceResponse apply(Void aVoid) {
return newResourceResponse(resourceId, "0", json(object(field("success", true))));
}
});
} catch (ServiceNotFoundException e) {
debug.warning("::SmsCollectionProvider:: ServiceNotFoundException on delete", e);
return new NotFoundException("Unable to delete SMS config: " + e.getMessage()).asPromise();
} catch (SMSException e) {
debug.warning("::SmsCollectionProvider:: SMSException on delete", e);
return new InternalServerErrorException("Unable to delete SMS config: " + e.getMessage()).asPromise();
} catch (SSOException e) {
debug.warning("::SmsCollectionProvider:: SSOException on delete", e);
return new InternalServerErrorException("Unable to delete SMS config: " + e.getMessage()).asPromise();
} catch (NotFoundException e) {
return e.asPromise();
}
}
/**
* Reads a child instance of config. The parent config referenced by the request path is found, and
* the config is read using the resourceId.
* {@inheritDoc}
*/
@Read(operationDescription = @Operation)
public Promise<ResourceResponse, ResourceException> readInstance(Context context, String resourceId) {
try {
ServiceConfigManager scm = getServiceConfigManager(context);
ServiceConfig config = parentSubConfigFor(context, scm);
ServiceConfig item = checkedInstanceSubConfig(context, resourceId, config);
JsonValue result = getJsonValue(realmFor(context), item, context, authModuleResourceName,
autoCreatedAuthModule);
return newResultPromise(newResourceResponse(resourceId, String.valueOf(result.hashCode()), result));
} catch (SMSException e) {
debug.warning("::SmsCollectionProvider:: SMSException on read", e);
return new InternalServerErrorException("Unable to read SMS config: " + e.getMessage()).asPromise();
} catch (SSOException | InternalServerErrorException e) {
debug.warning("::SmsCollectionProvider:: SSOException on read", e);
return new InternalServerErrorException("Unable to read SMS config: " + e.getMessage()).asPromise();
} catch (NotFoundException e) {
return e.asPromise();
}
}
/**
* Updates a child instance of config. The parent config referenced by the request path is found, and
* the config is updated using the resourceId.
* {@inheritDoc}
*/
@Update(operationDescription = @Operation)
public Promise<ResourceResponse, ResourceException> updateInstance(Context context, String resourceId,
UpdateRequest request) {
JsonValue content = request.getContent();
String realm = realmFor(context);
try {
Map<String, Set<String>> attrs = converter.fromJson(realm, content);
ServiceConfigManager scm = getServiceConfigManager(context);
ServiceConfig config = parentSubConfigFor(context, scm);
ServiceConfig node = checkedInstanceSubConfig(context, resourceId, config);
node.setAttributes(attrs);
JsonValue result = getJsonValue(realm, node, context, authModuleResourceName, autoCreatedAuthModule);
return newResultPromise(newResourceResponse(resourceId, String.valueOf(result.hashCode()), result));
} catch (ServiceNotFoundException e) {
debug.warning("::SmsCollectionProvider:: ServiceNotFoundException on update", e);
return new NotFoundException("Unable to update SMS config: " + e.getMessage()).asPromise();
} catch (SMSException e) {
debug.warning("::SmsCollectionProvider:: SMSException on update", e);
return new InternalServerErrorException("Unable to update SMS config: " + e.getMessage()).asPromise();
} catch (SSOException e) {
debug.warning("::SmsCollectionProvider:: SSOException on update", e);
return new InternalServerErrorException("Unable to update SMS config: " + e.getMessage()).asPromise();
} catch (ResourceException e) {
return e.asPromise();
}
}
/**
* Queries for child instances of config. The parent config referenced by the request path is found, and
* all child config for the type is returned.
* <p>
* Note that only query filter is supported, and only a filter of value {@code true} (i.e. all values).
* Sorting and paging are not supported.
* {@inheritDoc}
*/
@Query(operationDescription = @Operation, type = QueryType.FILTER, queryableFields = "*")
public Promise<QueryResponse, ResourceException> queryCollection(Context context, QueryRequest request,
QueryResourceHandler handler) {
if (!"true".equals(request.getQueryFilter().toString())) {
return new NotSupportedException("Query not supported: " + request.getQueryFilter()).asPromise();
}
if (request.getPagedResultsCookie() != null || request.getPagedResultsOffset() > 0 ||
request.getPageSize() > 0) {
return new NotSupportedException("Query paging not currently supported").asPromise();
}
try {
ServiceConfigManager scm = getServiceConfigManager(context);
String realm = realmFor(context);
if (subSchemaPath.isEmpty()) {
Set<String> instanceNames = new TreeSet<String>(scm.getInstanceNames());
for (String instanceName : instanceNames) {
ServiceConfig config = type == SchemaType.GLOBAL ? scm.getGlobalConfig(instanceName) :
scm.getOrganizationConfig(realm, instanceName);
if (config != null) {
handleServiceConfig(handler, realm, instanceName, config, context);
}
}
} else {
ServiceConfig config = parentSubConfigFor(context, scm);
Set<String> names = config.getSubConfigNames("*", lastSchemaNodeName());
for (String configName : names) {
ServiceConfig subConfig = config.getSubConfig(configName);
handleServiceConfig(handler, realm, configName, subConfig, context);
}
if (autoCreatedAuthModule) {
String instanceName = AUTO_CREATED_AUTHENTICATION_MODULES.inverse().get(authModuleResourceName);
handleServiceConfig(handler, realm, instanceName, config, context);
}
}
return newResultPromise(newQueryResponse());
} catch (SMSException e) {
debug.warning("::SmsCollectionProvider:: SMSException on query", e);
return new InternalServerErrorException("Unable to query SMS config: " + e.getMessage()).asPromise();
} catch (SSOException | InternalServerErrorException e) {
debug.warning("::SmsCollectionProvider:: SSOException on query", e);
return new InternalServerErrorException("Unable to query SMS config: " + e.getMessage()).asPromise();
} catch (NotFoundException e) {
return e.asPromise();
}
}
private void handleServiceConfig(QueryResourceHandler handler, String realm, String configName, ServiceConfig
subConfig, Context context) throws InternalServerErrorException {
JsonValue value = getJsonValue(realm, subConfig, context, authModuleResourceName, autoCreatedAuthModule);
handler.handleResource(newResourceResponse(configName, String.valueOf(value.hashCode()), value));
}
private static final long MAX_AWAIT_TIMEOUT = 5000L;
private Promise<Void, ResourceException> awaitCreation(Context context, String resourceId) {
final PromiseImpl<Void, ResourceException> awaitPromise = PromiseImpl.create();
await(context, resourceId, awaitPromise, currentTimeMillis(), awaitCreationResultHandler(awaitPromise),
awaitCreationExceptionHandler(context, resourceId, awaitPromise, currentTimeMillis()));
return awaitPromise;
}
private Promise<Void, ResourceException> awaitDeletion(Context context, String resourceId) {
final PromiseImpl<Void, ResourceException> awaitPromise = PromiseImpl.create();
await(context, resourceId, awaitPromise, currentTimeMillis(),
awaitDeletionResultHandler(context, resourceId, awaitPromise, currentTimeMillis()),
awaitDeletionExceptionHandler(context, resourceId, awaitPromise, currentTimeMillis()));
return awaitPromise;
}
private void await(Context context, String resourceId, PromiseImpl<Void, ResourceException> awaitPromise,
long startTime, ResultHandler<ResourceResponse> resultHandler,
ExceptionHandler<ResourceException> exceptionHandler) {
if (currentTimeMillis() - startTime > MAX_AWAIT_TIMEOUT) {
awaitPromise.handleResult(null);
return;
}
try {
Thread.sleep(100L);
} catch (InterruptedException e) {
debug.error("Thread interrupted while awaiting SMS resource creation/deletion", e);
awaitPromise.handleException(new InternalServerErrorException("", e));
}
readInstance(context, resourceId)
.thenOnResult(resultHandler)
.thenOnException(exceptionHandler);
}
private ResultHandler<ResourceResponse> awaitCreationResultHandler(
final PromiseImpl<Void, ResourceException> awaitPromise) {
return new ResultHandler<ResourceResponse>() {
@Override
public void handleResult(ResourceResponse result) {
awaitPromise.handleResult(null);
}
};
}
private ExceptionHandler<ResourceException> awaitCreationExceptionHandler(final Context context,
final String resourceId, final PromiseImpl<Void, ResourceException> awaitPromise, final long startTime) {
return new ExceptionHandler<ResourceException>() {
@Override
public void handleException(ResourceException exception) {
if (ResourceException.NOT_FOUND != exception.getCode()) {
debug.warning("Unexpected exception returned while awaiting SMS resource creation", exception);
}
await(context, resourceId, awaitPromise, startTime,
awaitCreationResultHandler(awaitPromise),
awaitCreationExceptionHandler(context, resourceId, awaitPromise, startTime));
}
};
}
private ResultHandler<ResourceResponse> awaitDeletionResultHandler(final Context context, final String resourceId,
final PromiseImpl<Void, ResourceException> awaitPromise, final long startTime) {
return new ResultHandler<ResourceResponse>() {
@Override
public void handleResult(ResourceResponse result) {
await(context, resourceId, awaitPromise, startTime,
awaitDeletionResultHandler(context, resourceId, awaitPromise, startTime),
awaitDeletionExceptionHandler(context, resourceId, awaitPromise, startTime));
}
};
}
private ExceptionHandler<ResourceException> awaitDeletionExceptionHandler(final Context context,
final String resourceId, final PromiseImpl<Void, ResourceException> awaitPromise, final long startTime) {
return new ExceptionHandler<ResourceException>() {
@Override
public void handleException(ResourceException exception) {
if (ResourceException.NOT_FOUND != exception.getCode()) {
debug.warning("Unexpected exception returned while awaiting SMS resource deletion", exception);
await(context, resourceId, awaitPromise, startTime,
awaitDeletionResultHandler(context, resourceId, awaitPromise, startTime),
awaitDeletionExceptionHandler(context, resourceId, awaitPromise, startTime));
} else {
awaitPromise.handleResult(null);
}
}
};
}
@Override
public ApiDescription api(ApiProducer<ApiDescription> apiProducer) {
return description;
}
@Override
public ApiDescription handleApiRequest(Context context, Request request) {
return description;
}
@Override
public void addDescriptorListener(Listener listener) {
}
@Override
public void removeDescriptorListener(Listener listener) {
}
}