@Override public InputStream getBody() { return (this.body != null ? this.body : StreamUtils.emptyInput()); }
@Override public InputStream getBinaryStream() throws SQLException { if (this.content != null) { return new ByteArrayInputStream(this.content); } else { return (this.binaryStream != null ? this.binaryStream : StreamUtils.emptyInput()); } }
@Override public InputStream getBody() { return (content != null ? new ByteArrayInputStream(content) : StreamUtils.emptyInput()); } @Override
@Override public InputStream getBody() throws IOException { ResponseBody body = this.response.body(); return (body != null ? body.byteStream() : StreamUtils.emptyInput()); }
@Override public InputStream getBody() throws IOException { HttpEntity entity = this.httpResponse.getEntity(); return (entity != null ? entity.getContent() : StreamUtils.emptyInput()); }
@Override public InputStream getBody() throws IOException { HttpEntity entity = this.httpResponse.getEntity(); return (entity != null ? entity.getContent() : StreamUtils.emptyInput()); }
@Override public Reader getCharacterStream() throws SQLException { if (this.content != null) { return new StringReader(this.content); } else if (this.characterStream != null) { return this.characterStream; } else { return new InputStreamReader( (this.asciiStream != null ? this.asciiStream : StreamUtils.emptyInput()), StandardCharsets.US_ASCII); } }
@Override public InputStream getBody() { return (this.body != null ? this.body : StreamUtils.emptyInput()); }
@Override public InputStream getBody() throws IOException { HttpEntity entity = this.httpResponse.getEntity(); return (entity != null ? entity.getContent() : StreamUtils.emptyInput()); }
@Override public InputStream getBody() throws IOException { ResponseBody body = this.response.body(); return (body != null ? body.byteStream() : StreamUtils.emptyInput()); }
@Override public InputStream getBody() throws IOException { HttpEntity entity = this.httpResponse.getEntity(); return (entity != null ? entity.getContent() : StreamUtils.emptyInput()); }
@Override public InputStream getAsciiStream() throws SQLException { try { if (this.content != null) { return new ByteArrayInputStream(this.content.getBytes(StandardCharsets.US_ASCII)); } else if (this.characterStream != null) { String tempContent = FileCopyUtils.copyToString(this.characterStream); return new ByteArrayInputStream(tempContent.getBytes(StandardCharsets.US_ASCII)); } else { return (this.asciiStream != null ? this.asciiStream : StreamUtils.emptyInput()); } } catch (IOException ex) { throw new SQLException("Failed to read stream content: " + ex); } }
@Override public InputStream getInputStream() throws IOException { if (!isAvailable()) { throw new IllegalStateException("File has been moved - cannot be read again"); } InputStream inputStream = this.fileItem.getInputStream(); return (inputStream != null ? inputStream : StreamUtils.emptyInput()); }
@Override public InputStream getInputStream() throws IOException { if (!isAvailable()) { throw new IllegalStateException("File has been moved - cannot be read again"); } InputStream inputStream = this.fileItem.getInputStream(); return (inputStream != null ? inputStream : StreamUtils.emptyInput()); }
@Test public void isEligibleForEtag() { MockHttpServletRequest request = new MockHttpServletRequest("GET", "/hotels"); MockHttpServletResponse response = new MockHttpServletResponse(); assertTrue(filter.isEligibleForEtag(request, response, 200, StreamUtils.emptyInput())); assertFalse(filter.isEligibleForEtag(request, response, 300, StreamUtils.emptyInput())); request = new MockHttpServletRequest("HEAD", "/hotels"); assertFalse(filter.isEligibleForEtag(request, response, 200, StreamUtils.emptyInput())); request = new MockHttpServletRequest("POST", "/hotels"); assertFalse(filter.isEligibleForEtag(request, response, 200, StreamUtils.emptyInput())); request = new MockHttpServletRequest("POST", "/hotels"); request.addHeader("Cache-Control","must-revalidate, no-store"); assertFalse(filter.isEligibleForEtag(request, response, 200, StreamUtils.emptyInput())); }
@Test public void getForObjectWithCustomUriTemplateHandler() throws Exception { DefaultUriBuilderFactory uriTemplateHandler = new DefaultUriBuilderFactory(); template.setUriTemplateHandler(uriTemplateHandler); mockSentRequest(GET, "http://example.com/hotels/1/pic/pics%2Flogo.png/size/150x150"); mockResponseStatus(HttpStatus.OK); given(response.getHeaders()).willReturn(new HttpHeaders()); given(response.getBody()).willReturn(StreamUtils.emptyInput()); Map<String, String> uriVariables = new HashMap<>(2); uriVariables.put("hotel", "1"); uriVariables.put("publicpath", "pics/logo.png"); uriVariables.put("scale", "150x150"); String url = "http://example.com/hotels/{hotel}/pic/{publicpath}/size/{scale}"; template.getForObject(url, String.class, uriVariables); verify(response).close(); }
@Test public void patchForObjectNull() throws Exception { mockTextPlainHttpMessageConverter(); HttpHeaders requestHeaders = new HttpHeaders(); mockSentRequest(PATCH, "http://example.com", requestHeaders); mockResponseStatus(HttpStatus.OK); HttpHeaders responseHeaders = new HttpHeaders(); responseHeaders.setContentType(MediaType.TEXT_PLAIN); responseHeaders.setContentLength(10); given(response.getHeaders()).willReturn(responseHeaders); given(response.getBody()).willReturn(StreamUtils.emptyInput()); String result = template.patchForObject("http://example.com", null, String.class); assertNull("Invalid POST result", result); assertEquals("Invalid content length", 0, requestHeaders.getContentLength()); verify(response).close(); }
@Test public void postForObjectNull() throws Exception { mockTextPlainHttpMessageConverter(); HttpHeaders requestHeaders = new HttpHeaders(); mockSentRequest(POST, "http://example.com", requestHeaders); mockResponseStatus(HttpStatus.OK); HttpHeaders responseHeaders = new HttpHeaders(); responseHeaders.setContentType(MediaType.TEXT_PLAIN); responseHeaders.setContentLength(10); given(response.getHeaders()).willReturn(responseHeaders); given(response.getBody()).willReturn(StreamUtils.emptyInput()); given(converter.read(String.class, response)).willReturn(null); String result = template.postForObject("http://example.com", null, String.class); assertNull("Invalid POST result", result); assertEquals("Invalid content length", 0, requestHeaders.getContentLength()); verify(response).close(); }
@Test public void postForEntityNull() throws Exception { mockTextPlainHttpMessageConverter(); HttpHeaders requestHeaders = new HttpHeaders(); mockSentRequest(POST, "http://example.com", requestHeaders); mockResponseStatus(HttpStatus.OK); HttpHeaders responseHeaders = new HttpHeaders(); responseHeaders.setContentType(MediaType.TEXT_PLAIN); responseHeaders.setContentLength(10); given(response.getHeaders()).willReturn(responseHeaders); given(response.getBody()).willReturn(StreamUtils.emptyInput()); given(converter.read(String.class, response)).willReturn(null); ResponseEntity<String> result = template.postForEntity("http://example.com", null, String.class); assertFalse("Invalid POST result", result.hasBody()); assertEquals("Invalid Content-Type", MediaType.TEXT_PLAIN, result.getHeaders().getContentType()); assertEquals("Invalid content length", 0, requestHeaders.getContentLength()); assertEquals("Invalid status code", HttpStatus.OK, result.getStatusCode()); verify(response).close(); }
@Test public void lastModifiedWorksWithResourceThatDoesNotSupportFileBasedReading() throws Exception { Resource resource = mock(Resource.class); // underlying File is asked for so that the last modified time can be checked... // And then mock the file changing; i.e. the File says it has been modified given(resource.lastModified()).willReturn(100L, 100L, 200L); // does not support File-based reading; delegates to InputStream-style reading... //resource.getFile(); //mock.setThrowable(new FileNotFoundException()); given(resource.getInputStream()).willReturn(StreamUtils.emptyInput()); ResourceScriptSource scriptSource = new ResourceScriptSource(resource); assertTrue("ResourceScriptSource must start off in the 'isModified' state (it obviously isn't).", scriptSource.isModified()); scriptSource.getScriptAsString(); assertFalse("ResourceScriptSource must not report back as being modified if the underlying File resource is not reporting a changed lastModified time.", scriptSource.isModified()); // Must now report back as having been modified assertTrue("ResourceScriptSource must report back as being modified if the underlying File resource is reporting a changed lastModified time.", scriptSource.isModified()); }