use of org.springframework.http.HttpHeaders in project spring-data-document-examples by spring-projects.
the class ResourceHttpMessageConverter method write.
public void write(Resource resource, MediaType contentType, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
HttpHeaders headers = outputMessage.getHeaders();
if (contentType == null || contentType.isWildcardType() || contentType.isWildcardSubtype()) {
contentType = getContentType(resource);
}
if (contentType != null) {
headers.setContentType(contentType);
}
Long contentLength = getContentLength(resource, contentType);
if (contentLength != null) {
headers.setContentLength(contentLength);
}
FileCopyUtils.copy(resource.getInputStream(), outputMessage.getBody());
outputMessage.getBody().flush();
}
use of org.springframework.http.HttpHeaders in project spring-boot by spring-projects.
the class ClassPathChangeUploader method performUpload.
private void performUpload(ClassLoaderFiles classLoaderFiles, byte[] bytes) throws IOException {
try {
while (true) {
try {
ClientHttpRequest request = this.requestFactory.createRequest(this.uri, HttpMethod.POST);
HttpHeaders headers = request.getHeaders();
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
headers.setContentLength(bytes.length);
FileCopyUtils.copy(bytes, request.getBody());
ClientHttpResponse response = request.execute();
Assert.state(response.getStatusCode() == HttpStatus.OK, "Unexpected " + response.getStatusCode() + " response uploading class files");
logUpload(classLoaderFiles);
return;
} catch (ConnectException ex) {
logger.warn("Failed to connect when uploading to " + this.uri + ". Upload will be retried in 2 seconds");
Thread.sleep(2000);
}
}
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
throw new IllegalStateException(ex);
}
}
use of org.springframework.http.HttpHeaders in project spring-boot by spring-projects.
the class HttpTunnelPayload method assignTo.
/**
* Assign this payload to the given {@link HttpOutputMessage}.
* @param message the message to assign this payload to
* @throws IOException in case of I/O errors
*/
public void assignTo(HttpOutputMessage message) throws IOException {
Assert.notNull(message, "Message must not be null");
HttpHeaders headers = message.getHeaders();
headers.setContentLength(this.data.remaining());
headers.add(SEQ_HEADER, Long.toString(getSequence()));
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
WritableByteChannel body = Channels.newChannel(message.getBody());
while (this.data.hasRemaining()) {
body.write(this.data);
}
body.close();
}
use of org.springframework.http.HttpHeaders in project spring-boot by spring-projects.
the class SampleMethodSecurityApplicationTests method testHome.
@Test
public void testHome() throws Exception {
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
ResponseEntity<String> entity = this.restTemplate.exchange("/", HttpMethod.GET, new HttpEntity<Void>(headers), String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody()).contains("<title>Login");
}
use of org.springframework.http.HttpHeaders in project spring-boot by spring-projects.
the class SampleMethodSecurityApplicationTests method testDenied.
@Test
public void testDenied() throws Exception {
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
MultiValueMap<String, String> form = new LinkedMultiValueMap<>();
form.set("username", "user");
form.set("password", "user");
getCsrf(form, headers);
ResponseEntity<String> entity = this.restTemplate.exchange("/login", HttpMethod.POST, new HttpEntity<>(form, headers), String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.FOUND);
String cookie = entity.getHeaders().getFirst("Set-Cookie");
headers.set("Cookie", cookie);
ResponseEntity<String> page = this.restTemplate.exchange(entity.getHeaders().getLocation(), HttpMethod.GET, new HttpEntity<Void>(headers), String.class);
assertThat(page.getStatusCode()).isEqualTo(HttpStatus.FORBIDDEN);
assertThat(page.getBody()).contains("Access denied");
}
Aggregations