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.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.awesomeagile.integrations.hackpad.RestTemplateHackpadClient.java

@Override
public void updateHackpad(PadIdentity padIdentity, String content) {
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.TEXT_HTML);
    HttpEntity<String> updateEntity = new HttpEntity<>(content, headers);
    HackpadStatus status = restTemplate.postForObject(fullUrl(UPDATE_URL), updateEntity, HackpadStatus.class,
            ImmutableMap.of("padId", padIdentity.getPadId()));
    if (!status.isSuccess()) {
        throw new RuntimeException("Failure to update a Hackpad.");
    }//from  w  ww . ja va2s  . com
}

From source file:org.trustedanalytics.h2oscoringengine.publisher.http.HttpCommunicationTest.java

private HttpEntity<String> createExpectedSimpleJsonRequest() {
    HttpHeaders headers = new HttpHeaders();
    headers.add("Accept", "application/json");
    headers.add("Content-type", "application/json");

    return new HttpEntity<>(headers);
}

From source file:com.kurento.kmf.jsonrpcconnector.client.JsonRpcClientWebSocket.java

public JsonRpcClientWebSocket(String url) {
    this(url, new HttpHeaders());
}

From source file:net.orpiske.tcs.client.services.TagCloudServiceClient.java

private HttpHeaders getHeaders() {
    String user = System.getenv("TCS_USER");
    String password = System.getenv("TCS_PASSWORD");

    if (user == null || user.isEmpty()) {
        logger.fatal("The backend system username is not provided (please set "
                + "the TCS_USER environment variable)");

        throw new InvalidCredentialsException("The backend system username is not provided");
    }/*from   ww  w .j a  v a2s  .  c  om*/

    if (password == null || password.isEmpty()) {
        logger.fatal("The backend system password is not provided (please set "
                + "the TCS_PASSWORD environment variable)");

        throw new InvalidCredentialsException("The backend system password is not provided");
    }

    String auth = user + ":" + password;

    HttpHeaders headers = new HttpHeaders();

    headers.setContentType(MediaType.APPLICATION_JSON);
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));

    byte[] encodedAuth = Base64.encodeBase64(auth.getBytes());
    headers.add("Authorization", "Basic " + new String(encodedAuth));

    return headers;
}

From source file:ch.wisv.areafiftylan.users.controller.UserRestController.java

/**
 * This method accepts POST requests on /users. It will send the input to the {@link UserService} to create a new
 * user//w w w.  j  a v a 2  s.c  o m
 *
 * @param input The user that has to be created. It consists of 3 fields. The username, the email and the plain-text
 *              password. The password is saved hashed using the BCryptPasswordEncoder
 *
 * @return The generated object, in JSON format.
 */
@RequestMapping(method = RequestMethod.POST)
public ResponseEntity<?> add(HttpServletRequest request, @Validated @RequestBody UserDTO input) {
    User save = userService.create(input, request);

    // Create headers to set the location of the created User object.
    HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.setLocation(ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}")
            .buildAndExpand(save.getId()).toUri());

    return createResponseEntity(HttpStatus.CREATED, httpHeaders,
            "User successfully created at " + httpHeaders.getLocation(), save);
}

From source file:spring.travel.site.controllers.LoginController.java

private ResponseEntity<User> success(User user) throws AuthException {
    HttpHeaders headers = new HttpHeaders();
    headers.add("Set-Cookie", cookie(user) + "; path=/");
    return new ResponseEntity<>(user, headers, HttpStatus.OK);
}

From source file:org.craftercms.commerce.client.AbstractRestCRUDService.java

public AbstractRestCRUDService(String crafterCommerceServerUrl) {
    this.crafterCommerceServerUrl = crafterCommerceServerUrl;
    restTemplate = new RestTemplate();
    httpHeaders = new HttpHeaders();
    httpHeaders.setContentType(MediaType.APPLICATION_JSON);
}

From source file:com.jee.shiro.rest.TaskRestController.java

@RequestMapping(method = RequestMethod.POST, consumes = "applaction/json")
public ResponseEntity<?> create(@RequestBody Task task, UriComponentsBuilder uriBuilder) {
    // JSR303 Bean Validator, RestExceptionHandler?.
    BeanValidators.validateWithException(validator, task);

    // ?//from w w w .  j  av a 2s  . co m
    taskService.saveTask(task);

    // Restful?url, ?id.
    Long id = task.getId();
    URI uri = uriBuilder.path("/api/v1/task/" + id).build().toUri();
    HttpHeaders headers = new HttpHeaders();
    headers.setLocation(uri);

    return new ResponseEntity(headers, HttpStatus.CREATED);
}

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

/**
 * constructor to initialize instance variables.
 *//*from  ww w  .java 2  s .c om*/
public AccountServiceQAIImpl() {
    this.restTemplate = new RestTemplate();
    this.objectMapper = new ObjectMapper();
    this.g2wUtils = new G2WUtility();

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