|
| 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.api.controllers; |
| 21 | + |
| 22 | +import com.fasterxml.jackson.annotation.JsonView; |
| 23 | +import io.swagger.v3.oas.annotations.Operation; |
| 24 | +import io.swagger.v3.oas.annotations.Parameter; |
| 25 | +import io.swagger.v3.oas.annotations.media.Content; |
| 26 | +import io.swagger.v3.oas.annotations.media.Schema; |
| 27 | +import io.swagger.v3.oas.annotations.responses.ApiResponse; |
| 28 | +import io.swagger.v3.oas.annotations.tags.Tag; |
| 29 | +import lombok.AllArgsConstructor; |
| 30 | +import org.apache.logging.log4j.LogManager; |
| 31 | +import org.apache.logging.log4j.Logger; |
| 32 | +import org.cerberus.core.api.controllers.wrappers.ResponseWrapper; |
| 33 | +import org.cerberus.core.api.dto.application.CountryEnvironmentParametersDTOV001; |
| 34 | +import org.cerberus.core.api.dto.robot.RobotDTOV001; |
| 35 | +import org.cerberus.core.api.dto.robot.RobotExecutorDTOV001; |
| 36 | +import org.cerberus.core.api.dto.robot.RobotExecutorMapperV001; |
| 37 | +import org.cerberus.core.api.dto.robot.RobotMapperV001; |
| 38 | +import org.cerberus.core.api.dto.views.View; |
| 39 | +import org.cerberus.core.api.exceptions.EntityNotFoundException; |
| 40 | +import org.cerberus.core.api.services.PublicApiAuthenticationService; |
| 41 | +import org.cerberus.core.crud.entity.Application; |
| 42 | +import org.cerberus.core.crud.entity.LogEvent; |
| 43 | +import org.cerberus.core.crud.entity.RobotExecutor; |
| 44 | +import org.cerberus.core.crud.service.ILogEventService; |
| 45 | +import org.cerberus.core.crud.service.impl.RobotExecutorService; |
| 46 | +import org.cerberus.core.crud.service.impl.RobotService; |
| 47 | +import org.cerberus.core.exception.CerberusException; |
| 48 | +import org.springframework.http.HttpStatus; |
| 49 | +import org.springframework.http.MediaType; |
| 50 | +import org.springframework.web.bind.annotation.*; |
| 51 | + |
| 52 | +import javax.servlet.http.HttpServletRequest; |
| 53 | +import javax.validation.Valid; |
| 54 | +import java.security.Principal; |
| 55 | + |
| 56 | +/** |
| 57 | + * @author bcivel |
| 58 | + */ |
| 59 | +@AllArgsConstructor |
| 60 | +@Tag(name = "Robot", description = "Endpoints related to Robots") |
| 61 | +@RestController |
| 62 | +@RequestMapping(path = "/public/robots") |
| 63 | +public class RobotController { |
| 64 | + |
| 65 | + private static final String API_VERSION_1 = "X-API-VERSION=1"; |
| 66 | + private static final String API_KEY = "X-API-KEY"; |
| 67 | + |
| 68 | + private final RobotService robotService; |
| 69 | + private final RobotExecutorService robotExecutorService; |
| 70 | + private final RobotMapperV001 robotMapper; |
| 71 | + private final RobotExecutorMapperV001 robotExecutorMapper; |
| 72 | + private final ILogEventService logEventService; |
| 73 | + private final PublicApiAuthenticationService apiAuthenticationService; |
| 74 | + |
| 75 | + private static final Logger LOG = LogManager.getLogger(RobotController.class); |
| 76 | + |
| 77 | + //FIND ROBOT BY NAME |
| 78 | + @GetMapping(path = "/{robot}", headers = API_VERSION_1, produces = MediaType.APPLICATION_JSON_VALUE) |
| 79 | + @Operation( |
| 80 | + summary = "Get Robot", |
| 81 | + description = "Get a robot by its name", |
| 82 | + responses = { |
| 83 | + @ApiResponse(responseCode = "200", description = "Found the robot", content = { @Content(mediaType = "application/json",schema = @Schema(implementation = RobotDTOV001.class))}), |
| 84 | + } |
| 85 | + ) |
| 86 | + @JsonView(View.Public.GET.class) |
| 87 | + @ResponseStatus(HttpStatus.OK) |
| 88 | + public ResponseWrapper<RobotDTOV001> findRobotByIdName( |
| 89 | + @Parameter(description = "Robot name") @PathVariable("robot") String robot, |
| 90 | + @Parameter(description = "X-API-KEY for authentication") @RequestHeader(name = API_KEY, required = false) String apiKey, |
| 91 | + @Parameter(hidden = true) HttpServletRequest request, |
| 92 | + @Parameter(hidden = true) Principal principal) throws CerberusException { |
| 93 | + |
| 94 | + String login = this.apiAuthenticationService.authenticateLogin(principal, apiKey); |
| 95 | + logEventService.createForPublicCalls("/public/robots", "CALL-GET", LogEvent.STATUS_INFO, String.format("API /applications called with URL: %s", request.getRequestURL()), request, login); |
| 96 | + |
| 97 | + return ResponseWrapper.wrap( |
| 98 | + this.robotMapper.toDTO( |
| 99 | + this.robotService.readByKey(robot) |
| 100 | + ) |
| 101 | + ); |
| 102 | + } |
| 103 | + |
| 104 | + |
| 105 | + //PATCH ROBOT EXECUTOR |
| 106 | + @PatchMapping(path = "/{robot}/{executor}", headers = {API_VERSION_1}, produces = MediaType.APPLICATION_JSON_VALUE) |
| 107 | + @Operation( |
| 108 | + summary = "Patch Robot Executor", |
| 109 | + description = "Patch an robot executor", |
| 110 | + responses = { |
| 111 | + @ApiResponse(responseCode = "200", description = "Patch the robot executor", content = { @Content(mediaType = "application/json",schema = @Schema(implementation = ResponseWrapper.class))}), |
| 112 | + } |
| 113 | + ) |
| 114 | + @JsonView(View.Public.GET.class) |
| 115 | + @ResponseStatus(HttpStatus.OK) |
| 116 | + public ResponseWrapper<RobotExecutorDTOV001> patchExecutor( |
| 117 | + @Parameter(description = "Robot name") @PathVariable("robot") String robot, |
| 118 | + @Parameter(description = "Executor name") @PathVariable("executor") String executor, |
| 119 | + @Parameter(description = "X-API-KEY for authentication") @RequestHeader(name = API_KEY, required = false) String apiKey, |
| 120 | + @Valid @JsonView(View.Public.PATCH.class) @RequestBody RobotExecutorDTOV001 robotExecutorToUpdate, |
| 121 | + @Parameter(hidden = true) HttpServletRequest request, |
| 122 | + @Parameter(hidden = true) Principal principal) throws CerberusException { |
| 123 | + |
| 124 | + String login = this.apiAuthenticationService.authenticateLogin(principal, apiKey); |
| 125 | + logEventService.createForPublicCalls("/public/robots/", "CALL-PATCH", LogEvent.STATUS_INFO, String.format("API /robots called with URL: %s", request.getRequestURL()), request, login); |
| 126 | + |
| 127 | + // We first get the application in order to retreive the system. |
| 128 | + RobotExecutor robotExecutor = this.robotExecutorService.readByKey(robot, executor).getItem(); |
| 129 | + if (robotExecutor == null) { |
| 130 | + throw new EntityNotFoundException(RobotExecutor.class, "robotExecutor", robot + "|" + executor); |
| 131 | + } |
| 132 | + |
| 133 | + LOG.warn(robotExecutorToUpdate.toString()); |
| 134 | + |
| 135 | + return ResponseWrapper.wrap( |
| 136 | + this.robotExecutorMapper.toDTO( |
| 137 | + this.robotExecutorService.updateRobotExecutorPATCH( |
| 138 | + robot, |
| 139 | + executor, |
| 140 | + robotExecutorToUpdate, |
| 141 | + principal, |
| 142 | + login |
| 143 | + )) |
| 144 | + ); |
| 145 | + } |
| 146 | + |
| 147 | +} |
0 commit comments