use of org.cerberus.crud.service.IUserService in project cerberus-source by cerberustesting.
the class NewRelease 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(LogEventService.class);
logEventService.createForPublicCalls("/NewRelease", "CALL", "NewRelease called : " + request.getRequestURL(), request);
IApplicationService MyApplicationService = appContext.getBean(ApplicationService.class);
IUserService MyUserService = appContext.getBean(UserService.class);
IProjectService MyProjectService = appContext.getBean(ProjectService.class);
IBuildRevisionParametersService buildRevisionParametersService = appContext.getBean(IBuildRevisionParametersService.class);
IFactoryBuildRevisionParameters factoryBuildRevisionParameters = appContext.getBean(IFactoryBuildRevisionParameters.class);
// Parsing all parameters.
String application = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("application"), "", charset);
String release = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("release"), "", charset);
String project = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("project"), "", charset);
String ticket = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("ticket"), "", charset);
String bug = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("bug"), "", charset);
String subject = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("subject"), "", charset);
String owner = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("owner"), "", charset);
String link = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("link"), "", charset);
// Those Parameters could be used later when Cerberus send the deploy request to Jenkins.
String jenkinsbuildid = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("jenkinsbuildid"), "", charset);
String mavengroupid = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("mavengroupid"), "", charset);
String mavenartifactid = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("mavenartifactid"), "", charset);
String mavenversion = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("mavenversion"), "", charset);
String repositoryurl = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("repositoryurl"), "", charset);
String helpMessage = "\nThis servlet is used to create or update a release entry in a 'NONE' build and 'NONE' revision.\n\nParameter list :\n" + "- application [mandatory] : the application that produced the release. This parameter must match the application list in Cerberus. [" + application + "]\n" + "- release : release number or svn number. This should be unique at the application level. 2 calls on the same application and release will update the other parameters on the same entry. [" + release + "]\n" + "- project : Project reference. [" + project + "]\n" + "- ticket : Ticket Reference. [" + ticket + "]\n" + "- bug : Bug reference. [" + bug + "]\n" + "- subject : A short description of the change. [" + subject + "]\n" + "- owner : User name of the developper/ person who did the commit. [" + owner + "]\n" + "- link : URL Link on detail documentation on the release. [" + link + "]\n\n" + "The following optional parameters could be used later when Cerberus send the deploy request to Jenkins.\n" + "- jenkinsbuildid : Jenkins Build ID. [" + jenkinsbuildid + "]\n" + "- mavengroupid : Maven Group ID. [" + mavengroupid + "]\n" + "- mavenartifactid : Maven Artifact ID. [" + mavenartifactid + "]\n" + "- repositoryurl : Repository URL. [" + repositoryurl + "]\n" + "- mavenversion : Maven Version. [" + mavenversion + "]\n";
DatabaseSpring database = appContext.getBean(DatabaseSpring.class);
Connection connection = database.connect();
try {
boolean error = false;
// Checking the parameter validity. If application has been entered, does it exist ?
if (!application.equalsIgnoreCase("") && !MyApplicationService.exist(application)) {
out.println("Error - Application does not exist : " + application);
error = true;
}
if (application.equalsIgnoreCase("")) {
out.println("Error - Parameter application is mandatory.");
error = true;
}
// Checking the parameter validity. If owner has been entered, does it exist ?
if (!owner.equalsIgnoreCase("")) {
if (MyUserService.isUserExist(owner)) {
// We get the exact name from Cerberus.
owner = MyUserService.findUserByKey(owner).getLogin();
} else {
out.println("Warning - User does not exist : " + owner);
}
}
// Checking the parameter validity. If project has been entered, does it exist ?
if (!project.equalsIgnoreCase("") && !MyProjectService.exist(project)) {
out.println("Warning - Project does not exist : " + project);
}
// Starting the database update only when no blocking error has been detected.
if (error == false) {
// In case the bugID is not defined, we try to guess it from the subject. should be between # and a space or CR.
if (StringUtil.isNullOrEmpty(bug)) {
String[] columns = subject.split("#");
if (columns.length >= 2) {
for (int i = 1; i < columns.length; i++) {
String[] columnsbis = columns[i].split(" ");
if (columnsbis.length >= 1) {
if (!columnsbis[0].contains(";")) {
// Bug number should not include ;
bug = columnsbis[0];
}
}
}
}
}
// Transaction and database update.
// Duplicate entry Verification. On the build/relivion not yet assigned (NONE/NONE),
// we verify that the application + release has not been submitted yet.
// if it exist, we update it in stead of inserting a new row.
// That correspond in the cases where the Jenkins pipe is executed several times
// on a single svn commit.
/**
* Verify if the entry already exists if already exists, update
* it else create it
*/
AnswerItem answer = buildRevisionParametersService.readByVarious2("NONE", "NONE", release, application);
BuildRevisionParameters buildRevisionParameters = (BuildRevisionParameters) answer.getItem();
if (answer.getResultMessage().getCode() == new MessageEvent(MessageEventEnum.DATA_OPERATION_OK).getCode() && buildRevisionParameters != null) {
out.println("Warning - Release entry already exist. Updating the existing entry : " + buildRevisionParameters.getId());
if (!project.isEmpty()) {
buildRevisionParameters.setProject(project);
}
if (!ticket.isEmpty()) {
buildRevisionParameters.setTicketIdFixed(ticket);
}
if (!bug.isEmpty()) {
buildRevisionParameters.setBugIdFixed(bug);
}
if (!subject.isEmpty()) {
buildRevisionParameters.setSubject(subject);
}
if (!owner.isEmpty()) {
buildRevisionParameters.setReleaseOwner(owner);
}
if (!link.isEmpty()) {
buildRevisionParameters.setLink(link);
}
if (!jenkinsbuildid.isEmpty()) {
buildRevisionParameters.setJenkinsBuildId(jenkinsbuildid);
}
if (!mavengroupid.isEmpty()) {
buildRevisionParameters.setMavenGroupId(mavengroupid);
}
if (!mavenartifactid.isEmpty()) {
buildRevisionParameters.setMavenArtifactId(mavenartifactid);
}
if (!mavenversion.isEmpty()) {
buildRevisionParameters.setMavenVersion(mavenversion);
}
if (!repositoryurl.isEmpty()) {
buildRevisionParameters.setRepositoryUrl(repositoryurl);
}
buildRevisionParametersService.update(buildRevisionParameters);
} else if (answer.getResultMessage().getCode() == new MessageEvent(MessageEventEnum.DATA_OPERATION_NO_DATA_FOUND).getCode()) {
buildRevisionParametersService.create(factoryBuildRevisionParameters.create(0, "NONE", "NONE", release, application, project, ticket, bug, link, owner, subject, null, jenkinsbuildid, mavengroupid, mavenartifactid, mavenversion, repositoryurl));
out.println("Release Inserted : '" + release + "' on '" + application + "' for user '" + owner + "'");
} else {
out.println("A problem occured : '" + answer.getResultMessage().getDescription());
}
} else {
// In case of errors, we display the help message.
out.println(helpMessage);
}
} catch (Exception e) {
LOG.warn(Infos.getInstance().getProjectNameAndVersion() + " - Exception catched.", e);
out.print("Error while inserting the release : ");
out.println(e.toString());
} finally {
out.close();
try {
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
LOG.warn(e.toString());
}
}
}
use of org.cerberus.crud.service.IUserService in project cerberus-source by cerberustesting.
the class UpdateUser method doPost.
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, IndexOutOfBoundsException {
// TODO create class Validator to validate all parameter from page
JSONObject jsonResponse = new JSONObject();
MessageEvent msg1 = new MessageEvent(MessageEventEnum.GENERIC_OK);
Answer ans = new Answer();
Answer finalAnswer = new Answer(msg1);
MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", ""));
ans.setResultMessage(msg);
String id = request.getParameter("id");
String login = request.getParameter("login");
String name = request.getParameter("name");
String email = request.getParameter("email");
String team = request.getParameter("team");
String systems = request.getParameter("systems");
String requests = request.getParameter("request");
String groups = request.getParameter("groups");
String defaultSystem = request.getParameter("defaultSystem");
if (StringUtil.isNullOrEmpty(login) || StringUtil.isNullOrEmpty(id)) {
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
msg.setDescription(msg.getDescription().replace("%ITEM%", "User").replace("%OPERATION%", "Update").replace("%REASON%", "User login is missing."));
ans.setResultMessage(msg);
} else {
LOG.info("Updating user " + login);
ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
IUserService userService = appContext.getBean(UserService.class);
IUserGroupService userGroupService = appContext.getBean(UserGroupService.class);
IFactoryUserSystem userSystemFactory = appContext.getBean(IFactoryUserSystem.class);
IUserSystemService userSystemService = appContext.getBean(IUserSystemService.class);
IFactoryUserGroup factoryGroup = new FactoryUserGroup();
User myUser;
List<UserGroup> newGroups = null;
List<UserSystem> newSystems = null;
try {
myUser = userService.findUserByKey(id);
List<String> listGroup = new ArrayList<String>();
JSONArray GroupArray = new JSONArray(request.getParameter("groups"));
for (int i = 0; i < GroupArray.length(); i++) {
listGroup.add(GroupArray.getString(i));
}
newGroups = new ArrayList<UserGroup>();
for (String group : listGroup) {
newGroups.add(factoryGroup.create(group));
}
myUser.setLogin(login);
myUser.setName(name);
myUser.setTeam(team);
newSystems = new ArrayList<UserSystem>();
JSONArray SystemArray = new JSONArray(request.getParameter("systems"));
List<String> listSystem = new ArrayList<String>();
for (int i = 0; i < SystemArray.length(); i++) {
listSystem.add(SystemArray.getString(i));
}
for (String system : listSystem) {
newSystems.add(userSystemFactory.create(login, system));
}
myUser.setDefaultSystem(defaultSystem);
myUser.setRequest(requests);
myUser.setEmail(email);
try {
ans = userService.update(myUser);
AnswerUtil.agregateAnswer(finalAnswer, (Answer) ans);
if (ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
/**
* Update was successful. Adding Log entry.
*/
ILogEventService logEventService = appContext.getBean(LogEventService.class);
logEventService.createForPrivateCalls("/UpdateUser", "UPDATE", "Updated user : " + login, request);
if (!newGroups.isEmpty()) {
userGroupService.updateUserGroups(myUser, newGroups);
/**
* Adding Log entry.
*/
logEventService = appContext.getBean(LogEventService.class);
logEventService.createForPrivateCalls("/UpdateUser", "UPDATE", "Updated user groups : " + login, request);
}
if (!newSystems.isEmpty()) {
request.getSession().setAttribute("MySystem", newSystems.get(0).getSystem());
userSystemService.updateUserSystems(myUser, newSystems);
/**
* Adding Log entry.
*/
logEventService = appContext.getBean(LogEventService.class);
logEventService.createForPrivateCalls("/UpdateUser", "UPDATE", "Updated user system : " + login, request);
}
}
/**
* Adding Log entry.
*/
finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) ans);
AnswerUtil.agregateAnswer(finalAnswer, (Answer) ans);
jsonResponse.put("messageType", finalAnswer.getResultMessage().getMessage().getCodeString());
jsonResponse.put("message", finalAnswer.getResultMessage().getDescription());
response.getWriter().print(jsonResponse);
} catch (CerberusException ex) {
response.getWriter().print(ex.getMessageError().getDescription());
}
} catch (CerberusException ex) {
response.getWriter().print(ex.getMessageError().getDescription());
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
use of org.cerberus.crud.service.IUserService in project cerberus-source by cerberustesting.
the class ChangeUserPassword method doPost.
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String login = request.getParameter("login");
String currentPassword = request.getParameter("currentPassword");
String newPassword = request.getParameter("newPassword");
String confirmPassword = request.getParameter("confirmPassword");
String resetPasswordToken = request.getParameter("resetPasswordToken");
ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
IUserService userService = appContext.getBean(UserService.class);
User myUser;
try {
JSONObject jsonResponse = new JSONObject();
try {
myUser = userService.findUserByKey(login);
AnswerItem ansPassword = userService.updateUserPassword(myUser, currentPassword, newPassword, confirmPassword, resetPasswordToken);
jsonResponse.put("messageType", ansPassword.getResultMessage().getMessage().getCodeString());
jsonResponse.put("message", ansPassword.getResultMessage().getDescription());
} catch (CerberusException ex1) {
// TODO:FN this need to be refactored //findUserByKey should return answer
jsonResponse.put("messageType", "KO");
jsonResponse.put("message", ex1.toString());
}
response.setContentType("application/json");
response.getWriter().print(jsonResponse.toString());
} catch (JSONException e) {
LOG.warn(e);
// returns a default error message with the json format that is able to be parsed by the client-side
response.setContentType("application/json");
response.getWriter().print(AnswerUtil.createGenericErrorAnswer());
}
}
use of org.cerberus.crud.service.IUserService in project cerberus-source by cerberustesting.
the class ChangeUserPasswordAdmin method doPost.
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String login = request.getParameter("login");
String newPassword = request.getParameter("newPassword");
ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
IUserService userService = appContext.getBean(UserService.class);
User myUser;
try {
JSONObject jsonResponse = new JSONObject();
try {
myUser = userService.findUserByKey(login);
AnswerItem ansPassword = userService.updateUserPasswordAdmin(myUser, newPassword);
jsonResponse.put("messageType", ansPassword.getResultMessage().getMessage().getCodeString());
jsonResponse.put("message", ansPassword.getResultMessage().getDescription());
} catch (CerberusException ex1) {
// TODO:FN this need to be refactored //findUserByKey should return answer
jsonResponse.put("messageType", "KO");
jsonResponse.put("message", ex1.toString());
}
response.setContentType("application/json");
response.getWriter().print(jsonResponse.toString());
} catch (JSONException e) {
LOG.warn(e);
// returns a default error message with the json format that is able to be parsed by the client-side
response.setContentType("application/json");
response.getWriter().print(AnswerUtil.createGenericErrorAnswer());
}
}
use of org.cerberus.crud.service.IUserService 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());
}
}
Aggregations