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);
}
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);
}
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();
}
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();
}
});
}
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);
}
Aggregations