Search in sources :

Example 21 with HttpHeaders

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();
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders)

Example 22 with HttpHeaders

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);
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ClientHttpRequest(org.springframework.http.client.ClientHttpRequest) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse) ConnectException(java.net.ConnectException)

Example 23 with HttpHeaders

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();
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) WritableByteChannel(java.nio.channels.WritableByteChannel)

Example 24 with HttpHeaders

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");
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 25 with HttpHeaders

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");
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

HttpHeaders (org.springframework.http.HttpHeaders)425 Test (org.junit.Test)205 ResponseEntity (org.springframework.http.ResponseEntity)63 HttpEntity (org.springframework.http.HttpEntity)62 URI (java.net.URI)52 MediaType (org.springframework.http.MediaType)50 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)42 ByteArrayInputStream (java.io.ByteArrayInputStream)30 HttpStatus (org.springframework.http.HttpStatus)30 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)30 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)30 IOException (java.io.IOException)21 Map (java.util.Map)21 ArrayList (java.util.ArrayList)19 List (java.util.List)18 ClientHttpResponse (org.springframework.http.client.ClientHttpResponse)18 MultiValueMap (org.springframework.util.MultiValueMap)18 HttpInputMessage (org.springframework.http.HttpInputMessage)17 TestRestTemplate (org.springframework.boot.test.web.client.TestRestTemplate)15 HttpOutputMessage (org.springframework.http.HttpOutputMessage)14