use of org.cerberus.util.answer.Answer in project cerberus-source by cerberustesting.
the class UpdateInvariant 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, CerberusException, JSONException {
JSONObject jsonResponse = new JSONObject();
Answer ans = new Answer();
MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", ""));
ans.setResultMessage(msg);
PolicyFactory policy = Sanitizers.FORMATTING.and(Sanitizers.LINKS);
String charset = request.getCharacterEncoding();
// Calling Servlet Transversal Util.
ServletUtil.servletStart(request);
String id = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("idName"), "", charset);
String value = request.getParameter("value");
String oriId = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("originalIdName"), "", charset);
String oriValue = request.getParameter("originalValue");
String description = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("description"), "", charset);
String veryShortDescField = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("veryShortDesc"), "", charset);
String gp1 = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("gp1"), "", charset);
String gp2 = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("gp2"), "", charset);
String gp3 = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("gp3"), "", charset);
String gp4 = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("gp4"), "", charset);
String gp5 = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("gp5"), "", charset);
String gp6 = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("gp6"), "", charset);
String gp7 = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("gp7"), "", charset);
String gp8 = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("gp8"), "", charset);
String gp9 = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("gp9"), "", charset);
Integer sort = 10;
boolean sort_error = false;
try {
if (request.getParameter("sort") != null && !request.getParameter("sort").equals("")) {
sort = Integer.valueOf(policy.sanitize(request.getParameter("sort")));
}
} catch (Exception ex) {
sort_error = true;
}
boolean userHasPermissions = request.isUserInRole("Administrator");
// Prepare the final answer.
MessageEvent msg1 = new MessageEvent(MessageEventEnum.GENERIC_OK);
Answer finalAnswer = new Answer(msg1);
/**
* Checking all constrains before calling the services.
*/
if (StringUtil.isNullOrEmpty(id)) {
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
msg.setDescription(msg.getDescription().replace("%ITEM%", "Invariant").replace("%OPERATION%", "Update").replace("%REASON%", "Invariant name is missing!"));
finalAnswer.setResultMessage(msg);
} else if (sort_error) {
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
msg.setDescription(msg.getDescription().replace("%ITEM%", "Invariant").replace("%OPERATION%", "Update").replace("%REASON%", "Could not manage to convert sort to an integer value!"));
finalAnswer.setResultMessage(msg);
} else if (!userHasPermissions) {
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
msg.setDescription(msg.getDescription().replace("%ITEM%", "Invariant").replace("%OPERATION%", "Update").replace("%REASON%", "You don't have the right to do that"));
finalAnswer.setResultMessage(msg);
} else {
/**
* All data seems cleans so we can call the services.
*/
ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
IInvariantService invariantService = appContext.getBean(IInvariantService.class);
AnswerItem resp = invariantService.readByKey(oriId, oriValue);
if (!(resp.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode()) && resp.getItem() != null)) {
/**
* Object could not be found. We stop here and report the error.
*/
finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) resp);
} else {
Invariant invariantData = (Invariant) resp.getItem();
if (!(resp.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode()) && resp.getItem() != null)) {
/**
* Object could not be found. We stop here and report the
* error.
*/
finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) resp);
} else {
if (invariantService.hasPermissionsUpdate(invariantData, request)) {
invariantData.setIdName(id);
invariantData.setValue(value);
invariantData.setSort(sort);
invariantData.setDescription(description);
invariantData.setVeryShortDesc(veryShortDescField);
invariantData.setGp1(gp1);
invariantData.setGp2(gp2);
invariantData.setGp3(gp3);
invariantData.setGp4(gp4);
invariantData.setGp5(gp5);
invariantData.setGp6(gp6);
invariantData.setGp7(gp7);
invariantData.setGp8(gp8);
invariantData.setGp9(gp9);
ans = invariantService.update(oriId, oriValue, invariantData);
finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) ans);
if (ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
/**
* Object updated. Adding Log entry.
*/
ILogEventService logEventService = appContext.getBean(LogEventService.class);
logEventService.createForPrivateCalls("/UpdateInvariant2", "UPDATE", "Update Invariant : ['" + id + "']", request);
}
} else {
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
msg.setDescription(msg.getDescription().replace("%ITEM%", "Invariant").replace("%OPERATION%", "Update").replace("%REASON%", "The Invariant is not Public!"));
ans.setResultMessage(msg);
finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) ans);
}
}
}
}
/**
* Formating and returning the json result.
*/
jsonResponse.put("messageType", finalAnswer.getResultMessage().getMessage().getCodeString());
jsonResponse.put("message", finalAnswer.getResultMessage().getDescription());
response.getWriter().print(jsonResponse);
response.getWriter().flush();
}
use of org.cerberus.util.answer.Answer in project cerberus-source by cerberustesting.
the class UpdateLabel 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, CerberusException, JSONException {
JSONObject jsonResponse = new JSONObject();
Answer ans = new Answer();
MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", ""));
ans.setResultMessage(msg);
PolicyFactory policy = Sanitizers.FORMATTING.and(Sanitizers.LINKS);
ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
String charset = request.getCharacterEncoding();
ILabelService labelService = appContext.getBean(ILabelService.class);
IFactoryLabel labelFactory = appContext.getBean(IFactoryLabel.class);
response.setContentType("application/json");
// Calling Servlet Transversal Util.
ServletUtil.servletStart(request);
/**
* Parsing and securing all required parameters.
*/
// Parameter that are already controled by GUI (no need to decode) --> We SECURE them
String system = policy.sanitize(request.getParameter("system"));
String type = policy.sanitize(request.getParameter("type"));
Integer id = Integer.valueOf(policy.sanitize(request.getParameter("id")));
String reqtype = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("reqtype"), "", charset);
String reqstatus = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("reqstatus"), "", charset);
String reqcriticity = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("reqcriticity"), "", charset);
// Parameter that needs to be secured --> We SECURE+DECODE them
String label = ParameterParserUtil.parseStringParamAndDecode(request.getParameter("label"), "", charset);
String color = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("color"), "", charset);
String parentLabel = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("parentLabel"), "", charset);
String description = ParameterParserUtil.parseStringParamAndDecode(request.getParameter("description"), "", charset);
String longDesc = ParameterParserUtil.parseStringParamAndDecode(request.getParameter("longdesc"), "", charset);
String usr = request.getUserPrincipal().getName();
/**
* Checking all constrains before calling the services.
*/
if (id == 0) {
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
msg.setDescription(msg.getDescription().replace("%ITEM%", "Label").replace("%OPERATION%", "Update").replace("%REASON%", "Label ID is missing."));
ans.setResultMessage(msg);
} else {
/**
* All data seems cleans so we can call the services.
*/
AnswerItem resp = labelService.readByKey(id);
if (!(resp.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode()) && resp.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%", "Label").replace("%OPERATION%", "Update").replace("%REASON%", "Label does not exist."));
ans.setResultMessage(msg);
} else {
/**
* The service was able to perform the query and confirm the
* object exist, then we can delete it.
*/
Timestamp updateDate = new Timestamp(new Date().getTime());
Label l = labelFactory.create(id, system, label, type, color, parentLabel, reqtype, reqstatus, reqcriticity, description, longDesc, null, null, usr, updateDate);
ans = labelService.update(l);
if (ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
/**
* Delete was successful. Adding Log entry.
*/
ILogEventService logEventService = appContext.getBean(LogEventService.class);
logEventService.createForPrivateCalls("/UpdateLabel", "UPDATE", "Update Label : ['" + id + "']", request);
}
}
}
/**
* Formating and returning the json result.
*/
jsonResponse.put("messageType", ans.getResultMessage().getMessage().getCodeString());
jsonResponse.put("message", ans.getResultMessage().getDescription());
response.getWriter().print(jsonResponse);
response.getWriter().flush();
}
use of org.cerberus.util.answer.Answer in project cerberus-source by cerberustesting.
the class UpdateParameter 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, CerberusException, JSONException {
JSONObject jsonResponse = new JSONObject();
Answer ans = new Answer();
MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", ""));
ans.setResultMessage(msg);
PolicyFactory policy = Sanitizers.FORMATTING.and(Sanitizers.LINKS);
String charset = request.getCharacterEncoding();
ILogEventService logEventService;
String id = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("id"), "", charset);
String value = ParameterParserUtil.parseStringParam(request.getParameter("value"), "");
String system = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("system"), "", charset);
String system1value = ParameterParserUtil.parseStringParam(request.getParameter("system1Value"), null);
String system1 = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("system1"), null, charset);
boolean userHasPermissions = request.isUserInRole("Administrator");
// Prepare the final answer.
MessageEvent msg1 = new MessageEvent(MessageEventEnum.GENERIC_OK);
Answer finalAnswer = new Answer(msg1);
/**
* Checking all constrains before calling the services.
*/
if (StringUtil.isNullOrEmpty(id) || StringUtil.isNullOrEmpty(system1)) {
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
msg.setDescription(msg.getDescription().replace("%ITEM%", "Parameter").replace("%OPERATION%", "Update").replace("%REASON%", "Parameter id or system1 is missing!"));
finalAnswer.setResultMessage(msg);
} else if (!userHasPermissions) {
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
msg.setDescription(msg.getDescription().replace("%ITEM%", "Parameter").replace("%OPERATION%", "Update").replace("%REASON%", "You don't have the right to do that"));
finalAnswer.setResultMessage(msg);
} else {
/**
* All data seems cleans so we can call the services.
*/
ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
IParameterService parameterService = appContext.getBean(IParameterService.class);
FactoryParameter factoryparameter = appContext.getBean(FactoryParameter.class);
Parameter para = factoryparameter.create(system, id, value, "");
ans = parameterService.save(para);
if (!ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode()) && !ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED.getCode())) {
/**
* Object could not be found. We stop here and report the error.
*/
finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) ans);
} else {
if (ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
logEventService = appContext.getBean(LogEventService.class);
logEventService.createForPrivateCalls("/UpdateParameter", "UPDATE", "Update Parameter : ['" + id + "','" + system + "']", request);
}
if (system1 != null && system1value != null) {
Parameter para1 = factoryparameter.create(system1, id, system1value, "");
ans = parameterService.save(para1);
if (ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
/**
* Object updated. Adding Log entry.
*/
logEventService = appContext.getBean(LogEventService.class);
logEventService.createForPrivateCalls("/UpdateParameter", "UPDATE", "Update Parameter : ['" + id + "','" + system1 + "']", request);
}
}
}
}
/**
* Formating and returning the json result.
*/
jsonResponse.put("messageType", finalAnswer.getResultMessage().getMessage().getCodeString());
jsonResponse.put("message", finalAnswer.getResultMessage().getDescription());
response.getWriter().print(jsonResponse);
response.getWriter().flush();
}
use of org.cerberus.util.answer.Answer in project cerberus-source by cerberustesting.
the class ForgotPassword 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 {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
IUserService userService = appContext.getBean(UserService.class);
IEmailService emailService = appContext.getBean(IEmailService.class);
IParameterService parameterService = appContext.getBean(ParameterService.class);
String system = "";
JSONObject jsonResponse = new JSONObject();
String login = ParameterParserUtil.parseStringParam(request.getParameter("login"), "");
/**
* Check if notification parameter is set to Y. If not, return an
* error
*/
String sendNotification = parameterService.findParameterByKey("cerberus_notification_accountcreation_activatenotification", system).getValue();
if (!sendNotification.equalsIgnoreCase("Y")) {
jsonResponse.put("messageType", "Error");
jsonResponse.put("message", "This functionality is not activated. Please contact your Cerberus Administrator.");
response.getWriter().print(jsonResponse);
response.getWriter().flush();
return;
}
/**
* If email not found in database, send error message
*/
AnswerItem ai = userService.readByKey(login);
User user = (User) ai.getItem();
if (user == null) {
jsonResponse.put("messageType", "Error");
jsonResponse.put("message", "Login submitted is unknown !");
response.getWriter().print(jsonResponse);
response.getWriter().flush();
return;
}
/**
* Update user setting a new value in requestresetpassword
*/
userService.requestResetPassword(user);
/**
* Send an email with the hash as a parameter
*/
Answer mailSent = new Answer(emailService.generateAndSendForgotPasswordEmail(user));
if (!mailSent.isCodeStringEquals("OK")) {
jsonResponse.put("messageType", "Error");
jsonResponse.put("message", "An error occured sending the notification. Detail : " + mailSent.getMessageDescription());
response.getWriter().print(jsonResponse);
response.getWriter().flush();
return;
}
/**
* Adding Log entry.
*/
ILogEventService logEventService = appContext.getBean(ILogEventService.class);
logEventService.createForPrivateCalls("/ForgotPassword", "CREATE", "User : " + login + " asked for password recovery", request);
/**
* Build Response Message
*/
jsonResponse.put("messageType", "OK");
jsonResponse.put("message", "An e-mail has been sent to the mailbox " + user.getEmail() + ".");
response.getWriter().print(jsonResponse);
response.getWriter().flush();
} catch (CerberusException myexception) {
response.getWriter().print(myexception.getMessageError().getDescription());
} catch (JSONException ex) {
LOG.warn(ex);
response.setContentType("application/json");
response.getWriter().print(AnswerUtil.createGenericErrorAnswer());
}
}
use of org.cerberus.util.answer.Answer in project cerberus-source by cerberustesting.
the class DeleteInvariant 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, CerberusException, JSONException {
JSONObject jsonResponse = new JSONObject();
Answer ans = new Answer();
MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", ""));
ans.setResultMessage(msg);
PolicyFactory policy = Sanitizers.FORMATTING.and(Sanitizers.LINKS);
String charset = request.getCharacterEncoding();
response.setContentType("application/json");
String id = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("idName"), "", charset);
String value = request.getParameter("value");
boolean userHasPermissions = request.isUserInRole("Administrator");
/**
* Checking all constrains before calling the services.
*/
if (StringUtil.isNullOrEmpty(id)) {
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
msg.setDescription(msg.getDescription().replace("%ITEM%", "Invariant").replace("%OPERATION%", "Delete").replace("%REASON%", "Invariant name is missing!"));
ans.setResultMessage(msg);
} else if (!userHasPermissions) {
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
msg.setDescription(msg.getDescription().replace("%ITEM%", "Invariant").replace("%OPERATION%", "Delete").replace("%REASON%", "You don't have the right to do that"));
ans.setResultMessage(msg);
} else {
/**
* All data seems cleans so we can call the services.
*/
ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
IInvariantService invariantService = appContext.getBean(IInvariantService.class);
Invariant invariantData = invariantService.convert(invariantService.readByKey(id, value));
if (invariantService.hasPermissionsDelete(invariantData, request)) {
ans = invariantService.delete(invariantData);
if (ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
/**
* Object updated. Adding Log entry.
*/
ILogEventService logEventService = appContext.getBean(LogEventService.class);
logEventService.createForPrivateCalls("/DeleteInvariant2", "DELETE", "Delete Invariant : ['" + id + "']", request);
}
} else {
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
msg.setDescription(msg.getDescription().replace("%ITEM%", "Invariant").replace("%OPERATION%", "Delete").replace("%REASON%", "You don't have the right to do that."));
ans.setResultMessage(msg);
}
}
/**
* Formating and returning the json result.
*/
jsonResponse.put("messageType", ans.getResultMessage().getMessage().getCodeString());
jsonResponse.put("message", ans.getResultMessage().getDescription());
response.getWriter().print(jsonResponse.toString());
response.getWriter().flush();
}
Aggregations