Best Cerberus-source code snippet using org.cerberus.servlet.crud.countryenvironment.CreateApplicationObject.processRequest
Source: CreateApplicationObject.java
...72 * @throws IOException if an I/O error occurs73 * @throws CerberusException74 * @throws JSONException75 */76 protected void processRequest(HttpServletRequest request, HttpServletResponse response)77 throws ServletException, IOException, CerberusException, JSONException {78 JSONObject jsonResponse = new JSONObject();79 Answer ans = new Answer();80 MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);81 msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", ""));82 ans.setResultMessage(msg);83 PolicyFactory policy = Sanitizers.FORMATTING.and(Sanitizers.LINKS);84 String charset = request.getCharacterEncoding();85 response.setContentType("application/json");86 // Calling Servlet Transversal Util.87 ServletUtil.servletStart(request);88 Map<String, String> fileData = new HashMap<String, String>();89 FileItem file = null;90 FileItemFactory factory = new DiskFileItemFactory();91 ServletFileUpload upload = new ServletFileUpload(factory);92 try {93 List<FileItem> fields = upload.parseRequest(request);94 Iterator<FileItem> it = fields.iterator();95 if (!it.hasNext()) {96 return;97 }98 while (it.hasNext()) {99 FileItem fileItem = it.next();100 boolean isFormField = fileItem.isFormField();101 if (isFormField) {102 fileData.put(fileItem.getFieldName(), fileItem.getString("UTF-8"));103 } else {104 file = fileItem;105 }106 }107 } catch (FileUploadException e) {108 e.printStackTrace();109 }110 /**111 * Parsing and securing all required parameters.112 */113 // Parameter that are already controled by GUI (no need to decode) --> We SECURE them114 // Parameter that needs to be secured --> We SECURE+DECODE them115 String application = ParameterParserUtil.parseStringParamAndDecode(fileData.get("application"), null, charset);116 String object = ParameterParserUtil.parseStringParamAndDecode(fileData.get("object"), null, charset);117 String value = ParameterParserUtil.parseStringParam(fileData.get("value"), null);118 String usrcreated = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getRemoteUser(), "", charset);119 String datecreated = new Timestamp(new java.util.Date().getTime()).toString();120 String usrmodif = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getRemoteUser(), "", charset);121 String datemodif = new Timestamp(new java.util.Date().getTime()).toString();122 // Parameter that we cannot secure as we need the html --> We DECODE them123 /**124 * Checking all constrains before calling the services.125 */126 if (StringUtil.isNullOrEmpty(application)) {127 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);128 msg.setDescription(msg.getDescription().replace("%ITEM%", "ApplicationObject")129 .replace("%OPERATION%", "Create")130 .replace("%REASON%", "Application name is missing!"));131 ans.setResultMessage(msg);132 } else if (StringUtil.isNullOrEmpty(object)) {133 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);134 msg.setDescription(msg.getDescription().replace("%ITEM%", "ApplicationObject")135 .replace("%OPERATION%", "Create")136 .replace("%REASON%", "Object name is missing!"));137 ans.setResultMessage(msg);138 } else {139 /**140 * All data seems cleans so we can call the services.141 */142 ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());143 IApplicationObjectService applicationobjectService = appContext.getBean(IApplicationObjectService.class);144 IFactoryApplicationObject factoryApplicationobject = appContext.getBean(IFactoryApplicationObject.class);145 String fileName = "";146 if (file != null) {147 fileName = file.getName();148 }149 ApplicationObject applicationData = factoryApplicationobject.create(-1, application, object, value, fileName, usrcreated, datecreated, usrmodif, datemodif);150 ans = applicationobjectService.create(applicationData);151 if (ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {152 /**153 * Object created. Adding Log entry.154 */155 ILogEventService logEventService = appContext.getBean(LogEventService.class);156 logEventService.createForPrivateCalls("/CreateApplicationObject", "CREATE", "Create Application Object: ['" + application + "','" + object + "']", request);157 if (file != null) {158 AnswerItem an = applicationobjectService.readByKey(application, object);159 if (an.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode()) && an.getItem() != null) {160 applicationData = (ApplicationObject) an.getItem();161 ans = applicationobjectService.uploadFile(applicationData.getID(), file);162 if (ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {163 }164 }165 }166 }167 }168 /**169 * Formating and returning the json result.170 */171 jsonResponse.put("messageType", ans.getResultMessage().getMessage().getCodeString());172 jsonResponse.put("message", ans.getResultMessage().getDescription());173 response.getWriter().print(jsonResponse);174 response.getWriter().flush();175 }176 // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">177 /**178 * Handles the HTTP <code>GET</code> method.179 *180 * @param request servlet request181 * @param response servlet response182 * @throws ServletException if a servlet-specific error occurs183 * @throws IOException if an I/O error occurs184 */185 @Override186 protected void doGet(HttpServletRequest request, HttpServletResponse response)187 throws ServletException, IOException {188 try {189 processRequest(request, response);190 } catch (CerberusException ex) {191 LOG.warn(ex);192 } catch (JSONException ex) {193 LOG.warn(ex);194 }195 }196 /**197 * Handles the HTTP <code>POST</code> method.198 *199 * @param request servlet request200 * @param response servlet response201 * @throws ServletException if a servlet-specific error occurs202 * @throws IOException if an I/O error occurs203 */204 @Override205 protected void doPost(HttpServletRequest request, HttpServletResponse response)206 throws ServletException, IOException {207 try {208 processRequest(request, response);209 } catch (CerberusException ex) {210 LOG.warn(ex);211 } catch (JSONException ex) {212 LOG.warn(ex);213 }214 }215 /**216 * Returns a short description of the servlet.217 *218 * @return a String containing servlet description219 */220 @Override221 public String getServletInfo() {222 return "Short description";...
Check out the latest blogs from LambdaTest on this topic:
When working on web automation with Selenium, I encountered scenarios where I needed to refresh pages from time to time. When does this happen? One scenario is that I needed to refresh the page to check that the data I expected to see was still available even after refreshing. Another possibility is to clear form data without going through each input individually.
The web paradigm has changed considerably over the last few years. Web 2.0, a term coined way back in 1999, was one of the pivotal moments in the history of the Internet. UGC (User Generated Content), ease of use, and interoperability for the end-users were the key pillars of Web 2.0. Consumers who were only consuming content up till now started creating different forms of content (e.g., text, audio, video, etc.).
In today’s world, an organization’s most valuable resource is its customers. However, acquiring new customers in an increasingly competitive marketplace can be challenging while maintaining a strong bond with existing clients. Implementing a customer relationship management (CRM) system will allow your organization to keep track of important customer information. This will enable you to market your services and products to these customers better.
The purpose of developing test cases is to ensure the application functions as expected for the customer. Test cases provide basic application documentation for every function, feature, and integrated connection. Test case development often detects defects in the design or missing requirements early in the development process. Additionally, well-written test cases provide internal documentation for all application processing. Test case development is an important part of determining software quality and keeping defects away from customers.
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!!