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:de.wirthedv.appname.SpringBootFacesApplicationTests.java

@Test
public void testJsfWelcomePageAccessibleByAdmin() throws Exception {
    HttpHeaders headers = new HttpHeaders();
    headers.add(WebSecurityConfiguration.PREAUTH_USER_HEADER, "admin");
    ResponseEntity<String> entity = new TestRestTemplate().exchange("http://localhost:" + this.port,
            HttpMethod.GET, new HttpEntity<Void>(headers), String.class);

    assertEquals(HttpStatus.OK, entity.getStatusCode());
    assertTrue("Wrong body:\n" + entity.getBody(), entity.getBody().contains("<h1>Home page</h1>"));
}

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

@ResponseBody
@RequestMapping(value = "items/{year}/read", method = RequestMethod.POST, headers = {
        "Content-type=application/json" })
public ResponseEntity<List<ItemDTO>> readItemByType(@RequestBody Map map, @PathVariable("year") String year) {
    Assert.notNull(year, "Year is null.");
    Assert.notNull(map, "Type is null.");
    String type = map.get("type").toString();
    String month = map.get("month") != null ? map.get("month").toString() : null;
    HttpHeaders headers = new HttpHeaders();
    headers.add("success", "Success");
    return new ResponseEntity<List<ItemDTO>>(reportService.readItemByType(type, year, month), headers,
            HttpStatus.OK);/*w w  w  .j  a v  a 2s.com*/
}

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

@Override
protected List<MushroomDto> doInBackground() throws Exception {
    String plainCreds = RestClient.USER_NAME + ":" + RestClient.PASSWORD;
    byte[] plainCredsBytes = plainCreds.getBytes();
    byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes);
    String base64Creds = new String(base64CredsBytes);

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

    HttpEntity<String> request = new HttpEntity<>(headers);
    RestTemplate restTemplate = new RestTemplate();
    ResponseEntity<MushroomDto[]> responseEntity = restTemplate.exchange(
            RestClient.SERVER_URL + "pa165/rest/mushroom/", HttpMethod.GET, request, MushroomDto[].class);
    MushroomDto[] mushroomDtoArray = responseEntity.getBody();
    List<MushroomDto> mushroomDtoList = new ArrayList<>();
    mushroomDtoList.addAll(Arrays.asList(mushroomDtoArray));
    return mushroomDtoList;
}

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

@ResponseBody
@RequestMapping(value = "report/{year}/get", method = RequestMethod.POST, headers = {
        "Content-type=application/json" })
public ResponseEntity<ReportDataSet> readFTypes(@PathVariable("year") String year, @RequestBody Map data) {
    logger.info(year);//  ww  w. j a  v a 2s  . co m
    logger.info(data);
    Assert.notNull(data, "Data is null.");
    Assert.notNull(year, "Year is null.");
    String types = data.get("types").toString();
    String months = data.get("months").toString();
    ReportDataSet reportDataSet = reportService.getReports(types, months, year);
    HttpHeaders headers = new HttpHeaders();
    headers.add("success", "Success");
    return new ResponseEntity<ReportDataSet>(reportDataSet, headers, HttpStatus.OK);
}

From source file:eu.agilejava.javaonedemo.api.CookBookUserResource.java

@RequestMapping(method = RequestMethod.POST, consumes = APPLICATION_JSON_VALUE)
public ResponseEntity<CookBookUser> create(@RequestBody CookBookUser user) {
    cookBookUserService.create(user);//from   w  ww  .java2 s . c  o m

    HttpHeaders responseHeaders = new HttpHeaders();
    responseHeaders.add("Location", ServletUriComponentsBuilder.fromCurrentRequestUri()
            .pathSegment(String.valueOf(user.getId())).build().toUriString());
    return new ResponseEntity<>(responseHeaders, HttpStatus.CREATED);
}

From source file:eu.agilejava.javaonedemo.api.RecipeResource.java

@RequestMapping(method = RequestMethod.POST, consumes = APPLICATION_JSON_VALUE)
public ResponseEntity<Recipe> create(@RequestBody Recipe recipe) {
    recipeService.create(recipe);//from ww  w  .j ava2s. c o m

    HttpHeaders responseHeaders = new HttpHeaders();
    responseHeaders.add("Location", ServletUriComponentsBuilder.fromCurrentRequestUri()
            .pathSegment(String.valueOf(recipe.getId())).build().toUriString());
    return new ResponseEntity<>(responseHeaders, HttpStatus.CREATED);
}

From source file:eu.agilejava.javaonedemo.api.CookBookResource.java

@RequestMapping(method = RequestMethod.POST, consumes = APPLICATION_JSON_VALUE)
public ResponseEntity<CookBook> create(@RequestBody CookBook cookBook) {
    cookBookService.create(cookBook);/*from   w  w  w.java  2 s. c o  m*/

    HttpHeaders responseHeaders = new HttpHeaders();
    responseHeaders.add("Location", ServletUriComponentsBuilder.fromCurrentRequestUri()
            .pathSegment(String.valueOf(cookBook.getId())).build().toUriString());
    return new ResponseEntity<>(responseHeaders, HttpStatus.CREATED);

}

From source file:eu.freme.broker.eservices.EPublishing.java

@RequestMapping(value = "/e-publishing/html", method = RequestMethod.POST)
public ResponseEntity<byte[]> htmlToEPub(@RequestParam("htmlZip") MultipartFile file,
        @RequestParam("metadata") String jMetadata)
        throws InvalidZipException, EPubCreationException, IOException, MissingMetadataException {

    if (file.getSize() > maxUploadSize) {
        double size = maxUploadSize / (1024.0 * 1024);
        return new ResponseEntity<>(new byte[0], HttpStatus.BAD_REQUEST);
        //throw new BadRequestException(String.format("The uploaded file is too large. The maximum file size for uploads is %.2f MB", size));
    }//from  w  w  w. j a  v a2 s.co  m

    Gson gson = new Gson();
    Metadata metadata = gson.fromJson(jMetadata, Metadata.class);
    HttpHeaders responseHeaders = new HttpHeaders();
    responseHeaders.add("Content-Type", "application/epub+zip");
    try {
        return new ResponseEntity<>(entityAPI.createEPUB(metadata, file.getInputStream()), HttpStatus.OK);
    } catch (InvalidZipException | EPubCreationException | IOException | MissingMetadataException ex) {
        logger.log(Level.SEVERE, ex.getMessage());
        throw ex;
    }
}

From source file:org.openlmis.notification.web.BaseWebIntegrationTest.java

private String fetchToken() {
    RestTemplate restTemplate = new RestTemplate();

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

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

    HttpEntity<String> request = new HttpEntity<>(headers);

    Map<String, Object> params = new HashMap<>();
    params.put("grant_type", "password");

    ResponseEntity<?> response = restTemplate.exchange(buildUri(authorizationUrl, params), HttpMethod.POST,
            request, Object.class);

    return ((Map<String, String>) response.getBody()).get("access_token");
}

From source file:svc.data.citations.datasources.tyler.TylerCitationDataSource.java

private List<Citation> performRestTemplateCall(URI uri) {
    HttpHeaders headers = new HttpHeaders();
    headers.add("apikey", tylerConfiguration.apiKey);
    HttpEntity<?> query = new HttpEntity<>(headers);
    ResponseEntity<List<TylerCitation>> tylerCitationsResponse = null;
    ParameterizedTypeReference<List<TylerCitation>> type = new ParameterizedTypeReference<List<TylerCitation>>() {
    };/*from  w  w w  .  j  a v a2  s.c o  m*/

    List<TylerCitation> tylerCitations = null;
    try {
        tylerCitationsResponse = restTemplate.exchange(uri, HttpMethod.GET, query, type);
        tylerCitations = tylerCitationsResponse.getBody();
        return citationFilter
                .RemoveCitationsWithExpiredDates(citationTransformer.fromTylerCitations(tylerCitations));
    } catch (RestClientException ex) {
        System.out.println("Tyler datasource is down.");
        return Lists.newArrayList();
    }

}