public Object get() { if (isSuccessful()) { return getResponse(); } else { return getErrorMessage(); } }
Cli( final Long streamedQueryRowLimit, final Long streamedQueryTimeoutMs, final KsqlRestClient restClient, final Console terminal ) { Objects.requireNonNull(restClient, "Must provide the CLI with a REST client"); Objects.requireNonNull(terminal, "Must provide the CLI with a terminal"); this.streamedQueryRowLimit = streamedQueryRowLimit; this.streamedQueryTimeoutMs = streamedQueryTimeoutMs; this.restClient = restClient; this.terminal = terminal; this.queryStreamExecutorService = Executors.newSingleThreadExecutor(); final Supplier<String> versionSuppler = () -> restClient.getServerInfo().getResponse().getVersion(); CliCommandRegisterUtil.registerDefaultCommands(this, terminal, versionSuppler, restClient); }
try (Scanner topicStreamScanner = new Scanner(topicResponse.getResponse(), UTF_8.name()); StatusClosable ignored = terminal.setStatusMessage("Press CTRL-C to interrupt") ) { topicPrintFuture.get(); } catch (final CancellationException exception) { topicResponse.getResponse().close(); terminal.writer().println("Topic printing ceased"); terminal.flush();
private void displayWelcomeMessage() { String serverVersion; try { serverVersion = restClient.getServerInfo().getResponse().getVersion(); } catch (final Exception exception) { serverVersion = "<unknown>"; } final String cliVersion = Version.getVersion(); final String helpReminderMessage = "Having trouble? " + "Type 'help' (case-insensitive) for a rundown of how things work!"; final PrintWriter writer = terminal.writer(); // Want to center the logo, but in the case of something like a fullscreen terminal, just // centering around the help message (longest single line of text in the welcome message) // should be enough; looks a little weird if you try to center the logo on a wide enough // screen and it just kind of ends up out in the middle of nowhere; hence, the call to // Math.min(terminal.getWidth(), helpReminderMessage.length()) final int consoleWidth = Math.min(terminal.getWidth(), helpReminderMessage.length()); WelcomeMsgUtils.displayWelcomeMessage(consoleWidth, writer); writer.printf( "CLI v%s, Server v%s located at %s%n", cliVersion, serverVersion, restClient.getServerAddress() ); writer.println(); writer.println(helpReminderMessage); writer.println(); terminal.flush(); }
private void handleStreamedQuery(final String query) throws IOException { final RestResponse<KsqlRestClient.QueryStream> queryResponse = restClient.makeQueryRequest(query); LOGGER.debug("Handling streamed query"); if (!queryResponse.isSuccessful()) { terminal.printErrorMessage(queryResponse.getErrorMessage()); } else { try (KsqlRestClient.QueryStream queryStream = queryResponse.getResponse(); StatusClosable ignored = terminal.setStatusMessage("Press CTRL-C to interrupt")) { streamResults(queryStream); } } }
private void printKsqlResponse(final RestResponse<KsqlEntityList> response) throws IOException { if (response.isSuccessful()) { final KsqlEntityList ksqlEntities = response.getResponse(); boolean noErrorFromServer = true; for (final KsqlEntity entity : ksqlEntities) { if (entity instanceof CommandStatusEntity && ( ((CommandStatusEntity) entity).getCommandStatus().getStatus() == CommandStatus.Status.ERROR) ) { final String fullMessage = ((CommandStatusEntity) entity).getCommandStatus().getMessage(); terminal.printError(fullMessage.split("\n")[0], fullMessage); noErrorFromServer = false; } } if (noErrorFromServer) { terminal.printKsqlEntityList(response.getResponse()); } } else { terminal.printErrorMessage(response.getErrorMessage()); } }
private ReceiverThread(final RestResponse<KsqlRestClient.QueryStream> queryResponse) { assertThat("not successful", queryResponse.isSuccessful(), is(true)); this.queryStream = queryResponse.getResponse(); this.thread = new Thread(() -> { try { while (queryStream.hasNext()) { final StreamedRow row = queryStream.next(); rows.add(row); } } catch (final Exception e) { exception.set(e); } }, "receiver-thread"); thread.setDaemon(true); thread.start(); }
@Test public void testKsqlResource() { final RestResponse<KsqlEntityList> results = ksqlRestClient.makeKsqlRequest("Test request"); assertThat(results, is(notNullValue())); assertThat(results.isSuccessful(), is(true)); final KsqlEntityList ksqlEntityList = results.getResponse(); assertThat(ksqlEntityList, hasSize(1)); assertThat(ksqlEntityList.get(0), is(instanceOf(ExecutionPlan.class))); }
@Test public void shouldReturnStatusForSpecificCommand() { // When: final RestResponse<CommandStatus> response = ksqlRestClient.makeStatusRequest("TOPIC/c1/CREATE"); // Then: assertThat(response, is(notNullValue())); assertThat(response.isSuccessful(), is(true)); assertThat(response.getResponse().getStatus(), is(CommandStatus.Status.SUCCESS)); }
@Test public void testStatus() { // When: final RestResponse<CommandStatuses> response = ksqlRestClient.makeStatusRequest(); // Then: assertThat(response, is(notNullValue())); assertThat(response.isSuccessful(), is(true)); assertThat(response.getResponse(), is(new CommandStatuses(ImmutableMap.of( new CommandId(CommandId.Type.TOPIC, "c1", CommandId.Action.CREATE), CommandStatus.Status.SUCCESS, new CommandId(CommandId.Type.TOPIC, "c2", CommandId.Action.CREATE), CommandStatus.Status.ERROR )))); }