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

The following examples show how to use org.apache.flink.runtime.rest.messages.ErrorResponseBody. 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: FlinkHttpObjectAggregator.java    From Flink-CEPplus 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 #2
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 #3
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 #4
Source File: HandlerUtils.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Sends the given response and status code to the given channel.
 *
 * @param channelHandlerContext identifying the open channel
 * @param httpRequest originating http request
 * @param response which should be sent
 * @param statusCode of the message to send
 * @param headers additional header values
 * @param <P> type of the response
 */
public static <P extends ResponseBody> CompletableFuture<Void> sendResponse(
		ChannelHandlerContext channelHandlerContext,
		HttpRequest httpRequest,
		P response,
		HttpResponseStatus statusCode,
		Map<String, String> headers) {
	StringWriter sw = new StringWriter();
	try {
		mapper.writeValue(sw, response);
	} catch (IOException ioe) {
		LOG.error("Internal server error. Could not map response to JSON.", ioe);
		return sendErrorResponse(
			channelHandlerContext,
			httpRequest,
			new ErrorResponseBody("Internal server error. Could not map response to JSON."),
			HttpResponseStatus.INTERNAL_SERVER_ERROR,
			headers);
	}
	return sendResponse(
		channelHandlerContext,
		httpRequest,
		sw.toString(),
		statusCode,
		headers);
}
 
Example #5
Source File: StaticFileServerHandler.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
protected void respondAsLeader(ChannelHandlerContext channelHandlerContext, RoutedRequest routedRequest, T gateway) throws Exception {
	final HttpRequest request = routedRequest.getRequest();
	final String requestPath;

	// make sure we request the "index.html" in case there is a directory request
	if (routedRequest.getPath().endsWith("/")) {
		requestPath = routedRequest.getPath() + "index.html";
	} else {
		requestPath = routedRequest.getPath();
	}

	try {
		respondToRequest(channelHandlerContext, request, requestPath);
	} catch (RestHandlerException rhe) {
		HandlerUtils.sendErrorResponse(
			channelHandlerContext,
			routedRequest.getRequest(),
			new ErrorResponseBody(rhe.getMessage()),
			rhe.getHttpResponseStatus(),
			responseHeaders);
	}
}
 
Example #6
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 #7
Source File: AbstractJobManagerFileHandler.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
protected CompletableFuture<Void> respondToRequest(ChannelHandlerContext ctx, HttpRequest httpRequest, HandlerRequest<EmptyRequestBody, M> handlerRequest, RestfulGateway gateway) {
	File file = getFile(handlerRequest);
	if (file != null && file.exists()) {
		try {
			HandlerUtils.transferFile(
				ctx,
				file,
				httpRequest);
		} catch (FlinkException e) {
			throw new CompletionException(new FlinkException("Could not transfer file to client.", e));
		}
		return CompletableFuture.completedFuture(null);
	} else {
		return HandlerUtils.sendErrorResponse(
			ctx,
			httpRequest,
			new ErrorResponseBody("This file does not exist in JobManager log dir."),
			HttpResponseStatus.NOT_FOUND,
			Collections.emptyMap());
	}
}
 
Example #8
Source File: HandlerUtils.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Sends the given response and status code to the given channel.
 *
 * @param channelHandlerContext identifying the open channel
 * @param httpRequest originating http request
 * @param response which should be sent
 * @param statusCode of the message to send
 * @param headers additional header values
 * @param <P> type of the response
 */
public static <P extends ResponseBody> CompletableFuture<Void> sendResponse(
		ChannelHandlerContext channelHandlerContext,
		HttpRequest httpRequest,
		P response,
		HttpResponseStatus statusCode,
		Map<String, String> headers) {
	StringWriter sw = new StringWriter();
	try {
		mapper.writeValue(sw, response);
	} catch (IOException ioe) {
		LOG.error("Internal server error. Could not map response to JSON.", ioe);
		return sendErrorResponse(
			channelHandlerContext,
			httpRequest,
			new ErrorResponseBody("Internal server error. Could not map response to JSON."),
			HttpResponseStatus.INTERNAL_SERVER_ERROR,
			headers);
	}
	return sendResponse(
		channelHandlerContext,
		httpRequest,
		sw.toString(),
		statusCode,
		headers);
}
 
Example #9
Source File: HandlerUtils.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
/**
 * Sends the given response and status code to the given channel.
 *
 * @param channelHandlerContext identifying the open channel
 * @param httpRequest originating http request
 * @param response which should be sent
 * @param statusCode of the message to send
 * @param headers additional header values
 * @param <P> type of the response
 */
public static <P extends ResponseBody> CompletableFuture<Void> sendResponse(
		ChannelHandlerContext channelHandlerContext,
		HttpRequest httpRequest,
		P response,
		HttpResponseStatus statusCode,
		Map<String, String> headers) {
	StringWriter sw = new StringWriter();
	try {
		mapper.writeValue(sw, response);
	} catch (IOException ioe) {
		LOG.error("Internal server error. Could not map response to JSON.", ioe);
		return sendErrorResponse(
			channelHandlerContext,
			httpRequest,
			new ErrorResponseBody("Internal server error. Could not map response to JSON."),
			HttpResponseStatus.INTERNAL_SERVER_ERROR,
			headers);
	}
	return sendResponse(
		channelHandlerContext,
		httpRequest,
		sw.toString(),
		statusCode,
		headers);
}
 
Example #10
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 #11
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 #12
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 #13
Source File: HandlerUtils.java    From flink 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 #14
Source File: PipelineErrorHandler.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
	logger.warn("Unhandled exception", cause);
	HandlerUtils.sendErrorResponse(
		ctx,
		false,
		new ErrorResponseBody("Internal server error: " + cause.getMessage()),
		HttpResponseStatus.INTERNAL_SERVER_ERROR,
		responseHeaders);
}
 
Example #15
Source File: HandlerUtils.java    From flink 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 #16
Source File: HandlerUtils.java    From flink 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 keepAlive If the connection should be kept alive.
 * @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,
		boolean keepAlive,
		ErrorResponseBody errorMessage,
		HttpResponseStatus statusCode,
		Map<String, String> headers) {

	StringWriter sw = new StringWriter();
	try {
		mapper.writeValue(sw, errorMessage);
	} catch (IOException e) {
		// this should never happen
		LOG.error("Internal server error. Could not map error response to JSON.", e);
		return sendResponse(
			channelHandlerContext,
			keepAlive,
			"Internal server error. Could not map error response to JSON.",
			HttpResponseStatus.INTERNAL_SERVER_ERROR,
			headers);
	}
	return sendResponse(
		channelHandlerContext,
		keepAlive,
		sw.toString(),
		statusCode,
		headers);
}
 
Example #17
Source File: PipelineErrorHandler.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
protected void channelRead0(ChannelHandlerContext ctx, HttpRequest message) {
	// we can't deal with this message. No one in the pipeline handled it. Log it.
	logger.warn("Unknown message received: {}", message);
	HandlerUtils.sendErrorResponse(
		ctx,
		message,
		new ErrorResponseBody("Bad request received."),
		HttpResponseStatus.BAD_REQUEST,
		Collections.emptyMap());
}
 
Example #18
Source File: LeaderRetrievalHandler.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
protected void channelRead0(
	ChannelHandlerContext channelHandlerContext,
	RoutedRequest routedRequest) {

	HttpRequest request = routedRequest.getRequest();

	OptionalConsumer<? extends T> optLeaderConsumer = OptionalConsumer.of(leaderRetriever.getNow());

	optLeaderConsumer.ifPresent(
		gateway -> {
			try {
				respondAsLeader(channelHandlerContext, routedRequest, gateway);
			} catch (Exception e) {
				logger.error("Error while responding to the http request.", e);
				HandlerUtils.sendErrorResponse(
					channelHandlerContext,
					request,
					new ErrorResponseBody("Error while responding to the http request."),
					HttpResponseStatus.INTERNAL_SERVER_ERROR,
					responseHeaders);
			}
		}
	).ifNotPresent(
		() ->
			HandlerUtils.sendErrorResponse(
				channelHandlerContext,
				request,
				new ErrorResponseBody("Service temporarily unavailable due to an ongoing leader election. Please refresh."),
				HttpResponseStatus.SERVICE_UNAVAILABLE,
				responseHeaders));
}
 
Example #19
Source File: StaticFileServerHandler.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
	if (ctx.channel().isActive()) {
		logger.error("Caught exception", cause);
		HandlerUtils.sendErrorResponse(
			ctx,
			false,
			new ErrorResponseBody("Internal server error."),
			INTERNAL_SERVER_ERROR,
			Collections.emptyMap());
	}
}
 
Example #20
Source File: RouterHandler.java    From flink with 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);
}
 
Example #21
Source File: FileUploadHandler.java    From flink with Apache License 2.0 5 votes vote down vote up
private void handleError(ChannelHandlerContext ctx, String errorMessage, HttpResponseStatus responseStatus, @Nullable Throwable e) {
	HttpRequest tmpRequest = currentHttpRequest;
	deleteUploadedFiles();
	reset();
	LOG.warn(errorMessage, e);
	HandlerUtils.sendErrorResponse(
		ctx,
		tmpRequest,
		new ErrorResponseBody(errorMessage),
		responseStatus,
		Collections.emptyMap()
	);
	ReferenceCountUtil.release(tmpRequest);
}
 
Example #22
Source File: HistoryServerStaticFileServerHandler.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
	if (ctx.channel().isActive()) {
		LOG.error("Caught exception", cause);
		HandlerUtils.sendErrorResponse(
			ctx,
			false,
			new ErrorResponseBody("Internal server error."),
			INTERNAL_SERVER_ERROR,
			Collections.emptyMap());
	}
}
 
Example #23
Source File: HistoryServerStaticFileServerHandler.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void channelRead0(ChannelHandlerContext ctx, RoutedRequest routedRequest) throws Exception {
	String requestPath = routedRequest.getPath();

	try {
		respondWithFile(ctx, routedRequest.getRequest(), requestPath);
	} catch (RestHandlerException rhe) {
		HandlerUtils.sendErrorResponse(
			ctx,
			routedRequest.getRequest(),
			new ErrorResponseBody(rhe.getMessage()),
			rhe.getHttpResponseStatus(),
			Collections.emptyMap());
	}
}
 
Example #24
Source File: HistoryServerStaticFileServerHandler.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
	if (ctx.channel().isActive()) {
		LOG.error("Caught exception", cause);
		HandlerUtils.sendErrorResponse(
			ctx,
			false,
			new ErrorResponseBody("Internal server error."),
			INTERNAL_SERVER_ERROR,
			Collections.emptyMap());
	}
}
 
Example #25
Source File: FileUploadHandler.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
private void handleError(ChannelHandlerContext ctx, String errorMessage, HttpResponseStatus responseStatus, @Nullable Throwable e) {
	HttpRequest tmpRequest = currentHttpRequest;
	deleteUploadedFiles();
	reset();
	LOG.warn(errorMessage, e);
	HandlerUtils.sendErrorResponse(
		ctx,
		tmpRequest,
		new ErrorResponseBody(errorMessage),
		responseStatus,
		Collections.emptyMap()
	);
	ReferenceCountUtil.release(tmpRequest);
}
 
Example #26
Source File: StaticFileServerHandler.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
	if (ctx.channel().isActive()) {
		logger.error("Caught exception", cause);
		HandlerUtils.sendErrorResponse(
			ctx,
			false,
			new ErrorResponseBody("Internal server error."),
			INTERNAL_SERVER_ERROR,
			Collections.emptyMap());
	}
}
 
Example #27
Source File: LeaderRetrievalHandler.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
protected void channelRead0(
	ChannelHandlerContext channelHandlerContext,
	RoutedRequest routedRequest) {

	HttpRequest request = routedRequest.getRequest();

	OptionalConsumer<? extends T> optLeaderConsumer = OptionalConsumer.of(leaderRetriever.getNow());

	optLeaderConsumer.ifPresent(
		gateway -> {
			try {
				respondAsLeader(channelHandlerContext, routedRequest, gateway);
			} catch (Exception e) {
				logger.error("Error while responding to the http request.", e);
				HandlerUtils.sendErrorResponse(
					channelHandlerContext,
					request,
					new ErrorResponseBody("Error while responding to the http request."),
					HttpResponseStatus.INTERNAL_SERVER_ERROR,
					responseHeaders);
			}
		}
	).ifNotPresent(
		() ->
			HandlerUtils.sendErrorResponse(
				channelHandlerContext,
				request,
				new ErrorResponseBody("Service temporarily unavailable due to an ongoing leader election. Please refresh."),
				HttpResponseStatus.SERVICE_UNAVAILABLE,
				responseHeaders));
}
 
Example #28
Source File: PipelineErrorHandler.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
protected void channelRead0(ChannelHandlerContext ctx, HttpRequest message) {
	// we can't deal with this message. No one in the pipeline handled it. Log it.
	logger.warn("Unknown message received: {}", message);
	HandlerUtils.sendErrorResponse(
		ctx,
		message,
		new ErrorResponseBody("Bad request received."),
		HttpResponseStatus.BAD_REQUEST,
		Collections.emptyMap());
}
 
Example #29
Source File: PipelineErrorHandler.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
	logger.warn("Unhandled exception", cause);
	HandlerUtils.sendErrorResponse(
		ctx,
		false,
		new ErrorResponseBody("Internal server error: " + cause.getMessage()),
		HttpResponseStatus.INTERNAL_SERVER_ERROR,
		responseHeaders);
}
 
Example #30
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);
}