Utilize SAP Composition Environment set of APIs, particularly Security API
The com.sap.security.api
namespace contains IUserAccountFactory
factory has getUserAccount(s) method which returns IUserAccount object (array of objects). It has getRoles method which perfectly meets your needs.
Together with addToRole method you can implement export/import of roles.
You can try to serialize roles of specific user with this sample:
import java.io.*;
import com.sap.security.api.IUser;
import com.sap.security.api.UMFactory;
// getting current user
try {
IUser user = UMFactory.getAuthenticator().getLoggedInUser();
if (user == null) { throw new SecurityException("User is invalid"); }
// getting user roles
Iterator<String> roles = loggedInUser.getRoles(true);
// serializing roles
while (roles.hasNext()) {
String roleId = roles.next();
IRole role = UMFactory.getRoleFactory().getRole(roleId);
FileOutputStream file = new FileOutputStream(filename);
ObjectOutputStream out = new ObjectOutputStream(file);
out.writeObject(role);
out.close();
file.close();
}
} catch (Exception e) {
logger.logError("[getAssignedRole] Error " + e.getMessage());
}