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

The following examples show how to use org.apache.flink.runtime.rest.handler.HandlerRequest#getUploadedFiles() . 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: JarUploadHandler.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
protected CompletableFuture<JarUploadResponseBody> handleRequest(
		@Nonnull final HandlerRequest<EmptyRequestBody, EmptyMessageParameters> request,
		@Nonnull final RestfulGateway gateway) throws RestHandlerException {
	Collection<File> uploadedFiles = request.getUploadedFiles();
	if (uploadedFiles.size() != 1) {
		throw new RestHandlerException("Exactly 1 file must be sent, received " + uploadedFiles.size() + '.', HttpResponseStatus.BAD_REQUEST);
	}
	final Path fileUpload = uploadedFiles.iterator().next().toPath();
	return CompletableFuture.supplyAsync(() -> {
		if (!fileUpload.getFileName().toString().endsWith(".jar")) {
			throw new CompletionException(new RestHandlerException(
				"Only Jar files are allowed.",
				HttpResponseStatus.BAD_REQUEST));
		} else {
			final Path destination = jarDir.resolve(UUID.randomUUID() + "_" + fileUpload.getFileName());
			try {
				Files.move(fileUpload, destination);
			} catch (IOException e) {
				throw new CompletionException(new RestHandlerException(
					String.format("Could not move uploaded jar file [%s] to [%s].",
						fileUpload,
						destination),
					HttpResponseStatus.INTERNAL_SERVER_ERROR,
					e));
			}
			return new JarUploadResponseBody(destination
				.normalize()
				.toString());
		}
	}, executor);
}
 
Example 2
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 3
Source File: JarUploadHandler.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
protected CompletableFuture<JarUploadResponseBody> handleRequest(
		@Nonnull final HandlerRequest<EmptyRequestBody, EmptyMessageParameters> request,
		@Nonnull final RestfulGateway gateway) throws RestHandlerException {
	Collection<File> uploadedFiles = request.getUploadedFiles();
	if (uploadedFiles.size() != 1) {
		throw new RestHandlerException("Exactly 1 file must be sent, received " + uploadedFiles.size() + '.', HttpResponseStatus.BAD_REQUEST);
	}
	final Path fileUpload = uploadedFiles.iterator().next().toPath();
	return CompletableFuture.supplyAsync(() -> {
		if (!fileUpload.getFileName().toString().endsWith(".jar")) {
			throw new CompletionException(new RestHandlerException(
				"Only Jar files are allowed.",
				HttpResponseStatus.BAD_REQUEST));
		} else {
			final Path destination = jarDir.resolve(UUID.randomUUID() + "_" + fileUpload.getFileName());
			try {
				Files.move(fileUpload, destination);
			} catch (IOException e) {
				throw new CompletionException(new RestHandlerException(
					String.format("Could not move uploaded jar file [%s] to [%s].",
						fileUpload,
						destination),
					HttpResponseStatus.INTERNAL_SERVER_ERROR,
					e));
			}
			return new JarUploadResponseBody(destination
				.normalize()
				.toString());
		}
	}, executor);
}
 
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: JarUploadHandler.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
protected CompletableFuture<JarUploadResponseBody> handleRequest(
		@Nonnull final HandlerRequest<EmptyRequestBody, EmptyMessageParameters> request,
		@Nonnull final RestfulGateway gateway) throws RestHandlerException {
	Collection<File> uploadedFiles = request.getUploadedFiles();
	if (uploadedFiles.size() != 1) {
		throw new RestHandlerException("Exactly 1 file must be sent, received " + uploadedFiles.size() + '.', HttpResponseStatus.BAD_REQUEST);
	}
	final Path fileUpload = uploadedFiles.iterator().next().toPath();
	return CompletableFuture.supplyAsync(() -> {
		if (!fileUpload.getFileName().toString().endsWith(".jar")) {
			throw new CompletionException(new RestHandlerException(
				"Only Jar files are allowed.",
				HttpResponseStatus.BAD_REQUEST));
		} else {
			final Path destination = jarDir.resolve(UUID.randomUUID() + "_" + fileUpload.getFileName());
			try {
				Files.move(fileUpload, destination);
			} catch (IOException e) {
				throw new CompletionException(new RestHandlerException(
					String.format("Could not move uploaded jar file [%s] to [%s].",
						fileUpload,
						destination),
					HttpResponseStatus.INTERNAL_SERVER_ERROR,
					e));
			}
			return new JarUploadResponseBody(destination
				.normalize()
				.toString());
		}
	}, executor);
}
 
Example 6
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()));
}