Create or update role mapping API

edit

Execution

edit

Creating and updating a role mapping can be performed using the security().putRoleMapping() method:

final RoleMapperExpression rules = AnyRoleMapperExpression.builder()
    .addExpression(FieldRoleMapperExpression.ofUsername("*"))
    .addExpression(FieldRoleMapperExpression.ofGroups("cn=admins,dc=example,dc=com"))
    .build();
final PutRoleMappingRequest request = new PutRoleMappingRequest("mapping-example", true,
    Collections.singletonList("superuser"), Collections.emptyList(), rules, null, RefreshPolicy.NONE);
final PutRoleMappingResponse response = client.security().putRoleMapping(request, RequestOptions.DEFAULT);

Response

edit

The returned PutRoleMappingResponse contains a single field, created. This field serves as an indication if a role mapping was created or if an existing entry was updated.

boolean isCreated = response.isCreated(); 

created is a boolean indicating whether the role mapping was created or updated

Asynchronous Execution

edit

This request can be executed asynchronously using the security().putRoleMappingAsync() method:

client.security().putRoleMappingAsync(request, RequestOptions.DEFAULT, listener); 

The PutRoleMappingRequest to execute and the ActionListener to use when the execution completes

The asynchronous method does not block and returns immediately. Once the request has completed the ActionListener is called back using the onResponse method if the execution successfully completed or using the onFailure method if it failed.

A typical listener for a PutRoleMappingResponse looks like:

ActionListener<PutRoleMappingResponse> listener = new ActionListener<PutRoleMappingResponse>() {
    @Override
    public void onResponse(PutRoleMappingResponse response) {
        
    }

    @Override
    public void onFailure(Exception e) {
        
    }
};

Called when the execution is successfully completed. The response is provided as an argument

Called in case of failure. The raised exception is provided as an argument