org.apache.flink.runtime.rest.messages.EmptyMessageParameters Java Examples

The following examples show how to use org.apache.flink.runtime.rest.messages.EmptyMessageParameters. 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: RestClientTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testConnectionTimeout() throws Exception {
	final Configuration config = new Configuration();
	config.setLong(RestOptions.CONNECTION_TIMEOUT, 1);
	try (final RestClient restClient = new RestClient(RestClientConfiguration.fromConfiguration(config), Executors.directExecutor())) {
		restClient.sendRequest(
			unroutableIp,
			80,
			new TestMessageHeaders(),
			EmptyMessageParameters.getInstance(),
			EmptyRequestBody.getInstance())
			.get(60, TimeUnit.SECONDS);
	} catch (final ExecutionException e) {
		final Throwable throwable = ExceptionUtils.stripExecutionException(e);
		assertThat(throwable, instanceOf(ConnectTimeoutException.class));
		assertThat(throwable.getMessage(), containsString(unroutableIp));
	}
}
 
Example #2
Source File: JobSubmitHandlerTest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Test
public void testSerializationFailureHandling() throws Exception {
	final Path jobGraphFile = TEMPORARY_FOLDER.newFile().toPath();
	DispatcherGateway mockGateway = new TestingDispatcherGateway.Builder()
		.setSubmitFunction(jobGraph -> CompletableFuture.completedFuture(Acknowledge.get()))
		.build();

	JobSubmitHandler handler = new JobSubmitHandler(
		() -> CompletableFuture.completedFuture(mockGateway),
		RpcUtils.INF_TIMEOUT,
		Collections.emptyMap(),
		TestingUtils.defaultExecutor(),
		configuration);

	JobSubmitRequestBody request = new JobSubmitRequestBody(jobGraphFile.toString(), Collections.emptyList(), Collections.emptyList());

	try {
		handler.handleRequest(new HandlerRequest<>(request, EmptyMessageParameters.getInstance()), mockGateway);
		Assert.fail();
	} catch (RestHandlerException rhe) {
		Assert.assertEquals(HttpResponseStatus.BAD_REQUEST, rhe.getHttpResponseStatus());
	}
}
 
Example #3
Source File: JobSubmitHandlerTest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Test
public void testSuccessfulJobSubmission() throws Exception {
	final Path jobGraphFile = TEMPORARY_FOLDER.newFile().toPath();
	try (ObjectOutputStream objectOut = new ObjectOutputStream(Files.newOutputStream(jobGraphFile))) {
		objectOut.writeObject(new JobGraph("testjob"));
	}

	TestingDispatcherGateway.Builder builder = new TestingDispatcherGateway.Builder();
	builder
		.setBlobServerPort(blobServer.getPort())
		.setSubmitFunction(jobGraph -> CompletableFuture.completedFuture(Acknowledge.get()))
		.setHostname("localhost");
	DispatcherGateway mockGateway = builder.build();

	JobSubmitHandler handler = new JobSubmitHandler(
		() -> CompletableFuture.completedFuture(mockGateway),
		RpcUtils.INF_TIMEOUT,
		Collections.emptyMap(),
		TestingUtils.defaultExecutor(),
		configuration);

	JobSubmitRequestBody request = new JobSubmitRequestBody(jobGraphFile.getFileName().toString(), Collections.emptyList(), Collections.emptyList());

	handler.handleRequest(new HandlerRequest<>(request, EmptyMessageParameters.getInstance(), Collections.emptyMap(), Collections.emptyMap(), Collections.singleton(jobGraphFile.toFile())), mockGateway)
		.get();
}
 
Example #4
Source File: RestClientMultipartTest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Test
public void testFileMultipart() throws Exception {
	Collection<FileUpload> files = MULTIPART_UPLOAD_RESOURCE.getFilesToUpload().stream()
		.map(file -> new FileUpload(file.toPath(), "application/octet-stream")).collect(Collectors.toList());

	CompletableFuture<EmptyResponseBody> responseFuture = restClient.sendRequest(
		MULTIPART_UPLOAD_RESOURCE.getServerSocketAddress().getHostName(),
		MULTIPART_UPLOAD_RESOURCE.getServerSocketAddress().getPort(),
		MULTIPART_UPLOAD_RESOURCE.getFileHandler().getMessageHeaders(),
		EmptyMessageParameters.getInstance(),
		EmptyRequestBody.getInstance(),
		files
	);

	responseFuture.get();
}
 
Example #5
Source File: RestClientTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testInvalidVersionRejection() throws Exception {
	try (final RestClient restClient = new RestClient(RestClientConfiguration.fromConfiguration(new Configuration()), Executors.directExecutor())) {
		CompletableFuture<EmptyResponseBody> invalidVersionResponse = restClient.sendRequest(
			unroutableIp,
			80,
			new TestMessageHeaders(),
			EmptyMessageParameters.getInstance(),
			EmptyRequestBody.getInstance(),
			Collections.emptyList(),
			RestAPIVersion.V0
		);
		Assert.fail("The request should have been rejected due to a version mismatch.");
	} catch (IllegalArgumentException e) {
		// expected
	}
}
 
Example #6
Source File: JobIdsHandler.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Override
protected CompletableFuture<JobIdsWithStatusOverview> handleRequest(
		@Nonnull HandlerRequest<EmptyRequestBody, EmptyMessageParameters> request,
		@Nonnull RestfulGateway gateway) throws RestHandlerException {

	return gateway.requestMultipleJobDetails(timeout).thenApply(
		multipleJobDetails -> new JobIdsWithStatusOverview(
			multipleJobDetails
				.getJobs()
				.stream()
				.map(
					jobDetails ->
						new JobIdsWithStatusOverview.JobIdWithStatus(
							jobDetails.getJobId(),
							jobDetails.getStatus()))
				.collect(Collectors.toList())
		)
	);
}
 
Example #7
Source File: JarUploadHandlerTest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Test
public void testFailedUpload() throws Exception {
	final Path uploadedFile = jarDir.resolve("Kafka010Example.jar");
	final HandlerRequest<EmptyRequestBody, EmptyMessageParameters> request = createRequest(uploadedFile);

	try {
		jarUploadHandler.handleRequest(request, mockDispatcherGateway).get();
		fail("Expected exception not thrown.");
	} catch (final ExecutionException e) {
		final Throwable throwable = ExceptionUtils.stripCompletionException(e.getCause());
		assertThat(throwable, instanceOf(RestHandlerException.class));
		final RestHandlerException restHandlerException = (RestHandlerException) throwable;
		assertThat(restHandlerException.getMessage(), containsString("Could not move uploaded jar file"));
		assertThat(restHandlerException.getHttpResponseStatus(), equalTo(HttpResponseStatus.INTERNAL_SERVER_ERROR));
	}
}
 
Example #8
Source File: JobSubmitHandlerTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testSerializationFailureHandling() throws Exception {
	final Path jobGraphFile = TEMPORARY_FOLDER.newFile().toPath();
	DispatcherGateway mockGateway = new TestingDispatcherGateway.Builder()
		.setSubmitFunction(jobGraph -> CompletableFuture.completedFuture(Acknowledge.get()))
		.build();

	JobSubmitHandler handler = new JobSubmitHandler(
		() -> CompletableFuture.completedFuture(mockGateway),
		RpcUtils.INF_TIMEOUT,
		Collections.emptyMap(),
		TestingUtils.defaultExecutor(),
		configuration);

	JobSubmitRequestBody request = new JobSubmitRequestBody(jobGraphFile.toString(), Collections.emptyList(), Collections.emptyList());

	try {
		handler.handleRequest(new HandlerRequest<>(request, EmptyMessageParameters.getInstance()), mockGateway);
		Assert.fail();
	} catch (RestHandlerException rhe) {
		Assert.assertEquals(HttpResponseStatus.BAD_REQUEST, rhe.getHttpResponseStatus());
	}
}
 
Example #9
Source File: DashboardConfigHandler.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
public DashboardConfigHandler(
		GatewayRetriever<? extends RestfulGateway> leaderRetriever,
		Time timeout,
		Map<String, String> responseHeaders,
		MessageHeaders<EmptyRequestBody, DashboardConfiguration, EmptyMessageParameters> messageHeaders,
		long refreshInterval) {
	super(leaderRetriever, timeout, responseHeaders, messageHeaders);

	dashboardConfiguration = DashboardConfiguration.from(refreshInterval, ZonedDateTime.now());
}
 
Example #10
Source File: JarListHandler.java    From flink with Apache License 2.0 5 votes vote down vote up
public JarListHandler(
		GatewayRetriever<? extends RestfulGateway> leaderRetriever,
		Time timeout,
		Map<String, String> responseHeaders,
		MessageHeaders<EmptyRequestBody, JarListInfo, EmptyMessageParameters> messageHeaders,
		CompletableFuture<String> localAddressFuture,
		File jarDir,
		Executor executor) {
	super(leaderRetriever, timeout, responseHeaders, messageHeaders);

	this.localAddressFuture = localAddressFuture;
	this.jarDir = requireNonNull(jarDir);
	this.executor = requireNonNull(executor);
}
 
Example #11
Source File: ShutdownHandler.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
public ShutdownHandler(
		final GatewayRetriever<? extends RestfulGateway> leaderRetriever,
		final Time timeout,
		final Map<String, String> responseHeaders,
		final MessageHeaders<EmptyRequestBody, EmptyResponseBody, EmptyMessageParameters> messageHeaders) {
	super(leaderRetriever, timeout, responseHeaders, messageHeaders);
}
 
Example #12
Source File: JarUploadHandlerTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test
public void testRejectNonJarFiles() throws Exception {
	final Path uploadedFile = Files.createFile(jarDir.resolve("katrin.png"));
	final HandlerRequest<EmptyRequestBody, EmptyMessageParameters> request = createRequest(uploadedFile);

	try {
		jarUploadHandler.handleRequest(request, mockDispatcherGateway).get();
		fail("Expected exception not thrown.");
	} catch (final ExecutionException e) {
		final Throwable throwable = ExceptionUtils.stripCompletionException(e.getCause());
		assertThat(throwable, instanceOf(RestHandlerException.class));
		final RestHandlerException restHandlerException = (RestHandlerException) throwable;
		assertThat(restHandlerException.getHttpResponseStatus(), equalTo(HttpResponseStatus.BAD_REQUEST));
	}
}
 
Example #13
Source File: JarListHandler.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
public JarListHandler(
		GatewayRetriever<? extends RestfulGateway> leaderRetriever,
		Time timeout,
		Map<String, String> responseHeaders,
		MessageHeaders<EmptyRequestBody, JarListInfo, EmptyMessageParameters> messageHeaders,
		CompletableFuture<String> localAddressFuture,
		File jarDir,
		Executor executor) {
	super(leaderRetriever, timeout, responseHeaders, messageHeaders);

	this.localAddressFuture = localAddressFuture;
	this.jarDir = requireNonNull(jarDir);
	this.executor = requireNonNull(executor);
}
 
Example #14
Source File: JarSubmissionITCase.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
private static JarListInfo listJars(JarListHandler handler, RestfulGateway restfulGateway) throws Exception {
	HandlerRequest<EmptyRequestBody, EmptyMessageParameters> listRequest = new HandlerRequest<>(
		EmptyRequestBody.getInstance(),
		EmptyMessageParameters.getInstance());
	return handler.handleRequest(listRequest, restfulGateway)
		.get();
}
 
Example #15
Source File: JarUploadHandlerTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
private static HandlerRequest<EmptyRequestBody, EmptyMessageParameters> createRequest(
		final Path uploadedFile) throws HandlerRequestException, IOException {
	return new HandlerRequest<>(
		EmptyRequestBody.getInstance(),
		EmptyMessageParameters.getInstance(),
		Collections.emptyMap(),
		Collections.emptyMap(),
		Collections.singleton(uploadedFile.toFile()));
}
 
Example #16
Source File: JarUploadHandlerTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private static HandlerRequest<EmptyRequestBody, EmptyMessageParameters> createRequest(
		final Path uploadedFile) throws HandlerRequestException, IOException {
	return new HandlerRequest<>(
		EmptyRequestBody.getInstance(),
		EmptyMessageParameters.getInstance(),
		Collections.emptyMap(),
		Collections.emptyMap(),
		Collections.singleton(uploadedFile.toFile()));
}
 
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: ClusterOverviewHandler.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
public ClusterOverviewHandler(
		GatewayRetriever<? extends RestfulGateway> leaderRetriever,
		Time timeout,
		Map<String, String> responseHeaders,
		MessageHeaders<EmptyRequestBody, ClusterOverviewWithVersion, EmptyMessageParameters> messageHeaders) {
	super(leaderRetriever, timeout, responseHeaders, messageHeaders);
}
 
Example #19
Source File: ClusterOverviewHandler.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<ClusterOverviewWithVersion> handleRequest(@Nonnull HandlerRequest<EmptyRequestBody, EmptyMessageParameters> request, @Nonnull RestfulGateway gateway) {
	CompletableFuture<ClusterOverview> overviewFuture = gateway.requestClusterOverview(timeout);

	return overviewFuture.thenApply(
		statusOverview -> ClusterOverviewWithVersion.fromStatusOverview(statusOverview, version, commitID));
}
 
Example #20
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 #21
Source File: JobsOverviewHandler.java    From flink with Apache License 2.0 5 votes vote down vote up
public JobsOverviewHandler(
		GatewayRetriever<? extends RestfulGateway> leaderRetriever,
		Time timeout,
		Map<String, String> responseHeaders,
		MessageHeaders<EmptyRequestBody, MultipleJobsDetails, EmptyMessageParameters> messageHeaders) {
	super(
		leaderRetriever,
		timeout,
		responseHeaders,
		messageHeaders);
}
 
Example #22
Source File: ClusterOverviewHandler.java    From flink with Apache License 2.0 5 votes vote down vote up
public ClusterOverviewHandler(
		GatewayRetriever<? extends RestfulGateway> leaderRetriever,
		Time timeout,
		Map<String, String> responseHeaders,
		MessageHeaders<EmptyRequestBody, ClusterOverviewWithVersion, EmptyMessageParameters> messageHeaders) {
	super(leaderRetriever, timeout, responseHeaders, messageHeaders);
}
 
Example #23
Source File: JobSubmitHandlerTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testFailedJobSubmission() throws Exception {
	final String errorMessage = "test";
	DispatcherGateway mockGateway = new TestingDispatcherGateway.Builder()
		.setSubmitFunction(jobgraph -> FutureUtils.completedExceptionally(new Exception(errorMessage)))
		.build();

	JobSubmitHandler handler = new JobSubmitHandler(
		() -> CompletableFuture.completedFuture(mockGateway),
		RpcUtils.INF_TIMEOUT,
		Collections.emptyMap(),
		TestingUtils.defaultExecutor(),
		configuration);

	final Path jobGraphFile = TEMPORARY_FOLDER.newFile().toPath();

	JobGraph jobGraph = new JobGraph("testjob");
	try (ObjectOutputStream objectOut = new ObjectOutputStream(Files.newOutputStream(jobGraphFile))) {
		objectOut.writeObject(jobGraph);
	}
	JobSubmitRequestBody request = new JobSubmitRequestBody(jobGraphFile.getFileName().toString(), Collections.emptyList(), Collections.emptyList());

	try {
		handler.handleRequest(new HandlerRequest<>(
				request,
				EmptyMessageParameters.getInstance(),
				Collections.emptyMap(),
				Collections.emptyMap(),
				Collections.singletonList(jobGraphFile.toFile())), mockGateway)
			.get();
	} catch (Exception e) {
		Throwable t = ExceptionUtils.stripExecutionException(e);
		Assert.assertEquals(errorMessage, t.getMessage());
	}
}
 
Example #24
Source File: TaskManagersHandler.java    From flink with Apache License 2.0 5 votes vote down vote up
public TaskManagersHandler(
		GatewayRetriever<? extends RestfulGateway> leaderRetriever,
		Time timeout,
		Map<String, String> responseHeaders,
		MessageHeaders<EmptyRequestBody, TaskManagersInfo, EmptyMessageParameters> messageHeaders,
		GatewayRetriever<ResourceManagerGateway> resourceManagerGatewayRetriever) {
	super(
		leaderRetriever,
		timeout,
		responseHeaders,
		messageHeaders,
		resourceManagerGatewayRetriever);
}
 
Example #25
Source File: RestClusterClientTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
protected CompletableFuture<EmptyResponseBody> handleRequest(@Nonnull HandlerRequest<EmptyRequestBody, EmptyMessageParameters> request, @Nonnull DispatcherGateway gateway) throws RestHandlerException {
	final CompletableFuture<EmptyResponseBody> result = responseQueue.poll();

	if (result != null) {
		return result;
	} else {
		return CompletableFuture.completedFuture(EmptyResponseBody.getInstance());
	}
}
 
Example #26
Source File: JarUploadHandler.java    From flink with Apache License 2.0 5 votes vote down vote up
public JarUploadHandler(
		final GatewayRetriever<? extends RestfulGateway> leaderRetriever,
		final Time timeout,
		final Map<String, String> responseHeaders,
		final MessageHeaders<EmptyRequestBody, JarUploadResponseBody, EmptyMessageParameters> messageHeaders,
		final Path jarDir,
		final Executor executor) {
	super(leaderRetriever, timeout, responseHeaders, messageHeaders);
	this.jarDir = requireNonNull(jarDir);
	this.executor = requireNonNull(executor);
}
 
Example #27
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 #28
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 #29
Source File: RestClientTest.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
/**
 * Tests that we fail the operation if the remote connection closes.
 */
@Test
public void testConnectionClosedHandling() throws Exception {
	final Configuration config = new Configuration();
	config.setLong(RestOptions.IDLENESS_TIMEOUT, 5000L);
	try (final ServerSocket serverSocket = new ServerSocket(0);
		final RestClient restClient = new RestClient(RestClientConfiguration.fromConfiguration(config), TestingUtils.defaultExecutor())) {

		final String targetAddress = "localhost";
		final int targetPort = serverSocket.getLocalPort();

		// start server
		final CompletableFuture<Socket> socketCompletableFuture = CompletableFuture.supplyAsync(CheckedSupplier.unchecked(serverSocket::accept));

		final CompletableFuture<EmptyResponseBody> responseFuture = restClient.sendRequest(
			targetAddress,
			targetPort,
			new TestMessageHeaders(),
			EmptyMessageParameters.getInstance(),
			EmptyRequestBody.getInstance(),
			Collections.emptyList());

		Socket connectionSocket = null;

		try {
			connectionSocket = socketCompletableFuture.get(TIMEOUT, TimeUnit.SECONDS);
		} catch (TimeoutException ignored) {
			// could not establish a server connection --> see that the response failed
			socketCompletableFuture.cancel(true);
		}

		if (connectionSocket != null) {
			// close connection
			connectionSocket.close();
		}

		try {
			responseFuture.get();
		} catch (ExecutionException ee) {
			if (!ExceptionUtils.findThrowable(ee, IOException.class).isPresent()) {
				throw ee;
			}
		}
	}
}
 
Example #30
Source File: JarUploadHeaders.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public EmptyMessageParameters getUnresolvedMessageParameters() {
	return EmptyMessageParameters.getInstance();
}