public static void checkBadRequest(boolean condition, String message, Object... args) { if (!condition) { badRequest(message, args); } }
public static <T> T checkNotNullBadRequest(T value, String message, Object... args) { if (value == null) { badRequest(message, args); } return value; }
private String getRunId(Optional<String> maybeRunId) { if (maybeRunId.isPresent()) { String runId = maybeRunId.get(); if (runId.length() > 100) { throw badRequest("RunId must be less than 100 characters. RunId %s has %s characters", runId, runId.length()); } else { return runId; } } else { return UUID.randomUUID().toString(); } }
private void checkForValidRFC5545Schedule(String schedule) { try { new RecurrenceRule(schedule); } catch (InvalidRecurrenceRuleException ex) { badRequest("Schedule %s is not a valid RFC5545 schedule, error is: %s", schedule, ex); } }
public SingularityWebhook checkSingularityWebhook(SingularityWebhook webhook) { checkNotNull(webhook, "Webhook is null"); checkNotNull(webhook.getUri(), "URI is null"); try { new URI(webhook.getUri()); } catch (URISyntaxException e) { badRequest("Invalid URI provided"); } return webhook; }
protected SingularityTaskId getTaskIdObject(String taskId) { try { return SingularityTaskId.valueOf(taskId); } catch (InvalidSingularityTaskIdException e) { throw badRequest("%s is not a valid task id: %s", taskId, e.getMessage()); } }
private SingularityPendingTaskId getPendingTaskIdFromStr(String pendingTaskIdStr) { try { return SingularityPendingTaskId.valueOf(pendingTaskIdStr); } catch (InvalidSingularityTaskIdException e) { throw badRequest("%s is not a valid pendingTaskId: %s", pendingTaskIdStr, e.getMessage()); } }
private SingularityTaskId getTaskIdFromStr(String activeTaskIdStr) { try { return SingularityTaskId.valueOf(activeTaskIdStr); } catch (InvalidSingularityTaskIdException e) { throw badRequest("%s is not a valid taskId: %s", activeTaskIdStr, e.getMessage()); } }
break; default: badRequest("Schedule %s is invalid, day of week (%s) is not 0-7", schedule, dayOfWeekValue); break;
private SingularityTaskHistory checkHistory(String taskId, SingularityUser user) { final SingularityTaskId taskIdObj = getTaskIdObject(taskId); final SingularityTaskHistory taskHistory = getTaskHistoryRequired(taskIdObj, user); if (!taskHistory.getDirectory().isPresent()) { logSupport.checkDirectoryAndContainerId(taskIdObj); throw badRequest("Task %s does not have a directory yet - check again soon (enqueued request to refetch)", taskId); } return taskHistory; }
public void checkValidShellCommand(final SingularityShellCommand shellCommand) { Optional<ShellCommandDescriptor> commandDescriptor = Iterables.tryFind(uiConfiguration.getShellCommands(), new Predicate<ShellCommandDescriptor>() { @Override public boolean apply(ShellCommandDescriptor input) { return input.getName().equals(shellCommand.getName()); } }); if (!commandDescriptor.isPresent()) { throw WebExceptions.badRequest("Shell command %s not in %s", shellCommand.getName(), uiConfiguration.getShellCommands()); } Set<String> options = Sets.newHashSetWithExpectedSize(commandDescriptor.get().getOptions().size()); for (ShellCommandOptionDescriptor option : commandDescriptor.get().getOptions()) { options.add(option.getName()); } if (shellCommand.getOptions().isPresent()) { for (String option : shellCommand.getOptions().get()) { if (!options.contains(option)) { throw WebExceptions.badRequest("Shell command %s does not have option %s (%s)", shellCommand.getName(), option, options); } } } } }
private SingularityDeploy checkImmediateRunDeploy(SingularityRequest request, SingularityDeploy deploy, SingularityRunNowRequest runNowRequest, List<SingularityTaskId> activeTasks, List<SingularityPendingTaskId> pendingTasks) { if (!request.isScheduled() && !request.isOneOff()) { throw badRequest("Can not request an immediate run of a non-scheduled / always running request (%s)", request); } return deploy.toBuilder() .setRunImmediately(Optional.of(fillRunNowRequest(Optional.of(runNowRequest)))) .build(); }
public void checkForAuthorizationByTaskId(String taskId, SingularityUser user, SingularityAuthorizationScope scope) { if (authEnabled) { checkForbidden(user.isAuthenticated(), "Not Authenticated!"); try { final SingularityTaskId taskIdObj = SingularityTaskId.valueOf(taskId); final Optional<SingularityRequestWithState> maybeRequest = requestManager.getRequest(taskIdObj.getRequestId()); if (maybeRequest.isPresent()) { checkForAuthorization(maybeRequest.get().getRequest(), user, scope); } } catch (InvalidSingularityTaskIdException e) { badRequest(e.getMessage()); } } }
throw badRequest("Can not request an immediate run of a non-scheduled / always running request (%s)", request); throw badRequest("Task launch delay can be at most %d days from now.", maxRunNowTaskLaunchDelay);
@POST @Path("/task/{taskId}/command") @Operation( summary = "Run a configured shell command against the given task", responses = { @ApiResponse(responseCode = "400", description = "Given shell command option doesn't exist"), @ApiResponse(responseCode = "403", description = "Given shell command doesn't exist") } ) @Consumes({ MediaType.APPLICATION_JSON }) public SingularityTaskShellCommandRequest runShellCommand( @Parameter(hidden = true) @Auth SingularityUser user, @Parameter(required = true, description = "Id of the task")@PathParam("taskId") String taskId, @RequestBody(required = true, description = "Object describing the command to be run") final SingularityShellCommand shellCommand) { SingularityTaskId taskIdObj = getTaskIdFromStr(taskId); authorizationHelper.checkForAuthorizationByTaskId(taskId, user, SingularityAuthorizationScope.WRITE); validator.checkActionEnabled(SingularityAction.RUN_SHELL_COMMAND); if (!taskManager.isActiveTask(taskId)) { throw WebExceptions.badRequest("%s is not an active task, can't run %s on it", taskId, shellCommand.getName()); } return startShellCommand(taskIdObj, shellCommand, user); }
badRequest("scheduleTimeZone %s does not map to a valid Java TimeZone object (e.g. 'US/Eastern' or 'GMT')", request.getScheduleTimeZone().get());
public static <T> T checkNotNullBadRequest(T value, String message, Object... args) { if (value == null) { badRequest(message, args); } return value; }
public SingularityWebhook checkSingularityWebhook(SingularityWebhook webhook) { checkNotNull(webhook, "Webhook is null"); checkNotNull(webhook.getUri(), "URI is null"); try { new URI(webhook.getUri()); } catch (URISyntaxException e) { badRequest("Invalid URI provided"); } return webhook; }
private SingularityTaskId getTaskIdFromStr(String activeTaskIdStr) { try { return SingularityTaskId.valueOf(activeTaskIdStr); } catch (InvalidSingularityTaskIdException e) { throw badRequest("%s is not a valid taskId: %s", activeTaskIdStr, e.getMessage()); } }
private SingularityDeploy checkImmediateRunDeploy(SingularityRequest request, SingularityDeploy deploy, SingularityRunNowRequest runNowRequest, List<SingularityTaskId> activeTasks, List<SingularityPendingTaskId> pendingTasks) { if (!request.isScheduled() && !request.isOneOff()) { throw badRequest("Can not request an immediate run of a non-scheduled / always running request (%s)", request); } return deploy.toBuilder() .setRunImmediately(Optional.of(fillRunNowRequest(Optional.of(runNowRequest)))) .build(); }