Search in sources :

Example 86 with HttpHeaders

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

the class SparklrRedirectController method photo.

@RequestMapping("/sparklr/redirect/{id}")
public ResponseEntity<BufferedImage> photo(@PathVariable String id) throws Exception {
    InputStream photo = sparklrService.loadSparklrPhoto(id);
    if (photo == null) {
        throw new UnavailableException("The requested photo does not exist");
    }
    BufferedImage body;
    MediaType contentType = MediaType.IMAGE_JPEG;
    Iterator<ImageReader> imageReaders = ImageIO.getImageReadersByMIMEType(contentType.toString());
    if (imageReaders.hasNext()) {
        ImageReader imageReader = imageReaders.next();
        ImageReadParam irp = imageReader.getDefaultReadParam();
        imageReader.setInput(new MemoryCacheImageInputStream(photo), true);
        body = imageReader.read(0, irp);
    } else {
        throw new HttpMessageNotReadableException("Could not find javax.imageio.ImageReader for Content-Type [" + contentType + "]");
    }
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.IMAGE_JPEG);
    return new ResponseEntity<BufferedImage>(body, headers, HttpStatus.OK);
}
Also used : ImageReadParam(javax.imageio.ImageReadParam) HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) HttpMessageNotReadableException(org.springframework.http.converter.HttpMessageNotReadableException) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) InputStream(java.io.InputStream) UnavailableException(javax.servlet.UnavailableException) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) MediaType(org.springframework.http.MediaType) ImageReader(javax.imageio.ImageReader) BufferedImage(java.awt.image.BufferedImage) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 87 with HttpHeaders

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

the class AuthorizationCodeGrantTests method testTokenAcquisitionWithRegisteredRedirect.

@Test
public void testTokenAcquisitionWithRegisteredRedirect() throws Exception {
    ResponseEntity<String> page = serverRunning.getForString("/tonr2/login.jsp");
    String cookie = page.getHeaders().getFirst("Set-Cookie");
    HttpHeaders headers = new HttpHeaders();
    headers.set("Cookie", cookie);
    Matcher matcher = Pattern.compile("(?s).*name=\"_csrf\".*?value=\"([^\"]+).*").matcher(page.getBody());
    MultiValueMap<String, String> form;
    form = new LinkedMultiValueMap<String, String>();
    form.add("username", "marissa");
    form.add("password", "wombat");
    if (matcher.matches()) {
        form.add("_csrf", matcher.group(1));
    }
    ResponseEntity<Void> response = serverRunning.postForStatus("/tonr2/login", headers, form);
    cookie = response.getHeaders().getFirst("Set-Cookie");
    headers = new HttpHeaders();
    headers.set("Cookie", cookie);
    // The registered redirect is /redirect, but /trigger is used as a test
    // because it is different (i.e. not the current request URI).
    String location = serverRunning.getForRedirect("/tonr2/sparklr/trigger", headers);
    location = authenticateAndApprove(location);
    assertTrue("Redirect location should be to the original photo URL: " + location, location.contains("sparklr/redirect"));
    HttpStatus status = serverRunning.getStatusCode(location, headers);
    assertEquals(HttpStatus.OK, status);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) Matcher(java.util.regex.Matcher) HttpStatus(org.springframework.http.HttpStatus) Test(org.junit.Test)

Example 88 with HttpHeaders

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

the class AuthorizationCodeGrantTests method authenticateAndApprove.

private String authenticateAndApprove(String location) {
    ResponseEntity<String> page = serverRunning.getForString("/sparklr2/login.jsp");
    String cookie = page.getHeaders().getFirst("Set-Cookie");
    Matcher matcher = Pattern.compile("(?s).*name=\"_csrf\".*?value=\"([^\"]+).*").matcher(page.getBody());
    MultiValueMap<String, String> form;
    form = new LinkedMultiValueMap<String, String>();
    form.add("username", "marissa");
    form.add("password", "koala");
    if (matcher.matches()) {
        form.add("_csrf", matcher.group(1));
    }
    HttpHeaders response = serverRunning.postForHeaders("/sparklr2/login", form);
    cookie = response.getFirst("Set-Cookie");
    HttpHeaders headers = new HttpHeaders();
    headers.set("Cookie", cookie);
    serverRunning.getForString(location, headers);
    // Should be on user approval page now
    form = new LinkedMultiValueMap<String, String>();
    form.add("user_oauth_approval", "true");
    form.add("scope.read", "true");
    response = serverRunning.postForHeaders("/sparklr2/oauth/authorize", form, headers);
    return response.getLocation().toString();
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) Matcher(java.util.regex.Matcher)

Example 89 with HttpHeaders

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

the class ServerRunning method postForHeaders.

public HttpHeaders postForHeaders(String path, MultiValueMap<String, String> formData, final HttpHeaders headers) {
    RequestCallback requestCallback = new NullRequestCallback();
    if (headers != null) {
        requestCallback = new RequestCallback() {

            public void doWithRequest(ClientHttpRequest request) throws IOException {
                request.getHeaders().putAll(headers);
            }
        };
    }
    StringBuilder builder = new StringBuilder(getUrl(path));
    if (!path.contains("?")) {
        builder.append("?");
    } else {
        builder.append("&");
    }
    for (String key : formData.keySet()) {
        for (String value : formData.get(key)) {
            builder.append(key + "=" + value);
            builder.append("&");
        }
    }
    builder.deleteCharAt(builder.length() - 1);
    return client.execute(builder.toString(), HttpMethod.POST, requestCallback, new ResponseExtractor<HttpHeaders>() {

        public HttpHeaders extractData(ClientHttpResponse response) throws IOException {
            return response.getHeaders();
        }
    });
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) RequestCallback(org.springframework.web.client.RequestCallback) IOException(java.io.IOException) ClientHttpRequest(org.springframework.http.client.ClientHttpRequest) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse)

Example 90 with HttpHeaders

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

the class ServerRunning method postForString.

public ResponseEntity<String> postForString(String path, HttpHeaders headers, MultiValueMap<String, String> formData) {
    HttpHeaders actualHeaders = new HttpHeaders();
    actualHeaders.putAll(headers);
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_FORM_URLENCODED));
    return client.exchange(getUrl(path), HttpMethod.POST, new HttpEntity<MultiValueMap<String, String>>(formData, headers), String.class);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) MultiValueMap(org.springframework.util.MultiValueMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap)

Aggregations

HttpHeaders (org.springframework.http.HttpHeaders)1676 Test (org.junit.Test)426 ResponseEntity (org.springframework.http.ResponseEntity)383 HttpEntity (org.springframework.http.HttpEntity)345 Test (org.junit.jupiter.api.Test)273 HashMap (java.util.HashMap)184 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)154 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)127 MediaType (org.springframework.http.MediaType)121 URI (java.net.URI)111 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)102 Map (java.util.Map)97 IOException (java.io.IOException)83 RestTemplate (org.springframework.web.client.RestTemplate)78 ArrayList (java.util.ArrayList)75 MessageHeaders (org.springframework.messaging.MessageHeaders)74 MultiValueMap (org.springframework.util.MultiValueMap)74 HttpStatus (org.springframework.http.HttpStatus)71 List (java.util.List)65 Timed (com.codahale.metrics.annotation.Timed)54