Example usage for org.springframework.http HttpEntity HttpEntity

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

Introduction

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

Prototype

public HttpEntity(MultiValueMap<String, String> headers) 

Source Link

Document

Create a new HttpEntity with the given headers and no body.

Usage

From source file:comsat.sample.freemarker.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:" + 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:com.consol.citrus.admin.connector.WebSocketPushMessageListener.java

/**
 * Push message to citrus-admin connector via REST API.
 * @param processId//from  w w w  . j a v  a 2 s  . c o m
 * @param message
 * @param direction
 */
protected void push(String processId, Message message, String direction) {
    try {
        if (!disabled) {
            ResponseEntity<String> response = getRestTemplate()
                    .exchange(
                            String.format("http://%s:%s/connector/message/%s?processId=%s", host, port,
                                    direction, processId),
                            HttpMethod.POST, new HttpEntity(message.toString()), String.class);

            if (response.getStatusCode().equals(HttpStatus.NOT_FOUND)) {
                disabled = true;
            }
        }
    } catch (RestClientException e) {
        log.error("Failed to push message to citrus-admin connector", e);
    }
}

From source file:com.logaritex.hadoop.configuration.manager.SimpleHttpService.java

@Override
public <R> R get(String url, Class<R> clazz, Object... params) {

    R response = restTemplate/*from  www.j  a  v  a 2 s. c o m*/
            .exchange(baseUrl + url, HttpMethod.GET, new HttpEntity<Object>(httpHeaders), clazz, params)
            .getBody();

    return response;
}

From source file:sample.jetty.SampleJetty8ApplicationTests.java

@Test
public void testHomeBasicAuth() throws Exception {
    //      ResponseEntity<String> entity = new TestRestTemplate().getForEntity(
    //            "http://localhost:" + this.port + "/static.html", String.class);

    ResponseEntity<String> entity = new TestRestTemplate().exchange(
            "http://localhost:" + this.port + "/static.html", HttpMethod.GET,
            new HttpEntity<String>(createHeaders("admin", "admin")), String.class);
    assertEquals(HttpStatus.OK, entity.getStatusCode());
    assertEquals("static", entity.getBody());
}

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

private AuthenticationResult authenticate(String url, UserBody user) {
    try {// ww w  . ja  v a2  s.  com
        ResponseEntity<UserBody> response = this.restOperations.exchange(url, HttpMethod.POST,
                new HttpEntity<UserBody>(user), UserBody.class);
        this.saveSessionId(response);
        this.saveAuthenticatedUser(response);
        return new AuthenticationResult(true);
    } catch (RestClientException e) {
        throw new AppGluRestClientException(e.getMessage(), e);
    }
}

From source file:com.tce.oauth2.spring.client.controller.UserInfoController.java

@RequestMapping("/userinfo")
public String index(HttpServletRequest request) {
    if (request.getSession().getAttribute("access_token") == null) {
        return "redirect:/";
    }/*from  ww  w.  j  a  v a 2s  . c om*/

    String accessToken = (String) request.getSession().getAttribute("access_token");
    HttpHeaders headers = new HttpHeaders();
    headers.set("Authorization", "Bearer " + accessToken);
    HttpEntity<?> entity = new HttpEntity<>(headers);

    ResponseEntity<User> response = restTemplate.exchange(OAUTH_URL + "/userinfo", HttpMethod.GET, entity,
            User.class);

    if (response.getStatusCode().is4xxClientError()) {
        return "redirect:/login";
    }

    User user = response.getBody();
    request.getSession(false).setAttribute("username", user.getUsername());
    return "redirect:/";
}

From source file:org.openlmis.fulfillment.service.AuthService.java

/**
 * Retrieves access token from the auth service.
 *
 * @return token./* www  .j  av a2 s  .  com*/
 */
@Cacheable("token")
public String obtainAccessToken() {
    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);

    RequestParameters params = RequestParameters.init().set("grant_type", "client_credentials");

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

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

From source file:id.co.teleanjar.ppobws.restclient.RestClientService.java

public Map<String, Object> inquiry(String idpel, String idloket, String merchantCode) throws IOException {
    String uri = "http://localhost:8080/ppobws/api/produk";
    RestTemplate restTemplate = new RestTemplate();

    HttpHeaders headers = createHeaders("superuser", "passwordku");
    UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(uri).queryParam("idpel", idpel)
            .queryParam("idloket", idloket).queryParam("merchantcode", merchantCode);
    HttpEntity<?> entity = new HttpEntity<>(headers);

    ResponseEntity<String> httpResp = restTemplate.exchange(builder.build().encode().toUri(), HttpMethod.GET,
            entity, String.class);
    String json = httpResp.getBody();
    LOGGER.info("JSON [{}]", json);
    LOGGER.info("STATUS [{}]", httpResp.getStatusCode().toString());

    Map<String, Object> map = new HashMap<>();
    ObjectMapper mapper = new ObjectMapper();

    map = mapper.readValue(json, new TypeReference<HashMap<String, Object>>() {
    });/*from  w  w  w  .j  av  a2  s .  c o  m*/
    return map;
}

From source file:edu.infsci2560.AboutIT.java

@Test
public void testAboutPageValid() throws Exception {
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
    ResponseEntity<String> entity = this.restTemplate.exchange("/about.html", HttpMethod.GET,
            new HttpEntity<>(headers), String.class);
    assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
    PageHtmlValidator.validatePage(entity.getBody());
}

From source file:com.bradley.musicapp.test.restapi.PersonRestControllerTest.java

private HttpEntity<?> getHttpEntity() {
    HttpHeaders requestHeaders = new HttpHeaders();
    requestHeaders.setAccept(Collections.singletonList(new MediaType("application", "json")));
    HttpEntity<?> requestEntity = new HttpEntity<>(requestHeaders);
    restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
    return requestEntity;
}