How to use processRequest method of org.cerberus.servlet.crud.testdata.UpdateTestDataLib class

Best Cerberus-source code snippet using org.cerberus.servlet.crud.testdata.UpdateTestDataLib.processRequest

Source:UpdateTestDataLib.java Github

copy

Full Screen

...77 * @param response servlet response78 * @throws ServletException if a servlet-specific error occurs79 * @throws IOException if an I/O error occurs80 */81 protected void processRequest(HttpServletRequest request, HttpServletResponse response)82 throws ServletException, IOException {83 JSONObject jsonResponse = new JSONObject();84 ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());85 Answer ans = new Answer();86 MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);87 msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", ""));88 ans.setResultMessage(msg);89 PolicyFactory policy = Sanitizers.FORMATTING.and(Sanitizers.LINKS);90 String charset = request.getCharacterEncoding();91 IFactoryTestDataLibData tdldFactory = appContext.getBean(IFactoryTestDataLibData.class);92 ITestDataLibDataService tdldService = appContext.getBean(ITestDataLibDataService.class);93 IParameterService parameterService = appContext.getBean(IParameterService.class);94 response.setContentType("application/json");95 Map<String, String> fileData = new HashMap<String, String>();96 FileItem file = null;97 FileItemFactory factory = new DiskFileItemFactory();98 ServletFileUpload upload = new ServletFileUpload(factory);99 try {100 List<FileItem> fields = upload.parseRequest(request);101 Iterator<FileItem> it = fields.iterator();102 if (!it.hasNext()) {103 return;104 }105 while (it.hasNext()) {106 FileItem fileItem = it.next();107 boolean isFormField = fileItem.isFormField();108 if (isFormField) {109 fileData.put(fileItem.getFieldName(), ParameterParserUtil.parseStringParamAndDecode(fileItem.getString("UTF-8"), "", charset));110 } else {111 file = fileItem;112 }113 }114 } catch (FileUploadException e) {115 e.printStackTrace();116 }117 /**118 * Parsing and securing all required parameters.119 */120 // Parameter that are already controled by GUI (no need to decode) --> We SECURE them121 String type = policy.sanitize(fileData.get("type"));122 String system = policy.sanitize(fileData.get("system"));123 String environment = policy.sanitize(fileData.get("environment"));124 String country = policy.sanitize(fileData.get("country"));125 String database = policy.sanitize(fileData.get("database"));126 String databaseUrl = policy.sanitize(fileData.get("databaseUrl"));127 String databaseCsv = policy.sanitize(fileData.get("databaseCsv"));128 // Parameter that needs to be secured --> We SECURE+DECODE them129 String name = fileData.get("name"); //this is mandatory130 String group = fileData.get("group");131 String description = fileData.get("libdescription");132 String service = fileData.get("service");133 // Parameter that we cannot secure as we need the html --> We DECODE them134 String script = fileData.get("script");135 String servicePath = fileData.get("servicepath");136 String method = fileData.get("method");137 String envelope = fileData.get("envelope");138 String csvUrl = fileData.get("csvUrl");139 String separator = fileData.get("separator");140 String activateAutoSubdata = fileData.get("subdataCheck");141 Integer testdatalibid = 0;142 boolean testdatalibid_error = true;143 try {144 if (fileData.get("testdatalibid") != null && !fileData.get("testdatalibid").isEmpty()) {145 testdatalibid = Integer.valueOf(fileData.get("testdatalibid"));146 testdatalibid_error = false;147 }148 } catch (NumberFormatException ex) {149 testdatalibid_error = true;150 LOG.warn(ex);151 }152 try {153 // Prepare the final answer.154 MessageEvent msg1 = new MessageEvent(MessageEventEnum.GENERIC_OK);155 Answer finalAnswer = new Answer(msg1);156 /**157 * Checking all constrains before calling the services.158 */159 if (StringUtil.isNullOrEmpty(name)) {160 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);161 msg.setDescription(msg.getDescription().replace("%ITEM%", "Test data library")162 .replace("%OPERATION%", "Update")163 .replace("%REASON%", "Test data library name is missing."));164 finalAnswer.setResultMessage(msg);165 } else if (testdatalibid_error) {166 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);167 msg.setDescription(msg.getDescription().replace("%ITEM%", "Test data library")168 .replace("%OPERATION%", "Update")169 .replace("%REASON%", "Could not manage to convert testdatalibid to an integer value or testdatalibid is missing."));170 finalAnswer.setResultMessage(msg);171 } else {172 /**173 * All data seems cleans so we can call the services.174 */175 //specific attributes176 ITestDataLibService libService = appContext.getBean(ITestDataLibService.class);177 AnswerItem resp = libService.readByKey(testdatalibid);178 if (!(resp.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode()) && resp.getItem() != null)) {179 /**180 * Object could not be found. We stop here and report the181 * error.182 */183 finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) resp);184 } else {185 /**186 * The service was able to perform the query and confirm the187 * object exist, then we can update it.188 */189 TestDataLib lib = (TestDataLib) resp.getItem();190 String fileName = lib.getCsvUrl();191 if (file != null) {192 ans = libService.uploadFile(lib.getTestDataLibID(), file);193 if (ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {194 fileName = file.getName();195 }196 }197 lib.setName(name);198 lib.setType(type);199 lib.setGroup(group);200 lib.setDescription(description);201 lib.setSystem(system);202 lib.setEnvironment(environment);203 lib.setCountry(country);204 lib.setDatabase(database);205 lib.setScript(script);206 lib.setDatabaseUrl(databaseUrl);207 lib.setServicePath(servicePath);208 lib.setService(service);209 lib.setMethod(method);210 lib.setEnvelope(envelope);211 lib.setDatabaseCsv(databaseCsv);212 if (file == null) {213 lib.setCsvUrl(csvUrl);214 } else {215 lib.setCsvUrl(File.separator + lib.getTestDataLibID() + File.separator + fileName);216 }217 lib.setSeparator(separator);218 lib.setLastModifier(request.getRemoteUser());219 ans = libService.update(lib);220 finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) ans);221 if (ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {222 /**223 * Update operation finished with success, then the224 * logging entry must be added.225 */226 ILogEventService logEventService = appContext.getBean(LogEventService.class);227 logEventService.createForPrivateCalls("/UpdateTestDataLib", "UPDATE", "Update TestDataLib - id: " + testdatalibid + " name: " + name + " system: "228 + system + " environment: " + environment + " country: " + country, request);229 }230 List<TestDataLibData> tdldList = new ArrayList();231 // Getting list of SubData from JSON Call232 if (fileData.get("subDataList") != null) {233 JSONArray objSubDataArray = new JSONArray(fileData.get("subDataList"));234 tdldList = getSubDataFromParameter(request, appContext, testdatalibid, objSubDataArray);235 }236 // When File has just been uploaded to servlet and flag to load the subdata value has been checked, we will parse it in order to automatically feed the subdata.237 if (file != null && activateAutoSubdata.equals("1")) {238 String str = "";239 try(BufferedReader reader = new BufferedReader(new FileReader(parameterService.getParameterStringByKey("cerberus_testdatalibcsv_path", "", null) + lib.getCsvUrl()));) {240 // First line of the file is split by separator.241 str = reader.readLine();242 String[] subData = (!lib.getSeparator().isEmpty()) ? str.split(lib.getSeparator()) : str.split(",");243 // We take the subdata from the servlet input.244 TestDataLibData firstLine = tdldList.get(0);245 tdldList = new ArrayList();246 firstLine.setColumnPosition("1");247 tdldList.add(firstLine);248 int i = 1;249 for (String item : subData) {250 String subdataName = "SUBDATA" + i;251 TestDataLibData tdld = tdldFactory.create(null, testdatalibid, subdataName, item, null, null, Integer.toString(i), null);252 tdldList.add(tdld);253 i++;254 }255 // Update the Database with the new list.256 } finally {257 try {258 file.getInputStream().close();259 } catch (Throwable ignore) {260 }261 }262 }263 ans = tdldService.compareListAndUpdateInsertDeleteElements(testdatalibid, tdldList);264 finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) ans);265 }266 }267 jsonResponse.put("messageType", finalAnswer.getResultMessage().getMessage().getCodeString());268 jsonResponse.put("message", finalAnswer.getResultMessage().getDescription());269 response.getWriter().print(jsonResponse);270 response.getWriter().flush();271 } catch (JSONException ex) {272 LOG.warn(ex);273 //returns a default error message with the json format that is able to be parsed by the client-side274 response.getWriter().print(AnswerUtil.createGenericErrorAnswer());275 }276 }277 private List<TestDataLibData> getSubDataFromParameter(HttpServletRequest request, ApplicationContext appContext, int testDataLibId, JSONArray json) throws JSONException {278 List<TestDataLibData> tdldList = new ArrayList();279 IFactoryTestDataLibData tdldFactory = appContext.getBean(IFactoryTestDataLibData.class);280 PolicyFactory policy = Sanitizers.FORMATTING.and(Sanitizers.LINKS);281 String charset = request.getCharacterEncoding();282 for (int i = 0; i < json.length(); i++) {283 JSONObject objectJson = json.getJSONObject(i);284 // Parameter that are already controled by GUI (no need to decode) --> We SECURE them285 boolean delete = objectJson.getBoolean("toDelete");286 Integer testDataLibDataId = objectJson.getInt("testDataLibDataID");287 // Parameter that needs to be secured --> We SECURE+DECODE them288 // NONE289 // Parameter that we cannot secure as we need the html --> We DECODE them290 String subdata = ParameterParserUtil.parseStringParamAndDecode(objectJson.getString("subData"), "", charset);291 String value = ParameterParserUtil.parseStringParamAndDecode(objectJson.getString("value"), "", charset);292 String column = ParameterParserUtil.parseStringParamAndDecode(objectJson.getString("column"), "", charset);293 String parsingAnswer = ParameterParserUtil.parseStringParam(objectJson.getString("parsingAnswer"), "");294 String columnPosition = ParameterParserUtil.parseStringParamAndDecode(objectJson.getString("columnPosition"), "", charset);295 String description = ParameterParserUtil.parseStringParamAndDecode(objectJson.getString("description"), "", charset);296 if (!delete) {297 TestDataLibData tdld = tdldFactory.create(testDataLibDataId, testDataLibId, subdata, value, column, parsingAnswer, columnPosition, description);298 tdldList.add(tdld);299 }300 }301 return tdldList;302 }303 // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">304 /**305 * Handles the HTTP <code>GET</code> method.306 *307 * @param request servlet request308 * @param response servlet response309 * @throws ServletException if a servlet-specific error occurs310 * @throws IOException if an I/O error occurs311 */312 @Override313 protected void doGet(HttpServletRequest request, HttpServletResponse response)314 throws ServletException, IOException {315 processRequest(request, response);316 }317 /**318 * Handles the HTTP <code>POST</code> method.319 *320 * @param request servlet request321 * @param response servlet response322 * @throws ServletException if a servlet-specific error occurs323 * @throws IOException if an I/O error occurs324 */325 @Override326 protected void doPost(HttpServletRequest request, HttpServletResponse response)327 throws ServletException, IOException {328 processRequest(request, response);329 }330 /**331 * Returns a short description of the servlet.332 *333 * @return a String containing servlet description334 */335 @Override336 public String getServletInfo() {337 return "Short description";338 }// </editor-fold>339}...

Full Screen

Full Screen

processRequest

Using AI Code Generation

copy

Full Screen

1 processRequest(request, response);2 }3 protected void processRequest(HttpServletRequest request, HttpServletResponse response)4 throws ServletException, IOException {5 response.setContentType("text/html;charset=UTF-8");6 try (PrintWriter out = response.getWriter()) {7 out.println("8");

Full Screen

Full Screen

processRequest

Using AI Code Generation

copy

Full Screen

1 public void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {2 response.setContentType("text/html;charset=UTF-8");3 try (PrintWriter out = response.getWriter()) {4 String test = request.getParameter("test");5 String testdatalib = request.getParameter("testdatalib");6 String system = request.getParameter("system");7 String country = request.getParameter("country");8 String environment = request.getParameter("environment");9 String database = request.getParameter("database");10 String environmentData = request.getParameter("environmentData");11 String data = request.getParameter("data");12 String group = request.getParameter("group");13 String description = request.getParameter("description");14 String servicePath = request.getParameter("servicePath");15 String method = request.getParameter("method");16 String envelope = request.getParameter("envelope");17 String service = request.getParameter("service");18 String operation = request.getParameter("operation");19 String contentType = request.getParameter("contentType");20 String parsingAnswer = request.getParameter("parsingAnswer");21 String parsingAnswerElement = request.getParameter("parsingAnswerElement");22 String parsingSoapFault = request.getParameter("parsingSoapFault");23 String parsingSoapFaultElement = request.getParameter("parsingSoapFaultElement");24 String type = request.getParameter("type");25 String subData = request.getParameter("subdata");26 String subLength = request.getParameter("sublength");27 String subNature = request.getParameter("subnature");28 String nature = request.getParameter("nature");29 String databaseUrl = request.getParameter("databaseUrl");30 String databaseLogin = request.getParameter("databaseLogin");31 String databasePassword = request.getParameter("databasePassword");32 String databaseScript = request.getParameter("databaseScript");33 String databaseSqlScript = request.getParameter("databaseSqlScript");34 String databaseSqlScriptLib = request.getParameter("databaseSqlScriptLib");35 String databaseSqlCreate = request.getParameter("databaseSqlCreate");36 String databaseSqlDrop = request.getParameter("databaseSqlDrop");37 String databaseSqlLib = request.getParameter("databaseSqlLib");38 String databaseCsv = request.getParameter("databaseCsv");39 String databaseCsvUrl = request.getParameter("databaseCsvUrl");40 String databaseCsvUser = request.getParameter("databaseCsvUser");41 String databaseCsvPassword = request.getParameter("databaseCsvPassword");42 String databaseCsvSeparator = request.getParameter("databaseCsvSeparator");43 String databaseCsvDelimiter = request.getParameter("databaseCsvDelimiter");44 String databaseCsvEscape = request.getParameter("databaseCsvEscape");

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Cerberus-source automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in UpdateTestDataLib

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful