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

The following examples show how to use org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpResponseStatus. These examples are extracted from open source projects. 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 Project: flink   Source File: FlinkHttpObjectAggregator.java    License: 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 2
Source Project: flink   Source File: RestClusterClientTest.java    License: 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 3
Source Project: flink   Source File: RestClusterClientTest.java    License: 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 4
Source Project: flink   Source File: CheckpointConfigHandler.java    License: 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 5
Source Project: Flink-CEPplus   Source File: RouterHandler.java    License: 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 6
Source Project: flink   Source File: JarUploadHandlerTest.java    License: 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 7
@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 8
Source Project: flink   Source File: StopWithSavepointHandlersTest.java    License: 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 9
Source Project: Flink-CEPplus   Source File: FileUploadHandlerTest.java    License: 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 10
Source Project: flink   Source File: RestClusterClientTest.java    License: 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 11
Source Project: flink   Source File: RestClusterClientTest.java    License: 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 12
Source Project: flink   Source File: StopWithSavepointHandlersTest.java    License: 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 13
Source Project: flink   Source File: JobSubmitHandler.java    License: 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 14
@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 15
Source Project: Flink-CEPplus   Source File: JobSubmitHandler.java    License: 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 16
Source Project: Flink-CEPplus   Source File: AbstractHandler.java    License: 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 17
Source Project: flink   Source File: FileUploadHandlerTest.java    License: 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 18
Source Project: flink   Source File: FileUploadHandlerTest.java    License: 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 19
Source Project: Flink-CEPplus   Source File: JarHandlerUtils.java    License: 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 20
Source Project: flink   Source File: HandlerUtils.java    License: 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 21
Source Project: Flink-CEPplus   Source File: JarHandlerParameterTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testFailIfProgArgsAreAsStringAndAsList() throws Exception {
	try {
		testParameterPrioritization(ProgramArgsParType.Both);
		fail("RestHandlerException is excepted");
	} catch (RestHandlerException e) {
		assertEquals(HttpResponseStatus.BAD_REQUEST, e.getHttpResponseStatus());
	}
}
 
Example 22
Source Project: flink   Source File: AbstractTaskManagerFileHandler.java    License: 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 23
Source Project: flink   Source File: TestBaseUtils.java    License: 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.");
}
 
Example 24
Source Project: Flink-CEPplus   Source File: FileUploadHandlerTest.java    License: 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 25
Source Project: flink   Source File: JarHandlerParameterTest.java    License: 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 26
Source Project: flink   Source File: MultipartUploadResource.java    License: 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 27
Source Project: flink   Source File: JarHandlerUtils.java    License: 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 28
Source Project: Flink-CEPplus   Source File: HandlerUtils.java    License: 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 Project: Flink-CEPplus   Source File: JobSubmitHandler.java    License: 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 30
Source Project: Flink-CEPplus   Source File: RouterHandler.java    License: Apache License 2.0 5 votes vote down vote up
private void respondNotFound(ChannelHandlerContext channelHandlerContext, HttpRequest request) {
	LOG.trace("Request could not be routed to any handler. Uri:{} Method:{}", request.getUri(), request.getMethod());
	HandlerUtils.sendErrorResponse(
		channelHandlerContext,
		request,
		new ErrorResponseBody("Not found."),
		HttpResponseStatus.NOT_FOUND,
		responseHeaders);
}