Search in sources :

Example 36 with HttpHeaders

use of org.springframework.http.HttpHeaders in project spring-security-oauth by spring-projects.

the class DefaultOAuth2ExceptionRenderer method handleHttpEntityResponse.

public void handleHttpEntityResponse(HttpEntity<?> responseEntity, ServletWebRequest webRequest) throws Exception {
    if (responseEntity == null) {
        return;
    }
    HttpInputMessage inputMessage = createHttpInputMessage(webRequest);
    HttpOutputMessage outputMessage = createHttpOutputMessage(webRequest);
    if (responseEntity instanceof ResponseEntity && outputMessage instanceof ServerHttpResponse) {
        ((ServerHttpResponse) outputMessage).setStatusCode(((ResponseEntity<?>) responseEntity).getStatusCode());
    }
    HttpHeaders entityHeaders = responseEntity.getHeaders();
    if (!entityHeaders.isEmpty()) {
        outputMessage.getHeaders().putAll(entityHeaders);
    }
    Object body = responseEntity.getBody();
    if (body != null) {
        writeWithMessageConverters(body, inputMessage, outputMessage);
    } else {
        // flush headers
        outputMessage.getBody();
    }
}
Also used : HttpInputMessage(org.springframework.http.HttpInputMessage) HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) HttpOutputMessage(org.springframework.http.HttpOutputMessage) ServerHttpResponse(org.springframework.http.server.ServerHttpResponse) ServletServerHttpResponse(org.springframework.http.server.ServletServerHttpResponse)

Example 37 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 38 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 39 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 40 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)484 Test (org.junit.Test)209 ResponseEntity (org.springframework.http.ResponseEntity)90 HttpEntity (org.springframework.http.HttpEntity)81 URI (java.net.URI)53 MediaType (org.springframework.http.MediaType)50 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)42 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)36 ByteArrayInputStream (java.io.ByteArrayInputStream)34 HttpStatus (org.springframework.http.HttpStatus)30 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)30 Map (java.util.Map)25 IOException (java.io.IOException)24 ArrayList (java.util.ArrayList)22 List (java.util.List)20 RestTemplate (org.springframework.web.client.RestTemplate)20 ClientHttpResponse (org.springframework.http.client.ClientHttpResponse)19 MultiValueMap (org.springframework.util.MultiValueMap)18 ExceptionHandler (org.springframework.web.bind.annotation.ExceptionHandler)18 HttpInputMessage (org.springframework.http.HttpInputMessage)17