Example usage for org.springframework.http HttpHeaders add

List of usage examples for org.springframework.http HttpHeaders add

Introduction

In this page you can find the example usage for org.springframework.http HttpHeaders add.

Prototype

@Override
public void add(String headerName, @Nullable String headerValue) 

Source Link

Document

Add the given, single header value under the given name.

Usage

From source file:com.google.cloud.servicebroker.awwvision.RedditScraper.java

@RequestMapping("/reddit")
String getRedditUrls(Model model, RestTemplate restTemplate) throws GeneralSecurityException {
    HttpHeaders headers = new HttpHeaders();
    headers.add(HttpHeaders.USER_AGENT, redditUserAgent);
    RedditResponse response = restTemplate
            .exchange(REDDIT_URL, HttpMethod.GET, new HttpEntity<String>(headers), RedditResponse.class)
            .getBody();//from w ww  .  j  a v a 2s.  c  om

    storeAndLabel(response);

    return "reddit";
}

From source file:example.users.UserControllerClientTests.java

private UserPayload issueGet(String path, MediaType mediaType) {

    HttpHeaders headers = new HttpHeaders();
    headers.add(HttpHeaders.ACCEPT, mediaType.toString());

    return template.exchange(path, HttpMethod.GET, new HttpEntity<Void>(headers), UserPayload.class).getBody();
}

From source file:jetbrains.buildServer.vsoRooms.rest.impl.VSOTeamRoomsAPIConnectionImpl.java

private HttpHeaders getRequestHeaders() {
    final HttpHeaders headers = new HttpHeaders();
    headers.add("Authorization", "Basic " + getEncodedCreds(myUser, myPassword));
    headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
    return headers;
}

From source file:org.cloudfoundry.identity.uaa.login.test.TestClient.java

private void restfulCreate(String adminAccessToken, String json, String url) {
    HttpHeaders headers = new HttpHeaders();
    headers.add("Authorization", "Bearer " + adminAccessToken);
    headers.add("Accept", "application/json");
    headers.add("Content-Type", "application/json");

    HttpEntity<String> requestEntity = new HttpEntity<String>(json, headers);
    ResponseEntity<Void> exchange = restTemplate.exchange(url, HttpMethod.POST, requestEntity, Void.class);
    Assert.assertEquals(HttpStatus.CREATED, exchange.getStatusCode());
}

From source file:com.himanshu.poc.springbootsec.SampleControllerSecurityTestIT.java

@Test
public void testSecureGetWithToken() {
    ResponseEntity<String> response = new TestRestTemplate("Himanshu", "Bhardwaj")
            .getForEntity(url.concat("/secure/generate/token/Himanshu"), String.class);
    logger.info("Response is :->" + response);
    String tokenReceived = response.getBody();
    Assert.assertThat(response.getStatusCode(), Matchers.equalTo(HttpStatus.OK));

    HttpHeaders headers = new HttpHeaders();
    headers.add("Authorization", "Basic ".concat(generateAuthorizationToken(tokenReceived)));

    HttpEntity<Object> requestEntity = new HttpEntity<Object>(headers);

    ResponseEntity<String> response2 = new TestRestTemplate().exchange(url.concat("/secure/sample/test"),
            HttpMethod.GET, requestEntity, String.class);
    logger.info("Response2 is :->" + response2);
    Assert.assertThat(response2.getStatusCode(), Matchers.equalTo(HttpStatus.OK));

    ResponseEntity<String> response3 = new TestRestTemplate()
            .exchange(url.concat("/secure/sample/test/forbidden"), HttpMethod.GET, requestEntity, String.class);
    logger.info("Response3 is :->" + response3);
    Assert.assertThat(response3.getStatusCode(), Matchers.equalTo(HttpStatus.FORBIDDEN));

}

From source file:org.apigw.authserver.web.controller.TokensController.java

@ExceptionHandler(ResourceNotFoundException.class)
public ResponseEntity<String> handleResourceNotFound(ResourceNotFoundException e) {
    log.info("ResourceNotFound: {}", e.getMessage());
    HttpHeaders headers = new HttpHeaders();
    headers.add("Content-Type", "text/plain");
    return new ResponseEntity<String>("Resource not found!", headers, HttpStatus.NOT_FOUND);
}

From source file:org.cloudfoundry.identity.uaa.login.feature.SamlLoginIT.java

@Test
public void testContentTypes() throws Exception {
    String loginUrl = baseUrl + "/login";

    HttpHeaders jsonHeaders = new HttpHeaders();
    jsonHeaders.add("Accept", "application/json");
    ResponseEntity<Map> jsonResponseEntity = restOperations.exchange(loginUrl, HttpMethod.GET,
            new HttpEntity<>(jsonHeaders), Map.class);
    assertThat(jsonResponseEntity.getHeaders().get("Content-Type").get(0),
            containsString(APPLICATION_JSON_VALUE));

    HttpHeaders htmlHeaders = new HttpHeaders();
    htmlHeaders.add("Accept", "text/html");
    ResponseEntity<Void> htmlResponseEntity = restOperations.exchange(loginUrl, HttpMethod.GET,
            new HttpEntity<>(htmlHeaders), Void.class);
    assertThat(htmlResponseEntity.getHeaders().get("Content-Type").get(0), containsString(TEXT_HTML_VALUE));

    HttpHeaders defaultHeaders = new HttpHeaders();
    defaultHeaders.add("Accept", "*/*");
    ResponseEntity<Void> defaultResponseEntity = restOperations.exchange(loginUrl, HttpMethod.GET,
            new HttpEntity<>(defaultHeaders), Void.class);
    assertThat(defaultResponseEntity.getHeaders().get("Content-Type").get(0), containsString(TEXT_HTML_VALUE));
}

From source file:org.cloudfoundry.identity.uaa.login.test.TestClient.java

public String getOAuthAccessToken(String username, String password, String grantType, String scope) {
    HttpHeaders headers = new HttpHeaders();
    headers.add("Authorization", getBasicAuthHeaderValue(username, password));

    MultiValueMap<String, String> postParameters = new LinkedMultiValueMap<String, String>();
    postParameters.add("grant_type", grantType);
    postParameters.add("client_id", username);
    postParameters.add("scope", scope);

    HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<MultiValueMap<String, String>>(
            postParameters, headers);/*from ww  w. j av  a2  s  .  c o m*/

    ResponseEntity<Map> exchange = restTemplate.exchange(baseUrl + "/oauth/token", HttpMethod.POST,
            requestEntity, Map.class);

    return exchange.getBody().get("access_token").toString();
}

From source file:ru.portal.controllers.RestController.java

@RequestMapping(value = { "/main" })
@ResponseBody//w  w  w .ja  v  a 2 s  .  com
public ResponseEntity<String> main() {
    HttpHeaders headers = new HttpHeaders();
    headers.add("Content-type", "application/json;charset=UTF-8");
    ResponseEntity responseEntity = new ResponseEntity<>("main", headers, HttpStatus.OK);

    return responseEntity;

}

From source file:ru.portal.controllers.RestController.java

@RequestMapping(method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE }, value = {
        "/grid" })
@ResponseStatus(HttpStatus.OK)/*from  w  w w  . j a  v  a2 s .c  o  m*/
@ResponseBody
public ResponseEntity<String> grid() throws JSONException {
    HttpHeaders headers = new HttpHeaders();
    headers.add("Content-type", "application/json;charset=UTF-8");

    return new ResponseEntity<>(generateGridJson(), headers, HttpStatus.OK);

}