Expand my Community achievements bar.

SOLVED

Need Java code sample to generate 'NetCentric' compatible YAML file in AEM to create ACL groups.

Avatar

Level 1

I'm working on java code to create YAML file (nested) to create ACL groups in AEM. For that i'm using JACKSON API.
I'm able to generate below format, but that is not working with AEM 'NetCentric'.

- group_config:
- sample-content-field-ops-abc:
name: Sample Content DA Field Operations ABC
membersOf: "everyone, sample-content-base-group, sample-content-field-ops-base"
members: ""
path: /home/groups/sample-authoring
- ace_config:
- sample-content-field-ops-abc:
- permission: allow
actions: read
privileges: jcr:read
repGlob: ""
.
.
.

The Error I'm getting when try to run this is AEM is-
"**e=java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to java.util.List**"
I'm using POJO class & Collections to generate this YAML file.
I thought, this is a 'Indentation' issue so tried below code to increase indentation

DefaultPrettyPrinter printer = new DefaultPrettyPrinter().withObjectIndenter(new DefaultIndenter(" ", "\\n"));
// ObjectMapper is instantiated just like before
ObjectMapper om = new ObjectMapper(yamlFactory);
om.enable(SerializationFeature.INDENT_OUTPUT);
// We write the `Config/Groups` into `yaml`
om.writer(printer).writeValue(new File(yAMLFileLocation), collectionObj);

Still no change in out come.

Can anyone help me? to identify what am I missing.

Java code I'm using-


@component(service = Servlet.class,
property = {
"sling.servlet.extensions=" + "json",
"sling.servlet.methods=" + HttpConstants.METHOD_GET,
"sling.servlet.resourceTypes=" + "mds/createyaml"})
public class CreateYAMLServlet extends SlingAllMethodsServlet {

private static final long serialVersionUID = 1869817785940488773L;

@reference
transient ResourceResolverFactory resourceResolverFactory;
private static final Logger LOG = LoggerFactory.getLogger(CreateYAMLServlet.class);

/**
* This method is overridden from SlingAllMethodsServlet to post the form data
* to external systems
*
* @Param request
* @Param response
*/
@Override
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response)
throws IOException {
doGet(request, response);
}

/**
* This method is overridden from SlingAllMethodsServlet to post the form data
* to external systems
*
* @Param request
* @Param response
*/
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)
throws IOException {
String yAMLFileLocation = "C:\\Users\\sample\\Documents\\sample\\YAML\\GeneratedYAML.yaml";
JsonObject jsonObject = new JsonObject();
JsonObject resultJson = new JsonObject();
ResourceResolver resourceResolver = null;
String csvFilePath = StringUtils.EMPTY;
//Members of List
String membersOf = "everyone, sample-content-base-group, sample-content-field-ops-base";
//Members List
String members = StringUtils.EMPTY;
String DA_GROUP_PATH = "/home/groups/distributed-authoring";
String groupNameLabel = "sample Content DA Field Operations ";
String groupName = "sample-content-field-ops-";
String GROUP_CONFIG = "group_config";
String CONTENT_HWS_LM_ROOT = "/content/sample-hws/language-masters/";
String CONTENT_HWS_ROOT = "/content/sample-hws/";
String CONTENT_DAM_sample_MDS = "/content/dam/sample-mds/";
String CONTENT_DAM_sample_DIGITAL = "/content/dam/sample-digital/";
List<Map<String, GroupConfig>> groupNameDetailsMapsList = new ArrayList<>();
Map<String, GroupConfig> groupNameConfigMap = null;
List<ACEGroup> aceGroupList = null;
List<Map<String, List<ACEGroup>>> aceGroupPermissionsList = new ArrayList<>();
Map<String, List<ACEGroup>> aCEGroupsMap = null;
try {
RequestParameterMap requestParameterMap = request.getRequestParameterMap();
//Get CSV file path.
if (requestParameterMap.containsKey("csvFilePath")) {
csvFilePath = requestParameterMap.getValue("csvFilePath").getString();
}
if (StringUtils.isNotEmpty(csvFilePath)) {
BufferedReader br = new BufferedReader(new FileReader(csvFilePath));
//Read the file line by line
String line = "";
while ((line = br.readLine()) != null) {
//Get all tokens available in line
String[] tokens = line.split(",");
LOG.info(Arrays.toString(tokens));
//Create Config details.
String marshaCode = tokens[0];
String brandCode = tokens[1];
String hotelPath = tokens[2];

GroupConfig gc = new GroupConfig();
gc.setName(groupNameLabel + "" + marshaCode);
gc.setMembersOf(membersOf);
gc.setMembers(members);
gc.setPath(DA_GROUP_PATH);

groupNameConfigMap = new HashMap();
groupNameConfigMap.put(groupName + marshaCode.toLowerCase(), gc);
groupNameDetailsMapsList.add(groupNameConfigMap);

//ACE groups
aceGroupList = new ArrayList<>();
String contentPathOne = CONTENT_HWS_LM_ROOT + "*/*/hotels/" + hotelPath.charAt(0);
ACEGroup aceGroupOne = createACEGroup(contentPathOne, "allow", "read", "jcr:read", "");

String contentPathTwo = CONTENT_HWS_LM_ROOT + "*/*/hotels/" + hotelPath.charAt(0);
ACEGroup aceGroupTwo = createACEGroup(contentPathTwo, "allow", "read", "jcr:read", "/jcr:*");

String contentPathThree = CONTENT_HWS_LM_ROOT + "*/*/hotels/" + hotelPath;
ACEGroup aceGroupThree = createACEGroup(contentPathThree, "allow", "read, create, modify", "", null);

String contentPathFour = CONTENT_HWS_ROOT + "*/*/hotels/" + hotelPath.charAt(0);
ACEGroup aceGroupFour = createACEGroup(contentPathFour, "allow", "read", "jcr:read", "");

String contentPathFive = CONTENT_HWS_ROOT + "*/*/hotels/" + hotelPath.charAt(0);
ACEGroup aceGroupFive = createACEGroup(contentPathFive, "allow", "", "jcr:read", "/jcr:*");

String contentPathSix = CONTENT_HWS_LM_ROOT + "*/*/hotels/" + hotelPath;
ACEGroup aceGroupSix = createACEGroup(contentPathSix, "allow", "read, create, modify", "", null);

String contentPathSeven = CONTENT_DAM_sample_MDS + marshaCode.toLowerCase();
ACEGroup aceGroupSeven = createACEGroup(contentPathSeven, "allow", "read", "", null);

String contentPathEight = CONTENT_DAM_sample_DIGITAL + brandCode;
ACEGroup aceGroupEight = createACEGroup(contentPathEight, "allow", "", "jcr:read", "");

String contentPathNine = CONTENT_DAM_sample_DIGITAL + brandCode;
ACEGroup aceGroupNine = createACEGroup(contentPathNine, "allow", "", "jcr:read", "/jcr:*");

String contentPathTen = CONTENT_DAM_sample_DIGITAL + brandCode + "/*";
ACEGroup aceGroupTen = createACEGroup(contentPathTen, "allow", "read", "jcr:read", "");

String contentPathEleven = CONTENT_DAM_sample_DIGITAL + brandCode + "/*";
ACEGroup aceGroupEleven = createACEGroup(contentPathEleven, "allow", "read", "jcr:read", "/jcr:*");

String contentPathTwelve = CONTENT_DAM_sample_DIGITAL + brandCode + "/*/*";
ACEGroup aceGroupTwelve = createACEGroup(contentPathTwelve, "allow", "read", "jcr:read", "");

String contentPathThirteen = CONTENT_DAM_sample_DIGITAL + brandCode + "/*/*";
ACEGroup aceGroupThirteen = createACEGroup(contentPathThirteen, "allow", "read", "jcr:read", "/jcr:*");

String contentPathFourteen = CONTENT_DAM_sample_DIGITAL + brandCode + "/*/*/" + hotelPath.charAt(0);
ACEGroup aceGroupFourteen = createACEGroup(contentPathFourteen, "allow", "read", "jcr:read", "");

String contentPathFifteen = CONTENT_DAM_sample_DIGITAL + brandCode + "/*/*/" + hotelPath.charAt(0);
ACEGroup aceGroupFifteen = createACEGroup(contentPathFifteen, "allow", "read", "jcr:read", "/jcr:*");

String contentPathSixteen = CONTENT_DAM_sample_DIGITAL + brandCode + "/*/hws/" + hotelPath.charAt(0) + "/" + marshaCode.toLowerCase();
ACEGroup aceGroupSixteen = createACEGroup(contentPathSixteen, "allow", "read", "", null);

aceGroupList.add(aceGroupOne);
aceGroupList.add(aceGroupTwo);
aceGroupList.add(aceGroupThree);
aceGroupList.add(aceGroupFour);
aceGroupList.add(aceGroupFive);
aceGroupList.add(aceGroupSix);
aceGroupList.add(aceGroupSeven);
aceGroupList.add(aceGroupEight);
aceGroupList.add(aceGroupNine);
aceGroupList.add(aceGroupTen);
aceGroupList.add(aceGroupEleven);
aceGroupList.add(aceGroupTwelve);
aceGroupList.add(aceGroupThirteen);
aceGroupList.add(aceGroupFourteen);
aceGroupList.add(aceGroupFifteen);
aceGroupList.add(aceGroupSixteen);
aCEGroupsMap = new HashMap();
aCEGroupsMap.put(groupName + marshaCode.toLowerCase(), aceGroupList);
aceGroupPermissionsList.add(aCEGroupsMap);

}
}

Map<String, List<Map<String, GroupConfig>>> groupConfigMap = new HashMap();
Map<String, List<Map<String, List<ACEGroup>>>> groupConfigMapOne = new HashMap();
groupConfigMap.put("group_config", groupNameDetailsMapsList);
groupConfigMapOne.put("ace_config", aceGroupPermissionsList);

List toplevelList = new ArrayList();
toplevelList.add(groupConfigMap);
toplevelList.add(groupConfigMapOne);


YAMLFactory yamlFactory = new YAMLFactory();
YAMLGenerator.Feature ygf = YAMLGenerator.Feature.valueOf("WRITE_DOC_START_MARKER");
YAMLGenerator.Feature ygf1 = YAMLGenerator.Feature.valueOf("MINIMIZE_QUOTES");
yamlFactory.enable(ygf1);
yamlFactory.disable(ygf);

// ObjectMapper is instantiated just like before
ObjectMapper om = new ObjectMapper(yamlFactory);
// We write the `Config/Groups` into `yaml`
om.writeValue(new File(yAMLFileLocation), toplevelList);

/* jsonObject.addProperty(AmazonConstants.RESULT, AmazonConstants.SUCCESS);
resultJson.add("resultJson", jsonObject);
//
response.setContentType("text/html;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
response.setHeader("Pragma", "no-cache");
response.getWriter().write(resultJson.toString());*/
} catch (JsonIOException e) {
LOG.error("JsonIOException in CreateYAMLServlet.", e);
} finally {
if (resourceResolver != null && resourceResolver.isLive()) {
resourceResolver.close();
}
}
}

private ACEGroup createACEGroup(String path, String permission, String actions, String privileges, String repGlob) {
ACEGroup aceGroup = new ACEGroup();
aceGroup.setPath(path);
aceGroup.setPermission(permission);
aceGroup.setActions(actions);
aceGroup.setPrivileges(privileges);
aceGroup.setRepGlob(repGlob);
return aceGroup;
}

private class GroupConfig {

String name = StringUtils.EMPTY;
String membersOf;
String members;
String path = StringUtils.EMPTY;

public GroupConfig() {

}

public void setName(String name) {
this.name = name;
}

public String getName() {
return name;
}

public String getMembersOf() {
return membersOf;
}

public void setMembersOf(String membersOf) {
this.membersOf = membersOf;
}

public String getMembers() {
return members;
}

public void setMembers(String members) {
this.members = members;
}

public String getPath() {
return path;
}

public void setPath(String path) {
this.path = path;
}
}

@JsonInclude(JsonInclude.Include.NON_NULL)
private class ACEGroup {
String permission = StringUtils.EMPTY;
String actions = StringUtils.EMPTY;
String privileges = StringUtils.EMPTY;
String repGlob = StringUtils.EMPTY;
String path = StringUtils.EMPTY;

public String getPermission() {
return permission;
}

public void setPermission(String permission) {
this.permission = permission;
}

public String getActions() {
return actions;
}

public void setActions(String actions) {
this.actions = actions;
}

public String getPrivileges() {
return privileges;
}

public void setPrivileges(String privileges) {
this.privileges = privileges;
}

public String getRepGlob() {
return repGlob;
}

public void setRepGlob(String repGlob) {
this.repGlob = repGlob;
}

public String getPath() {
return path;
}

public void setPath(String path) {
this.path = path;
}
}
}

 

Thanks,
Raju

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi,

You need to follow the proper spacing, example

 

- group_config:
    - sample-content-field-ops-abc: null
      name: Sample Content DA Field Operations ABC
      membersOf: 'everyone, sample-content-base-group, sample-content-field-ops-base'
      members: ''
      path: /home/groups/sample-authoring
- ace_config:
    - sample-content-field-ops-abc:
        - permission: allow
          actions: read
          privileges: 'jcr:read'
          repGlob: ''


Arun Patidar

View solution in original post

1 Reply

Avatar

Correct answer by
Community Advisor

Hi,

You need to follow the proper spacing, example

 

- group_config:
    - sample-content-field-ops-abc: null
      name: Sample Content DA Field Operations ABC
      membersOf: 'everyone, sample-content-base-group, sample-content-field-ops-base'
      members: ''
      path: /home/groups/sample-authoring
- ace_config:
    - sample-content-field-ops-abc:
        - permission: allow
          actions: read
          privileges: 'jcr:read'
          repGlob: ''


Arun Patidar