|
| 1 | +/** |
| 2 | + * Cerberus Copyright (C) 2013 - 2025 cerberustesting |
| 3 | + * 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 modify |
| 8 | + * it under the terms of the GNU General Public License as published by |
| 9 | + * the Free Software Foundation, either version 3 of the License, or |
| 10 | + * (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 of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License |
| 18 | + * along with Cerberus. If not, see <http://www.gnu.org/licenses/>. |
| 19 | + */ |
| 20 | +package org.cerberus.core.apiprivate; |
| 21 | + |
| 22 | +import com.google.gson.Gson; |
| 23 | +import io.swagger.v3.oas.annotations.Operation; |
| 24 | +import org.apache.logging.log4j.LogManager; |
| 25 | +import org.apache.logging.log4j.Logger; |
| 26 | +import org.cerberus.core.api.dto.ai.LogAIUsageMonthlyStatsDTOV001; |
| 27 | +import org.cerberus.core.api.dto.ai.LogAIUsageStatsDTOV001; |
| 28 | +import org.cerberus.core.crud.entity.Robot; |
| 29 | +import org.cerberus.core.crud.entity.UserPrompt; |
| 30 | +import org.cerberus.core.crud.entity.stats.UserPromptStats; |
| 31 | +import org.cerberus.core.crud.service.impl.RobotService; |
| 32 | +import org.cerberus.core.crud.service.impl.UserPromptService; |
| 33 | +import org.cerberus.core.engine.entity.MessageEvent; |
| 34 | +import org.cerberus.core.enums.MessageEventEnum; |
| 35 | +import org.cerberus.core.util.answer.AnswerItem; |
| 36 | +import org.cerberus.core.util.answer.AnswerList; |
| 37 | +import org.cerberus.core.util.datatable.DataTableInformation; |
| 38 | +import org.cerberus.core.util.servlet.ServletUtil; |
| 39 | +import org.json.JSONArray; |
| 40 | +import org.json.JSONException; |
| 41 | +import org.json.JSONObject; |
| 42 | +import org.owasp.html.PolicyFactory; |
| 43 | +import org.owasp.html.Sanitizers; |
| 44 | +import org.springframework.beans.factory.annotation.Autowired; |
| 45 | +import org.springframework.format.annotation.DateTimeFormat; |
| 46 | +import org.springframework.http.HttpStatus; |
| 47 | +import org.springframework.http.ResponseEntity; |
| 48 | +import org.springframework.web.bind.annotation.*; |
| 49 | + |
| 50 | +import javax.servlet.http.HttpServletRequest; |
| 51 | +import java.sql.Timestamp; |
| 52 | +import java.time.LocalDate; |
| 53 | +import java.time.LocalDateTime; |
| 54 | +import java.util.List; |
| 55 | + |
| 56 | +/** |
| 57 | + * @author bcivel |
| 58 | + */ |
| 59 | +@RestController |
| 60 | +@RequestMapping("/robots") |
| 61 | +public class RobotPrivateController { |
| 62 | + |
| 63 | + private static final Logger LOG = LogManager.getLogger(RobotPrivateController.class); |
| 64 | + private final PolicyFactory policy = Sanitizers.FORMATTING.and(Sanitizers.LINKS); |
| 65 | + |
| 66 | + @Autowired |
| 67 | + RobotService robotService; |
| 68 | + |
| 69 | + @Operation(hidden=true) |
| 70 | + @PostMapping("/read") |
| 71 | + public String read(HttpServletRequest request) { |
| 72 | + |
| 73 | + // Calling Servlet Transversal Util. |
| 74 | + ServletUtil.servletStart(request); |
| 75 | + boolean userHasPermissions = request.isUserInRole("Integrator"); |
| 76 | + |
| 77 | + JSONObject object = new JSONObject(); |
| 78 | + try { |
| 79 | + |
| 80 | + AnswerItem<JSONObject> answer = new AnswerItem<>(new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED)); |
| 81 | + AnswerList<Robot> robotList = new AnswerList<>(); |
| 82 | + |
| 83 | + DataTableInformation dti = new DataTableInformation(request, "robot,type,platform,browser,version,isActive,userAgent,screenSize,robotDecli,lbexemethod,description"); |
| 84 | + robotList = robotService.readByCriteria(true, true, dti.getStartPosition(), dti.getLength(), dti.getColumnName(), dti.getSort(), dti.getSearchParameter(), dti.getIndividualSearch()); |
| 85 | + |
| 86 | + JSONArray jsonArray = new JSONArray(); |
| 87 | + if (robotList.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {//the service was able to perform the query, then we should get all values |
| 88 | + for (Robot robot : robotList.getDataList()) { |
| 89 | + Gson gson = new Gson(); |
| 90 | + jsonArray.put(new JSONObject(gson.toJson(robot)).put("hasPermissions", userHasPermissions)); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + object.put("contentTable", jsonArray); |
| 95 | + object.put("hasPermissions", userHasPermissions); |
| 96 | + object.put("iTotalRecords", robotList.getTotalRows()); |
| 97 | + object.put("iTotalDisplayRecords", robotList.getTotalRows()); |
| 98 | + object.put("messageType", "OK"); |
| 99 | + |
| 100 | + } catch (JSONException ex) { |
| 101 | + LOG.warn(ex); |
| 102 | + } |
| 103 | + return object.toString(); |
| 104 | + } |
| 105 | + |
| 106 | + |
| 107 | + /** |
| 108 | + * Read Distinct Value Of Column |
| 109 | + * |
| 110 | + * @param request |
| 111 | + * @return |
| 112 | + */ |
| 113 | + @Operation(hidden=true) |
| 114 | + @GetMapping("readDistinctValueOfColumn") |
| 115 | + public String readDistinctValueOfColumn(HttpServletRequest request, |
| 116 | + @RequestParam("columnName") String columnName) { |
| 117 | + |
| 118 | + boolean userHasPermissions = request.isUserInRole("Integrator"); |
| 119 | + |
| 120 | + JSONObject object = new JSONObject(); |
| 121 | + try { |
| 122 | + DataTableInformation dti = new DataTableInformation(request, "robot,type,platform,browser,version,isActive,userAgent,screenSize,robotDecli,lbexemethod,description"); |
| 123 | + AnswerList robotList = robotService.readDistinctValuesByCriteria(dti.getSearchParameter(), dti.getIndividualSearch(), columnName); |
| 124 | + object.put("distinctValues", robotList.getDataList()); |
| 125 | + |
| 126 | + } catch (JSONException ex) { |
| 127 | + LOG.warn(ex); |
| 128 | + } |
| 129 | + return object.toString(); |
| 130 | + } |
| 131 | + |
| 132 | + |
| 133 | +} |
0 commit comments