@Override protected ClientHttpResponse executeInternal(HttpHeaders headers) throws IOException { try { if (this.body != null) { this.body.close(); } else { SimpleBufferingClientHttpRequest.addHeaders(this.connection, headers); this.connection.connect(); // Immediately trigger the request in a no-output scenario as well this.connection.getResponseCode(); } } catch (IOException ex) { // ignore } return new SimpleClientHttpResponse(this.connection); }
@Test // SPR-14040 public void shouldDrainErrorStreamWhenResponseClosed() throws Exception { byte[] buf = new byte[6]; TestByteArrayInputStream is = new TestByteArrayInputStream("SpringSpring".getBytes(StandardCharsets.UTF_8)); given(this.connection.getErrorStream()).willReturn(is); InputStream responseStream = this.response.getBody(); responseStream.read(buf); assertThat(new String(buf, StandardCharsets.UTF_8), is("Spring")); assertThat(is.available(), is(6)); this.response.close(); assertThat(is.available(), is(0)); assertTrue(is.isClosed()); verify(this.connection, never()).disconnect(); }
@Override public void close() { try { if (this.responseStream == null) { getBody(); } StreamUtils.drain(this.responseStream); this.responseStream.close(); } catch (Exception ex) { // ignore } }
public int getRawStatusCode() throws IOException { try { return this.connection.getResponseCode(); } catch (IOException ex) { return handleIOException(ex); } }
@Test // SPR-17181 public void shouldDrainResponseEvenIfResponseNotRead() throws Exception { TestByteArrayInputStream is = new TestByteArrayInputStream("SpringSpring".getBytes(StandardCharsets.UTF_8)); given(this.connection.getErrorStream()).willReturn(null); given(this.connection.getInputStream()).willReturn(is); this.response.close(); assertThat(is.available(), is(0)); assertTrue(is.isClosed()); verify(this.connection, never()).disconnect(); }
@Override public HttpStatus getStatusCode() throws IOException { return HttpStatus.valueOf(getRawStatusCode()); }
@Override public void close() { try { if (this.responseStream == null) { getBody(); } StreamUtils.drain(this.responseStream); this.responseStream.close(); } catch (Exception ex) { // ignore } }
public String getStatusText() throws IOException { try { return this.connection.getResponseMessage(); } catch (IOException ex) { return HttpStatus.valueOf(handleIOException(ex)).getReasonPhrase(); } }
@Override public ClientHttpResponse call() throws Exception { try { if (body != null) { body.close(); } else { SimpleBufferingClientHttpRequest.addHeaders(connection, headers); connection.connect(); // Immediately trigger the request in a no-output scenario as well connection.getResponseCode(); } } catch (IOException ex) { // ignore } return new SimpleClientHttpResponse(connection); } });
@Test // SPR-16773 public void shouldNotDrainWhenErrorStreamClosed() throws Exception { InputStream is = mock(InputStream.class); given(this.connection.getErrorStream()).willReturn(is); doNothing().when(is).close(); given(is.read(any())).willThrow(new NullPointerException("from HttpURLConnection#ErrorStream")); InputStream responseStream = this.response.getBody(); responseStream.close(); this.response.close(); verify(is).close(); }
@Override public void close() { try { if (this.responseStream == null) { getBody(); } StreamUtils.drain(this.responseStream); this.responseStream.close(); } catch (Exception ex) { // ignore } }
@Override protected ClientHttpResponse executeInternal(HttpHeaders headers) throws IOException { try { if (this.body != null) { this.body.close(); } else { SimpleBufferingClientHttpRequest.addHeaders(this.connection, headers); this.connection.connect(); // Immediately trigger the request in a no-output scenario as well this.connection.getResponseCode(); } } catch (IOException ex) { // ignore } return new SimpleClientHttpResponse(this.connection); }
@Test // SPR-14040 public void shouldDrainStreamWhenResponseClosed() throws Exception { byte[] buf = new byte[6]; TestByteArrayInputStream is = new TestByteArrayInputStream("SpringSpring".getBytes(StandardCharsets.UTF_8)); given(this.connection.getErrorStream()).willReturn(null); given(this.connection.getInputStream()).willReturn(is); InputStream responseStream = this.response.getBody(); responseStream.read(buf); assertThat(new String(buf, StandardCharsets.UTF_8), is("Spring")); assertThat(is.available(), is(6)); this.response.close(); assertThat(is.available(), is(0)); assertTrue(is.isClosed()); verify(this.connection, never()).disconnect(); }
@Override public ClientHttpResponse call() throws Exception { try { if (body != null) { body.close(); } else { SimpleBufferingClientHttpRequest.addHeaders(connection, headers); connection.connect(); // Immediately trigger the request in a no-output scenario as well connection.getResponseCode(); } } catch (IOException ex) { // ignore } return new SimpleClientHttpResponse(connection); } });
@Test // SPR-14040 public void shouldNotCloseConnectionWhenResponseClosed() throws Exception { TestByteArrayInputStream is = new TestByteArrayInputStream("Spring".getBytes(StandardCharsets.UTF_8)); given(this.connection.getErrorStream()).willReturn(null); given(this.connection.getInputStream()).willReturn(is); InputStream responseStream = this.response.getBody(); assertThat(StreamUtils.copyToString(responseStream, StandardCharsets.UTF_8), is("Spring")); this.response.close(); assertTrue(is.isClosed()); verify(this.connection, never()).disconnect(); }
@Override protected ClientHttpResponse executeInternal(HttpHeaders headers, byte[] bufferedOutput) throws IOException { addHeaders(this.connection, headers); // JDK <1.8 doesn't support getOutputStream with HTTP DELETE if (getMethod() == HttpMethod.DELETE && bufferedOutput.length == 0) { this.connection.setDoOutput(false); } if (this.connection.getDoOutput() && this.outputStreaming) { this.connection.setFixedLengthStreamingMode(bufferedOutput.length); } this.connection.connect(); if (this.connection.getDoOutput()) { FileCopyUtils.copy(bufferedOutput, this.connection.getOutputStream()); } else { // Immediately trigger the request in a no-output scenario as well this.connection.getResponseCode(); } return new SimpleClientHttpResponse(this.connection); }
@Override public ClientHttpResponse call() throws Exception { SimpleBufferingClientHttpRequest.addHeaders(connection, headers); // JDK <1.8 doesn't support getOutputStream with HTTP DELETE if (getMethod() == HttpMethod.DELETE && bufferedOutput.length == 0) { connection.setDoOutput(false); } if (connection.getDoOutput() && outputStreaming) { connection.setFixedLengthStreamingMode(bufferedOutput.length); } connection.connect(); if (connection.getDoOutput()) { FileCopyUtils.copy(bufferedOutput, connection.getOutputStream()); } else { // Immediately trigger the request in a no-output scenario as well connection.getResponseCode(); } return new SimpleClientHttpResponse(connection); } });
@Override public ClientHttpResponse call() throws Exception { SimpleBufferingClientHttpRequest.addHeaders(connection, headers); // JDK <1.8 doesn't support getOutputStream with HTTP DELETE if (getMethod() == HttpMethod.DELETE && bufferedOutput.length == 0) { connection.setDoOutput(false); } if (connection.getDoOutput() && outputStreaming) { connection.setFixedLengthStreamingMode(bufferedOutput.length); } connection.connect(); if (connection.getDoOutput()) { FileCopyUtils.copy(bufferedOutput, connection.getOutputStream()); } else { // Immediately trigger the request in a no-output scenario as well connection.getResponseCode(); } return new SimpleClientHttpResponse(connection); } });
@Override protected ClientHttpResponse executeInternal(HttpHeaders headers, byte[] bufferedOutput) throws IOException { addHeaders(this.connection, headers); // JDK <1.8 doesn't support getOutputStream with HTTP DELETE if (getMethod() == HttpMethod.DELETE && bufferedOutput.length == 0) { this.connection.setDoOutput(false); } if (this.connection.getDoOutput() && this.outputStreaming) { this.connection.setFixedLengthStreamingMode(bufferedOutput.length); } this.connection.connect(); if (this.connection.getDoOutput()) { FileCopyUtils.copy(bufferedOutput, this.connection.getOutputStream()); } else { // Immediately trigger the request in a no-output scenario as well this.connection.getResponseCode(); } return new SimpleClientHttpResponse(this.connection); }
@Override protected ClientHttpResponse executeInternal(HttpHeaders headers) throws IOException { try { if (this.body != null) { this.body.close(); } else { writeHeaders(headers); this.connection.connect(); } } catch (IOException ex) { // ignore } return new SimpleClientHttpResponse(this.connection); }