stats.responseSize(response.getResponseSize()); try { if (response.getStatusCode() == HttpStatus.OK.code() && response.hasValue()) { callback.success(response.getValue()); else if (response.getStatusCode() == HttpStatus.SERVICE_UNAVAILABLE.code()) { callback.failed(new ServiceUnavailableException(uri)); Exception cause = response.getException(); if (cause == null) { if (response.getStatusCode() == HttpStatus.OK.code()) { cause = new PrestoException(REMOTE_TASK_ERROR, format("Expected response from %s is empty", uri)); uri, HttpStatus.OK.code(), response.getStatusCode(), response.getStatusMessage(), response.getResponseBody()));
@Test public void testTransactionSupport() { Request request = preparePost() .setUri(uriFor("/v1/statement")) .setBodyGenerator(createStaticBodyGenerator("start transaction", UTF_8)) .setHeader(PRESTO_USER, "user") .setHeader(PRESTO_SOURCE, "source") .setHeader(PRESTO_TRANSACTION_ID, "none") .build(); JsonResponse<QueryResults> queryResults = client.execute(request, createFullJsonResponseHandler(QUERY_RESULTS_CODEC)); ImmutableList.Builder<List<Object>> data = ImmutableList.builder(); while (true) { if (queryResults.getValue().getData() != null) { data.addAll(queryResults.getValue().getData()); } if (queryResults.getValue().getNextUri() == null) { break; } queryResults = client.execute(prepareGet().setUri(queryResults.getValue().getNextUri()).build(), createFullJsonResponseHandler(QUERY_RESULTS_CODEC)); } assertNull(queryResults.getValue().getError()); assertNotNull(queryResults.getHeader(PRESTO_STARTED_TRANSACTION_ID)); }
if (response.getStatusCode() == HttpStatus.OK.code() && response.hasValue()) { processResponse(response); return true; if (response.getStatusCode() != HttpStatus.SERVICE_UNAVAILABLE.code()) { throw requestFailedException("fetching next", request, response);
JsonResponse<Map<String, Object>> createResponse = client.execute(request, createFullJsonResponseHandler(mapJsonCodec(String.class, Object.class))); assertEquals(createResponse.getStatusCode(), Status.CREATED.getStatusCode()); String id = createResponse.getValue().get("id").toString();
stats.responseSize(response.getResponseSize()); try { if (response.getStatusCode() == HttpStatus.OK.code() && response.hasValue()) { callback.success(response.getValue()); else if (response.getStatusCode() == HttpStatus.SERVICE_UNAVAILABLE.code()) { callback.failed(new ServiceUnavailableException(uri)); Exception cause = response.getException(); if (cause == null) { if (response.getStatusCode() == HttpStatus.OK.code()) { cause = new PrestoException(REMOTE_TASK_ERROR, format("Expected response from %s is empty", uri)); uri, HttpStatus.OK.code(), response.getStatusCode(), response.getStatusMessage(), response.getResponseBody()));
if (response.getStatusCode() == HttpStatus.OK.code() && response.hasValue()) { callback.success(response.getValue()); else if (response.getStatusCode() == HttpStatus.SERVICE_UNAVAILABLE.code()) { callback.failed(new ServiceUnavailableException(uri)); Exception cause = response.getException(); if (cause == null) { if (response.getStatusCode() == HttpStatus.OK.code()) { cause = new PrestoException(REMOTE_TASK_ERROR, format("Expected response from %s is empty", uri)); uri, HttpStatus.OK.code(), response.getStatusCode(), response.getStatusMessage(), response.getResponseBody()));
JsonResponse<Map<String, Object>> response = client.execute(request, createFullJsonResponseHandler(mapCodec)); assertEquals(response.getStatusCode(), Status.CREATED.getStatusCode()); Slot slot = agent.getAllSlots().iterator().next(); assertEquals(response.getHeader(HttpHeaders.LOCATION), uriBuilderFrom(server.getBaseUrl()).appendPath("/v1/agent/slot/").appendPath(slot.getId().toString()).toString()); assertEquals(response.getHeader(CONTENT_TYPE), MediaType.APPLICATION_JSON); .build(); assertEquals(response.getValue(), expected);
stats.responseSize(response.getResponseSize()); try { if (response.getStatusCode() == HttpStatus.OK.code() && response.hasValue()) { callback.success(response.getValue()); else if (response.getStatusCode() == HttpStatus.SERVICE_UNAVAILABLE.code()) { callback.failed(new ServiceUnavailableException(uri)); Exception cause = response.getException(); if (cause == null) { if (response.getStatusCode() == HttpStatus.OK.code()) { cause = new PrestoException(REMOTE_TASK_ERROR, format("Expected response from %s is empty", uri)); uri, HttpStatus.OK.code(), response.getStatusCode(), response.getStatusMessage(), response.getResponseBody()));
JsonResponse<Map<String, Object>> response = client.execute(request, createFullJsonResponseHandler(mapCodec)); assertEquals(response.getStatusCode(), Status.CREATED.getStatusCode()); Slot slot = agent.getAllSlots().iterator().next(); assertEquals(response.getHeader(HttpHeaders.LOCATION), uriBuilderFrom(server.getBaseUrl()).appendPath("/v1/agent/slot/").appendPath(slot.getId().toString()).toString()); assertEquals(response.getHeader(CONTENT_TYPE), MediaType.APPLICATION_JSON); .build(); assertEquals(response.getValue(), expected);
@Test public void testTransactionSupport() { Request request = preparePost() .setUri(uriFor("/v1/statement")) .setBodyGenerator(createStaticBodyGenerator("start transaction", UTF_8)) .setHeader(PRESTO_USER, "user") .setHeader(PRESTO_SOURCE, "source") .setHeader(PRESTO_TRANSACTION_ID, "none") .build(); JsonResponse<QueryResults> queryResults = client.execute(request, createFullJsonResponseHandler(QUERY_RESULTS_CODEC)); ImmutableList.Builder<List<Object>> data = ImmutableList.builder(); while (true) { if (queryResults.getValue().getData() != null) { data.addAll(queryResults.getValue().getData()); } if (queryResults.getValue().getNextUri() == null) { break; } queryResults = client.execute(prepareGet().setUri(queryResults.getValue().getNextUri()).build(), createFullJsonResponseHandler(QUERY_RESULTS_CODEC)); } assertNull(queryResults.getValue().getError()); assertNotNull(queryResults.getHeader(PRESTO_STARTED_TRANSACTION_ID)); }
private void processResponse(JsonResponse<QueryResults> response) { for (String setSession : response.getHeaders().get(PRESTO_SET_SESSION)) { List<String> keyValue = SESSION_HEADER_SPLITTER.splitToList(setSession); if (keyValue.size() != 2) { continue; } setSessionProperties.put(keyValue.get(0), keyValue.size() > 1 ? keyValue.get(1) : ""); } for (String clearSession : response.getHeaders().get(PRESTO_CLEAR_SESSION)) { resetSessionProperties.add(clearSession); } String startedTransactionId = response.getHeader(PRESTO_STARTED_TRANSACTION_ID); if (startedTransactionId != null) { this.startedtransactionId.set(startedTransactionId); } if (response.getHeader(PRESTO_CLEAR_TRANSACTION_ID) != null) { clearTransactionId.set(true); } currentResults.set(response.getValue()); }
@Test public void testTransactionSupport() { Request request = preparePost() .setUri(uriFor("/v1/statement")) .setBodyGenerator(createStaticBodyGenerator("start transaction", UTF_8)) .setHeader(PRESTO_USER, "user") .setHeader(PRESTO_SOURCE, "source") .setHeader(PRESTO_TRANSACTION_ID, "none") .build(); JsonResponse<QueryResults> queryResults = client.execute(request, createFullJsonResponseHandler(QUERY_RESULTS_CODEC)); ImmutableList.Builder<List<Object>> data = ImmutableList.builder(); while (true) { if (queryResults.getValue().getData() != null) { data.addAll(queryResults.getValue().getData()); } if (queryResults.getValue().getNextUri() == null) { break; } queryResults = client.execute(prepareGet().setUri(queryResults.getValue().getNextUri()).build(), createFullJsonResponseHandler(QUERY_RESULTS_CODEC)); } assertNull(queryResults.getValue().getError()); assertNotNull(queryResults.getHeader(PRESTO_STARTED_TRANSACTION_ID)); }
public StatementClient(HttpClient httpClient, JsonCodec<QueryResults> queryResultsCodec, ClientSession session, String query) { requireNonNull(httpClient, "httpClient is null"); requireNonNull(queryResultsCodec, "queryResultsCodec is null"); requireNonNull(session, "session is null"); requireNonNull(query, "query is null"); this.httpClient = httpClient; this.responseHandler = createFullJsonResponseHandler(queryResultsCodec); this.debug = session.isDebug(); this.timeZoneId = session.getTimeZoneId(); this.query = query; this.requestTimeoutNanos = session.getClientRequestTimeout().roundTo(NANOSECONDS); Request request = buildQueryRequest(session, query); JsonResponse<QueryResults> response = httpClient.execute(request, responseHandler); if (response.getStatusCode() != HttpStatus.OK.code() || !response.hasValue()) { throw requestFailedException("starting query", request, response); } processResponse(response); }
@Test public void testValidJson() { User user = new User("Joe", 25); String json = codec.toJson(user); JsonResponse<User> response = handler.handle(null, mockResponse(OK, JSON_UTF_8, json)); assertTrue(response.hasValue()); assertEquals(response.getJsonBytes(), json.getBytes(UTF_8)); assertEquals(response.getJson(), json); assertEquals(response.getValue().getName(), user.getName()); assertEquals(response.getValue().getAge(), user.getAge()); assertNotSame(response.getJson(), response.getJson()); assertNotSame(response.getJsonBytes(), response.getJsonBytes()); assertNotSame(response.getResponseBytes(), response.getResponseBytes()); assertNotSame(response.getResponseBody(), response.getResponseBody()); assertEquals(response.getResponseBytes(), response.getJsonBytes()); assertEquals(response.getResponseBody(), response.getJson()); }
@Test public void testValidJson() { User user = new User("Joe", 25); String json = codec.toJson(user); JsonResponse<User> response = handler.handle(null, mockResponse(OK, JSON_UTF_8, json)); assertTrue(response.hasValue()); assertEquals(response.getJsonBytes(), json.getBytes(UTF_8)); assertEquals(response.getJson(), json); assertEquals(response.getValue().getName(), user.getName()); assertEquals(response.getValue().getAge(), user.getAge()); assertNotSame(response.getJson(), response.getJson()); assertNotSame(response.getJsonBytes(), response.getJsonBytes()); assertNotSame(response.getResponseBytes(), response.getResponseBytes()); assertNotSame(response.getResponseBody(), response.getResponseBody()); assertEquals(response.getResponseBytes(), response.getJsonBytes()); assertEquals(response.getResponseBody(), response.getJson()); }
@Test public void testValidJson() { User user = new User("Joe", 25); String json = codec.toJson(user); JsonResponse<User> response = handler.handle(null, mockResponse(OK, JSON_UTF_8, json)); assertTrue(response.hasValue()); assertEquals(response.getJsonBytes(), json.getBytes(UTF_8)); assertEquals(response.getJson(), json); assertEquals(response.getValue().getName(), user.getName()); assertEquals(response.getValue().getAge(), user.getAge()); assertNotSame(response.getJson(), response.getJson()); assertNotSame(response.getJsonBytes(), response.getJsonBytes()); assertNotSame(response.getResponseBytes(), response.getResponseBytes()); assertNotSame(response.getResponseBody(), response.getResponseBody()); assertEquals(response.getResponseBytes(), response.getJsonBytes()); assertEquals(response.getResponseBody(), response.getJson()); }
@Test public void testInvalidJsonGetValue() { String json = "{\"age\": \"foo\"}"; JsonResponse<User> response = handler.handle(null, mockResponse(OK, JSON_UTF_8, json)); try { response.getValue(); fail("expected exception"); } catch (IllegalStateException e) { assertEquals(e.getMessage(), "Response does not contain a JSON value"); assertEquals(e.getCause(), response.getException()); assertEquals(response.getJsonBytes(), json.getBytes(UTF_8)); assertEquals(response.getJson(), json); assertEquals(response.getResponseBytes(), response.getJsonBytes()); assertEquals(response.getResponseBody(), response.getJson()); } }
@Test public void testInvalidJsonGetValue() { String json = "{\"age\": \"foo\"}"; JsonResponse<User> response = handler.handle(null, mockResponse(OK, JSON_UTF_8, json)); try { response.getValue(); fail("expected exception"); } catch (IllegalStateException e) { assertEquals(e.getMessage(), "Response does not contain a JSON value"); assertEquals(e.getCause(), response.getException()); assertEquals(response.getJsonBytes(), json.getBytes(UTF_8)); assertEquals(response.getJson(), json); assertEquals(response.getResponseBytes(), response.getJsonBytes()); assertEquals(response.getResponseBody(), response.getJson()); } }
@Test public void testInvalidJsonGetValue() { String json = "{\"age\": \"foo\"}"; JsonResponse<User> response = handler.handle(null, mockResponse(OK, JSON_UTF_8, json)); try { response.getValue(); fail("expected exception"); } catch (IllegalStateException e) { assertEquals(e.getMessage(), "Response does not contain a JSON value"); assertEquals(e.getCause(), response.getException()); assertEquals(response.getJsonBytes(), json.getBytes(UTF_8)); assertEquals(response.getJson(), json); assertEquals(response.getResponseBytes(), response.getJsonBytes()); assertEquals(response.getResponseBody(), response.getJson()); } }
@Test public void testInvalidJson() { String json = "{\"age\": \"foo\"}"; JsonResponse<User> response = handler.handle(null, mockResponse(OK, JSON_UTF_8, json)); assertFalse(response.hasValue()); assertEquals(response.getException().getMessage(), "Unable to create " + User.class + " from JSON response:\n" + json); assertTrue(response.getException().getCause() instanceof IllegalArgumentException); assertEquals(response.getException().getCause().getMessage(), "Invalid [simple type, class io.airlift.http.client.TestFullJsonResponseHandler$User] json bytes"); assertEquals(response.getJsonBytes(), json.getBytes(UTF_8)); assertEquals(response.getJson(), json); assertEquals(response.getResponseBytes(), response.getJsonBytes()); assertEquals(response.getResponseBody(), response.getJson()); }