Document inDoc = null;
try { inDoc = initializeInput(messageNumber, aMessage); }
catch (JMSException jmse) {
// An error occurred creating the input document from the JMS message
// passed in. Log it and publish a sync error.
Error error = new Error();
error.setErrorNumber("OpenEAI-2101");
error.setErrorDescription("An error occurred creating the input " +
"document from the JMS message passed in. The exception is: " +
jmse.getMessage());
logger.fatal("[PasswordSyncCommand.execute]" +
error.getErrorDescription());
return;
}
// Get the message metadata.
Element eControlArea = getControlArea(inDoc.getRootElement());
String messageCategory = eControlArea.getAttribute("messageCategory")
.getValue();
String messageObject = eControlArea.getAttribute("messageObject")
.getValue();
String messageRelease = eControlArea.getAttribute("messageRelease")
.getValue();
String messageAction = eControlArea.getAttribute("messageAction")
.getValue();
String messageType = eControlArea.getAttribute("messageType").getValue();
logger.info("[PasswordSyncCommand.execute] Received a(n) " + messageCategory
+ "." + messageObject + "." + messageAction + "-" + messageType +
" (release " + messageRelease + ") message.");
// Verify that this is a message we support.
ArrayList errors = new ArrayList();
if (messageObject.equalsIgnoreCase("EnterpriseUserPassword") == false) {
Error error = new Error();
error.setType("application");
error.setErrorNumber("OpenEAI-1002");
error.setErrorDescription("Message object '" + messageObject +
"' is not supported.");
errors.add(error);
}
if (messageType.equalsIgnoreCase("Sync") == false) {
Error error = new Error();
error.setType("application");
error.setErrorNumber("OpenEAI-1002");
error.setErrorDescription("Message type '" + messageType + "' is not " +
"supported.");
errors.add(errors);
}
if (errors.size() > 0) {
ListIterator iterator = errors.listIterator();
while (iterator.hasNext()) {
Error error = (Error)iterator.next();
logger.fatal("[PasswordSyncCommand.execute] " +
error.getErrorDescription());
}
publishSyncError(eControlArea, errors);
return;
}
// Get two configured EnterpriseUserPassword from AppConfig.
EnterpriseUserPassword currentEntUserPassword = null;
EnterpriseUserPassword newEntUserPassword = null;
try {
currentEntUserPassword = (EnterpriseUserPassword)getAppConfig()
.getObject("EnterpriseUserPassword");
newEntUserPassword = (EnterpriseUserPassword)getAppConfig()
.getObject("EnterpriseUserPassword");
}
catch (EnterpriseConfigurationObjectException ecoe) {
// An error occurred getting an object from AppConfig. Log it and
// publish a sync error message.
Error error = new Error();
error.setType("application");
error.setErrorNumber("OpenEAI-3001");
error.setErrorDescription("An error occurred getting an object from " +
"AppConfig. the exception is: " + ecoe.getMessage());
errors = new ArrayList();
errors.add(error);
publishSyncError(eControlArea, errors, ecoe);
return;
}
// Handle an EnterpriseUserPassword.CreateSync
if (messageAction.equalsIgnoreCase("Create")) {
// Get the new state of the EnterpriseUserPassword and build an
// EnterpriseUserPassword object.
Element eNewPassword = inDoc.getRootElement().getChild("DataArea")
.getChild("NewData").getChild("EnterpriseUserPassword");
try {
newEntUserPassword.buildObjectFromInput(eNewPassword);
}
catch (EnterpriseLayoutException ele) {
// An error occurred building the EnterpriseUserPassword object from the
// EnterpriseUserPassword element contained in the NewData element of
// the message. Log it and publish a sync error message.
Error error = new Error();
error.setType("system");
error.setErrorNumber("SakaiGateway-1002");
error.setErrorDescription("An error occurred building the " +
"EnterpriseUserPassword object from the EnterpriseUserPassword " +
"element contained in the NewData element of the message. The " +
"exception is: " + ele.getMessage());
errors = new ArrayList();
errors.add(error);
publishSyncError(eControlArea, errors, ele);
return;
}
// Determine whether the user exists in the database.
String userId = getUserId(newEntUserPassword);
String instId = newEntUserPassword.getEnterpriseUser()
.getLightweightPerson().getInstitutionalId();
BasicPerson bp=null;
try {
logger.info("[PasswordSyncCommand.execute] Querying for BasicPerson.");
bp = getBasicPerson(instId);
logger.info("[PasswordSyncCommand.execute] Queried for BasicPerson.");
} catch (EnterpriseConfigurationObjectException e) {
// Log it, publish a error message, and return.
String errMsg = "EnterpriseConfigurationObjectException: " + e.getMessage();
logger.error("[PasswordSyncCommand.execute] " + errMsg);
Error error = new Error();
error.setType("system");
error.setErrorNumber("SakaiGateway-1014");
error.setErrorDescription(errMsg);
errors = new ArrayList();
errors.add(error);
logger.info("[PasswordSyncCommand.execute] Publishing Sync.Error-Sync.");
publishSyncError(eControlArea, errors);
return;
} catch (EnterpriseFieldException e) {
// Log it, publish a error message, and return.
String errMsg = "EnterpriseFieldException: " + e.getMessage();
logger.error("[PasswordSyncCommand.execute] " + errMsg);
Error error = new Error();
error.setType("system");
error.setErrorNumber("SakaiGateway-1014");
error.setErrorDescription(errMsg);
errors = new ArrayList();
errors.add(error);
logger.info("[PasswordSyncCommand.execute] Publishing Sync.Error-Sync.");
publishSyncError(eControlArea, errors);
return;
} catch (EnterpriseObjectQueryException e) {
// Log it, publish a error message, and return.
String errMsg = "EnterpriseObjectQueryException: " + e.getMessage();
logger.error("[PasswordSyncCommand.execute] " + errMsg);
Error error = new Error();
error.setType("system");
error.setErrorNumber("SakaiGateway-1014");
error.setErrorDescription(errMsg);
errors = new ArrayList();
errors.add(error);
logger.info("[PasswordSyncCommand.execute] Publishing Sync.Error-Sync.");
publishSyncError(eControlArea, errors);
return;
}
if (bp==null) {
// No BasicPerson exists for this instId.
// Log it, publish a error message, and return.
String errMsg = "No BasicPerson exists for " + instId;
logger.debug("[PasswordSyncCommand.execute] " + errMsg);
Error error = new Error();
error.setType("application");
error.setErrorNumber("SakaiGateway-1012");
error.setErrorDescription(errMsg);
errors = new ArrayList();
errors.add(error);
publishSyncError(eControlArea, errors);
return;
}
PortalPerson portalPerson = null;
try { portalPerson = retrievePortalPersonByUserId(userId); }
catch (CommandException ce) {
// An error occurred querying the portal database to determine whether
// the user exists. Log it, publish a error message, and return.
String errMsg = "An error occurred querying the portal database to " +
"determine whether the user exists.";
logger.debug("[PasswordSyncCommand.execute] " + errMsg);
Error error = new Error();
error.setType("system");
error.setErrorNumber("SakaiGateway-2001");
error.setErrorDescription(errMsg);
errors = new ArrayList();
errors.add(error);
publishSyncError(eControlArea, errors, ce);
return;
}
// If the user does exist, publish a error message indicating
// that the user already exists.
if (portalPerson != null) {
Error error = new Error();
error.setType("application");
error.setErrorNumber("SakaiGateway-1011");
error.setErrorDescription("User with user ID " + userId +
") exists in the portal. Cannot reset the password for this user.");
logger.fatal("[PasswordSyncCommand.execute] " +
error.getErrorDescription());
errors = new ArrayList();
errors.add(error);
publishSyncError(eControlArea, errors);
return;
}
// Create the portal user with info from BasicPerson and the
// user's password to be the new value indicated in the message.
else {
logger.info("[PasswordSyncCommand.execute] BasicPerson="+bp);
portalPerson = new PortalPerson();
portalPerson.setEmailAddress(getEmailAddress(bp.getEmail()));
portalPerson.setUserId(userId);
portalPerson.setInstId(instId);
portalPerson.setFirstName(bp.getName().getFirstName());
portalPerson.setLastName(bp.getName().getLastName());
// Encypt the password.
String encryptedPassword=null;
String cleartextPassword = newEntUserPassword.getPassword().getValue();
try {
encryptedPassword = hashPassword(cleartextPassword);
logger.info("[PasswordSyncCommand.execute] cleartext password is: " +
cleartextPassword);
logger.info("[PasswordSyncCommand.execute] excrypted password is: " +
encryptedPassword);
} catch (NoSuchAlgorithmException nsae) {
Error error = new Error();
error.setType("application");
error.setErrorNumber("SakaiGateway-1002");
error.setErrorDescription("A NoSuchAlgorithmException occurred encrypting the user " +
"password. The exception is: " + nsae.getMessage());
logger.fatal("[PasswordSyncCommand.execute] " +
error.getErrorDescription());
errors = new ArrayList();
errors.add(error);
publishSyncError(eControlArea, errors, nsae);
return;
} catch (MessagingException nsae) {
Error error = new Error();
error.setType("application");
error.setErrorNumber("SakaiGateway-1002");
error.setErrorDescription("A MessagingException occurred encrypting the user " +
"password. The exception is: " + nsae.getMessage());
logger.fatal("[PasswordSyncCommand.execute] " +
error.getErrorDescription());
errors = new ArrayList();
errors.add(error);
publishSyncError(eControlArea, errors, nsae);
return;
} catch (IOException nsae) {
Error error = new Error();
error.setType("application");
error.setErrorNumber("SakaiGateway-2022");
error.setErrorDescription("An IOException occurred encrypting the user " +
"password. The exception is: " + nsae.getMessage());
logger.fatal("[PasswordSyncCommand.execute] " +
error.getErrorDescription());
errors = new ArrayList();
errors.add(error);
publishSyncError(eControlArea, errors, nsae);
return;
}
portalPerson.setPassword(encryptedPassword);
try {
createPortalPerson(portalPerson);
} catch (CommandException e) {
Error error = new Error();
error.setType("system");
error.setErrorNumber("SakaiGateway-1020");
String errmsg = "An error occurred building the portal person. The exception is: " +
e.getMessage();
error.setErrorDescription(errmsg);
errors = new ArrayList();
errors.add(error);
publishSyncError(eControlArea, errors, e);
logger.error("[PasswordSyncCommand.execute] Can't create portal person. \n"+errmsg);
return;
}
}
}
// Handle an EnterpriseUserPassword.Update-Sync.
if (messageAction.equalsIgnoreCase("Update")) {
// Get the new state of the EnterpriseUserPassword and build an
// EnterpriseUserPassword object.
Element eNewPassword = inDoc.getRootElement().getChild("DataArea")
.getChild("NewData").getChild("EnterpriseUserPassword");
try {
newEntUserPassword.buildObjectFromInput(eNewPassword);
}
catch (EnterpriseLayoutException ele) {
// An error occurred building the EnterpriseUserPassword object from the
// EnterpriseUserPassword element contained in the NewData element of
// the message. Log it and publish a sync error message.
Error error = new Error();
error.setType("system");
error.setErrorNumber("SakaiGateway-1002");
error.setErrorDescription("An error occurred building the " +
"EnterpriseUserPassword object from the EnterpriseUserPassword " +
"element contained in the NewData element of the message. The " +
"exception is: " + ele.getMessage());
errors = new ArrayList();
errors.add(error);
publishSyncError(eControlArea, errors, ele);
return;
}
// Determine whether the user exists in the database.
String userId = getUserId(newEntUserPassword);
PortalPerson portalPerson = null;
try {
portalPerson = retrievePortalPersonByUserId(userId);
}
catch (CommandException ce) {
// An error occurred querying the portal database to determine whether
// the user exists. Log it, publish a error message, and return.
String errMsg = "An error occurred querying the portal database to " +
"determine whether the user exists.";
logger.debug("[PasswordSyncCommand.execute] " + errMsg);
Error error = new Error();
error.setType("system");
error.setErrorNumber("SakaiGateway-2001");
error.setErrorDescription(errMsg);
errors = new ArrayList();
errors.add(error);
publishSyncError(eControlArea, errors, ce);
return;
}
// If the user does not exist, publish a error message indicating
// that the user does not exist.
if (portalPerson == null) {
Error error = new Error();
error.setType("application");
error.setErrorNumber("SakaiGateway-1001");
error.setErrorDescription("No user with user ID " + userId +
") exists in the portal. Cannot reset the password for this user.");
logger.fatal("[PasswordSyncCommand.execute] " +
error.getErrorDescription());
errors = new ArrayList();
errors.add(error);
publishSyncError(eControlArea, errors);
return;
}
// Otherwise, the user already exists in the portal, so reset the
// user's password to be the new value indicated in the message.
else {
// Encypt the password.
String encryptedPassword=null;
String cleartextPassword = newEntUserPassword.getPassword().getValue();
try {
encryptedPassword = hashPassword(cleartextPassword);
logger.info("[PasswordSyncCommand.execute] cleartext password is: " +
cleartextPassword);
logger.info("[PasswordSyncCommand.execute] excrypted password is: " +
encryptedPassword);
} catch (NoSuchAlgorithmException nsae) {
Error error = new Error();
error.setType("application");
error.setErrorNumber("SakaiGateway-1002");
error.setErrorDescription("A NoSuchAlgorithmException occurred encrypting the user " +
"password. The exception is: " + nsae.getMessage());
logger.fatal("[PasswordSyncCommand.execute] " +
error.getErrorDescription());
errors = new ArrayList();
errors.add(error);
publishSyncError(eControlArea, errors, nsae);
return;
} catch (MessagingException nsae) {
Error error = new Error();
error.setType("application");
error.setErrorNumber("SakaiGateway-1002");
error.setErrorDescription("A MessagingException occurred encrypting the user " +
"password. The exception is: " + nsae.getMessage());
logger.fatal("[PasswordSyncCommand.execute] " +
error.getErrorDescription());
errors = new ArrayList();
errors.add(error);
publishSyncError(eControlArea, errors, nsae);
return;
} catch (IOException nsae) {
Error error = new Error();
error.setType("application");
error.setErrorNumber("SakaiGateway-1002");
error.setErrorDescription("An IOException occurred encrypting the user " +
"password. The exception is: " + nsae.getMessage());
logger.fatal("[PasswordSyncCommand.execute] " +
error.getErrorDescription());
errors = new ArrayList();
errors.add(error);
publishSyncError(eControlArea, errors, nsae);
return;
}
// Update the password in the database.
try { updatePassword(userId, encryptedPassword); }
catch (CommandException ce) {
// An error occurred accessing the database to update the user
// password. Log it, publish a error message, and return.
String errMsg = "An error occurred accessing the database to " +
"update the user password.";
logger.debug("[PasswordSyncCommand.execute] " + errMsg);
Error error = new Error();
error.setType("system");
error.setErrorNumber("SakaiGateway-2001");
error.setErrorDescription(errMsg);
errors = new ArrayList();
errors.add(error);
publishSyncError(eControlArea, errors, ce);
return;
}
return;
}
}
// Otherwise, the message is unsupported. Log it and publish a sync error.
else {
Error error = new Error();
error.setType("application");
error.setErrorNumber("OpenEAI-1002");
error.setErrorDescription("Unsupported message action. This command only "
+ "supports an action of 'update'");
errors = new ArrayList();
errors.add(error);
publishSyncError(eControlArea, errors);
return;