org.apache.flink.runtime.rest.handler.router.RoutedRequest Java Examples

The following examples show how to use org.apache.flink.runtime.rest.handler.router.RoutedRequest. 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: 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 #2
Source File: StaticFileServerHandler.java    From Flink-CEPplus 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";
	}
	// in case the files being accessed are logs or stdout files, find appropriate paths.
	else if (routedRequest.getPath().equals("/jobmanager/log") || routedRequest.getPath().equals("/jobmanager/stdout")) {
		requestPath = "";
	} else {
		requestPath = routedRequest.getPath();
	}

	respondToRequest(channelHandlerContext, request, requestPath);
}
 
Example #3
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";
	}
	// in case the files being accessed are logs or stdout files, find appropriate paths.
	else if (routedRequest.getPath().equals("/jobmanager/log") || routedRequest.getPath().equals("/jobmanager/stdout")) {
		requestPath = "";
	} else {
		requestPath = routedRequest.getPath();
	}

	respondToRequest(channelHandlerContext, request, requestPath);
}
 
Example #4
Source File: AbstractHandlerTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testFileCleanup() throws Exception {
	final Path dir = temporaryFolder.newFolder().toPath();
	final Path file = dir.resolve("file");
	Files.createFile(file);

	RestfulGateway mockRestfulGateway = new TestingRestfulGateway.Builder()
		.build();

	final GatewayRetriever<RestfulGateway> mockGatewayRetriever = () ->
		CompletableFuture.completedFuture(mockRestfulGateway);

	CompletableFuture<Void> requestProcessingCompleteFuture = new CompletableFuture<>();
	TestHandler handler = new TestHandler(requestProcessingCompleteFuture, mockGatewayRetriever);

	RouteResult<?> routeResult = new RouteResult<>("", "", Collections.emptyMap(), Collections.emptyMap(), "");
	HttpRequest request = new DefaultFullHttpRequest(
		HttpVersion.HTTP_1_1,
		HttpMethod.GET,
		TestHandler.TestHeaders.INSTANCE.getTargetRestEndpointURL(),
		Unpooled.wrappedBuffer(new byte[0]));
	RoutedRequest<?> routerRequest = new RoutedRequest<>(routeResult, request);

	Attribute<FileUploads> attribute = new SimpleAttribute();
	attribute.set(new FileUploads(dir));
	Channel channel = mock(Channel.class);
	when(channel.attr(any(AttributeKey.class))).thenReturn(attribute);

	ChannelHandlerContext context = mock(ChannelHandlerContext.class);
	when(context.channel()).thenReturn(channel);

	handler.respondAsLeader(context, routerRequest, mockRestfulGateway);

	// the (asynchronous) request processing is not yet complete so the files should still exist
	Assert.assertTrue(Files.exists(file));
	requestProcessingCompleteFuture.complete(null);
	Assert.assertFalse(Files.exists(file));
}
 
Example #5
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 #6
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 #7
Source File: AbstractHandlerTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testFileCleanup() throws Exception {
	final Path dir = temporaryFolder.newFolder().toPath();
	final Path file = dir.resolve("file");
	Files.createFile(file);

	RestfulGateway mockRestfulGateway = TestingRestfulGateway.newBuilder()
		.build();

	final GatewayRetriever<RestfulGateway> mockGatewayRetriever = () ->
		CompletableFuture.completedFuture(mockRestfulGateway);

	CompletableFuture<Void> requestProcessingCompleteFuture = new CompletableFuture<>();
	TestHandler handler = new TestHandler(requestProcessingCompleteFuture, mockGatewayRetriever);

	RouteResult<?> routeResult = new RouteResult<>("", "", Collections.emptyMap(), Collections.emptyMap(), "");
	HttpRequest request = new DefaultFullHttpRequest(
		HttpVersion.HTTP_1_1,
		HttpMethod.GET,
		TestHandler.TestHeaders.INSTANCE.getTargetRestEndpointURL(),
		Unpooled.wrappedBuffer(new byte[0]));
	RoutedRequest<?> routerRequest = new RoutedRequest<>(routeResult, request);

	Attribute<FileUploads> attribute = new SimpleAttribute();
	attribute.set(new FileUploads(dir));
	Channel channel = mock(Channel.class);
	when(channel.attr(any(AttributeKey.class))).thenReturn(attribute);

	ChannelHandlerContext context = mock(ChannelHandlerContext.class);
	when(context.channel()).thenReturn(channel);

	handler.respondAsLeader(context, routerRequest, mockRestfulGateway);

	// the (asynchronous) request processing is not yet complete so the files should still exist
	Assert.assertTrue(Files.exists(file));
	requestProcessingCompleteFuture.complete(null);
	Assert.assertFalse(Files.exists(file));
}
 
Example #8
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 #9
Source File: ConstantTextHandler.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
protected void channelRead0(ChannelHandlerContext ctx, RoutedRequest routed) throws Exception {
	HttpResponse response = new DefaultFullHttpResponse(
		HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.wrappedBuffer(encodedText));

	response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, encodedText.length);
	response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/plain");

	KeepAliveWrite.flush(ctx, routed.getRequest(), response);
}
 
Example #10
Source File: AbstractHandlerTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test
public void testFileCleanup() throws Exception {
	final Path dir = temporaryFolder.newFolder().toPath();
	final Path file = dir.resolve("file");
	Files.createFile(file);

	RestfulGateway mockRestfulGateway = TestingRestfulGateway.newBuilder()
		.build();

	final GatewayRetriever<RestfulGateway> mockGatewayRetriever = () ->
		CompletableFuture.completedFuture(mockRestfulGateway);

	CompletableFuture<Void> requestProcessingCompleteFuture = new CompletableFuture<>();
	TestHandler handler = new TestHandler(requestProcessingCompleteFuture, mockGatewayRetriever);

	RouteResult<?> routeResult = new RouteResult<>("", "", Collections.emptyMap(), Collections.emptyMap(), "");
	HttpRequest request = new DefaultFullHttpRequest(
		HttpVersion.HTTP_1_1,
		HttpMethod.GET,
		TestHandler.TestHeaders.INSTANCE.getTargetRestEndpointURL(),
		Unpooled.wrappedBuffer(new byte[0]));
	RoutedRequest<?> routerRequest = new RoutedRequest<>(routeResult, request);

	Attribute<FileUploads> attribute = new SimpleAttribute();
	attribute.set(new FileUploads(dir));
	Channel channel = mock(Channel.class);
	when(channel.attr(any(AttributeKey.class))).thenReturn(attribute);

	ChannelHandlerContext context = mock(ChannelHandlerContext.class);
	when(context.channel()).thenReturn(channel);

	handler.respondAsLeader(context, routerRequest, mockRestfulGateway);

	// the (asynchronous) request processing is not yet complete so the files should still exist
	Assert.assertTrue(Files.exists(file));
	requestProcessingCompleteFuture.complete(null);
	Assert.assertFalse(Files.exists(file));
}
 
Example #11
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 #12
Source File: ConstantTextHandler.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
protected void channelRead0(ChannelHandlerContext ctx, RoutedRequest routed) throws Exception {
	HttpResponse response = new DefaultFullHttpResponse(
		HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.wrappedBuffer(encodedText));

	response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, encodedText.length);
	response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/plain");

	KeepAliveWrite.flush(ctx, routed.getRequest(), response);
}
 
Example #13
Source File: LeaderRetrievalHandlerTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
protected void respondAsLeader(ChannelHandlerContext channelHandlerContext, RoutedRequest routedRequest, RestfulGateway gateway) throws Exception {
	Assert.assertTrue(channelHandlerContext.channel().eventLoop().inEventLoop());
	HttpResponse response = HandlerRedirectUtils.getResponse(HttpResponseStatus.OK, RESPONSE_MESSAGE);
	KeepAliveWrite.flush(channelHandlerContext.channel(), routedRequest.getRequest(), response);
}
 
Example #14
Source File: HistoryServerStaticFileServerHandler.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public void channelRead0(ChannelHandlerContext ctx, RoutedRequest routedRequest) throws Exception {
	String requestPath = routedRequest.getPath();

	respondWithFile(ctx, routedRequest.getRequest(), requestPath);
}
 
Example #15
Source File: LeaderRetrievalHandlerTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
protected void respondAsLeader(ChannelHandlerContext channelHandlerContext, RoutedRequest routedRequest, RestfulGateway gateway) throws Exception {
	Assert.assertTrue(channelHandlerContext.channel().eventLoop().inEventLoop());
	HttpResponse response = HandlerRedirectUtils.getResponse(HttpResponseStatus.OK, RESPONSE_MESSAGE);
	KeepAliveWrite.flush(channelHandlerContext.channel(), routedRequest.getRequest(), response);
}
 
Example #16
Source File: LeaderRetrievalHandlerTest.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
@Override
protected void respondAsLeader(ChannelHandlerContext channelHandlerContext, RoutedRequest routedRequest, RestfulGateway gateway) throws Exception {
	Assert.assertTrue(channelHandlerContext.channel().eventLoop().inEventLoop());
	HttpResponse response = HandlerRedirectUtils.getResponse(HttpResponseStatus.OK, RESPONSE_MESSAGE);
	KeepAliveWrite.flush(channelHandlerContext.channel(), routedRequest.getRequest(), response);
}
 
Example #17
Source File: HistoryServerStaticFileServerHandler.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
@Override
public void channelRead0(ChannelHandlerContext ctx, RoutedRequest routedRequest) throws Exception {
	String requestPath = routedRequest.getPath();

	respondWithFile(ctx, routedRequest.getRequest(), requestPath);
}
 
Example #18
Source File: LeaderRetrievalHandler.java    From flink with Apache License 2.0 votes vote down vote up
protected abstract void respondAsLeader(ChannelHandlerContext channelHandlerContext, RoutedRequest request, T gateway) throws Exception; 
Example #19
Source File: LeaderRetrievalHandler.java    From Flink-CEPplus with Apache License 2.0 votes vote down vote up
protected abstract void respondAsLeader(ChannelHandlerContext channelHandlerContext, RoutedRequest request, T gateway) throws Exception; 
Example #20
Source File: LeaderRetrievalHandler.java    From flink with Apache License 2.0 votes vote down vote up
protected abstract void respondAsLeader(ChannelHandlerContext channelHandlerContext, RoutedRequest request, T gateway) throws Exception;