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.billkoch.examples.people.controllers.PeopleController.java

@RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Person> create(@RequestBody Person person) {
    Person persistedPerson = peopleRepository.save(person);

    HttpHeaders headers = new HttpHeaders();
    headers.setLocation(ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}")
            .buildAndExpand(person.getId()).toUri());

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

From source file:org.openlmis.fulfillment.service.request.RequestHeaders.java

/**
 * Converts this instance to {@link HttpHeaders}.
 */// www .  ja va 2 s  .c  om
public HttpHeaders toHeaders() {
    HttpHeaders httpHeaders = new HttpHeaders();
    forEach((entry -> httpHeaders.set(entry.getKey(), entry.getValue())));
    return httpHeaders;
}

From source file:example.helloworld.CubbyholeAuthenticationTests.java

/**
 * Write some data to Vault before Vault can be used as {@link VaultPropertySource}.
 *//*www.j  a  v  a  2s .c o  m*/
@BeforeClass
public static void beforeClass() {

    VaultOperations vaultOperations = new VaultTestConfiguration().vaultTemplate();
    vaultOperations.write("secret/myapp/configuration", Collections.singletonMap("configuration.key", "value"));

    VaultResponse response = vaultOperations.doWithSession(new RestOperationsCallback<VaultResponse>() {

        @Override
        public VaultResponse doWithRestOperations(RestOperations restOperations) {

            HttpHeaders headers = new HttpHeaders();
            headers.add("X-Vault-Wrap-TTL", "10m");

            return restOperations.postForObject("auth/token/create", new HttpEntity<Object>(headers),
                    VaultResponse.class);
        }
    });

    // Response Wrapping requires Vault 0.6.0+
    Map<String, String> wrapInfo = response.getWrapInfo();
    initialToken = VaultToken.of(wrapInfo.get("token"));
}

From source file:cz.muni.fi.mushroomhunter.restclient.MushroomCreateSwingWorker.java

@Override
protected Void doInBackground() throws Exception {
    MushroomDto mushroomDto = new MushroomDto();
    mushroomDto.setName(restClient.getTfMushroomName().getText());

    mushroomDto.setType(cz.fi.muni.pa165.mushroomhunter.api.Type
            .valueOf(restClient.getComboBoxMushroomType().getSelectedItem().toString()));

    //to create date object only month is used, day and year are fixed values
    String dateInString = restClient.getComboBoxMushroomStartOfOccurence().getSelectedItem().toString()
            + " 1, 2000";

    SimpleDateFormat formatter = new SimpleDateFormat("MMMM d, yyyy", new Locale("en_US"));

    mushroomDto.setStartOfOccurence(formatter.parse(dateInString));

    //to create date object only month is used, day and year are fixed values
    dateInString = restClient.getComboBoxMushroomEndOfOccurence().getSelectedItem().toString() + " 1, 2000";
    mushroomDto.setEndOfOccurence(formatter.parse(dateInString));

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    List<MediaType> mediaTypeList = new ArrayList<>();
    mediaTypeList.add(MediaType.ALL);/*from  w ww  .j  a  v a2 s .  c om*/
    headers.setAccept(mediaTypeList);

    ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
    String json = ow.writeValueAsString(mushroomDto);

    String plainCreds = RestClient.USER_NAME + ":" + RestClient.PASSWORD;
    byte[] plainCredsBytes = plainCreds.getBytes();
    byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes);
    String base64Creds = new String(base64CredsBytes);

    headers.add("Authorization", "Basic " + base64Creds);

    HttpEntity request = new HttpEntity(json, headers);

    RestTemplate restTemplate = new RestTemplate();
    Long[] result = restTemplate.postForObject(RestClient.SERVER_URL + "pa165/rest/mushroom", request,
            Long[].class);

    System.out.println("Id of the created mushroom: " + result[0]);
    RestClient.getMushroomIDs().add(result[0]);
    return null;
}

From source file:net.acesinc.convergentui.content.ContentFetchCommand.java

@Override
protected ContentResponse run() throws Exception {
    log.debug("Getting live content from [ " + location + " ]");
    try {/*from www . ja  v  a 2 s  . c om*/
        HttpServletRequest request = requestContext.getRequest();
        MultiValueMap<String, String> headers = this.helper.buildZuulRequestHeaders(request);

        if (request.getQueryString() != null && !request.getQueryString().isEmpty()) {
            MultiValueMap<String, String> params = this.helper.buildZuulRequestQueryParams(request);
        }

        HttpHeaders requestHeaders = new HttpHeaders();
        for (String key : headers.keySet()) {
            for (String s : headers.get(key)) {
                requestHeaders.add(key, s);
            }
        }
        HttpEntity requestEntity = new HttpEntity(null, requestHeaders);

        ResponseEntity<Object> exchange = this.restTemplate.exchange(location, HttpMethod.GET, requestEntity,
                Object.class);

        ContentResponse response = new ContentResponse();
        response.setContent(exchange.getBody());
        response.setContentType(exchange.getHeaders().getContentType());
        response.setError(false);

        return response;
    } catch (Exception e) {
        log.debug("Error fetching live content from [ " + location + " ]", e);
        throw e;
    }
}

From source file:be.solidx.hot.rest.RestController.java

protected ResponseEntity<byte[]> buildJSONResponse(Object response, HttpStatus httpStatus) {
    return buildJSONResponse(response, new HttpHeaders(), httpStatus);
}

From source file:com.orange.cepheus.broker.ConfigurationTest.java

@Test
public void getHeadersForBroker() {
    configuration.setRemoteServiceName("SERVICE");
    configuration.setRemoteServicePath("PATH");
    configuration.setRemoteAuthToken("TOKEN");
    configuration.setRemoteForwardUpdateContext(false);

    HttpHeaders httpHeaders = new HttpHeaders();
    configuration.addRemoteHeaders(httpHeaders);

    assertEquals("SERVICE", httpHeaders.getFirst("Fiware-Service"));
    assertEquals("PATH", httpHeaders.getFirst("Fiware-ServicePath"));
    assertEquals("TOKEN", httpHeaders.getFirst("X-Auth-Token"));
}

From source file:fi.hsl.parkandride.front.HubController.java

@RequestMapping(method = POST, value = HUBS, produces = APPLICATION_JSON_VALUE)
public ResponseEntity<Hub> createHub(@RequestBody Hub hub, User currentUser, UriComponentsBuilder builder) {
    log.info("createHub");
    Hub newHub = hubService.createHub(hub, currentUser);
    log.info("createHub({})", newHub.id);

    HttpHeaders headers = new HttpHeaders();
    headers.setLocation(builder.path(HUB).buildAndExpand(newHub.id).toUri());
    return new ResponseEntity<>(newHub, headers, CREATED);
}

From source file:org.openbaton.autoscaling.api.exceptions.GlobalExceptionHandler.java

@ExceptionHandler({ NotFoundException.class, NoResultException.class })
@ResponseStatus(value = HttpStatus.NOT_FOUND)
protected ResponseEntity<Object> handleNotFoundException(Exception e, WebRequest request) {
    log.error("Exception with message " + e.getMessage() + " was thrown");
    ExceptionResource exc = new ExceptionResource("Not Found", e.getMessage());
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);

    return handleExceptionInternal(e, exc, headers, HttpStatus.UNPROCESSABLE_ENTITY, request);
}

From source file:com.nebhale.devoxx2013.web.GameController.java

@RequestMapping(method = RequestMethod.POST, value = "")
@Transactional//from  w w w.j ava2  s . c  o  m
ResponseEntity<Void> create() {
    Game game = this.gameService.create();

    HttpHeaders headers = new HttpHeaders();
    headers.setLocation(linkTo(GameController.class).slash(game).toUri());

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