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:edu.infsci2560.services.LocationsService.java

@RequestMapping(method = RequestMethod.PUT, consumes = "application/json", produces = "application/json")
public ResponseEntity<Location> update(@RequestBody Location locations, @PathVariable("id") long id) {
    HttpHeaders headers = new HttpHeaders();
    return new ResponseEntity<>(repository.save(locations), headers, HttpStatus.OK);
}

From source file:eu.dime.dnsregister.controllers.RecordsController.java

@RequestMapping(value = "/{id}", headers = "Accept=application/json")
@ResponseBody/*from ww w.j a  v  a  2 s  .  c  o m*/
public ResponseEntity<String> showJson(@PathVariable("id") Integer id) {
    Records records = Records.findRecords(id);
    HttpHeaders headers = new HttpHeaders();
    headers.add("Content-Type", "application/json; charset=utf-8");
    if (records == null) {
        return new ResponseEntity<String>(headers, HttpStatus.NOT_FOUND);
    }
    return new ResponseEntity<String>(records.toJson(), headers, HttpStatus.OK);
}

From source file:org.openbaton.nse.beans.connectivitymanageragent.ConnectivityManagerRequestor.java

public Host getHost() {
    String url = configuration.getBaseUrl() + "/hosts";
    HttpHeaders headers = new HttpHeaders();
    HttpEntity<String> getEntity = new HttpEntity<>(headers);
    logger.debug("REQUESTING HOSTS to " + url);
    ResponseEntity<String> hosts = template.exchange(url, HttpMethod.GET, getEntity, String.class);

    logger.debug("hosts " + hosts.getBody());

    if (!hosts.getStatusCode().is2xxSuccessful()) {
        return null;
    } else {// w ww  .  j a v  a2  s. com
        return mapper.fromJson(hosts.getBody(), Host.class);
    }
}

From source file:org.keycloak.adapters.springsecurity.service.KeycloakDirectAccessGrantService.java

@Override
public RefreshableKeycloakSecurityContext login(String username, String password) throws VerificationException {

    final MultiValueMap<String, String> body = new LinkedMultiValueMap<>();
    final HttpHeaders headers = new HttpHeaders();

    headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
    body.set("username", username);
    body.set("password", password);
    body.set(OAuth2Constants.GRANT_TYPE, OAuth2Constants.PASSWORD);

    AccessTokenResponse response = template.postForObject(keycloakDeployment.getTokenUrl(),
            new HttpEntity<>(body, headers), AccessTokenResponse.class);

    return KeycloakSpringAdapterUtils.createKeycloakSecurityContext(keycloakDeployment, response);
}

From source file:reconf.server.services.property.ClientReadPropertyService.java

@RequestMapping(value = "/{prod}/{comp}/{prop}", method = RequestMethod.GET)
@Transactional(readOnly = true)//from  w  ww  .  jav a2 s .c  om
public ResponseEntity<String> doIt(@PathVariable("prod") String product, @PathVariable("comp") String component,
        @PathVariable("prop") String property,
        @RequestParam(value = "instance", required = false, defaultValue = "unknown") String instance) {

    PropertyKey key = new PropertyKey(product, component, property);
    List<String> errors = DomainValidator.checkForErrors(key);
    Property reqProperty = new Property(key);

    HttpHeaders headers = new HttpHeaders();
    if (!errors.isEmpty()) {
        addErrorHeader(headers, errors, reqProperty);
        return new ResponseEntity<String>(headers, HttpStatus.BAD_REQUEST);
    }

    List<Property> dbProperties = properties
            .findByKeyProductAndKeyComponentAndKeyNameOrderByRulePriorityDescKeyRuleNameAsc(key.getProduct(),
                    key.getComponent(), key.getName());
    for (Property dbProperty : dbProperties) {
        try {
            if (isMatch(instance, dbProperty)) {
                addRuleHeader(headers, dbProperty);
                return new ResponseEntity<String>(dbProperty.getValue(), headers, HttpStatus.OK);
            }
        } catch (Exception e) {
            log.error("error applying rule", e);
            addRuleHeader(headers, dbProperty);
            addErrorHeader(headers, Collections.singletonList("rule error"), reqProperty);
            return new ResponseEntity<String>(headers, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
    return new ResponseEntity<String>(HttpStatus.NOT_FOUND);
}

From source file:eu.falcon.semantic.client.DenaClient.java

public static String getInstanceAttributes(String instanceURI) {

    final String uri = "http://falconsemanticmanager.euprojects.net/api/v1/ontology/instance/attributes";
    //final String uri = "http://localhost:8090/api/v1/ontology/instance/attributes";

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);

    RestTemplate restTemplate = new RestTemplate();

    HttpEntity<String> entity = new HttpEntity<>(instanceURI, headers);

    String result = restTemplate.postForObject(uri, entity, String.class);

    return result;
}

From source file:fr.mby.opa.pics.web.controller.ImageController.java

@RequestMapping(value = "{id}", method = RequestMethod.GET)
public ResponseEntity<byte[]> getImage(@PathVariable final Long id, final HttpServletRequest request,
        final HttpServletResponse response) throws Exception {

    ResponseEntity<byte[]> responseEntity = null;

    if (id != null) {

        final BinaryImage image = this.pictureDao.findImageById(id);
        if (image != null) {
            final byte[] thumbnailData = image.getData();

            final HttpHeaders responseHeaders = new HttpHeaders();
            responseHeaders.setContentType(MediaType.parseMediaType("image/" + image.getFormat()));
            responseHeaders.setContentLength(thumbnailData.length);
            responseHeaders.set("Content-Disposition", "filename=\"" + image.getFilename() + '\"');

            responseEntity = new ResponseEntity<byte[]>(thumbnailData, responseHeaders, HttpStatus.OK);
        }/*from  ww  w. ja v  a  2 s  . com*/
    }

    if (responseEntity == null) {
        responseEntity = new ResponseEntity<byte[]>(null, null, HttpStatus.NOT_FOUND);
    }

    return responseEntity;
}

From source file:com.example.SampleWebFreeMarkerApplicationTests.java

@Test
public void testFreeMarkerErrorTemplate() 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 + "/" + contextPath + "/does-not-exist", HttpMethod.GET,
            requestEntity, String.class);

    assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
    assertThat(responseEntity.getBody()).contains("Something went wrong: 404 Not Found");
}

From source file:com.expedia.client.WunderGroundClient.java

@Override
public ResponseEntity<Response> getXMLResponse(Object request) {
    ResponseEntity<Response> responseEntity = null;
    Weather weather = null;/*from   www .  j  av a 2s  .c o  m*/

    if (request instanceof Weather) {
        weather = (Weather) request;
        List<MediaType> mediaTypes = new ArrayList<MediaType>();
        mediaTypes.add(MediaType.APPLICATION_XML);
        HttpHeaders headers = new HttpHeaders();
        headers.setAccept(mediaTypes);
        HttpEntity<Weather> httpEntity = new HttpEntity<Weather>(null, headers);
        try {
            System.out.println("Hitting weather service!");
            responseEntity = restTemplate.exchange(weatherServiceXmlUrl, HttpMethod.GET, httpEntity,
                    Response.class, weatherApiKey, weather.getZipCode());
        } catch (RuntimeException e) {
            e.printStackTrace();
            weather.setErrorDesc("Get failed" + e.getMessage());
        }
    }
    return responseEntity;
}

From source file:movies.spring.jdbc.SampleMovieApplicationTests.java

@Test
public void testMovie() throws Exception {
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(asList(MediaType.APPLICATION_JSON));
    TestRestTemplate template = new TestRestTemplate();
    template.setMessageConverters(//from www.j av a  2s.com
            Arrays.<HttpMessageConverter<?>>asList(new MappingJackson2HttpMessageConverter()));
    ResponseEntity<Map> entity = template.getForEntity("http://localhost:" + this.port + "/movie/{title}",
            Map.class, "The Matrix");
    assertEquals(HttpStatus.OK, entity.getStatusCode());
    assertEquals("The Matrix", entity.getBody().get("title"));
}