org.apache.flink.runtime.rest.util.RestClientException Java Examples

The following examples show how to use org.apache.flink.runtime.rest.util.RestClientException. 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: RestClient.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
private static <P extends ResponseBody> CompletableFuture<P> parseResponse(JsonResponse rawResponse, JavaType responseType) {
	CompletableFuture<P> responseFuture = new CompletableFuture<>();
	final JsonParser jsonParser = objectMapper.treeAsTokens(rawResponse.json);
	try {
		P response = objectMapper.readValue(jsonParser, responseType);
		responseFuture.complete(response);
	} catch (IOException originalException) {
		// the received response did not matched the expected response type

		// lets see if it is an ErrorResponse instead
		try {
			ErrorResponseBody error = objectMapper.treeToValue(rawResponse.getJson(), ErrorResponseBody.class);
			responseFuture.completeExceptionally(new RestClientException(error.errors.toString(), rawResponse.getHttpResponseStatus()));
		} catch (JsonProcessingException jpe2) {
			// if this fails it is either the expected type or response type was wrong, most likely caused
			// by a client/search MessageHeaders mismatch
			LOG.error("Received response was neither of the expected type ({}) nor an error. Response={}", responseType, rawResponse, jpe2);
			responseFuture.completeExceptionally(
				new RestClientException(
					"Response was neither of the expected type(" + responseType + ") nor an error.",
					originalException,
					rawResponse.getHttpResponseStatus()));
		}
	}
	return responseFuture;
}
 
Example #2
Source File: RestServerEndpointITCase.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that requests larger than {@link #TEST_REST_MAX_CONTENT_LENGTH} are rejected.
 */
@Test
public void testShouldRespectMaxContentLengthLimitForRequests() throws Exception {
	testHandler.handlerBody = id -> {
		throw new AssertionError("Request should not arrive at server.");
	};

	try {
		sendRequestToTestHandler(new TestRequest(2, createStringOfSize(TEST_REST_MAX_CONTENT_LENGTH))).get();
		fail("Expected exception not thrown");
	} catch (final ExecutionException e) {
		final Throwable throwable = ExceptionUtils.stripExecutionException(e);
		assertThat(throwable, instanceOf(RestClientException.class));
		assertThat(throwable.getMessage(), containsString("Try to raise"));
	}
}
 
Example #3
Source File: RestServerEndpointITCase.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that requests larger than {@link #TEST_REST_MAX_CONTENT_LENGTH} are rejected.
 */
@Test
public void testShouldRespectMaxContentLengthLimitForRequests() throws Exception {
	testHandler.handlerBody = id -> {
		throw new AssertionError("Request should not arrive at server.");
	};

	try {
		sendRequestToTestHandler(new TestRequest(2, createStringOfSize(TEST_REST_MAX_CONTENT_LENGTH))).get();
		fail("Expected exception not thrown");
	} catch (final ExecutionException e) {
		final Throwable throwable = ExceptionUtils.stripExecutionException(e);
		assertThat(throwable, instanceOf(RestClientException.class));
		assertThat(throwable.getMessage(), containsString("Try to raise"));
	}
}
 
Example #4
Source File: RestClient.java    From flink with Apache License 2.0 6 votes vote down vote up
private static <P extends ResponseBody> CompletableFuture<P> parseResponse(JsonResponse rawResponse, JavaType responseType) {
	CompletableFuture<P> responseFuture = new CompletableFuture<>();
	final JsonParser jsonParser = objectMapper.treeAsTokens(rawResponse.json);
	try {
		P response = objectMapper.readValue(jsonParser, responseType);
		responseFuture.complete(response);
	} catch (IOException originalException) {
		// the received response did not matched the expected response type

		// lets see if it is an ErrorResponse instead
		try {
			ErrorResponseBody error = objectMapper.treeToValue(rawResponse.getJson(), ErrorResponseBody.class);
			responseFuture.completeExceptionally(new RestClientException(error.errors.toString(), rawResponse.getHttpResponseStatus()));
		} catch (JsonProcessingException jpe2) {
			// if this fails it is either the expected type or response type was wrong, most likely caused
			// by a client/search MessageHeaders mismatch
			LOG.error("Received response was neither of the expected type ({}) nor an error. Response={}", responseType, rawResponse, jpe2);
			responseFuture.completeExceptionally(
				new RestClientException(
					"Response was neither of the expected type(" + responseType + ") nor an error.",
					originalException,
					rawResponse.getHttpResponseStatus()));
		}
	}
	return responseFuture;
}
 
Example #5
Source File: RestClient.java    From flink with Apache License 2.0 6 votes vote down vote up
private static <P extends ResponseBody> CompletableFuture<P> parseResponse(JsonResponse rawResponse, JavaType responseType) {
	CompletableFuture<P> responseFuture = new CompletableFuture<>();
	final JsonParser jsonParser = objectMapper.treeAsTokens(rawResponse.json);
	try {
		P response = objectMapper.readValue(jsonParser, responseType);
		responseFuture.complete(response);
	} catch (IOException originalException) {
		// the received response did not matched the expected response type

		// lets see if it is an ErrorResponse instead
		try {
			ErrorResponseBody error = objectMapper.treeToValue(rawResponse.getJson(), ErrorResponseBody.class);
			responseFuture.completeExceptionally(new RestClientException(error.errors.toString(), rawResponse.getHttpResponseStatus()));
		} catch (JsonProcessingException jpe2) {
			// if this fails it is either the expected type or response type was wrong, most likely caused
			// by a client/search MessageHeaders mismatch
			LOG.error("Received response was neither of the expected type ({}) nor an error. Response={}", responseType, rawResponse, jpe2);
			responseFuture.completeExceptionally(
				new RestClientException(
					"Response was neither of the expected type(" + responseType + ") nor an error.",
					originalException,
					rawResponse.getHttpResponseStatus()));
		}
	}
	return responseFuture;
}
 
Example #6
Source File: RestServerEndpointITCase.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that requests larger than {@link #TEST_REST_MAX_CONTENT_LENGTH} are rejected.
 */
@Test
public void testShouldRespectMaxContentLengthLimitForRequests() throws Exception {
	testHandler.handlerBody = id -> {
		throw new AssertionError("Request should not arrive at server.");
	};

	try {
		sendRequestToTestHandler(new TestRequest(2, createStringOfSize(TEST_REST_MAX_CONTENT_LENGTH))).get();
		fail("Expected exception not thrown");
	} catch (final ExecutionException e) {
		final Throwable throwable = ExceptionUtils.stripExecutionException(e);
		assertThat(throwable, instanceOf(RestClientException.class));
		assertThat(throwable.getMessage(), containsString("Try to raise"));
	}
}
 
Example #7
Source File: RestClusterClient.java    From flink with Apache License 2.0 5 votes vote down vote up
private static Predicate<Throwable> httpExceptionCodePredicate(Predicate<Integer> statusCodePredicate) {
	return (throwable) -> ExceptionUtils.findThrowable(throwable, RestClientException.class)
		.map(restClientException -> {
			final int code = restClientException.getHttpResponseStatus().code();
			return statusCodePredicate.test(code);
		})
		.orElse(false);
}
 
Example #8
Source File: RestServerEndpointITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Tests that a bad handler request (HandlerRequest cannot be created) is reported as a BAD_REQUEST
 * and not an internal server error.
 *
 * <p>See FLINK-7663
 */
@Test
public void testBadHandlerRequest() throws Exception {
	final FaultyTestParameters parameters = new FaultyTestParameters();

	parameters.faultyJobIDPathParameter.resolve(PATH_JOB_ID);
	((TestParameters) parameters).jobIDQueryParameter.resolve(Collections.singletonList(QUERY_JOB_ID));

	CompletableFuture<TestResponse> response = restClient.sendRequest(
		serverAddress.getHostName(),
		serverAddress.getPort(),
		new TestHeaders(),
		parameters,
		new TestRequest(2));

	try {
		response.get();

		fail("The request should fail with a bad request return code.");
	} catch (ExecutionException ee) {
		Throwable t = ExceptionUtils.stripExecutionException(ee);

		assertTrue(t instanceof RestClientException);

		RestClientException rce = (RestClientException) t;

		assertEquals(HttpResponseStatus.BAD_REQUEST, rce.getHttpResponseStatus());
	}
}
 
Example #9
Source File: RestClient.java    From flink with Apache License 2.0 5 votes vote down vote up
private void readRawResponse(FullHttpResponse msg) {
	ByteBuf content = msg.content();

	JsonNode rawResponse;
	try (InputStream in = new ByteBufInputStream(content)) {
		rawResponse = objectMapper.readTree(in);
		LOG.debug("Received response {}.", rawResponse);
	} catch (JsonProcessingException je) {
		LOG.error("Response was not valid JSON.", je);
		// let's see if it was a plain-text message instead
		content.readerIndex(0);
		try (ByteBufInputStream in = new ByteBufInputStream(content)) {
			byte[] data = new byte[in.available()];
			in.readFully(data);
			String message = new String(data);
			LOG.error("Unexpected plain-text response: {}", message);
			jsonFuture.completeExceptionally(new RestClientException("Response was not valid JSON, but plain-text: " + message, je, msg.getStatus()));
		} catch (IOException e) {
			jsonFuture.completeExceptionally(new RestClientException("Response was not valid JSON, nor plain-text.", je, msg.getStatus()));
		}
		return;
	} catch (IOException ioe) {
		LOG.error("Response could not be read.", ioe);
		jsonFuture.completeExceptionally(new RestClientException("Response could not be read.", ioe, msg.getStatus()));
		return;
	}
	jsonFuture.complete(new JsonResponse(rawResponse, msg.getStatus()));
}
 
Example #10
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 #11
Source File: RestClusterClient.java    From flink with Apache License 2.0 5 votes vote down vote up
private static Predicate<Throwable> httpExceptionCodePredicate(Predicate<Integer> statusCodePredicate) {
	return (throwable) -> ExceptionUtils.findThrowable(throwable, RestClientException.class)
		.map(restClientException -> {
			final int code = restClientException.getHttpResponseStatus().code();
			return statusCodePredicate.test(code);
		})
		.orElse(false);
}
 
Example #12
Source File: RestServerEndpointITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Tests that a bad handler request (HandlerRequest cannot be created) is reported as a BAD_REQUEST
 * and not an internal server error.
 *
 * <p>See FLINK-7663
 */
@Test
public void testBadHandlerRequest() throws Exception {
	final FaultyTestParameters parameters = new FaultyTestParameters();

	parameters.faultyJobIDPathParameter.resolve(PATH_JOB_ID);
	((TestParameters) parameters).jobIDQueryParameter.resolve(Collections.singletonList(QUERY_JOB_ID));

	CompletableFuture<TestResponse> response = restClient.sendRequest(
		serverAddress.getHostName(),
		serverAddress.getPort(),
		new TestHeaders(),
		parameters,
		new TestRequest(2));

	try {
		response.get();

		fail("The request should fail with a bad request return code.");
	} catch (ExecutionException ee) {
		Throwable t = ExceptionUtils.stripExecutionException(ee);

		assertTrue(t instanceof RestClientException);

		RestClientException rce = (RestClientException) t;

		assertEquals(HttpResponseStatus.BAD_REQUEST, rce.getHttpResponseStatus());
	}
}
 
Example #13
Source File: RestClient.java    From flink with Apache License 2.0 5 votes vote down vote up
private void readRawResponse(FullHttpResponse msg) {
	ByteBuf content = msg.content();

	JsonNode rawResponse;
	try (InputStream in = new ByteBufInputStream(content)) {
		rawResponse = objectMapper.readTree(in);
		LOG.debug("Received response {}.", rawResponse);
	} catch (JsonProcessingException je) {
		LOG.error("Response was not valid JSON.", je);
		// let's see if it was a plain-text message instead
		content.readerIndex(0);
		try (ByteBufInputStream in = new ByteBufInputStream(content)) {
			byte[] data = new byte[in.available()];
			in.readFully(data);
			String message = new String(data);
			LOG.error("Unexpected plain-text response: {}", message);
			jsonFuture.completeExceptionally(new RestClientException("Response was not valid JSON, but plain-text: " + message, je, msg.getStatus()));
		} catch (IOException e) {
			jsonFuture.completeExceptionally(new RestClientException("Response was not valid JSON, nor plain-text.", je, msg.getStatus()));
		}
		return;
	} catch (IOException ioe) {
		LOG.error("Response could not be read.", ioe);
		jsonFuture.completeExceptionally(new RestClientException("Response could not be read.", ioe, msg.getStatus()));
		return;
	}
	jsonFuture.complete(new JsonResponse(rawResponse, msg.getStatus()));
}
 
Example #14
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 #15
Source File: RestClusterClient.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
private static Predicate<Throwable> httpExceptionCodePredicate(Predicate<Integer> statusCodePredicate) {
	return (throwable) -> ExceptionUtils.findThrowable(throwable, RestClientException.class)
		.map(restClientException -> {
			final int code = restClientException.getHttpResponseStatus().code();
			return statusCodePredicate.test(code);
		})
		.orElse(false);
}
 
Example #16
Source File: RestServerEndpointITCase.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Tests that a bad handler request (HandlerRequest cannot be created) is reported as a BAD_REQUEST
 * and not an internal server error.
 *
 * <p>See FLINK-7663
 */
@Test
public void testBadHandlerRequest() throws Exception {
	final FaultyTestParameters parameters = new FaultyTestParameters();

	parameters.faultyJobIDPathParameter.resolve(PATH_JOB_ID);
	((TestParameters) parameters).jobIDQueryParameter.resolve(Collections.singletonList(QUERY_JOB_ID));

	CompletableFuture<TestResponse> response = restClient.sendRequest(
		serverAddress.getHostName(),
		serverAddress.getPort(),
		new TestHeaders(),
		parameters,
		new TestRequest(2));

	try {
		response.get();

		fail("The request should fail with a bad request return code.");
	} catch (ExecutionException ee) {
		Throwable t = ExceptionUtils.stripExecutionException(ee);

		assertTrue(t instanceof RestClientException);

		RestClientException rce = (RestClientException) t;

		assertEquals(HttpResponseStatus.BAD_REQUEST, rce.getHttpResponseStatus());
	}
}
 
Example #17
Source File: RestClient.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
private void readRawResponse(FullHttpResponse msg) {
	ByteBuf content = msg.content();

	JsonNode rawResponse;
	try (InputStream in = new ByteBufInputStream(content)) {
		rawResponse = objectMapper.readTree(in);
		LOG.debug("Received response {}.", rawResponse);
	} catch (JsonProcessingException je) {
		LOG.error("Response was not valid JSON.", je);
		// let's see if it was a plain-text message instead
		content.readerIndex(0);
		try (ByteBufInputStream in = new ByteBufInputStream(content)) {
			byte[] data = new byte[in.available()];
			in.readFully(data);
			String message = new String(data);
			LOG.error("Unexpected plain-text response: {}", message);
			jsonFuture.completeExceptionally(new RestClientException("Response was not valid JSON, but plain-text: " + message, je, msg.getStatus()));
		} catch (IOException e) {
			jsonFuture.completeExceptionally(new RestClientException("Response was not valid JSON, nor plain-text.", je, msg.getStatus()));
		}
		return;
	} catch (IOException ioe) {
		LOG.error("Response could not be read.", ioe);
		jsonFuture.completeExceptionally(new RestClientException("Response could not be read.", ioe, msg.getStatus()));
		return;
	}
	jsonFuture.complete(new JsonResponse(rawResponse, msg.getStatus()));
}
 
Example #18
Source File: RestClient.java    From Flink-CEPplus 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 #19
Source File: JarRunHandlerTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Test
public void testRunJar() throws Exception {
	Path uploadDir = TMP.newFolder().toPath();

	Path actualUploadDir = uploadDir.resolve("flink-web-upload");
	Files.createDirectory(actualUploadDir);

	Path emptyJar = actualUploadDir.resolve("empty.jar");
	Files.createFile(emptyJar);

	Configuration config = new Configuration();
	config.setString(WebOptions.UPLOAD_DIR, uploadDir.toString());

	MiniClusterResource clusterResource = new MiniClusterResource(
		new MiniClusterResourceConfiguration.Builder()
			.setConfiguration(config)
			.setNumberTaskManagers(1)
			.setNumberSlotsPerTaskManager(1)
			.build());
	clusterResource.before();

	try {
		Configuration clientConfig = clusterResource.getClientConfiguration();
		RestClient client = new RestClient(RestClientConfiguration.fromConfiguration(clientConfig), TestingUtils.defaultExecutor());

		try {
			JarRunHeaders headers = JarRunHeaders.getInstance();
			JarRunMessageParameters parameters = headers.getUnresolvedMessageParameters();
			parameters.jarIdPathParameter.resolve(emptyJar.getFileName().toString());

			String host = clientConfig.getString(RestOptions.ADDRESS);
			int port = clientConfig.getInteger(RestOptions.PORT);

			try {
				client.sendRequest(host, port, headers, parameters, new JarRunRequestBody())
					.get();
			} catch (Exception e) {
				Optional<RestClientException> expected = ExceptionUtils.findThrowable(e, RestClientException.class);
				if (expected.isPresent()) {
					// implies the job was actually submitted
					assertTrue(expected.get().getMessage().contains("ProgramInvocationException"));
					// original cause is preserved in stack trace
					assertThat(expected.get().getMessage(), containsString("ZipException: zip file is empty"));
					// implies the jar was registered for the job graph (otherwise the jar name would not occur in the exception)
					// implies the jar was uploaded (otherwise the file would not be found at all)
					assertTrue(expected.get().getMessage().contains("empty.jar"));
				} else {
					throw e;
				}
			}
		} finally {
			client.shutdown(Time.milliseconds(10));
		}
	} finally {
		clusterResource.after();
	}
}
 
Example #20
Source File: JarRunHandlerTest.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
@Test
public void testRunJar() throws Exception {
	Path uploadDir = TMP.newFolder().toPath();

	Path actualUploadDir = uploadDir.resolve("flink-web-upload");
	Files.createDirectory(actualUploadDir);

	Path emptyJar = actualUploadDir.resolve("empty.jar");
	Files.createFile(emptyJar);

	Configuration config = new Configuration();
	config.setString(WebOptions.UPLOAD_DIR, uploadDir.toString());

	MiniClusterResource clusterResource = new MiniClusterResource(
		new MiniClusterResourceConfiguration.Builder()
			.setConfiguration(config)
			.setNumberTaskManagers(1)
			.setNumberSlotsPerTaskManager(1)
			.build());
	clusterResource.before();

	try {
		Configuration clientConfig = clusterResource.getClientConfiguration();
		RestClient client = new RestClient(RestClientConfiguration.fromConfiguration(clientConfig), TestingUtils.defaultExecutor());

		try {
			JarRunHeaders headers = JarRunHeaders.getInstance();
			JarRunMessageParameters parameters = headers.getUnresolvedMessageParameters();
			parameters.jarIdPathParameter.resolve(emptyJar.getFileName().toString());

			String host = clientConfig.getString(RestOptions.ADDRESS);
			int port = clientConfig.getInteger(RestOptions.PORT);

			try {
				client.sendRequest(host, port, headers, parameters, new JarRunRequestBody())
					.get();
			} catch (Exception e) {
				Optional<RestClientException> expected = ExceptionUtils.findThrowable(e, RestClientException.class);
				if (expected.isPresent()) {
					// implies the job was actually submitted
					assertTrue(expected.get().getMessage().contains("ProgramInvocationException"));
					// original cause is preserved in stack trace
					assertThat(expected.get().getMessage(), containsString("ZipException"));
					// implies the jar was registered for the job graph (otherwise the jar name would not occur in the exception)
					// implies the jar was uploaded (otherwise the file would not be found at all)
					assertTrue(expected.get().getMessage().contains("empty.jar'. zip file is empty"));
				} else {
					throw e;
				}
			}
		} finally {
			client.shutdown(Time.milliseconds(10));
		}
	} finally {
		clusterResource.after();
	}
}