Example usage for org.springframework.http HttpHeaders set

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

Introduction

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

Prototype

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

Source Link

Document

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

Usage

From source file:sample.RestTests.java

@Test
public void authenticateWithXAuthTokenWorks() {
    String auth = getAuth("user", "password");
    HttpHeaders headers = getHttpHeaders();
    headers.set(AUTHORIZATION, BASIC + auth);
    ResponseEntity<User> entity = getForUser(this.baseUrl + "/", headers, User.class);

    String token = entity.getHeaders().getFirst(X_AUTH_TOKEN);

    HttpHeaders authTokenHeader = new HttpHeaders();
    authTokenHeader.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
    authTokenHeader.set(X_AUTH_TOKEN, token);
    ResponseEntity<User> authTokenResponse = getForUser(this.baseUrl + "/", authTokenHeader, User.class);
    assertThat(authTokenResponse.getStatusCode()).isEqualTo(HttpStatus.OK);
    assertThat(authTokenResponse.getBody().getUsername()).isEqualTo("user");
}

From source file:edu.wisc.cypress.dao.benstmt.RestBenefitStatementDao.java

@Cacheable(cacheName = "benefitStatement", exceptionCacheName = "cypressUnknownExceptionCache")
@Override/*  w  w  w.ja  va  2s  .  c o m*/
public BenefitStatements getBenefitStatements(String emplid) {
    final HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.set("HRID", emplid);

    final XmlBenefitStatements xmlBenefitStatements = this.restOperations.getForObject(this.statementsUrl,
            XmlBenefitStatements.class, httpHeaders, emplid);

    return this.mapBenefitStatements(xmlBenefitStatements);
}

From source file:org.cloudfoundry.identity.uaa.integration.NativeApplicationIntegrationTests.java

/**
 * tests that a client secret is required.
 *//* w  w  w . j  a v a2 s.  c  o  m*/
@Test
public void testSecretRequired() throws Exception {
    MultiValueMap<String, String> formData = new LinkedMultiValueMap<String, String>();
    formData.add("grant_type", "password");
    formData.add("username", resource.getUsername());
    formData.add("password", resource.getPassword());
    formData.add("scope", "cloud_controller.read");
    HttpHeaders headers = new HttpHeaders();
    headers.set("Authorization", "Basic " + new String(Base64.encode("no-such-client:".getBytes("UTF-8"))));
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
    ResponseEntity<String> response = serverRunning.postForString("/oauth/token", formData, headers);
    assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode());
}

From source file:com.codeabovelab.dm.cluman.cluster.registry.DockerRegistryAuthAdapter.java

private HttpHeaders createHeaders(RegistryCredentials registryCredentials) {
    HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.set(AUTHORIZATION, createBasicHeader(registryCredentials));
    return httpHeaders;
}

From source file:org.appverse.web.framework.backend.frontfacade.rest.MvcExceptionHandlerTests.java

@Test
public void testNotFound() throws Exception {
    // Login first
    TestLoginInfo loginInfo = login();//from w w  w  . jav a2 s  .  c  o  m
    HttpHeaders headers = new HttpHeaders();
    headers.set("Cookie", loginInfo.getJsessionid());
    HttpEntity<String> entity = new HttpEntity<String>(headers);

    // Calling protected resource - requires CSRF token
    UriComponentsBuilder builder = UriComponentsBuilder
            .fromHttpUrl("http://localhost:" + port + baseApiPath + "/badurl")
            .queryParam(DEFAULT_CSRF_PARAMETER_NAME, loginInfo.getXsrfToken());

    ResponseEntity<String> responseEntity = restTemplate.exchange(builder.build().encode().toUri(),
            HttpMethod.GET, entity, String.class);
    assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
}

From source file:com.art4ul.jcoon.handlers.CookieValueAnnotationBeforeHandler.java

@Override
public void doHandle(Context context, Annotation annotation, Object paramValue) {
    CookieValue cookieValueAnnotation = (CookieValue) annotation;
    if (paramValue != null && cookieValueAnnotation.value() != null) {
        HttpHeaders httpHeaders = context.getHttpHeaders();
        Object paramObj = WrapperUtil.getWreppedObject(paramValue);
        httpHeaders.set("Cookie", cookieValueAnnotation.value() + "=" + paramObj.toString());
    }/*  ww w  .j av  a2 s  .c  om*/
}

From source file:org.cloudfoundry.identity.uaa.integration.NativeApplicationIntegrationTests.java

/**
 * tests a happy-day flow of the Resource Owner Password Credentials grant type. (formerly native application
 * profile).//from www  . j a  v a2 s  .  com
 */
@Test
public void testHappyDay() throws Exception {

    MultiValueMap<String, String> formData = new LinkedMultiValueMap<String, String>();
    formData.add("grant_type", "password");
    formData.add("username", resource.getUsername());
    formData.add("password", resource.getPassword());
    formData.add("scope", "cloud_controller.read");
    HttpHeaders headers = new HttpHeaders();
    headers.set("Authorization",
            testAccounts.getAuthorizationHeader(resource.getClientId(), resource.getClientSecret()));
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
    ResponseEntity<String> response = serverRunning.postForString("/oauth/token", formData, headers);
    assertEquals(HttpStatus.OK, response.getStatusCode());
    assertEquals("no-store", response.getHeaders().getFirst("Cache-Control"));
}

From source file:org.appverse.web.framework.backend.frontfacade.rest.MvcExceptionHandlerTests.java

@Test
public void test() throws Exception {
    // Login first
    TestLoginInfo loginInfo = login();/* w w  w.ja  va 2s. com*/
    HttpHeaders headers = new HttpHeaders();
    headers.set("Cookie", loginInfo.getJsessionid());
    HttpEntity<String> entity = new HttpEntity<String>(headers);

    // Calling protected resource - requires CSRF token
    UriComponentsBuilder builder = UriComponentsBuilder
            .fromHttpUrl("http://localhost:" + port + baseApiPath + "/hello")
            .queryParam(DEFAULT_CSRF_PARAMETER_NAME, loginInfo.getXsrfToken());
    ResponseEntity<String> responseEntity = restTemplate.exchange(builder.build().encode().toUri(),
            HttpMethod.GET, entity, String.class);

    String data = responseEntity.getBody();
    assertEquals("Hello World!", data);
    assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
}

From source file:edu.wisc.cypress.dao.benstmt.RestBenefitStatementDao.java

@Override
public void getBenefitStatement(String emplid, int year, String docId, String mode,
        ProxyResponse proxyResponse) {/*from ww  w .  j  ava 2  s.c o  m*/
    final HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.set("HRID", emplid);

    final String yearStr = Integer.toString(year);
    final String yearCode;
    if (yearStr.length() > 2) {
        yearCode = yearStr.substring(2);
    } else if (yearStr.length() < 2) {
        yearCode = StringUtils.leftPad(yearStr, 2, '0');
    } else {
        yearCode = yearStr;
    }
    this.restOperations.proxyRequest(proxyResponse, this.statementUrl, HttpMethod.GET, httpHeaders, yearCode,
            docId, mode);
}

From source file:org.appverse.web.framework.backend.frontfacade.rest.MvcExceptionHandlerTests.java

@Test
public void testExceptionHandler() throws Exception {
    // Login first
    TestLoginInfo loginInfo = login();/*ww  w . j av  a 2 s .c  o m*/
    HttpHeaders headers = new HttpHeaders();
    headers.set("Cookie", loginInfo.getJsessionid());
    HttpEntity<String> entity = new HttpEntity<String>(headers);

    // Calling protected resource - requires CSRF token
    UriComponentsBuilder builder = UriComponentsBuilder
            .fromHttpUrl("http://localhost:" + port + baseApiPath + "/exc")
            .queryParam(DEFAULT_CSRF_PARAMETER_NAME, loginInfo.getXsrfToken());

    ResponseEntity<ResponseDataVO> responseEntity = restTemplate.exchange(builder.build().encode().toUri(),
            HttpMethod.GET, entity, ResponseDataVO.class);

    ResponseDataVO data = responseEntity.getBody();
    assertEquals(500, data.getErrorVO().getCode());
    assertTrue("contains message", data.getErrorVO().getMessage().contains("kk"));
    assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());

}