Java Code Examples for org.apache.flink.runtime.rest.handler.router.Router

The following examples show how to use org.apache.flink.runtime.rest.handler.router.Router. 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-CEPplus   Source File: HistoryServer.java    License: Apache License 2.0 6 votes vote down vote up
void start() throws IOException, InterruptedException {
	synchronized (startupShutdownLock) {
		LOG.info("Starting history server.");

		Files.createDirectories(webDir.toPath());
		LOG.info("Using directory {} as local cache.", webDir);

		Router router = new Router();
		router.addGet("/:*", new HistoryServerStaticFileServerHandler(webDir));

		if (!webDir.exists() && !webDir.mkdirs()) {
			throw new IOException("Failed to create local directory " + webDir.getAbsoluteFile() + ".");
		}

		createDashboardConfigFile();

		archiveFetcher.start();

		netty = new WebFrontendBootstrap(router, LOG, webDir, serverSSLFactory, webAddress, webPort, config);
	}
}
 
Example 2
Source Project: Flink-CEPplus   Source File: RestServerEndpoint.java    License: Apache License 2.0 6 votes vote down vote up
private static void registerHandler(Router router, String handlerURL, HttpMethodWrapper httpMethod, ChannelInboundHandler handler) {
	switch (httpMethod) {
		case GET:
			router.addGet(handlerURL, handler);
			break;
		case POST:
			router.addPost(handlerURL, handler);
			break;
		case DELETE:
			router.addDelete(handlerURL, handler);
			break;
		case PATCH:
			router.addPatch(handlerURL, handler);
			break;
		default:
			throw new RuntimeException("Unsupported http method: " + httpMethod + '.');
	}
}
 
Example 3
Source Project: flink   Source File: HistoryServer.java    License: Apache License 2.0 6 votes vote down vote up
void start() throws IOException, InterruptedException {
	synchronized (startupShutdownLock) {
		LOG.info("Starting history server.");

		Files.createDirectories(webDir.toPath());
		LOG.info("Using directory {} as local cache.", webDir);

		Router router = new Router();
		router.addGet("/:*", new HistoryServerStaticFileServerHandler(webDir));

		if (!webDir.exists() && !webDir.mkdirs()) {
			throw new IOException("Failed to create local directory " + webDir.getAbsoluteFile() + ".");
		}

		createDashboardConfigFile();

		archiveFetcher.start();

		netty = new WebFrontendBootstrap(router, LOG, webDir, serverSSLFactory, webAddress, webPort, config);
	}
}
 
Example 4
Source Project: flink   Source File: RestServerEndpoint.java    License: Apache License 2.0 6 votes vote down vote up
private static void registerHandler(Router router, String handlerURL, HttpMethodWrapper httpMethod, ChannelInboundHandler handler) {
	switch (httpMethod) {
		case GET:
			router.addGet(handlerURL, handler);
			break;
		case POST:
			router.addPost(handlerURL, handler);
			break;
		case DELETE:
			router.addDelete(handlerURL, handler);
			break;
		case PATCH:
			router.addPatch(handlerURL, handler);
			break;
		default:
			throw new RuntimeException("Unsupported http method: " + httpMethod + '.');
	}
}
 
Example 5
Source Project: flink   Source File: HistoryServer.java    License: Apache License 2.0 6 votes vote down vote up
void start() throws IOException, InterruptedException {
	synchronized (startupShutdownLock) {
		LOG.info("Starting history server.");

		Files.createDirectories(webDir.toPath());
		LOG.info("Using directory {} as local cache.", webDir);

		Router router = new Router();
		router.addGet("/:*", new HistoryServerStaticFileServerHandler(webDir));

		if (!webDir.exists() && !webDir.mkdirs()) {
			throw new IOException("Failed to create local directory " + webDir.getAbsoluteFile() + ".");
		}

		createDashboardConfigFile();

		archiveFetcher.start();

		netty = new WebFrontendBootstrap(router, LOG, webDir, serverSSLFactory, webAddress, webPort, config);
	}
}
 
Example 6
Source Project: flink   Source File: RestServerEndpoint.java    License: Apache License 2.0 6 votes vote down vote up
private static void registerHandler(Router router, String handlerURL, HttpMethodWrapper httpMethod, ChannelInboundHandler handler) {
	switch (httpMethod) {
		case GET:
			router.addGet(handlerURL, handler);
			break;
		case POST:
			router.addPost(handlerURL, handler);
			break;
		case DELETE:
			router.addDelete(handlerURL, handler);
			break;
		case PATCH:
			router.addPatch(handlerURL, handler);
			break;
		default:
			throw new RuntimeException("Unsupported http method: " + httpMethod + '.');
	}
}
 
Example 7
Source Project: Flink-CEPplus   Source File: RestServerEndpoint.java    License: Apache License 2.0 5 votes vote down vote up
private static void registerHandler(Router router, Tuple2<RestHandlerSpecification, ChannelInboundHandler> specificationHandler, Logger log) {
	final String handlerURL = specificationHandler.f0.getTargetRestEndpointURL();
	// setup versioned urls
	for (final RestAPIVersion supportedVersion : specificationHandler.f0.getSupportedAPIVersions()) {
		final String versionedHandlerURL = '/' + supportedVersion.getURLVersionPrefix() + handlerURL;
		log.debug("Register handler {} under {}@{}.", specificationHandler.f1, specificationHandler.f0.getHttpMethod(), versionedHandlerURL);
		registerHandler(router, versionedHandlerURL, specificationHandler.f0.getHttpMethod(), specificationHandler.f1);
		if (supportedVersion.isDefaultVersion()) {
			// setup unversioned url for convenience and backwards compatibility
			log.debug("Register handler {} under {}@{}.", specificationHandler.f1, specificationHandler.f0.getHttpMethod(), handlerURL);
			registerHandler(router, handlerURL, specificationHandler.f0.getHttpMethod(), specificationHandler.f1);
		}
	}
}
 
Example 8
Source Project: flink   Source File: RestServerEndpoint.java    License: Apache License 2.0 5 votes vote down vote up
private static void registerHandler(Router router, Tuple2<RestHandlerSpecification, ChannelInboundHandler> specificationHandler, Logger log) {
	final String handlerURL = specificationHandler.f0.getTargetRestEndpointURL();
	// setup versioned urls
	for (final RestAPIVersion supportedVersion : specificationHandler.f0.getSupportedAPIVersions()) {
		final String versionedHandlerURL = '/' + supportedVersion.getURLVersionPrefix() + handlerURL;
		log.debug("Register handler {} under {}@{}.", specificationHandler.f1, specificationHandler.f0.getHttpMethod(), versionedHandlerURL);
		registerHandler(router, versionedHandlerURL, specificationHandler.f0.getHttpMethod(), specificationHandler.f1);
		if (supportedVersion.isDefaultVersion()) {
			// setup unversioned url for convenience and backwards compatibility
			log.debug("Register handler {} under {}@{}.", specificationHandler.f1, specificationHandler.f0.getHttpMethod(), handlerURL);
			registerHandler(router, handlerURL, specificationHandler.f0.getHttpMethod(), specificationHandler.f1);
		}
	}
}
 
Example 9
Source Project: flink   Source File: RestServerEndpoint.java    License: Apache License 2.0 5 votes vote down vote up
private static void registerHandler(Router router, Tuple2<RestHandlerSpecification, ChannelInboundHandler> specificationHandler, Logger log) {
	final String handlerURL = specificationHandler.f0.getTargetRestEndpointURL();
	// setup versioned urls
	for (final RestAPIVersion supportedVersion : specificationHandler.f0.getSupportedAPIVersions()) {
		final String versionedHandlerURL = '/' + supportedVersion.getURLVersionPrefix() + handlerURL;
		log.debug("Register handler {} under {}@{}.", specificationHandler.f1, specificationHandler.f0.getHttpMethod(), versionedHandlerURL);
		registerHandler(router, versionedHandlerURL, specificationHandler.f0.getHttpMethod(), specificationHandler.f1);
		if (supportedVersion.isDefaultVersion()) {
			// setup unversioned url for convenience and backwards compatibility
			log.debug("Register handler {} under {}@{}.", specificationHandler.f1, specificationHandler.f0.getHttpMethod(), handlerURL);
			registerHandler(router, handlerURL, specificationHandler.f0.getHttpMethod(), specificationHandler.f1);
		}
	}
}
 
Example 10
Source Project: Flink-CEPplus   Source File: LeaderRetrievalHandlerTest.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Tests the behaviour of the LeaderRetrievalHandler under the following conditions.
 *
 * <p>1. No gateway resolved --> service unavailable
 * 2. leader gateway
 * @throws Exception
 */
@Test
public void testLeaderRetrievalGateway() throws Exception {
	final String restPath = "/testing";

	final Configuration configuration = new Configuration();
	final Router router = new Router();
	final Time timeout = Time.seconds(10L);
	final CompletableFuture<RestfulGateway> gatewayFuture = new CompletableFuture<>();
	final GatewayRetriever<RestfulGateway> gatewayRetriever = () -> gatewayFuture;
	final RestfulGateway gateway = TestingRestfulGateway.newBuilder().build();

	final TestingHandler testingHandler = new TestingHandler(
		gatewayRetriever,
		timeout);

	router.addGet(restPath, testingHandler);
	WebFrontendBootstrap bootstrap = new WebFrontendBootstrap(
		router,
		log,
		null,
		null,
		"localhost",
		0,
		configuration);

	try (HttpTestClient httpClient = new HttpTestClient("localhost", bootstrap.getServerPort())) {
		// 1. no leader gateway available --> Service unavailable
		httpClient.sendGetRequest(restPath, FutureUtils.toFiniteDuration(timeout));

		HttpTestClient.SimpleHttpResponse response = httpClient.getNextResponse(FutureUtils.toFiniteDuration(timeout));

		Assert.assertEquals(HttpResponseStatus.SERVICE_UNAVAILABLE, response.getStatus());

		// 2. with leader
		gatewayFuture.complete(gateway);

		httpClient.sendGetRequest(restPath, FutureUtils.toFiniteDuration(timeout));

		response = httpClient.getNextResponse(FutureUtils.toFiniteDuration(timeout));

		Assert.assertEquals(HttpResponseStatus.OK, response.getStatus());
		Assert.assertEquals(RESPONSE_MESSAGE, response.getContent());

	} finally {
		bootstrap.shutdown();
	}
}
 
Example 11
@Test
public void testRespondWithFile() throws Exception {
	File webDir = tmp.newFolder("webDir");
	Router router = new Router()
		.addGet("/:*", new HistoryServerStaticFileServerHandler(webDir));
	WebFrontendBootstrap webUI = new WebFrontendBootstrap(
		router,
		LoggerFactory.getLogger(HistoryServerStaticFileServerHandlerTest.class),
		tmp.newFolder("uploadDir"),
		null,
		"localhost",
		0,
		new Configuration());

	int port = webUI.getServerPort();
	try {
		// verify that 404 message is returned when requesting a non-existent file
		String notFound404 = HistoryServerTest.getFromHTTP("http://localhost:" + port + "/hello");
		Assert.assertThat(notFound404, containsString("not found"));

		// verify that a) a file can be loaded using the ClassLoader and b) that the HistoryServer
		// index_hs.html is injected
		String index = HistoryServerTest.getFromHTTP("http://localhost:" + port + "/index.html");
		Assert.assertTrue(index.contains("Completed Jobs"));

		// verify that index.html is appended if the request path ends on '/'
		String index2 = HistoryServerTest.getFromHTTP("http://localhost:" + port + "/");
		Assert.assertEquals(index, index2);

		// verify that a 404 message is returned when requesting a directory
		File dir = new File(webDir, "dir.json");
		dir.mkdirs();
		String dirNotFound404 = HistoryServerTest.getFromHTTP("http://localhost:" + port + "/dir");
		Assert.assertTrue(dirNotFound404.contains("not found"));

		// verify that a 404 message is returned when requesting a file outside the webDir
		tmp.newFile("secret");
		String x = HistoryServerTest.getFromHTTP("http://localhost:" + port + "/../secret");
		Assert.assertTrue(x.contains("not found"));
	} finally {
		webUI.shutdown();
	}
}
 
Example 12
Source Project: flink   Source File: LeaderRetrievalHandlerTest.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Tests the behaviour of the LeaderRetrievalHandler under the following conditions.
 *
 * <p>1. No gateway resolved --> service unavailable
 * 2. leader gateway
 * @throws Exception
 */
@Test
public void testLeaderRetrievalGateway() throws Exception {
	final String restPath = "/testing";

	final Configuration configuration = new Configuration();
	final Router router = new Router();
	final Time timeout = Time.seconds(10L);
	final CompletableFuture<RestfulGateway> gatewayFuture = new CompletableFuture<>();
	final GatewayRetriever<RestfulGateway> gatewayRetriever = () -> gatewayFuture;
	final RestfulGateway gateway = TestingRestfulGateway.newBuilder().build();

	final TestingHandler testingHandler = new TestingHandler(
		gatewayRetriever,
		timeout);

	router.addGet(restPath, testingHandler);
	WebFrontendBootstrap bootstrap = new WebFrontendBootstrap(
		router,
		log,
		null,
		null,
		"localhost",
		0,
		configuration);

	try (HttpTestClient httpClient = new HttpTestClient("localhost", bootstrap.getServerPort())) {
		// 1. no leader gateway available --> Service unavailable
		httpClient.sendGetRequest(restPath, FutureUtils.toFiniteDuration(timeout));

		HttpTestClient.SimpleHttpResponse response = httpClient.getNextResponse(FutureUtils.toFiniteDuration(timeout));

		Assert.assertEquals(HttpResponseStatus.SERVICE_UNAVAILABLE, response.getStatus());

		// 2. with leader
		gatewayFuture.complete(gateway);

		httpClient.sendGetRequest(restPath, FutureUtils.toFiniteDuration(timeout));

		response = httpClient.getNextResponse(FutureUtils.toFiniteDuration(timeout));

		Assert.assertEquals(HttpResponseStatus.OK, response.getStatus());
		Assert.assertEquals(RESPONSE_MESSAGE, response.getContent());

	} finally {
		bootstrap.shutdown();
	}
}
 
Example 13
@Test
public void testRespondWithFile() throws Exception {
	File webDir = tmp.newFolder("webDir");
	Router router = new Router()
		.addGet("/:*", new HistoryServerStaticFileServerHandler(webDir));
	WebFrontendBootstrap webUI = new WebFrontendBootstrap(
		router,
		LoggerFactory.getLogger(HistoryServerStaticFileServerHandlerTest.class),
		tmp.newFolder("uploadDir"),
		null,
		"localhost",
		0,
		new Configuration());

	int port = webUI.getServerPort();
	try {
		// verify that 404 message is returned when requesting a non-existent file
		String notFound404 = HistoryServerTest.getFromHTTP("http://localhost:" + port + "/hello");
		Assert.assertThat(notFound404, containsString("not found"));

		// verify that a) a file can be loaded using the ClassLoader and b) that the HistoryServer
		// index_hs.html is injected
		String index = HistoryServerTest.getFromHTTP("http://localhost:" + port + "/index.html");
		Assert.assertTrue(index.contains("Apache Flink Web Dashboard"));

		// verify that index.html is appended if the request path ends on '/'
		String index2 = HistoryServerTest.getFromHTTP("http://localhost:" + port + "/");
		Assert.assertEquals(index, index2);

		// verify that a 404 message is returned when requesting a directory
		File dir = new File(webDir, "dir.json");
		dir.mkdirs();
		String dirNotFound404 = HistoryServerTest.getFromHTTP("http://localhost:" + port + "/dir");
		Assert.assertTrue(dirNotFound404.contains("not found"));

		// verify that a 404 message is returned when requesting a file outside the webDir
		tmp.newFile("secret");
		String x = HistoryServerTest.getFromHTTP("http://localhost:" + port + "/../secret");
		Assert.assertTrue(x.contains("not found"));
	} finally {
		webUI.shutdown();
	}
}
 
Example 14
Source Project: flink   Source File: LeaderRetrievalHandlerTest.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Tests the behaviour of the LeaderRetrievalHandler under the following conditions.
 *
 * <p>1. No gateway resolved --> service unavailable
 * 2. leader gateway
 * @throws Exception
 */
@Test
public void testLeaderRetrievalGateway() throws Exception {
	final String restPath = "/testing";

	final Configuration configuration = new Configuration();
	final Router router = new Router();
	final Time timeout = Time.seconds(10L);
	final CompletableFuture<RestfulGateway> gatewayFuture = new CompletableFuture<>();
	final GatewayRetriever<RestfulGateway> gatewayRetriever = () -> gatewayFuture;
	final RestfulGateway gateway = new TestingRestfulGateway.Builder().build();

	final TestingHandler testingHandler = new TestingHandler(
		gatewayRetriever,
		timeout);

	router.addGet(restPath, testingHandler);
	WebFrontendBootstrap bootstrap = new WebFrontendBootstrap(
		router,
		log,
		null,
		null,
		"localhost",
		0,
		configuration);

	try (HttpTestClient httpClient = new HttpTestClient("localhost", bootstrap.getServerPort())) {
		// 1. no leader gateway available --> Service unavailable
		httpClient.sendGetRequest(restPath, FutureUtils.toDuration(timeout));

		HttpTestClient.SimpleHttpResponse response = httpClient.getNextResponse(FutureUtils.toDuration(timeout));

		Assert.assertEquals(HttpResponseStatus.SERVICE_UNAVAILABLE, response.getStatus());

		// 2. with leader
		gatewayFuture.complete(gateway);

		httpClient.sendGetRequest(restPath, FutureUtils.toDuration(timeout));

		response = httpClient.getNextResponse(FutureUtils.toDuration(timeout));

		Assert.assertEquals(HttpResponseStatus.OK, response.getStatus());
		Assert.assertEquals(RESPONSE_MESSAGE, response.getContent());

	} finally {
		bootstrap.shutdown();
	}
}
 
Example 15
@Test
public void testRespondWithFile() throws Exception {
	File webDir = tmp.newFolder("webDir");
	Router router = new Router()
		.addGet("/:*", new HistoryServerStaticFileServerHandler(webDir));
	WebFrontendBootstrap webUI = new WebFrontendBootstrap(
		router,
		LoggerFactory.getLogger(HistoryServerStaticFileServerHandlerTest.class),
		tmp.newFolder("uploadDir"),
		null,
		"localhost",
		0,
		new Configuration());

	int port = webUI.getServerPort();
	try {
		// verify that 404 message is returned when requesting a non-existent file
		Tuple2<Integer, String> notFound404 = HistoryServerTest.getFromHTTP("http://localhost:" + port + "/hello");
		Assert.assertThat(notFound404.f0, is(404));
		Assert.assertThat(notFound404.f1, containsString("not found"));

		// verify that a) a file can be loaded using the ClassLoader and b) that the HistoryServer
		// index_hs.html is injected
		Tuple2<Integer, String> index = HistoryServerTest.getFromHTTP("http://localhost:" + port + "/index.html");
		Assert.assertThat(index.f0, is(200));
		Assert.assertThat(index.f1, containsString("Apache Flink Web Dashboard"));

		// verify that index.html is appended if the request path ends on '/'
		Tuple2<Integer, String> index2 = HistoryServerTest.getFromHTTP("http://localhost:" + port + "/");
		Assert.assertEquals(index, index2);

		// verify that a 405 message is returned when requesting a directory
		File dir = new File(webDir, "dir.json");
		dir.mkdirs();
		Tuple2<Integer, String> dirNotFound = HistoryServerTest.getFromHTTP("http://localhost:" + port + "/dir");
		Assert.assertThat(dirNotFound.f0, is(405));
		Assert.assertThat(dirNotFound.f1, containsString("not found"));

		// verify that a 403 message is returned when requesting a file outside the webDir
		tmp.newFile("secret");
		Tuple2<Integer, String> dirOutsideDirectory = HistoryServerTest.getFromHTTP("http://localhost:" + port + "/../secret");
		Assert.assertThat(dirOutsideDirectory.f0, is(403));
		Assert.assertThat(dirOutsideDirectory.f1, containsString("Forbidden"));
	} finally {
		webUI.shutdown();
	}
}