public byte[] send(byte[] request) { // TODO back-off policy? while (true) { try { final HttpURLConnection connection = openConnection(); connection.setRequestMethod("POST"); connection.setDoInput(true); connection.setDoOutput(true); try (DataOutputStream wr = new DataOutputStream(connection.getOutputStream())) { wr.write(request); wr.flush(); wr.close(); } final int responseCode = connection.getResponseCode(); final InputStream inputStream; if (responseCode == HttpURLConnection.HTTP_UNAVAILABLE) { // Could be sitting behind a load-balancer, try again. continue; } else if (responseCode != HttpURLConnection.HTTP_OK) { inputStream = connection.getErrorStream(); } else { inputStream = connection.getInputStream(); } return AvaticaUtils.readFullyToBytes(inputStream); } catch (IOException e) { throw new RuntimeException(e); } } }
/** * Creates the HTTP client that communicates with the Avatica server. * * @param connection The {@link AvaticaConnection}. * @param config The configuration. * @return An {@link AvaticaHttpClient} implementation. */ AvaticaHttpClient getHttpClient(AvaticaConnection connection, ConnectionConfig config) { URL url; try { url = new URL(config.url()); } catch (MalformedURLException e) { throw new RuntimeException(e); } return new AvaticaHttpClientImpl(url); }
@Test(expected = RuntimeException.class) public void testServerError() throws Exception { // HTTP 500 should error out URL url = new URL("http://127.0.0.1:8765"); final HttpURLConnection cnxn = Mockito.mock(HttpURLConnection.class); ByteArrayOutputStream baos = new ByteArrayOutputStream(); // Create the HTTP client AvaticaHttpClientImpl client = new AvaticaHttpClientImpl(url) { @Override HttpURLConnection openConnection() throws IOException { return cnxn; } }; // HTTP 500 Mockito.when(cnxn.getResponseCode()).thenReturn(HttpURLConnection.HTTP_INTERNAL_ERROR); Mockito.when(cnxn.getOutputStream()).thenReturn(baos); // Should throw an RTE client.send(REQUEST.getBytes(StandardCharsets.UTF_8)); }
@Test(expected = RuntimeException.class) public void testServerError() throws Exception { // HTTP 500 should error out URL url = new URL("http://127.0.0.1:8765"); final HttpURLConnection cnxn = Mockito.mock(HttpURLConnection.class); ByteArrayOutputStream baos = new ByteArrayOutputStream(); // Create the HTTP client AvaticaHttpClientImpl client = new AvaticaHttpClientImpl(url) { @Override HttpURLConnection openConnection() throws IOException { return cnxn; } }; // HTTP 500 Mockito.when(cnxn.getResponseCode()).thenReturn(HttpURLConnection.HTTP_INTERNAL_ERROR); Mockito.when(cnxn.getOutputStream()).thenReturn(baos); // Should throw an RTE client.send(REQUEST.getBytes(StandardCharsets.UTF_8)); }
@Test(expected = RuntimeException.class) public void testServerError() throws Exception { // HTTP 500 should error out URL url = new URL("http://127.0.0.1:8765"); final HttpURLConnection cnxn = Mockito.mock(HttpURLConnection.class); ByteArrayOutputStream baos = new ByteArrayOutputStream(); // Create the HTTP client AvaticaHttpClientImpl client = new AvaticaHttpClientImpl(url) { @Override HttpURLConnection openConnection() throws IOException { return cnxn; } }; // HTTP 500 Mockito.when(cnxn.getResponseCode()).thenReturn(HttpURLConnection.HTTP_INTERNAL_ERROR); Mockito.when(cnxn.getOutputStream()).thenReturn(baos); // Should throw an RTE client.send(REQUEST.getBytes(StandardCharsets.UTF_8)); }
final HttpURLConnection connection = openConnection(); connection.setRequestMethod("POST"); connection.setDoInput(true);
@Test public void testRetryOnUnavailable() throws Exception { // HTTP-503, try again URL url = new URL("http://127.0.0.1:8765"); final HttpURLConnection cnxn = Mockito.mock(HttpURLConnection.class); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayInputStream bais = new ByteArrayInputStream(RESPONSE.getBytes(StandardCharsets.UTF_8)); // Create the HTTP client AvaticaHttpClientImpl client = new AvaticaHttpClientImpl(url) { @Override HttpURLConnection openConnection() throws IOException { return cnxn; } }; // HTTP 503 then 200 Mockito.when(cnxn.getResponseCode()).thenReturn(HttpURLConnection.HTTP_UNAVAILABLE, HttpURLConnection.HTTP_OK); Mockito.when(cnxn.getOutputStream()).thenReturn(baos); Mockito.when(cnxn.getInputStream()).thenReturn(bais); byte[] response = client.send(REQUEST.getBytes(StandardCharsets.UTF_8)); assertArrayEquals(RESPONSE.getBytes(StandardCharsets.UTF_8), response); }
final HttpURLConnection connection = openConnection(); connection.setRequestMethod("POST"); connection.setDoInput(true);
@Test public void testRetryOnUnavailable() throws Exception { // HTTP-503, try again URL url = new URL("http://127.0.0.1:8765"); final HttpURLConnection cnxn = Mockito.mock(HttpURLConnection.class); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayInputStream bais = new ByteArrayInputStream(RESPONSE.getBytes(StandardCharsets.UTF_8)); // Create the HTTP client AvaticaHttpClientImpl client = new AvaticaHttpClientImpl(url) { @Override HttpURLConnection openConnection() throws IOException { return cnxn; } }; // HTTP 503 then 200 Mockito.when(cnxn.getResponseCode()).thenReturn(HttpURLConnection.HTTP_UNAVAILABLE, HttpURLConnection.HTTP_OK); Mockito.when(cnxn.getOutputStream()).thenReturn(baos); Mockito.when(cnxn.getInputStream()).thenReturn(bais); byte[] response = client.send(REQUEST.getBytes(StandardCharsets.UTF_8)); assertArrayEquals(RESPONSE.getBytes(StandardCharsets.UTF_8), response); }
@Test public void testRetryOnUnavailable() throws Exception { // HTTP-503, try again URL url = new URL("http://127.0.0.1:8765"); final HttpURLConnection cnxn = Mockito.mock(HttpURLConnection.class); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayInputStream bais = new ByteArrayInputStream(RESPONSE.getBytes(StandardCharsets.UTF_8)); // Create the HTTP client AvaticaHttpClientImpl client = new AvaticaHttpClientImpl(url) { @Override HttpURLConnection openConnection() throws IOException { return cnxn; } }; // HTTP 503 then 200 Mockito.when(cnxn.getResponseCode()).thenReturn(HttpURLConnection.HTTP_UNAVAILABLE, HttpURLConnection.HTTP_OK); Mockito.when(cnxn.getOutputStream()).thenReturn(baos); Mockito.when(cnxn.getInputStream()).thenReturn(bais); byte[] response = client.send(REQUEST.getBytes(StandardCharsets.UTF_8)); assertArrayEquals(RESPONSE.getBytes(StandardCharsets.UTF_8), response); }