// Create end endity profile if neccesary
String endEntityProfileName = "Autoenroll-" + SUPPORTEDCERTIFICATETEMPLATES[templateIndex];
boolean newEndEntityProfile = false;
endEntityProfileSession.removeEndEntityProfile(admin, endEntityProfileName); // TODO: This for debug and really inefficient..
EndEntityProfile endEntityProfile = endEntityProfileSession.getEndEntityProfile(admin, endEntityProfileName);
if (endEntityProfile == null) {
endEntityProfile = new EndEntityProfile(false);
try {
endEntityProfile.setValue(EndEntityProfile.DEFAULTCERTPROFILE, 0, "" + certProfileId);
endEntityProfile.setValue(EndEntityProfile.AVAILCERTPROFILES, 0, "" + certProfileId);
endEntityProfile.setValue(EndEntityProfile.DEFAULTCA, 0, "" + caid);
endEntityProfile.setValue(EndEntityProfile.AVAILCAS, 0, "" + caid);
endEntityProfile.setUse(EndEntityProfile.CLEARTEXTPASSWORD, 0,true);
endEntityProfile.setValue(EndEntityProfile.CLEARTEXTPASSWORD,0,EndEntityProfile.TRUE);
endEntityProfile.removeField(DnComponents.COMMONNAME, 0); // We will add the right number of CNs later
endEntityProfileSession.addEndEntityProfile(admin, endEntityProfileName, endEntityProfile);
newEndEntityProfile = true;
} catch (EndEntityProfileExistsException e) {
throw new EJBException(e); // We just checked for this so this cannot happen
}
}
String[] requiredFields = DNFIELDS[templateIndex];
for (int i=0; i<requiredFields.length; i++) {
if (GET_SUBJECTDN_FROM_AD.equals(requiredFields[i])) {
log.info("Got DN "+ fetchedSubjectDN + " for user " + usernameShort);
if (fetchedSubjectDN == null) {
return -1;
}
}
}
if (newEndEntityProfile) {
for (int i=0; i<requiredFields.length; i++) {
if (GET_SUBJECTDN_FROM_AD.equals(requiredFields[i])) {
DNFieldExtractor dnfe = new DNFieldExtractor(fetchedSubjectDN, DNFieldExtractor.TYPE_SUBJECTDN);
// Loop through all fields in DN
HashMap hmFields = dnfe.getNumberOfFields();
for (int j=0; j<100; j++) { // TODO: 100 is really an internal constant..
Integer fieldsOfType = (Integer) hmFields.get(Integer.valueOf(j));
if (fieldsOfType != null) {
log.info("fieldsOfType="+fieldsOfType);
for (int k = 0; k<fieldsOfType; k++) {
endEntityProfile.addField(DnComponents.dnIdToProfileId(j));
endEntityProfile.setRequired(DnComponents.dnIdToProfileId(j), k, true);
log.info("Added a " + DnComponents.dnIdToProfileId(j) + " field and set it required.");
}
}
}
} else {
int count = 0;
for (int j=0; j<i; j++) {
if (requiredFields[i].equals(requiredFields[j])) {
count++;
}
}
endEntityProfile.addField(requiredFields[i]);
endEntityProfile.setRequired(requiredFields[i], count, true);
}
}
}
endEntityProfileSession.changeEndEntityProfile(admin, endEntityProfileName, endEntityProfile);
return endEntityProfileSession.getEndEntityProfileId(admin, endEntityProfileName);