Best Cerberus-source code snippet using org.cerberus.crud.service.impl.UserService.deleteUser
Source:DeleteUser.java
1/**2 * Cerberus Copyright (C) 2013 - 2017 cerberustesting3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.4 *5 * This file is part of Cerberus.6 *7 * Cerberus is free software: you can redistribute it and/or modify8 * it under the terms of the GNU General Public License as published by9 * the Free Software Foundation, either version 3 of the License, or10 * (at your option) any later version.11 *12 * Cerberus is distributed in the hope that it will be useful,13 * but WITHOUT ANY WARRANTY; without even the implied warranty of14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the15 * GNU General Public License for more details.16 *17 * You should have received a copy of the GNU General Public License18 * along with Cerberus. If not, see <http://www.gnu.org/licenses/>.19 */20package org.cerberus.servlet.crud.usermanagement;21import org.cerberus.crud.entity.User;22import org.cerberus.engine.entity.MessageEvent;23import org.cerberus.crud.service.IUserService;24import org.cerberus.crud.service.ILogEventService;25import org.cerberus.crud.service.impl.LogEventService;26import org.cerberus.enums.MessageEventEnum;27import org.cerberus.exception.CerberusException;28import org.cerberus.util.ParameterParserUtil;29import org.cerberus.util.StringUtil;30import org.cerberus.util.answer.Answer;31import org.cerberus.util.answer.AnswerItem;32import org.json.JSONException;33import org.json.JSONObject;34import org.owasp.html.PolicyFactory;35import org.owasp.html.Sanitizers;36import org.springframework.context.ApplicationContext;37import org.springframework.web.context.support.WebApplicationContextUtils;38import javax.servlet.ServletException;39import javax.servlet.http.HttpServlet;40import javax.servlet.http.HttpServletRequest;41import javax.servlet.http.HttpServletResponse;42import java.io.IOException;43import javax.servlet.annotation.WebServlet;44/**45 * @author bcivel46 */47@WebServlet(name = "DeleteUser", urlPatterns = {"/DeleteUser"})48public class DeleteUser extends HttpServlet {49 private static final org.apache.logging.log4j.Logger LOG = org.apache.logging.log4j.LogManager.getLogger(DeleteUser.class);50 /**51 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>52 * methods.53 *54 * @param request servlet request55 * @param response servlet response56 * @throws ServletException if a servlet-specific error occurs57 * @throws IOException if an I/O error occurs58 */59 protected void processRequest(HttpServletRequest request, HttpServletResponse response)60 throws ServletException, IOException, CerberusException, JSONException {61 JSONObject jsonResponse = new JSONObject();62 Answer ans = new Answer();63 MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);64 msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", ""));65 ans.setResultMessage(msg);66 PolicyFactory policy = Sanitizers.FORMATTING.and(Sanitizers.LINKS);67 String charset = request.getCharacterEncoding() == null ? "UTF-8" : request.getCharacterEncoding();68 String login = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("login"), "", charset);69 boolean userHasPermissions = request.isUserInRole("Administrator");70 /**71 * Checking all constrains before calling the services.72 */73 if (StringUtil.isNullOrEmpty(login)) {74 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);75 msg.setDescription(msg.getDescription().replace("%ITEM%", "User")76 .replace("%OPERATION%", "Delete")77 .replace("%REASON%", "User name is missing!"));78 ans.setResultMessage(msg);79 } else if (!userHasPermissions) {80 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);81 msg.setDescription(msg.getDescription().replace("%ITEM%", "User")82 .replace("%OPERATION%", "Delete")83 .replace("%REASON%", "You don't have the right to do that"));84 ans.setResultMessage(msg);85 } else {86 /**87 * All data seems cleans so we can call the services.88 */89 ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());90 IUserService userService = appContext.getBean(IUserService.class);91 AnswerItem resp = userService.readByKey(login);92 if (resp.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {93 if (resp.getItem() != null) {94 ans = userService.delete((User) resp.getItem());95 if (ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {96 /**97 * Object updated. Adding Log entry.98 */99 ILogEventService logEventService = appContext.getBean(LogEventService.class);100 logEventService.createForPrivateCalls("/DeleteUser", "DELETE", "Delete User : ['" + login + "']", request);101 }102 } else {103 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);104 msg.setDescription(msg.getDescription().replace("%ITEM%", "User")105 .replace("%OPERATION%", "Delete")106 .replace("%REASON%", "User not found"));107 ans.setResultMessage(msg);108 }109 }110 }111 /**112 * Formating and returning the json result.113 */114 jsonResponse.put("messageType", ans.getResultMessage().getMessage().getCodeString());115 jsonResponse.put("message", ans.getResultMessage().getDescription());116 response.getWriter().print(jsonResponse);117 response.getWriter().flush();118 }119 // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">120 /**121 * Handles the HTTP <code>GET</code> method.122 *123 * @param request servlet request124 * @param response servlet response125 * @throws ServletException if a servlet-specific error occurs126 * @throws IOException if an I/O error occurs127 */128 @Override129 protected void doGet(HttpServletRequest request, HttpServletResponse response)130 throws ServletException, IOException {131 try {132 processRequest(request, response);133 } catch (CerberusException ex) {134 LOG.warn(ex);135 } catch (JSONException ex) {136 LOG.warn(ex);137 }138 }139 /**140 * Handles the HTTP <code>POST</code> method.141 *142 * @param request servlet request143 * @param response servlet response144 * @throws ServletException if a servlet-specific error occurs145 * @throws IOException if an I/O error occurs146 */147 @Override148 protected void doPost(HttpServletRequest request, HttpServletResponse response)149 throws ServletException, IOException {150 try {151 processRequest(request, response);152 } catch (CerberusException ex) {153 LOG.warn(ex);154 } catch (JSONException ex) {155 LOG.warn(ex);156 }157 }158 /**159 * Returns a short description of the servlet.160 *161 * @return a String containing servlet description162 */163 @Override164 public String getServletInfo() {165 return "Short description";166 }// </editor-fold>167}...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!