org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpResponseStatus Java Examples

The following examples show how to use org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpResponseStatus. 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: FileUploadHandlerTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testFileMultipart() throws Exception {
	OkHttpClient client = createOkHttpClientWithNoTimeouts();

	MultipartUploadResource.MultipartFileHandler fileHandler = MULTIPART_UPLOAD_RESOURCE.getFileHandler();

	Request jsonRequest = buildJsonRequest(fileHandler.getMessageHeaders().getTargetRestEndpointURL(), new MultipartUploadResource.TestRequestBody());
	try (Response response = client.newCall(jsonRequest).execute()) {
		// JSON payload did not match expected format
		assertEquals(HttpResponseStatus.BAD_REQUEST.code(), response.code());
	}

	Request fileRequest = buildFileRequest(fileHandler.getMessageHeaders().getTargetRestEndpointURL());
	try (Response response = client.newCall(fileRequest).execute()) {
		assertEquals(fileHandler.getMessageHeaders().getResponseStatusCode().code(), response.code());
	}

	Request mixedRequest = buildMixedRequest(fileHandler.getMessageHeaders().getTargetRestEndpointURL(), new MultipartUploadResource.TestRequestBody());
	try (Response response = client.newCall(mixedRequest).execute()) {
		// JSON payload did not match expected format
		assertEquals(HttpResponseStatus.BAD_REQUEST.code(), response.code());
	}

	verifyNoFileIsRegisteredToDeleteOnExitHook();
}
 
Example #2
Source File: JarUploadHandlerTest.java    From flink 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 #3
Source File: RestClusterClientSavepointTriggerTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testTriggerSavepointRetry() throws Exception {
	final TriggerId triggerId = new TriggerId();
	final String expectedSavepointDir = "hello";

	final AtomicBoolean failRequest = new AtomicBoolean(true);
	try (final RestServerEndpoint restServerEndpoint = createRestServerEndpoint(
		request -> triggerId,
		trigger -> {
			if (failRequest.compareAndSet(true, false)) {
				throw new RestHandlerException("expected", HttpResponseStatus.SERVICE_UNAVAILABLE);
			} else {
				return new SavepointInfo(expectedSavepointDir, null);
			}
		})) {

		final RestClusterClient<?> restClusterClient = createRestClusterClient(restServerEndpoint.getServerAddress().getPort());

		final String savepointPath = restClusterClient.triggerSavepoint(new JobID(), null).get();
		assertEquals(expectedSavepointDir, savepointPath);
	}
}
 
Example #4
Source File: RouterHandler.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Override
protected void channelRead0(ChannelHandlerContext channelHandlerContext, HttpRequest httpRequest) {
	if (HttpHeaders.is100ContinueExpected(httpRequest)) {
		channelHandlerContext.writeAndFlush(new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.CONTINUE));
		return;
	}

	// Route
	HttpMethod method = httpRequest.getMethod();
	QueryStringDecoder qsd = new QueryStringDecoder(httpRequest.uri());
	RouteResult<?> routeResult = router.route(method, qsd.path(), qsd.parameters());

	if (routeResult == null) {
		respondNotFound(channelHandlerContext, httpRequest);
		return;
	}

	routed(channelHandlerContext, routeResult, httpRequest);
}
 
Example #5
Source File: CheckpointConfigHandler.java    From flink with Apache License 2.0 6 votes vote down vote up
private static CheckpointConfigInfo createCheckpointConfigInfo(AccessExecutionGraph executionGraph) throws RestHandlerException {
	final CheckpointCoordinatorConfiguration checkpointCoordinatorConfiguration = executionGraph.getCheckpointCoordinatorConfiguration();

	if (checkpointCoordinatorConfiguration == null) {
		throw new RestHandlerException(
			"Checkpointing is not enabled for this job (" + executionGraph.getJobID() + ").",
			HttpResponseStatus.NOT_FOUND);
	} else {
		CheckpointRetentionPolicy retentionPolicy = checkpointCoordinatorConfiguration.getCheckpointRetentionPolicy();

		CheckpointConfigInfo.ExternalizedCheckpointInfo externalizedCheckpointInfo = new CheckpointConfigInfo.ExternalizedCheckpointInfo(
				retentionPolicy != CheckpointRetentionPolicy.NEVER_RETAIN_AFTER_TERMINATION,
				retentionPolicy != CheckpointRetentionPolicy.RETAIN_ON_CANCELLATION);

		String stateBackendName = executionGraph.getStateBackendName().orElse(null);

		return new CheckpointConfigInfo(
			checkpointCoordinatorConfiguration.isExactlyOnce() ? CheckpointConfigInfo.ProcessingMode.EXACTLY_ONCE : CheckpointConfigInfo.ProcessingMode.AT_LEAST_ONCE,
			checkpointCoordinatorConfiguration.getCheckpointInterval(),
			checkpointCoordinatorConfiguration.getCheckpointTimeout(),
			checkpointCoordinatorConfiguration.getMinPauseBetweenCheckpoints(),
			checkpointCoordinatorConfiguration.getMaxConcurrentCheckpoints(),
			externalizedCheckpointInfo,
			stateBackendName);
	}
}
 
Example #6
Source File: JobSubmitHandler.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
private CompletableFuture<JobGraph> uploadJobGraphFiles(
		DispatcherGateway gateway,
		CompletableFuture<JobGraph> jobGraphFuture,
		Collection<Path> jarFiles,
		Collection<Tuple2<String, Path>> artifacts,
		Configuration configuration) {
	CompletableFuture<Integer> blobServerPortFuture = gateway.getBlobServerPort(timeout);

	return jobGraphFuture.thenCombine(blobServerPortFuture, (JobGraph jobGraph, Integer blobServerPort) -> {
		final InetSocketAddress address = new InetSocketAddress(gateway.getHostname(), blobServerPort);
		try {
			ClientUtils.uploadJobGraphFiles(jobGraph, jarFiles, artifacts, () -> new BlobClient(address, configuration));
		} catch (FlinkException e) {
			throw new CompletionException(new RestHandlerException(
				"Could not upload job files.",
				HttpResponseStatus.INTERNAL_SERVER_ERROR,
				e));
		}
		return jobGraph;
	});
}
 
Example #7
Source File: StopWithSavepointHandlersTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testTriggerSavepointNoDirectory() throws Exception {
	TestingRestfulGateway testingRestfulGateway = new TestingRestfulGateway.Builder()
			.setStopWithSavepointFunction((JobID jobId, String directory) -> CompletableFuture.completedFuture(COMPLETED_SAVEPOINT_EXTERNAL_POINTER))
			.build();

	try {
		savepointTriggerHandler.handleRequest(
				triggerSavepointRequestWithDefaultDirectory(),
				testingRestfulGateway).get();
		fail("Expected exception not thrown.");
	} catch (RestHandlerException rhe) {
		assertThat(
				rhe.getMessage(),
				equalTo("Config key [state.savepoints.dir] is not set. " +
						"Property [targetDirectory] must be provided."));
		assertThat(rhe.getHttpResponseStatus(), equalTo(HttpResponseStatus.BAD_REQUEST));
	}
}
 
Example #8
Source File: RestClusterClientTest.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that the send operation is being retried.
 */
@Test
public void testRetriableSendOperationIfConnectionErrorOrServiceUnavailable() throws Exception {
	final PingRestHandler pingRestHandler = new PingRestHandler(
		FutureUtils.completedExceptionally(new RestHandlerException("test exception", HttpResponseStatus.SERVICE_UNAVAILABLE)),
		CompletableFuture.completedFuture(EmptyResponseBody.getInstance()));

	try (final TestRestServerEndpoint restServerEndpoint = createRestServerEndpoint(pingRestHandler)) {
		RestClusterClient<?> restClusterClient = createRestClusterClient(restServerEndpoint.getServerAddress().getPort());

		try {
			final AtomicBoolean firstPollFailed = new AtomicBoolean();
			failHttpRequest = (messageHeaders, messageParameters, requestBody) ->
				messageHeaders instanceof PingRestHandlerHeaders && !firstPollFailed.getAndSet(true);

			restClusterClient.sendRequest(PingRestHandlerHeaders.INSTANCE).get();
		} finally {
			restClusterClient.close();
		}
	}
}
 
Example #9
Source File: AbstractSubtaskAttemptHandler.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Override
protected R handleRequest(HandlerRequest<EmptyRequestBody, M> request, AccessExecutionVertex executionVertex) throws RestHandlerException {
	final Integer attemptNumber = request.getPathParameter(SubtaskAttemptPathParameter.class);

	final AccessExecution currentAttempt = executionVertex.getCurrentExecutionAttempt();
	if (attemptNumber == currentAttempt.getAttemptNumber()) {
		return handleRequest(request, currentAttempt);
	} else if (attemptNumber >= 0 && attemptNumber < currentAttempt.getAttemptNumber()) {
		final AccessExecution execution = executionVertex.getPriorExecutionAttempt(attemptNumber);

		if (execution != null) {
			return handleRequest(request, execution);
		} else {
			throw new RestHandlerException("Attempt " + attemptNumber + " not found in subtask " +
				executionVertex.getTaskNameWithSubtaskIndex(), HttpResponseStatus.NOT_FOUND);
		}
	} else {
		throw new RestHandlerException("Invalid attempt num " + attemptNumber, HttpResponseStatus.NOT_FOUND);
	}
}
 
Example #10
Source File: RestClusterClientTest.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that the send operation is being retried.
 */
@Test
public void testRetriableSendOperationIfConnectionErrorOrServiceUnavailable() throws Exception {
	final PingRestHandler pingRestHandler = new PingRestHandler(
		FutureUtils.completedExceptionally(new RestHandlerException("test exception", HttpResponseStatus.SERVICE_UNAVAILABLE)),
		CompletableFuture.completedFuture(EmptyResponseBody.getInstance()));

	try (final TestRestServerEndpoint restServerEndpoint = createRestServerEndpoint(pingRestHandler)) {
		RestClusterClient<?> restClusterClient = createRestClusterClient(restServerEndpoint.getServerAddress().getPort());

		try {
			final AtomicBoolean firstPollFailed = new AtomicBoolean();
			failHttpRequest = (messageHeaders, messageParameters, requestBody) ->
				messageHeaders instanceof PingRestHandlerHeaders && !firstPollFailed.getAndSet(true);

			restClusterClient.sendRequest(PingRestHandlerHeaders.INSTANCE).get();
		} finally {
			restClusterClient.shutdown();
		}
	}
}
 
Example #11
Source File: FileUploadHandlerTest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Test
public void testFileMultipart() throws Exception {
	OkHttpClient client = new OkHttpClient();

	MultipartUploadResource.MultipartFileHandler fileHandler = MULTIPART_UPLOAD_RESOURCE.getFileHandler();

	Request jsonRequest = buildJsonRequest(fileHandler.getMessageHeaders().getTargetRestEndpointURL(), new MultipartUploadResource.TestRequestBody());
	try (Response response = client.newCall(jsonRequest).execute()) {
		// JSON payload did not match expected format
		assertEquals(HttpResponseStatus.BAD_REQUEST.code(), response.code());
	}

	Request fileRequest = buildFileRequest(fileHandler.getMessageHeaders().getTargetRestEndpointURL());
	try (Response response = client.newCall(fileRequest).execute()) {
		assertEquals(fileHandler.getMessageHeaders().getResponseStatusCode().code(), response.code());
	}

	Request mixedRequest = buildMixedRequest(fileHandler.getMessageHeaders().getTargetRestEndpointURL(), new MultipartUploadResource.TestRequestBody());
	try (Response response = client.newCall(mixedRequest).execute()) {
		// JSON payload did not match expected format
		assertEquals(HttpResponseStatus.BAD_REQUEST.code(), response.code());
	}
}
 
Example #12
Source File: AbstractHandler.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
private CompletableFuture<Void> handleException(Throwable throwable, ChannelHandlerContext ctx, HttpRequest httpRequest) {
	if (throwable instanceof RestHandlerException) {
		RestHandlerException rhe = (RestHandlerException) throwable;
		if (log.isDebugEnabled()) {
			log.error("Exception occurred in REST handler.", rhe);
		} else {
			log.error("Exception occurred in REST handler: {}", rhe.getMessage());
		}
		return HandlerUtils.sendErrorResponse(
			ctx,
			httpRequest,
			new ErrorResponseBody(rhe.getMessage()),
			rhe.getHttpResponseStatus(),
			responseHeaders);
	} else {
		log.error("Unhandled exception.", throwable);
		String stackTrace = String.format("<Exception on server side:%n%s%nEnd of exception on server side>",
			ExceptionUtils.stringifyException(throwable));
		return HandlerUtils.sendErrorResponse(
			ctx,
			httpRequest,
			new ErrorResponseBody(Arrays.asList("Internal server error.", stackTrace)),
			HttpResponseStatus.INTERNAL_SERVER_ERROR,
			responseHeaders);
	}
}
 
Example #13
Source File: RestClusterClientTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
protected CompletableFuture<AsynchronousOperationResult<AsynchronousOperationInfo>> handleRequest(@Nonnull HandlerRequest<EmptyRequestBody, SavepointDisposalStatusMessageParameters> request, @Nonnull DispatcherGateway gateway) throws RestHandlerException {
	final TriggerId actualTriggerId = request.getPathParameter(TriggerIdPathParameter.class);

	if (actualTriggerId.equals(triggerId)) {
		final OptionalFailure<AsynchronousOperationInfo> nextResponse = responses.poll();

		if (nextResponse != null) {
			if (nextResponse.isFailure()) {
				throw new RestHandlerException("Failure", HttpResponseStatus.BAD_REQUEST, nextResponse.getFailureCause());
			} else {
				return CompletableFuture.completedFuture(AsynchronousOperationResult.completed(nextResponse.getUnchecked()));
			}
		} else {
			throw new AssertionError();
		}
	} else {
		throw new AssertionError();
	}
}
 
Example #14
Source File: RestClusterClientTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
protected CompletableFuture<AsynchronousOperationResult<AsynchronousOperationInfo>> handleRequest(@Nonnull HandlerRequest<EmptyRequestBody, SavepointDisposalStatusMessageParameters> request, @Nonnull DispatcherGateway gateway) throws RestHandlerException {
	final TriggerId actualTriggerId = request.getPathParameter(TriggerIdPathParameter.class);

	if (actualTriggerId.equals(triggerId)) {
		final OptionalFailure<AsynchronousOperationInfo> nextResponse = responses.poll();

		if (nextResponse != null) {
			if (nextResponse.isFailure()) {
				throw new RestHandlerException("Failure", HttpResponseStatus.BAD_REQUEST, nextResponse.getFailureCause());
			} else {
				return CompletableFuture.completedFuture(AsynchronousOperationResult.completed(nextResponse.getUnchecked()));
			}
		} else {
			throw new AssertionError();
		}
	} else {
		throw new AssertionError();
	}
}
 
Example #15
Source File: StopWithSavepointHandlersTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testTriggerSavepointNoDirectory() throws Exception {
	TestingRestfulGateway testingRestfulGateway = new TestingRestfulGateway.Builder()
			.setStopWithSavepointFunction((JobID jobId, String directory) -> CompletableFuture.completedFuture(COMPLETED_SAVEPOINT_EXTERNAL_POINTER))
			.build();

	try {
		savepointTriggerHandler.handleRequest(
				triggerSavepointRequestWithDefaultDirectory(),
				testingRestfulGateway).get();
		fail("Expected exception not thrown.");
	} catch (RestHandlerException rhe) {
		assertThat(
				rhe.getMessage(),
				equalTo("Config key [state.savepoints.dir] is not set. " +
						"Property [targetDirectory] must be provided."));
		assertThat(rhe.getHttpResponseStatus(), equalTo(HttpResponseStatus.BAD_REQUEST));
	}
}
 
Example #16
Source File: JobSubmitHandler.java    From flink with Apache License 2.0 6 votes vote down vote up
private CompletableFuture<JobGraph> uploadJobGraphFiles(
		DispatcherGateway gateway,
		CompletableFuture<JobGraph> jobGraphFuture,
		Collection<Path> jarFiles,
		Collection<Tuple2<String, Path>> artifacts,
		Configuration configuration) {
	CompletableFuture<Integer> blobServerPortFuture = gateway.getBlobServerPort(timeout);

	return jobGraphFuture.thenCombine(blobServerPortFuture, (JobGraph jobGraph, Integer blobServerPort) -> {
		final InetSocketAddress address = new InetSocketAddress(gateway.getHostname(), blobServerPort);
		try {
			ClientUtils.uploadJobGraphFiles(jobGraph, jarFiles, artifacts, () -> new BlobClient(address, configuration));
		} catch (FlinkException e) {
			throw new CompletionException(new RestHandlerException(
				"Could not upload job files.",
				HttpResponseStatus.INTERNAL_SERVER_ERROR,
				e));
		}
		return jobGraph;
	});
}
 
Example #17
Source File: FlinkHttpObjectAggregator.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
protected void decode(
		final ChannelHandlerContext ctx,
		final HttpObject msg,
		final List<Object> out) throws Exception {

	try {
		super.decode(ctx, msg, out);
	} catch (final TooLongFrameException e) {
		HandlerUtils.sendErrorResponse(
			ctx,
			false,
			new ErrorResponseBody(String.format(
				e.getMessage() + " Try to raise [%s]",
				RestOptions.SERVER_MAX_CONTENT_LENGTH.key())),
			HttpResponseStatus.REQUEST_ENTITY_TOO_LARGE,
			responseHeaders);
	}
}
 
Example #18
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 #19
Source File: RestClient.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
protected void channelRead0(ChannelHandlerContext ctx, Object msg) {
	if (msg instanceof HttpResponse && ((HttpResponse) msg).status().equals(REQUEST_ENTITY_TOO_LARGE)) {
		jsonFuture.completeExceptionally(
			new RestClientException(
				String.format(
					REQUEST_ENTITY_TOO_LARGE + ". Try to raise [%s]",
					RestOptions.CLIENT_MAX_CONTENT_LENGTH.key()),
				((HttpResponse) msg).status()));
	} else if (msg instanceof FullHttpResponse) {
		readRawResponse((FullHttpResponse) msg);
	} else {
		LOG.error("Implementation error: Received a response that wasn't a FullHttpResponse.");
		if (msg instanceof HttpResponse) {
			jsonFuture.completeExceptionally(
				new RestClientException(
					"Implementation error: Received a response that wasn't a FullHttpResponse.",
					((HttpResponse) msg).getStatus()));
		} else {
			jsonFuture.completeExceptionally(
				new RestClientException(
					"Implementation error: Received a response that wasn't a FullHttpResponse.",
					HttpResponseStatus.INTERNAL_SERVER_ERROR));
		}

	}
	ctx.close();
}
 
Example #20
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 #21
Source File: HandlerUtils.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Sends the given response and status code to the given channel.
 *
 * @param channelHandlerContext identifying the open channel
 * @param keepAlive If the connection should be kept alive.
 * @param message which should be sent
 * @param statusCode of the message to send
 * @param headers additional header values
 */
public static CompletableFuture<Void> sendResponse(
		@Nonnull ChannelHandlerContext channelHandlerContext,
		boolean keepAlive,
		@Nonnull String message,
		@Nonnull HttpResponseStatus statusCode,
		@Nonnull Map<String, String> headers) {
	HttpResponse response = new DefaultHttpResponse(HTTP_1_1, statusCode);

	response.headers().set(CONTENT_TYPE, RestConstants.REST_CONTENT_TYPE);

	for (Map.Entry<String, String> headerEntry : headers.entrySet()) {
		response.headers().set(headerEntry.getKey(), headerEntry.getValue());
	}

	if (keepAlive) {
		response.headers().set(CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
	}

	byte[] buf = message.getBytes(ConfigConstants.DEFAULT_CHARSET);
	ByteBuf b = Unpooled.copiedBuffer(buf);
	HttpHeaders.setContentLength(response, buf.length);

	// write the initial line and the header.
	channelHandlerContext.write(response);

	channelHandlerContext.write(b);

	ChannelFuture lastContentFuture = channelHandlerContext.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);

	// close the connection, if no keep-alive is needed
	if (!keepAlive) {
		lastContentFuture.addListener(ChannelFutureListener.CLOSE);
	}

	return toCompletableFuture(lastContentFuture);
}
 
Example #22
Source File: JarHandlerParameterTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testConfigurationViaJsonRequestFailWithProgArgsAsStringAndList() throws Exception {
	try {
		testConfigurationViaJsonRequest(ProgramArgsParType.Both);
		fail("RestHandlerException is excepted");
	} catch (RestHandlerException e) {
		assertEquals(HttpResponseStatus.BAD_REQUEST, e.getHttpResponseStatus());
	}
}
 
Example #23
Source File: MesosArtifactServer.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Writes a simple  error response message.
 *
 * @param ctx    The channel context to write the response to.
 * @param status The response status.
 */
private static void sendError(ChannelHandlerContext ctx, HttpResponseStatus status) {
	FullHttpResponse response = new DefaultFullHttpResponse(
		HTTP_1_1, status, Unpooled.copiedBuffer("Failure: " + status + "\r\n", CharsetUtil.UTF_8));
	HttpHeaders.setHeader(response, CONTENT_TYPE, "text/plain; charset=UTF-8");

	// close the connection as soon as the error message is sent.
	ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
}
 
Example #24
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 #25
Source File: AbstractTaskManagerFileHandler.java    From flink with Apache License 2.0 5 votes vote down vote up
private CompletableFuture<TransientBlobKey> loadTaskManagerFile(Tuple2<ResourceID, String> taskManagerIdAndFileName) throws RestHandlerException {
	log.debug("Load file from TaskManager {}.", taskManagerIdAndFileName.f0);

	final ResourceManagerGateway resourceManagerGateway = resourceManagerGatewayRetriever
		.getNow()
		.orElseThrow(() -> {
			log.debug("Could not connect to ResourceManager right now.");
			return new RestHandlerException(
				"Cannot connect to ResourceManager right now. Please try to refresh.",
				HttpResponseStatus.NOT_FOUND);
		});

	return requestFileUpload(resourceManagerGateway, taskManagerIdAndFileName);
}
 
Example #26
Source File: JarHandlerParameterTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testConfigurationViaJsonRequestFailWithProgArgsAsStringAndList() throws Exception {
	try {
		testConfigurationViaJsonRequest(ProgramArgsParType.Both);
		fail("RestHandlerException is excepted");
	} catch (RestHandlerException e) {
		assertEquals(HttpResponseStatus.BAD_REQUEST, e.getHttpResponseStatus());
	}
}
 
Example #27
Source File: FileUploadHandlerTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test
public void testUploadCleanupOnUnknownAttribute() throws IOException {
	OkHttpClient client = new OkHttpClient();

	Request request = buildMixedRequestWithUnknownAttribute(MULTIPART_UPLOAD_RESOURCE.getMixedHandler().getMessageHeaders().getTargetRestEndpointURL());
	try (Response response = client.newCall(request).execute()) {
		assertEquals(HttpResponseStatus.BAD_REQUEST.code(), response.code());
	}
	MULTIPART_UPLOAD_RESOURCE.assertUploadDirectoryIsEmpty();
}
 
Example #28
Source File: HandlerUtils.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Sends the given error response and status code to the given channel.
 *
 * @param channelHandlerContext identifying the open channel
 * @param httpRequest originating http request
 * @param errorMessage which should be sent
 * @param statusCode of the message to send
 * @param headers additional header values
 */
public static CompletableFuture<Void> sendErrorResponse(
		ChannelHandlerContext channelHandlerContext,
		HttpRequest httpRequest,
		ErrorResponseBody errorMessage,
		HttpResponseStatus statusCode,
		Map<String, String> headers) {

	return sendErrorResponse(
		channelHandlerContext,
		HttpHeaders.isKeepAlive(httpRequest),
		errorMessage,
		statusCode,
		headers);
}
 
Example #29
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 #30
Source File: TestBaseUtils.java    From flink with Apache License 2.0 5 votes vote down vote up
public static String getFromHTTP(String url, Time timeout) throws Exception {
	final URL u = new URL(url);
	LOG.info("Accessing URL " + url + " as URL: " + u);

	final long deadline = timeout.toMilliseconds() + System.currentTimeMillis();

	while (System.currentTimeMillis() <= deadline) {
		HttpURLConnection connection = (HttpURLConnection) u.openConnection();
		connection.setConnectTimeout(100000);
		connection.connect();

		if (Objects.equals(HttpResponseStatus.SERVICE_UNAVAILABLE, HttpResponseStatus.valueOf(connection.getResponseCode()))) {
			// service not available --> Sleep and retry
			LOG.debug("Web service currently not available. Retrying the request in a bit.");
			Thread.sleep(100L);
		} else {
			InputStream is;

			if (connection.getResponseCode() >= 400) {
				// error!
				LOG.warn("HTTP Response code when connecting to {} was {}", url, connection.getResponseCode());
				is = connection.getErrorStream();
			} else {
				is = connection.getInputStream();
			}

			return IOUtils.toString(is, ConfigConstants.DEFAULT_CHARSET);
		}
	}

	throw new TimeoutException("Could not get HTTP response in time since the service is still unavailable.");
}