Java Code Examples for org.apache.flink.runtime.rest.handler.HandlerRequest#getRequestBody()

The following examples show how to use org.apache.flink.runtime.rest.handler.HandlerRequest#getRequestBody() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JarRunHandler.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
private SavepointRestoreSettings getSavepointRestoreSettings(
		final @Nonnull HandlerRequest<JarRunRequestBody, JarRunMessageParameters> request)
			throws RestHandlerException {

	final JarRunRequestBody requestBody = request.getRequestBody();

	final boolean allowNonRestoredState = fromRequestBodyOrQueryParameter(
		requestBody.getAllowNonRestoredState(),
		() -> getQueryParameter(request, AllowNonRestoredStateQueryParameter.class),
		false,
		log);
	final String savepointPath = fromRequestBodyOrQueryParameter(
		emptyToNull(requestBody.getSavepointPath()),
		() -> emptyToNull(getQueryParameter(request, SavepointPathQueryParameter.class)),
		null,
		log);
	final SavepointRestoreSettings savepointRestoreSettings;
	if (savepointPath != null) {
		savepointRestoreSettings = SavepointRestoreSettings.forPath(
			savepointPath,
			allowNonRestoredState);
	} else {
		savepointRestoreSettings = SavepointRestoreSettings.none();
	}
	return savepointRestoreSettings;
}
 
Example 2
Source File: JarRunHandler.java    From flink with Apache License 2.0 6 votes vote down vote up
private SavepointRestoreSettings getSavepointRestoreSettings(
		final @Nonnull HandlerRequest<JarRunRequestBody, JarRunMessageParameters> request)
			throws RestHandlerException {

	final JarRunRequestBody requestBody = request.getRequestBody();

	final boolean allowNonRestoredState = fromRequestBodyOrQueryParameter(
		requestBody.getAllowNonRestoredState(),
		() -> getQueryParameter(request, AllowNonRestoredStateQueryParameter.class),
		false,
		log);
	final String savepointPath = fromRequestBodyOrQueryParameter(
		emptyToNull(requestBody.getSavepointPath()),
		() -> emptyToNull(getQueryParameter(request, SavepointPathQueryParameter.class)),
		null,
		log);
	final SavepointRestoreSettings savepointRestoreSettings;
	if (savepointPath != null) {
		savepointRestoreSettings = SavepointRestoreSettings.forPath(
			savepointPath,
			allowNonRestoredState);
	} else {
		savepointRestoreSettings = SavepointRestoreSettings.none();
	}
	return savepointRestoreSettings;
}
 
Example 3
Source File: JarRunHandler.java    From flink with Apache License 2.0 6 votes vote down vote up
private SavepointRestoreSettings getSavepointRestoreSettings(
		final @Nonnull HandlerRequest<JarRunRequestBody, JarRunMessageParameters> request)
			throws RestHandlerException {

	final JarRunRequestBody requestBody = request.getRequestBody();

	final boolean allowNonRestoredState = fromRequestBodyOrQueryParameter(
		requestBody.getAllowNonRestoredState(),
		() -> getQueryParameter(request, AllowNonRestoredStateQueryParameter.class),
		false,
		log);
	final String savepointPath = fromRequestBodyOrQueryParameter(
		emptyToNull(requestBody.getSavepointPath()),
		() -> emptyToNull(getQueryParameter(request, SavepointPathQueryParameter.class)),
		null,
		log);
	final SavepointRestoreSettings savepointRestoreSettings;
	if (savepointPath != null) {
		savepointRestoreSettings = SavepointRestoreSettings.forPath(
			savepointPath,
			allowNonRestoredState);
	} else {
		savepointRestoreSettings = SavepointRestoreSettings.none();
	}
	return savepointRestoreSettings;
}
 
Example 4
Source File: JobSubmitHandler.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
protected CompletableFuture<JobSubmitResponseBody> handleRequest(@Nonnull HandlerRequest<JobSubmitRequestBody, EmptyMessageParameters> request, @Nonnull DispatcherGateway gateway) throws RestHandlerException {
	final Collection<File> uploadedFiles = request.getUploadedFiles();
	final Map<String, Path> nameToFile = uploadedFiles.stream().collect(Collectors.toMap(
		File::getName,
		Path::fromLocalFile
	));

	if (uploadedFiles.size() != nameToFile.size()) {
		throw new RestHandlerException(
			String.format("The number of uploaded files was %s than the expected count. Expected: %s Actual %s",
				uploadedFiles.size() < nameToFile.size() ? "lower" : "higher",
				nameToFile.size(),
				uploadedFiles.size()),
			HttpResponseStatus.BAD_REQUEST
		);
	}

	final JobSubmitRequestBody requestBody = request.getRequestBody();

	if (requestBody.jobGraphFileName == null) {
		throw new RestHandlerException(
			String.format("The %s field must not be omitted or be null.",
				JobSubmitRequestBody.FIELD_NAME_JOB_GRAPH),
			HttpResponseStatus.BAD_REQUEST);
	}

	CompletableFuture<JobGraph> jobGraphFuture = loadJobGraph(requestBody, nameToFile);

	Collection<Path> jarFiles = getJarFilesToUpload(requestBody.jarFileNames, nameToFile);

	Collection<Tuple2<String, Path>> artifacts = getArtifactFilesToUpload(requestBody.artifactFileNames, nameToFile);

	CompletableFuture<JobGraph> finalizedJobGraphFuture = uploadJobGraphFiles(gateway, jobGraphFuture, jarFiles, artifacts, configuration);

	CompletableFuture<Acknowledge> jobSubmissionFuture = finalizedJobGraphFuture.thenCompose(jobGraph -> gateway.submitJob(jobGraph, timeout));

	return jobSubmissionFuture.thenCombine(jobGraphFuture,
		(ack, jobGraph) -> new JobSubmitResponseBody("/jobs/" + jobGraph.getJobID()));
}
 
Example 5
Source File: MultipartUploadResource.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
protected CompletableFuture<EmptyResponseBody> handleRequest(@Nonnull HandlerRequest<TestRequestBody, EmptyMessageParameters> request, @Nonnull RestfulGateway gateway) throws RestHandlerException {
	Collection<Path> uploadedFiles = request.getUploadedFiles().stream().map(File::toPath).collect(Collectors.toList());
	if (!uploadedFiles.isEmpty()) {
		throw new RestHandlerException("This handler should not have received file uploads.", HttpResponseStatus.INTERNAL_SERVER_ERROR);
	}
	this.lastReceivedRequest = request.getRequestBody();
	return CompletableFuture.completedFuture(EmptyResponseBody.getInstance());
}
 
Example 6
Source File: RestServerEndpointITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
protected CompletableFuture<TestResponse> handleRequest(@Nonnull HandlerRequest<TestRequest, TestParameters> request, RestfulGateway gateway) {
	assertEquals(request.getPathParameter(JobIDPathParameter.class), PATH_JOB_ID);
	assertEquals(request.getQueryParameter(JobIDQueryParameter.class).get(0), QUERY_JOB_ID);

	final int id = request.getRequestBody().id;
	return handlerBody.apply(id);
}
 
Example 7
Source File: JobSubmitHandler.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
protected CompletableFuture<JobSubmitResponseBody> handleRequest(@Nonnull HandlerRequest<JobSubmitRequestBody, EmptyMessageParameters> request, @Nonnull DispatcherGateway gateway) throws RestHandlerException {
	final Collection<File> uploadedFiles = request.getUploadedFiles();
	final Map<String, Path> nameToFile = uploadedFiles.stream().collect(Collectors.toMap(
		File::getName,
		Path::fromLocalFile
	));

	if (uploadedFiles.size() != nameToFile.size()) {
		throw new RestHandlerException(
			String.format("The number of uploaded files was %s than the expected count. Expected: %s Actual %s",
				uploadedFiles.size() < nameToFile.size() ? "lower" : "higher",
				nameToFile.size(),
				uploadedFiles.size()),
			HttpResponseStatus.BAD_REQUEST
		);
	}

	final JobSubmitRequestBody requestBody = request.getRequestBody();

	if (requestBody.jobGraphFileName == null) {
		throw new RestHandlerException(
			String.format("The %s field must not be omitted or be null.",
				JobSubmitRequestBody.FIELD_NAME_JOB_GRAPH),
			HttpResponseStatus.BAD_REQUEST);
	}

	CompletableFuture<JobGraph> jobGraphFuture = loadJobGraph(requestBody, nameToFile);

	Collection<Path> jarFiles = getJarFilesToUpload(requestBody.jarFileNames, nameToFile);

	Collection<Tuple2<String, Path>> artifacts = getArtifactFilesToUpload(requestBody.artifactFileNames, nameToFile);

	CompletableFuture<JobGraph> finalizedJobGraphFuture = uploadJobGraphFiles(gateway, jobGraphFuture, jarFiles, artifacts, configuration);

	CompletableFuture<Acknowledge> jobSubmissionFuture = finalizedJobGraphFuture.thenCompose(jobGraph -> gateway.submitJob(jobGraph, timeout));

	return jobSubmissionFuture.thenCombine(jobGraphFuture,
		(ack, jobGraph) -> new JobSubmitResponseBody("/jobs/" + jobGraph.getJobID()));
}
 
Example 8
Source File: JarHandlerUtils.java    From flink with Apache License 2.0 5 votes vote down vote up
/** Parse program arguments in jar run or plan request. */
private static <R extends JarRequestBody, M extends MessageParameters> List<String> getProgramArgs(
		HandlerRequest<R, M> request, Logger log) throws RestHandlerException {
	JarRequestBody requestBody = request.getRequestBody();
	@SuppressWarnings("deprecation")
	List<String> programArgs = tokenizeArguments(
		fromRequestBodyOrQueryParameter(
			emptyToNull(requestBody.getProgramArguments()),
			() -> getQueryParameter(request, ProgramArgsQueryParameter.class),
			null,
			log));
	List<String> programArgsList =
		fromRequestBodyOrQueryParameter(
			requestBody.getProgramArgumentsList(),
			() -> request.getQueryParameter(ProgramArgQueryParameter.class),
			null,
			log);
	if (!programArgsList.isEmpty()) {
		if (!programArgs.isEmpty()) {
			throw new RestHandlerException(
				"Confusing request: programArgs and programArgsList are specified, please, use only programArgsList",
				HttpResponseStatus.BAD_REQUEST);
		}
		return programArgsList;
	} else {
		return programArgs;
	}
}
 
Example 9
Source File: JarHandlerUtils.java    From flink with Apache License 2.0 5 votes vote down vote up
public static <R extends JarRequestBody> JarHandlerContext fromRequest(
		@Nonnull final HandlerRequest<R, ?> request,
		@Nonnull final Path jarDir,
		@Nonnull final Logger log) throws RestHandlerException {
	final JarRequestBody requestBody = request.getRequestBody();

	final String pathParameter = request.getPathParameter(JarIdPathParameter.class);
	Path jarFile = jarDir.resolve(pathParameter);

	String entryClass = fromRequestBodyOrQueryParameter(
		emptyToNull(requestBody.getEntryClassName()),
		() -> emptyToNull(getQueryParameter(request, EntryClassQueryParameter.class)),
		null,
		log);

	List<String> programArgs = JarHandlerUtils.getProgramArgs(request, log);

	int parallelism = fromRequestBodyOrQueryParameter(
		requestBody.getParallelism(),
		() -> getQueryParameter(request, ParallelismQueryParameter.class),
		CoreOptions.DEFAULT_PARALLELISM.defaultValue(),
		log);

	JobID jobId = fromRequestBodyOrQueryParameter(
		requestBody.getJobId(),
		() -> null, // No support via query parameter
		null, // Delegate default job ID to actual JobGraph generation
		log);

	return new JarHandlerContext(jarFile, entryClass, programArgs, parallelism, jobId);
}
 
Example 10
Source File: RestServerEndpointITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
protected CompletableFuture<TestResponse> handleRequest(@Nonnull HandlerRequest<TestRequest, TestParameters> request, RestfulGateway gateway) {
	assertEquals(request.getPathParameter(JobIDPathParameter.class), PATH_JOB_ID);
	assertEquals(request.getQueryParameter(JobIDQueryParameter.class).get(0), QUERY_JOB_ID);

	final int id = request.getRequestBody().id;
	return handlerBody.apply(id);
}
 
Example 11
Source File: MultipartUploadResource.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
protected CompletableFuture<EmptyResponseBody> handleRequest(@Nonnull HandlerRequest<TestRequestBody, EmptyMessageParameters> request, @Nonnull RestfulGateway gateway) throws RestHandlerException {
	Collection<Path> uploadedFiles = request.getUploadedFiles().stream().map(File::toPath).collect(Collectors.toList());
	if (!uploadedFiles.isEmpty()) {
		throw new RestHandlerException("This handler should not have received file uploads.", HttpResponseStatus.INTERNAL_SERVER_ERROR);
	}
	this.lastReceivedRequest = request.getRequestBody();
	return CompletableFuture.completedFuture(EmptyResponseBody.getInstance());
}
 
Example 12
Source File: JarHandlerUtils.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
public static <R extends JarRequestBody> JarHandlerContext fromRequest(
		@Nonnull final HandlerRequest<R, ?> request,
		@Nonnull final Path jarDir,
		@Nonnull final Logger log) throws RestHandlerException {
	final JarRequestBody requestBody = request.getRequestBody();

	final String pathParameter = request.getPathParameter(JarIdPathParameter.class);
	Path jarFile = jarDir.resolve(pathParameter);

	String entryClass = fromRequestBodyOrQueryParameter(
		emptyToNull(requestBody.getEntryClassName()),
		() -> emptyToNull(getQueryParameter(request, EntryClassQueryParameter.class)),
		null,
		log);

	List<String> programArgs = JarHandlerUtils.getProgramArgs(request, log);

	int parallelism = fromRequestBodyOrQueryParameter(
		requestBody.getParallelism(),
		() -> getQueryParameter(request, ParallelismQueryParameter.class),
		ExecutionConfig.PARALLELISM_DEFAULT,
		log);

	JobID jobId = fromRequestBodyOrQueryParameter(
		requestBody.getJobId(),
		() -> null, // No support via query parameter
		null, // Delegate default job ID to actual JobGraph generation
		log);

	return new JarHandlerContext(jarFile, entryClass, programArgs, parallelism, jobId);
}
 
Example 13
Source File: JarHandlerUtils.java    From flink with Apache License 2.0 5 votes vote down vote up
/** Parse program arguments in jar run or plan request. */
private static <R extends JarRequestBody, M extends MessageParameters> List<String> getProgramArgs(
		HandlerRequest<R, M> request, Logger log) throws RestHandlerException {
	JarRequestBody requestBody = request.getRequestBody();
	@SuppressWarnings("deprecation")
	List<String> programArgs = tokenizeArguments(
		fromRequestBodyOrQueryParameter(
			emptyToNull(requestBody.getProgramArguments()),
			() -> getQueryParameter(request, ProgramArgsQueryParameter.class),
			null,
			log));
	List<String> programArgsList =
		fromRequestBodyOrQueryParameter(
			requestBody.getProgramArgumentsList(),
			() -> request.getQueryParameter(ProgramArgQueryParameter.class),
			null,
			log);
	if (!programArgsList.isEmpty()) {
		if (!programArgs.isEmpty()) {
			throw new RestHandlerException(
				"Confusing request: programArgs and programArgsList are specified, please, use only programArgsList",
				HttpResponseStatus.BAD_REQUEST);
		}
		return programArgsList;
	} else {
		return programArgs;
	}
}
 
Example 14
Source File: JarHandlerUtils.java    From flink with Apache License 2.0 5 votes vote down vote up
public static <R extends JarRequestBody> JarHandlerContext fromRequest(
		@Nonnull final HandlerRequest<R, ?> request,
		@Nonnull final Path jarDir,
		@Nonnull final Logger log) throws RestHandlerException {
	final JarRequestBody requestBody = request.getRequestBody();

	final String pathParameter = request.getPathParameter(JarIdPathParameter.class);
	Path jarFile = jarDir.resolve(pathParameter);

	String entryClass = fromRequestBodyOrQueryParameter(
		emptyToNull(requestBody.getEntryClassName()),
		() -> emptyToNull(getQueryParameter(request, EntryClassQueryParameter.class)),
		null,
		log);

	List<String> programArgs = JarHandlerUtils.getProgramArgs(request, log);

	int parallelism = fromRequestBodyOrQueryParameter(
		requestBody.getParallelism(),
		() -> getQueryParameter(request, ParallelismQueryParameter.class),
		ExecutionConfig.PARALLELISM_DEFAULT,
		log);

	JobID jobId = fromRequestBodyOrQueryParameter(
		requestBody.getJobId(),
		() -> null, // No support via query parameter
		null, // Delegate default job ID to actual JobGraph generation
		log);

	return new JarHandlerContext(jarFile, entryClass, programArgs, parallelism, jobId);
}
 
Example 15
Source File: RestServerEndpointITCase.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
protected CompletableFuture<TestResponse> handleRequest(@Nonnull HandlerRequest<TestRequest, TestParameters> request, RestfulGateway gateway) {
	assertEquals(request.getPathParameter(JobIDPathParameter.class), PATH_JOB_ID);
	assertEquals(request.getQueryParameter(JobIDQueryParameter.class).get(0), QUERY_JOB_ID);

	final int id = request.getRequestBody().id;
	return handlerBody.apply(id);
}
 
Example 16
Source File: MultipartUploadResource.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
protected CompletableFuture<EmptyResponseBody> handleRequest(@Nonnull HandlerRequest<TestRequestBody, EmptyMessageParameters> request, @Nonnull RestfulGateway gateway) throws RestHandlerException {
	Collection<Path> uploadedFiles = request.getUploadedFiles().stream().map(File::toPath).collect(Collectors.toList());
	if (!uploadedFiles.isEmpty()) {
		throw new RestHandlerException("This handler should not have received file uploads.", HttpResponseStatus.INTERNAL_SERVER_ERROR);
	}
	this.lastReceivedRequest = request.getRequestBody();
	return CompletableFuture.completedFuture(EmptyResponseBody.getInstance());
}
 
Example 17
Source File: JobSubmitHandler.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
protected CompletableFuture<JobSubmitResponseBody> handleRequest(@Nonnull HandlerRequest<JobSubmitRequestBody, EmptyMessageParameters> request, @Nonnull DispatcherGateway gateway) throws RestHandlerException {
	final Collection<File> uploadedFiles = request.getUploadedFiles();
	final Map<String, Path> nameToFile = uploadedFiles.stream().collect(Collectors.toMap(
		File::getName,
		Path::fromLocalFile
	));

	if (uploadedFiles.size() != nameToFile.size()) {
		throw new RestHandlerException(
			String.format("The number of uploaded files was %s than the expected count. Expected: %s Actual %s",
				uploadedFiles.size() < nameToFile.size() ? "lower" : "higher",
				nameToFile.size(),
				uploadedFiles.size()),
			HttpResponseStatus.BAD_REQUEST
		);
	}

	final JobSubmitRequestBody requestBody = request.getRequestBody();

	if (requestBody.jobGraphFileName == null) {
		throw new RestHandlerException(
			String.format("The %s field must not be omitted or be null.",
				JobSubmitRequestBody.FIELD_NAME_JOB_GRAPH),
			HttpResponseStatus.BAD_REQUEST);
	}

	CompletableFuture<JobGraph> jobGraphFuture = loadJobGraph(requestBody, nameToFile);

	Collection<Path> jarFiles = getJarFilesToUpload(requestBody.jarFileNames, nameToFile);

	Collection<Tuple2<String, Path>> artifacts = getArtifactFilesToUpload(requestBody.artifactFileNames, nameToFile);

	CompletableFuture<JobGraph> finalizedJobGraphFuture = uploadJobGraphFiles(gateway, jobGraphFuture, jarFiles, artifacts, configuration);

	CompletableFuture<Acknowledge> jobSubmissionFuture = finalizedJobGraphFuture.thenCompose(jobGraph -> gateway.submitJob(jobGraph, timeout));

	return jobSubmissionFuture.thenCombine(jobGraphFuture,
		(ack, jobGraph) -> new JobSubmitResponseBody("/jobs/" + jobGraph.getJobID()));
}
 
Example 18
Source File: JarHandlerUtils.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/** Parse program arguments in jar run or plan request. */
private static <R extends JarRequestBody, M extends MessageParameters> List<String> getProgramArgs(
		HandlerRequest<R, M> request, Logger log) throws RestHandlerException {
	JarRequestBody requestBody = request.getRequestBody();
	@SuppressWarnings("deprecation")
	List<String> programArgs = tokenizeArguments(
		fromRequestBodyOrQueryParameter(
			emptyToNull(requestBody.getProgramArguments()),
			() -> getQueryParameter(request, ProgramArgsQueryParameter.class),
			null,
			log));
	List<String> programArgsList =
		fromRequestBodyOrQueryParameter(
			requestBody.getProgramArgumentsList(),
			() -> request.getQueryParameter(ProgramArgQueryParameter.class),
			null,
			log);
	if (!programArgsList.isEmpty()) {
		if (!programArgs.isEmpty()) {
			throw new RestHandlerException(
				"Confusing request: programArgs and programArgsList are specified, please, use only programArgsList",
				HttpResponseStatus.BAD_REQUEST);
		}
		return programArgsList;
	} else {
		return programArgs;
	}
}
 
Example 19
Source File: MultipartUploadResource.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
protected CompletableFuture<EmptyResponseBody> handleRequest(@Nonnull HandlerRequest<TestRequestBody, EmptyMessageParameters> request, @Nonnull RestfulGateway gateway) throws RestHandlerException {
	MultipartFileHandler.verifyFileUpload(expectedFiles, request.getUploadedFiles().stream().map(File::toPath).collect(Collectors.toList()));
	this.lastReceivedRequest = request.getRequestBody();
	return CompletableFuture.completedFuture(EmptyResponseBody.getInstance());
}
 
Example 20
Source File: MultipartUploadResource.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
@Override
protected CompletableFuture<EmptyResponseBody> handleRequest(@Nonnull HandlerRequest<TestRequestBody, EmptyMessageParameters> request, @Nonnull RestfulGateway gateway) throws RestHandlerException {
	MultipartFileHandler.verifyFileUpload(expectedFiles, request.getUploadedFiles().stream().map(File::toPath).collect(Collectors.toList()));
	this.lastReceivedRequest = request.getRequestBody();
	return CompletableFuture.completedFuture(EmptyResponseBody.getInstance());
}