private <T> RestResponse<T> postRequest( final String path, final Object jsonEntity, final Optional<Integer> readTimeoutMs, final boolean closeResponse, final Function<Response, T> mapper) { Response response = null; try { final WebTarget target = client.target(serverAddress) .path(path); readTimeoutMs.ifPresent(timeout -> target.property(ClientProperties.READ_TIMEOUT, timeout)); response = target .request(MediaType.APPLICATION_JSON_TYPE) .post(Entity.json(jsonEntity)); return response.getStatus() == Response.Status.OK.getStatusCode() ? RestResponse.successful(mapper.apply(response)) : createErrorResponse(path, response); } catch (final ProcessingException e) { if (shouldRetry(readTimeoutMs, e)) { return postRequest(path, jsonEntity, calcReadTimeout(readTimeoutMs), closeResponse, mapper); } throw new KsqlRestClientException("Error issuing POST to KSQL server. path:" + path, e); } catch (final Exception e) { throw new KsqlRestClientException("Error issuing POST to KSQL server. path:" + path, e); } finally { if (response != null && closeResponse) { response.close(); } } }