UrlsWorker/src/main/java/eu/openaire/urls_worker/controllers/GeneralController.java

107 lines
4.0 KiB
Java

package eu.openaire.urls_worker.controllers;
import eu.openaire.urls_worker.UrlsWorkerApplication;
import eu.openaire.urls_worker.util.UriBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("")
public class GeneralController {
private static final Logger logger = LoggerFactory.getLogger(GeneralController.class);
public GeneralController() {}
@GetMapping("isAlive")
public ResponseEntity<?> isWorkerAlive() {
logger.info("Received an \"isAlive\" request.");
return ResponseEntity.ok().build();
}
public static boolean shouldShutdownWorker = false;
@GetMapping("shutdownWorker/{shutdownCode}")
public ResponseEntity<?> shutdownWorkerGracefully(@PathVariable String shutdownCode, HttpServletRequest request)
{
String initMsg = "Received a \"shutdownWorker\" request.";
ResponseEntity<?> responseEntity = passSecurityChecks(request, shutdownCode, initMsg);
if ( responseEntity != null )
return responseEntity;
shouldShutdownWorker = true;
logger.info(initMsg + " The worker will shutdown, after finishing current work.");
return ResponseEntity.ok().build();
}
@GetMapping("cancelShutdownWorker/{cancelCode}")
public ResponseEntity<?> cancelShutdownWorkerGracefully(@PathVariable String cancelCode, HttpServletRequest request)
{
String initMsg = "Received a \"cancelShutdownWorker\" request.";
ResponseEntity<?> responseEntity = passSecurityChecks(request, cancelCode, initMsg);
if ( responseEntity != null )
return responseEntity;
shouldShutdownWorker = false;
logger.info(initMsg + " Any previous \"shutdownWorker\"-request is canceled. The \"maxAssignmentsBatchesToHandleBeforeShutdown\" will still be honored (if it's set).");
return ResponseEntity.ok().build();
}
@GetMapping("getHandledAssignmentsCounts")
public ResponseEntity<?> getHandledAssignmentsCounts()
{
List<Long> handledAssignmentsCounts = new ArrayList<>(FullTextsController.assignmentsNumsHandledAndLocallyDeleted.size()/2);
for ( Map.Entry<Long,Boolean> entry : FullTextsController.assignmentsNumsHandledAndLocallyDeleted.entrySet() )
{
if ( entry.getValue().equals(true) )
handledAssignmentsCounts.add(entry.getKey());
}
return ResponseEntity.ok(handledAssignmentsCounts);
}
public static ResponseEntity<?> passSecurityChecks(HttpServletRequest request, String code, String initMsg)
{
if ( request == null ) {
logger.error(initMsg + " The \"HttpServletRequest\" is null!");
return ResponseEntity.internalServerError().build();
}
String remoteAddr = request.getHeader("X-FORWARDED-FOR");
if ( remoteAddr == null || "".equals(remoteAddr) )
remoteAddr = request.getRemoteAddr();
if ( ! (remoteAddr.equals("127.0.0.1") || remoteAddr.equals(UriBuilder.ip)) ) {
logger.error(initMsg + " The request came from another IP: " + remoteAddr + " | while this worker has the IP: " + UriBuilder.ip);
return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
}
if ( !code.equals(UrlsWorkerApplication.shutdownOrCancelCode) ) {
String errorMsg = initMsg + " But, it contains an invalid code: " + code;
logger.error(errorMsg);
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(errorMsg);
}
return null; // The checks are passing.
}
}