Example usage for org.springframework.http HttpHeaders HttpHeaders

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

Introduction

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

Prototype

public HttpHeaders() 

Source Link

Document

Construct a new, empty instance of the HttpHeaders object.

Usage

From source file:com.biz.report.controller.ReportController.java

@RequestMapping(value = "/type-name")
public ResponseEntity<List<String>> selectTag() {
    List<String> list = reportService.getTypes();

    HttpHeaders headers = new HttpHeaders();
    headers.add("success", "Success");
    return new ResponseEntity<List<String>>(list, headers, HttpStatus.OK);
}

From source file:com.citrix.g2w.webdriver.dependencies.AuthServiceImpl.java

/**
 * constructor to initialize instance variables.
 *//*www. ja v a  2  s.  co m*/
public AuthServiceImpl() {
    this.restTemplate = new RestTemplate();
    this.objectMapper = new ObjectMapper();

    this.authSvcHeaders = new HttpHeaders();
    this.authSvcHeaders.add("Content-Type", "application/json");
    this.authSvcHeaders.add("Accept", "application/json");
}

From source file:org.cloudfoundry.identity.uaa.login.integration.AuthorizationCodeGrantIntegrationTests.java

@Test
public void testSuccessfulAuthorizationCodeFlow() throws Exception {

    HttpHeaders headers = new HttpHeaders();
    // TODO: should be able to handle just TEXT_HTML
    headers.setAccept(Arrays.asList(MediaType.TEXT_HTML, MediaType.ALL));

    AuthorizationCodeResourceDetails resource = testAccounts.getDefaultAuthorizationCodeResource();

    URI uri = serverRunning.buildUri("/oauth/authorize").queryParam("response_type", "code")
            .queryParam("state", "mystateid").queryParam("client_id", resource.getClientId())
            .queryParam("redirect_uri", resource.getPreEstablishedRedirectUri()).build();
    ResponseEntity<Void> result = serverRunning.getForResponse(uri.toString(), headers);
    assertEquals(HttpStatus.FOUND, result.getStatusCode());
    String location = result.getHeaders().getLocation().toString();

    if (result.getHeaders().containsKey("Set-Cookie")) {
        String cookie = result.getHeaders().getFirst("Set-Cookie");
        headers.set("Cookie", cookie);
    }//from  ww w  .  ja  v  a  2 s  . com

    ResponseEntity<String> response = serverRunning.getForString(location, headers);
    // should be directed to the login screen...
    String body = response.getBody();
    assertTrue(body.contains("/login.do"));
    assertTrue(body.contains("username"));
    assertTrue(body.contains("password"));

    MultiValueMap<String, String> formData = new LinkedMultiValueMap<String, String>();
    formData.add("username", testAccounts.getUserName());
    formData.add("password", testAccounts.getPassword());

    // Should be redirected to the original URL, but now authenticated
    result = serverRunning.postForResponse("/login.do", headers, formData);
    assertEquals(HttpStatus.FOUND, result.getStatusCode());

    if (result.getHeaders().containsKey("Set-Cookie")) {
        String cookie = result.getHeaders().getFirst("Set-Cookie");
        headers.set("Cookie", cookie);
    }

    response = serverRunning.getForString(result.getHeaders().getLocation().toString(), headers);
    if (response.getStatusCode() == HttpStatus.OK) {
        body = response.getBody();
        // The grant access page should be returned
        assertTrue(body.contains("Application Authorization"));
        // Forms should have the right action
        assertTrue(body.matches("(?s).*\\saction=\"\\S*oauth/authorize\".*"));

        formData.clear();
        formData.add("user_oauth_approval", "true");
        result = serverRunning.postForResponse("/oauth/authorize", headers, formData);
        assertEquals(HttpStatus.FOUND, result.getStatusCode());
        location = result.getHeaders().getLocation().toString();
    } else {
        // Token cached so no need for second approval
        assertEquals(HttpStatus.FOUND, response.getStatusCode());
        location = response.getHeaders().getLocation().toString();
    }
    assertTrue("Wrong location: " + location,
            location.matches(resource.getPreEstablishedRedirectUri() + ".*code=.+"));
    assertFalse("Location should not contain cookie: " + location,
            location.matches(resource.getPreEstablishedRedirectUri() + ".*cookie=.+"));

    formData.clear();
    formData.add("client_id", resource.getClientId());
    formData.add("redirect_uri", resource.getPreEstablishedRedirectUri());
    formData.add("grant_type", "authorization_code");
    formData.add("code", location.split("code=")[1].split("&")[0]);
    HttpHeaders tokenHeaders = new HttpHeaders();
    tokenHeaders.set("Authorization",
            testAccounts.getAuthorizationHeader(resource.getClientId(), resource.getClientSecret()));
    @SuppressWarnings("rawtypes")
    ResponseEntity<Map> tokenResponse = serverRunning.postForMap("/oauth/token", formData, tokenHeaders);
    assertEquals(HttpStatus.OK, tokenResponse.getStatusCode());
}

From source file:au.id.hazelwood.sos.web.controller.framework.GlobalExceptionHandler.java

@ExceptionHandler(value = Exception.class)
public ResponseEntity<Object> handleAllUnknown(Exception ex, WebRequest request) {
    return handleExceptionInternal(ex, null, new HttpHeaders(), HttpStatus.INTERNAL_SERVER_ERROR, request);
}

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

@Test
public void testSuccessfulAuthorizationCodeFlow() throws Exception {

    HttpHeaders headers = new HttpHeaders();
    // TODO: should be able to handle just TEXT_HTML
    headers.setAccept(Arrays.asList(MediaType.TEXT_HTML, MediaType.ALL));

    AuthorizationCodeResourceDetails resource = testAccounts.getDefaultAuthorizationCodeResource();

    URI uri = serverRunning.buildUri("/oauth/authorize").queryParam("response_type", "code")
            .queryParam("state", "mystateid").queryParam("client_id", resource.getClientId())
            .queryParam("redirect_uri", resource.getPreEstablishedRedirectUri()).build();
    ResponseEntity<Void> result = serverRunning.getForResponse(uri.toString(), headers);
    assertEquals(HttpStatus.FOUND, result.getStatusCode());
    String location = result.getHeaders().getLocation().toString();

    if (result.getHeaders().containsKey("Set-Cookie")) {
        String cookie = result.getHeaders().getFirst("Set-Cookie");
        headers.set("Cookie", cookie);
    }/*from  w  w  w . j  a  v a2  s .  com*/

    ResponseEntity<String> response = serverRunning.getForString(location, headers);
    // should be directed to the login screen...
    assertTrue(response.getBody().contains("/login.do"));
    assertTrue(response.getBody().contains("auth_key"));
    assertTrue(response.getBody().contains("password"));

    MultiValueMap<String, String> formData = new LinkedMultiValueMap<String, String>();
    formData.add("auth_key", testAccounts.getUserName());
    formData.add("password", testAccounts.getPassword());

    // Should be redirected to the original URL, but now authenticated
    result = serverRunning.postForResponse("/login.do", headers, formData);
    assertEquals(HttpStatus.FOUND, result.getStatusCode());

    if (result.getHeaders().containsKey("Set-Cookie")) {
        String cookie = result.getHeaders().getFirst("Set-Cookie");
        headers.set("Cookie", cookie);
    }

    response = serverRunning.getForString(result.getHeaders().getLocation().toString(), headers);
    if (response.getStatusCode() == HttpStatus.OK) {
        // The grant access page should be returned
        assertTrue(response.getBody().contains("Do you authorize"));

        formData.clear();
        formData.add("user_oauth_approval", "true");
        result = serverRunning.postForResponse("/oauth/authorize", headers, formData);
        assertEquals(HttpStatus.FOUND, result.getStatusCode());
        location = result.getHeaders().getLocation().toString();
    } else {
        // Token cached so no need for second approval
        assertEquals(HttpStatus.FOUND, response.getStatusCode());
        location = response.getHeaders().getLocation().toString();
    }
    assertTrue("Wrong location: " + location,
            location.matches(resource.getPreEstablishedRedirectUri() + ".*code=.+"));

    formData.clear();
    formData.add("client_id", resource.getClientId());
    formData.add("redirect_uri", resource.getPreEstablishedRedirectUri());
    formData.add("grant_type", "authorization_code");
    formData.add("code", location.split("code=")[1].split("&")[0]);
    HttpHeaders tokenHeaders = new HttpHeaders();
    tokenHeaders.set("Authorization",
            testAccounts.getAuthorizationHeader(resource.getClientId(), resource.getClientSecret()));
    @SuppressWarnings("rawtypes")
    ResponseEntity<Map> tokenResponse = serverRunning.postForMap("/oauth/token", formData, tokenHeaders);
    assertEquals(HttpStatus.OK, tokenResponse.getStatusCode());
    @SuppressWarnings("unchecked")
    Map<String, String> body = tokenResponse.getBody();
    Jwt token = JwtHelper.decode(body.get("access_token"));
    assertTrue("Wrong claims: " + token.getClaims(), token.getClaims().contains("\"aud\""));
    assertTrue("Wrong claims: " + token.getClaims(), token.getClaims().contains("\"user_id\""));
}

From source file:$.RestExceptionHandler.java

@ExceptionHandler(value = { ConstraintViolationException.class })
    public final ResponseEntity<?> handleException(ConstraintViolationException ex, WebRequest request) {
        Map<String, String> errors = BeanValidators.extractPropertyAndMessage(ex.getConstraintViolations());
        String body = jsonMapper.toJson(errors);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.parseMediaType(MediaTypes.TEXT_PLAIN_UTF_8));
        return handleExceptionInternal(ex, body, headers, HttpStatus.BAD_REQUEST, request);
    }/*  ww w.ja v  a  2  s  . c  om*/

From source file:com.appglu.impl.AbstractAppGluApiTest.java

protected HttpHeaders createDefaultHttpHeaders() {
    return new HttpHeaders();
}

From source file:comsat.sample.mustache.SampleWebMustacheApplicationTests.java

@Test
public void testMustacheErrorTemplate() throws Exception {
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
    HttpEntity<String> requestEntity = new HttpEntity<String>(headers);

    ResponseEntity<String> responseEntity = new TestRestTemplate().exchange(
            "http://localhost:" + this.port + "/does-not-exist", HttpMethod.GET, requestEntity, String.class);

    assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
    assertTrue("Wrong body:\n" + responseEntity.getBody(),
            responseEntity.getBody().contains("Something went wrong: 404 Not Found"));
}

From source file:comsat.sample.velocity.SampleWebVelocityApplicationTests.java

@Test
public void testVelocityErrorTemplate() throws Exception {
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
    HttpEntity<String> requestEntity = new HttpEntity<String>(headers);

    ResponseEntity<String> responseEntity = new TestRestTemplate().exchange(
            "http://localhost:" + this.port + "/does-not-exist", HttpMethod.GET, requestEntity, String.class);

    assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
    assertTrue("Wrong body:\n" + responseEntity.getBody(),
            responseEntity.getBody().contains("Something went wrong: 404 Not Found"));
}

From source file:net.eusashead.hateoas.conditional.interceptor.AsyncTestController.java

@RequestMapping(method = RequestMethod.PUT)
public Callable<ResponseEntity<Void>> put() {
    return new Callable<ResponseEntity<Void>>() {

        @Override/*from www.  ja  va2  s.  c o  m*/
        public ResponseEntity<Void> call() throws Exception {
            HttpHeaders headers = new HttpHeaders();
            headers.setETag("\"123456\"");
            return new ResponseEntity<Void>(headers, HttpStatus.NO_CONTENT);
        }
    };
}