use of org.cerberus.service.email.IEmailGenerationService in project cerberus-source by cerberustesting.
the class GetNotification method processRequest.
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
* @throws org.cerberus.exception.CerberusException
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, CerberusException, JSONException {
JSONObject jsonResponse = new JSONObject();
Answer answer = new Answer();
MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", ""));
answer.setResultMessage(msg);
PolicyFactory policy = Sanitizers.FORMATTING.and(Sanitizers.LINKS);
response.setContentType("application/json");
/**
* Parsing and securing all required parameters.
*/
String system = policy.sanitize(request.getParameter("system"));
String country = policy.sanitize(request.getParameter("country"));
String env = policy.sanitize(request.getParameter("environment"));
String build = policy.sanitize(request.getParameter("build"));
String revision = policy.sanitize(request.getParameter("revision"));
String chain = policy.sanitize(request.getParameter("chain"));
// Init Answer with potencial error from Parsing parameter.
// AnswerItem answer = new AnswerItem(msg);
String eMailContent = "";
ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
IEmailGenerationService emailService = appContext.getBean(IEmailGenerationService.class);
if (request.getParameter("system") == null) {
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
msg.setDescription(msg.getDescription().replace("%ITEM%", "GetNotification").replace("%OPERATION%", "Get").replace("%REASON%", "System name is missing!"));
answer.setResultMessage(msg);
} else if (request.getParameter("event") == null) {
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
msg.setDescription(msg.getDescription().replace("%ITEM%", "GetNotification").replace("%OPERATION%", "Get").replace("%REASON%", "event is missing!"));
answer.setResultMessage(msg);
} else if (request.getParameter("event").equals("newbuildrevision")) {
try {
// ID parameter is specified so we return the unique record of object.
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
msg.setDescription(msg.getDescription().replace("%ITEM%", "GetNotification").replace("%OPERATION%", "Get"));
answer.setResultMessage(msg);
Email email = emailService.generateRevisionChangeEmail(system, country, env, build, revision);
jsonResponse.put("notificationTo", email.getTo());
jsonResponse.put("notificationCC", email.getCc());
jsonResponse.put("notificationSubject", email.getSubject());
jsonResponse.put("notificationBody", email.getBody());
} catch (Exception ex) {
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
msg.setDescription(msg.getDescription().replace("%ITEM%", "GetNotification").replace("%OPERATION%", "Get").replace("%REASON%", ex.toString()));
answer.setResultMessage(msg);
}
} else if (request.getParameter("event").equals("disableenvironment")) {
try {
// ID parameter is specified so we return the unique record of object.
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
msg.setDescription(msg.getDescription().replace("%ITEM%", "GetNotification").replace("%OPERATION%", "Get"));
answer.setResultMessage(msg);
Email email = emailService.generateDisableEnvEmail(system, country, env);
jsonResponse.put("notificationTo", email.getTo());
jsonResponse.put("notificationCC", email.getCc());
jsonResponse.put("notificationSubject", email.getSubject());
jsonResponse.put("notificationBody", email.getBody());
} catch (Exception ex) {
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
msg.setDescription(msg.getDescription().replace("%ITEM%", "GetNotification").replace("%OPERATION%", "Get").replace("%REASON%", ex.toString()));
answer.setResultMessage(msg);
}
} else if (request.getParameter("event").equals("newchain")) {
try {
// ID parameter is specified so we return the unique record of object.
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
msg.setDescription(msg.getDescription().replace("%ITEM%", "GetNotification").replace("%OPERATION%", "Get"));
answer.setResultMessage(msg);
Email email = emailService.generateNewChainEmail(system, country, env, chain);
jsonResponse.put("notificationTo", email.getTo());
jsonResponse.put("notificationCC", email.getCc());
jsonResponse.put("notificationSubject", email.getSubject());
jsonResponse.put("notificationBody", email.getBody());
} catch (Exception ex) {
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
msg.setDescription(msg.getDescription().replace("%ITEM%", "GetNotification").replace("%OPERATION%", "Get").replace("%REASON%", ex.toString()));
answer.setResultMessage(msg);
}
} else {
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
msg.setDescription(msg.getDescription().replace("%ITEM%", "GetNotification").replace("%OPERATION%", "Get").replace("%REASON%", "Unknown invalidityReason!"));
answer.setResultMessage(msg);
}
/**
* Formating and returning the json result.
*/
jsonResponse.put("messageType", answer.getResultMessage().getMessage().getCodeString());
jsonResponse.put("message", answer.getResultMessage().getDescription());
response.getWriter().print(jsonResponse);
response.getWriter().flush();
}
use of org.cerberus.service.email.IEmailGenerationService in project cerberus-source by cerberustesting.
the class NewBuildRev method processRequest.
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, JSONException {
JSONObject jsonResponse = new JSONObject();
AnswerItem answerItem = new AnswerItem();
MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", ""));
answerItem.setResultMessage(msg);
PolicyFactory policy = Sanitizers.FORMATTING.and(Sanitizers.LINKS);
response.setContentType("application/json");
/**
* Parsing and securing all required parameters.
*/
String system = policy.sanitize(request.getParameter("system"));
String country = policy.sanitize(request.getParameter("country"));
String env = policy.sanitize(request.getParameter("environment"));
String build = policy.sanitize(request.getParameter("build"));
String revision = policy.sanitize(request.getParameter("revision"));
// Init Answer with potencial error from Parsing parameter.
// AnswerItem answer = new AnswerItem(msg);
String eMailContent;
ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
IEmailService emailService = appContext.getBean(IEmailService.class);
IEmailGenerationService emailGenerationService = appContext.getBean(IEmailGenerationService.class);
IParameterService parameterService = appContext.getBean(IParameterService.class);
ICountryEnvParamService countryEnvParamService = appContext.getBean(ICountryEnvParamService.class);
ICountryEnvParam_logService countryEnvParam_logService = appContext.getBean(ICountryEnvParam_logService.class);
ILogEventService logEventService = appContext.getBean(LogEventService.class);
if (request.getParameter("system") == null) {
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
msg.setDescription(msg.getDescription().replace("%ITEM%", ITEM).replace("%OPERATION%", OPERATION).replace("%REASON%", "System name is missing!"));
answerItem.setResultMessage(msg);
} else if (request.getParameter("country") == null) {
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
msg.setDescription(msg.getDescription().replace("%ITEM%", ITEM).replace("%OPERATION%", OPERATION).replace("%REASON%", "Country is missing!"));
answerItem.setResultMessage(msg);
} else if (request.getParameter("environment") == null) {
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
msg.setDescription(msg.getDescription().replace("%ITEM%", ITEM).replace("%OPERATION%", OPERATION).replace("%REASON%", "Environment is missing!"));
answerItem.setResultMessage(msg);
} else if (request.getParameter("build") == null) {
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
msg.setDescription(msg.getDescription().replace("%ITEM%", ITEM).replace("%OPERATION%", OPERATION).replace("%REASON%", "Build is missing!"));
answerItem.setResultMessage(msg);
} else if (request.getParameter("revision") == null) {
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
msg.setDescription(msg.getDescription().replace("%ITEM%", ITEM).replace("%OPERATION%", OPERATION).replace("%REASON%", "Revision is missing!"));
answerItem.setResultMessage(msg);
} else {
// All parameters are OK we can start performing the operation.
// Getting the contryEnvParam based on the parameters.
answerItem = countryEnvParamService.readByKey(system, country, env);
if (!(answerItem.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode()) && answerItem.getItem() != null)) {
/**
* Object could not be found. We stop here and report the error.
*/
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", OPERATION).replace("%REASON%", OBJECT_NAME + " ['" + system + "','" + country + "','" + env + "'] does not exist. Cannot activate it!"));
answerItem.setResultMessage(msg);
} else {
/**
* The service was able to perform the query and confirm the
* object exist, then we can update it.
*/
// Email Calculation. Email must be calcuated before we update the Build and revision in order to have the old build revision still available in the mail.
String OutputMessage = "";
Email email = null;
try {
email = emailGenerationService.generateRevisionChangeEmail(system, country, env, build, revision);
} catch (Exception ex) {
LOG.warn(Infos.getInstance().getProjectNameAndVersion() + " - Exception catched.", ex);
logEventService.createForPrivateCalls("/NewBuildRev", "NEWBUILDREV", "Warning on New Build/Revision environment : ['" + system + "','" + country + "','" + env + "'] " + ex.getMessage(), request);
OutputMessage = ex.getMessage();
}
// We update the object.
CountryEnvParam cepData = (CountryEnvParam) answerItem.getItem();
cepData.setBuild(build);
cepData.setRevision(revision);
cepData.setActive(true);
Answer answer = countryEnvParamService.update(cepData);
if (!(answer.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode()))) {
/**
* Object could not be updated. We stop here and report the
* error.
*/
answerItem.setResultMessage(answer.getResultMessage());
} else {
/**
* Update was successful.
*/
// Adding Log entry.
logEventService.createForPrivateCalls("/NewBuildRev", "UPDATE", "Updated CountryEnvParam : ['" + system + "','" + country + "','" + env + "']", request);
// Adding CountryEnvParam Log entry.
countryEnvParam_logService.createLogEntry(system, country, env, build, revision, "New Build Revision.", request.getUserPrincipal().getName());
/**
* Email notification.
*/
try {
emailService.sendHtmlMail(email);
} catch (Exception e) {
LOG.warn(Infos.getInstance().getProjectNameAndVersion() + " - Exception catched.", e);
logEventService.createForPrivateCalls("/NewBuildRev", "NEWBUILDREV", "Warning on New Build/Revision environment : ['" + system + "','" + country + "','" + env + "'] " + e.getMessage(), request);
OutputMessage = e.getMessage();
}
if (OutputMessage.equals("")) {
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
msg.setDescription(msg.getDescription().replace("%ITEM%", "Environment").replace("%OPERATION%", OPERATION));
answerItem.setResultMessage(msg);
} else {
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
msg.setDescription(msg.getDescription().replace("%ITEM%", "Environment").replace("%OPERATION%", OPERATION).concat(" Just one warning : ").concat(OutputMessage));
answerItem.setResultMessage(msg);
}
}
}
}
/**
* Formating and returning the json result.
*/
jsonResponse.put("messageType", answerItem.getResultMessage().getMessage().getCodeString());
jsonResponse.put("message", answerItem.getResultMessage().getDescription());
response.getWriter().print(jsonResponse);
response.getWriter().flush();
}
use of org.cerberus.service.email.IEmailGenerationService in project cerberus-source by cerberustesting.
the class NewBuildRevisionV000 method processRequest.
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
String charset = request.getCharacterEncoding();
ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
/**
* Adding Log entry.
*/
ILogEventService logEventService = appContext.getBean(ILogEventService.class);
logEventService.createForPublicCalls("/NewBuildRevisionV000", "CALL", "NewBuildRevisionV000 called : " + request.getRequestURL(), request);
ICountryEnvParamService countryEnvParamService = appContext.getBean(ICountryEnvParamService.class);
IInvariantService invariantService = appContext.getBean(IInvariantService.class);
IBuildRevisionInvariantService buildRevisionInvariantService = appContext.getBean(IBuildRevisionInvariantService.class);
IEmailService emailService = appContext.getBean(IEmailService.class);
IEmailGenerationService emailGenerationService = appContext.getBean(IEmailGenerationService.class);
ICountryEnvParam_logService countryEnvParam_logService = appContext.getBean(ICountryEnvParam_logService.class);
IParameterService parameterService = appContext.getBean(IParameterService.class);
// Parsing all parameters.
String system = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("system"), "", charset);
String country = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("country"), "", charset);
String environment = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("environment"), "", charset);
String build = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("build"), "", charset);
String revision = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("revision"), "", charset);
String helpMessage = "\nThis servlet is used to inform Cerberus that a new Build and Revision has been deployed on a system.\n\nParameter list :\n" + "- system [mandatory] : the system where the Build Revision has been deployed. [" + system + "]\n" + "- country [mandatory] : the country where the Build Revision has been deployed. You can use ALL if you want to perform the action for all countries that exist for the given system and environement. [" + country + "]\n" + "- environment [mandatory] : the environment where the Build Revision has been deployed. [" + environment + "]\n" + "- build [mandatory] : the build that has been deployed. [" + build + "]\n" + "- revision [mandatory] : the revision that has been deployed. [" + revision + "]\n";
boolean error = false;
// Checking the parameter validity. If application has been entered, does it exist ?
if (system.equalsIgnoreCase("")) {
out.println("Error - Parameter system is mandatory.");
error = true;
}
if (!system.equalsIgnoreCase("") && !invariantService.isInvariantExist("SYSTEM", system)) {
out.println("Error - System does not exist : " + system);
error = true;
}
if (environment.equalsIgnoreCase("")) {
out.println("Error - Parameter environment is mandatory.");
error = true;
}
if (!environment.equalsIgnoreCase("") && !invariantService.isInvariantExist("ENVIRONMENT", environment)) {
out.println("Error - Environment does not exist : " + environment);
error = true;
}
if (country.equalsIgnoreCase("")) {
out.println("Error - Parameter country is mandatory.");
error = true;
} else if (!country.equalsIgnoreCase(PARAMETERALL)) {
if (!invariantService.isInvariantExist("COUNTRY", country)) {
out.println("Error - Country does not exist : " + country);
error = true;
}
if (!error) {
if (!countryEnvParamService.exist(system, country, environment)) {
out.println("Error - System/Country/Environment does not exist : " + system + "/" + country + "/" + environment);
error = true;
}
}
}
if (build.equalsIgnoreCase("")) {
out.println("Error - Parameter build is mandatory.");
error = true;
}
if (!build.equalsIgnoreCase("") && !buildRevisionInvariantService.exist(system, 1, build)) {
out.println("Error - Build does not exist : " + build);
error = true;
}
if (revision.equalsIgnoreCase("")) {
out.println("Error - Parameter revision is mandatory.");
error = true;
}
if (!revision.equalsIgnoreCase("") && !buildRevisionInvariantService.exist(system, 2, revision)) {
out.println("Error - Revision does not exist : " + revision);
error = true;
}
// Starting the database update only when no blocking error has been detected.
if (error == false) {
/**
* Getting the list of objects to treat.
*/
// We update the object.
MessageEvent msg = new MessageEvent(MessageEventEnum.GENERIC_OK);
Answer finalAnswer = new Answer(msg);
AnswerList answerList = new AnswerList();
if (country.equalsIgnoreCase(PARAMETERALL)) {
country = null;
}
answerList = countryEnvParamService.readByVarious(system, country, environment, null, null, null);
finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) answerList);
for (CountryEnvParam cepData : (List<CountryEnvParam>) answerList.getDataList()) {
// Email Calculation. Email must be calcuated before we update the Build and revision in order to have the old build revision still available in the mail.
String OutputMessage = "";
Email email = null;
try {
email = emailGenerationService.generateRevisionChangeEmail(cepData.getSystem(), cepData.getCountry(), cepData.getEnvironment(), build, revision);
} catch (Exception ex) {
LOG.warn(Infos.getInstance().getProjectNameAndVersion() + " - Exception catched.", ex);
logEventService.createForPrivateCalls("/NewBuildRevisionV000", "NEWBUILDREV", "Warning on New Build/Revision environment : ['" + cepData.getSystem() + "','" + cepData.getCountry() + "','" + cepData.getEnvironment() + "'] " + ex.getMessage(), request);
OutputMessage = ex.getMessage();
}
/**
* For each object, we can update it.
*/
cepData.setBuild(build);
cepData.setRevision(revision);
cepData.setActive(true);
Answer answerUpdate = countryEnvParamService.update(cepData);
if (!(answerUpdate.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode()))) {
/**
* Object could not be updated. We stop here and report the
* error.
*/
finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, answerUpdate);
} else {
/**
* Update was successful.
*/
// Adding Log entry.
logEventService.createForPrivateCalls("/NewBuildRevisionV000", "UPDATE", "Updated CountryEnvParam : ['" + cepData.getSystem() + "','" + cepData.getCountry() + "','" + cepData.getEnvironment() + "']", request);
// Adding CountryEnvParam Log entry.
countryEnvParam_logService.createLogEntry(cepData.getSystem(), cepData.getCountry(), cepData.getEnvironment(), build, revision, "New Build Revision.", "PublicCall");
/**
* Email notification.
*/
try {
// Sending the email
emailService.sendHtmlMail(email);
} catch (Exception e) {
LOG.warn(Infos.getInstance().getProjectNameAndVersion() + " - Exception catched.", e);
logEventService.createForPrivateCalls("/NewBuildRevisionV000", "NEWBUILDREV", "Warning on New Build/Revision environment : ['" + cepData.getSystem() + "','" + cepData.getCountry() + "','" + cepData.getEnvironment() + "'] " + e.getMessage(), request);
OutputMessage = e.getMessage();
}
if (OutputMessage.equals("")) {
msg = new MessageEvent(MessageEventEnum.GENERIC_OK);
Answer answerSMTP = new AnswerList(msg);
finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, answerSMTP);
} else {
msg = new MessageEvent(MessageEventEnum.GENERIC_WARNING);
msg.setDescription(msg.getDescription().replace("%REASON%", OutputMessage + " when sending email for " + cepData.getSystem() + "/" + cepData.getCountry() + "/" + cepData.getEnvironment()));
Answer answerSMTP = new AnswerList(msg);
finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, answerSMTP);
}
}
}
/**
* Formating and returning the result.
*/
out.println(finalAnswer.getResultMessage().getMessage().getCodeString() + " - " + finalAnswer.getResultMessage().getDescription());
} else {
// In case of errors, we display the help message.
out.println(helpMessage);
}
}
Aggregations