package eu.openaire.urls_worker.components; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Component; import org.springframework.web.client.HttpServerErrorException; import org.springframework.web.client.RestTemplate; @Component public class ConnWithController { private static final Logger logger = LoggerFactory.getLogger(ConnWithController.class); private final String controllerBaseUrl; public ConnWithController(@Value("${info.controllerBaseUrl}") String controllerBaseUrl) { this.controllerBaseUrl = controllerBaseUrl; } public boolean postShutdownReportToController(String workerId) { logger.info("Going to \"postShutdownReportToController\"."); try { ResponseEntity responseEntity = new RestTemplate().postForEntity(this.controllerBaseUrl + "workerShutdownReport?workerId=" + workerId, null, String.class); int responseCode = responseEntity.getStatusCodeValue(); if ( responseCode != HttpStatus.OK.value() ) { logger.error("HTTP-Connection problem with the submission of the \"postShutdownReportToController\"! Error-code was: " + responseCode); return false; } } catch (HttpServerErrorException hsee) { logger.error("The Controller failed to handle the \"postShutdownReportToController\": " + hsee.getMessage()); return false; } catch (Exception e) { logger.error("Error for \"postShutdownReportToController\" to the Controller.", e); return false; } return true; } }