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:org.trustedanalytics.h2oscoringengine.publisher.steps.AppBitsUploadingStepTest.java

private HttpEntity<MultiValueMap<String, Object>> createTestAppBitsRequest() throws IOException {
    HttpEntity<String> resourcesPart = new HttpEntity<String>("[]");
    HttpEntity<ByteArrayResource> dataPart = new HttpEntity<>(
            new ByteArrayResource(Files.readAllBytes(testAppBitsPath)), HttpCommunication.zipHeaders());

    MultiValueMap<String, Object> multiPartRequest = new LinkedMultiValueMap<>();
    multiPartRequest.add("resources", resourcesPart);
    multiPartRequest.add("application", dataPart);

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

    return new HttpEntity<>(multiPartRequest, headers);
}

From source file:org.moserp.product.rest.ProductController.java

@ResponseBody
@RequestMapping(method = RequestMethod.GET, value = "/{productId}/quantityOnHand")
public Quantity getProductQuantityOnHand(@RequestHeader(value = "Authorization") String authorization,
        @PathVariable String productId) {
    if (!moduleRegistry.isModuleRegistered(OtherResources.MODULE_INVENTORY)) {
        return Quantity.ZERO;
    }//w  w  w.  j  av  a 2 s .c o  m
    HttpHeaders headers = new HttpHeaders();
    headers.put("Authorization", Collections.singletonList(authorization));
    HttpEntity request = new HttpEntity(headers);
    String url = "http://" + OtherResources.MODULE_INVENTORY + "/" + OtherResources.PRODUCTS + "/" + productId
            + "/quantityOnHand";
    ResponseEntity<Quantity> responseEntity = restTemplate.exchange(url, HttpMethod.GET, request,
            Quantity.class);
    return responseEntity.getBody();
}

From source file:com.codeabovelab.dm.cluman.cluster.registry.DockerRegistryAuthAdapter.java

@SuppressWarnings("unchecked")
private String getToken(AuthInfo authInfo) {
    RegistryCredentials registryCredentials = provider.getRegistryCredentials();
    // realm="https://auth.docker.io/token",service="registry.docker.io"
    // get https://auth.docker.io/token?service=registry.docker.io
    try {//w w  w.  j a  v  a  2s. c o m
        URI path = getPath(authInfo);
        HttpEntity<String> request = null;
        if (checkCredentials()) {
            request = new HttpEntity<>(createHeaders(registryCredentials));
        }
        Map<String, String> token = restTemplate.exchange(path, HttpMethod.GET, request, Map.class).getBody();

        if (!token.isEmpty()) {
            return token.get("token");
        }
    } catch (HttpClientErrorException e) {
        log.error("Can't do request " + e.getResponseBodyAsString(), e);
    } catch (Exception e) {
        log.error("Can't do request", e);
    }
    return null;
}

From source file:com.demo.FakeAuthorizator.java

private void doGet(String url) {
    System.out.println("try to send to " + url);
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
    headers.setContentType(MediaType.APPLICATION_JSON);
    HttpEntity<String> entity = new HttpEntity<String>(headers);
    ResponseEntity<String> rese = restTemplate.exchange(url, HttpMethod.GET, entity, String.class);
    System.out.println("RESPONSE STRING " + rese.getBody());
}

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

private HttpEntity<String> createExpectedBasicAuthRequest(String basicAuthToken) {
    HttpHeaders headers = new HttpHeaders();
    headers.add("Authorization", "Basic " + basicAuthToken);

    return new HttpEntity<>(headers);
}

From source file:example.users.UserControllerClientTests.java

private UserPayload issueGet(String path, MediaType mediaType) {

    HttpHeaders headers = new HttpHeaders();
    headers.add(HttpHeaders.ACCEPT, mediaType.toString());

    return template.exchange(path, HttpMethod.GET, new HttpEntity<Void>(headers), UserPayload.class).getBody();
}

From source file:cz.zcu.kiv.eeg.mobile.base.ws.asynctask.TestCredentials.java

@Override
protected UserInfo doInBackground(Void... params) {
    SharedPreferences credentials = getCredentials();
    String username = credentials.getString("tmp_username", null);
    String password = credentials.getString("tmp_password", null);
    String url = credentials.getString("tmp_url", null) + Values.SERVICE_USER + "login";

    setState(RUNNING, R.string.working_ws_credentials);
    HttpAuthentication authHeader = new HttpBasicAuthentication(username, password);
    HttpHeaders requestHeaders = new HttpHeaders();
    requestHeaders.setAuthorization(authHeader);
    requestHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_XML));
    HttpEntity<Object> entity = new HttpEntity<Object>(requestHeaders);

    SSLSimpleClientHttpRequestFactory factory = new SSLSimpleClientHttpRequestFactory();
    // Create a new RestTemplate instance
    RestTemplate restTemplate = new RestTemplate(factory);
    restTemplate.getMessageConverters().add(new SimpleXmlHttpMessageConverter());

    try {//from   w w  w  . j a v a2 s.com
        // Make the network request
        Log.d(TAG, url);
        ResponseEntity<UserInfo> userInfo = restTemplate.exchange(url, HttpMethod.GET, entity, UserInfo.class);
        return (Values.user = userInfo.getBody());
    } catch (Exception e) {
        Log.e(TAG, e.getLocalizedMessage(), e);
        setState(ERROR, e);
        return null;
    } finally {
        setState(DONE);
    }
}

From source file:org.hypernovae.entapps.actuator.ui.SampleActuatorUiApplicationTests.java

@Test
public void testError() throws Exception {
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
    ResponseEntity<String> entity = new TestRestTemplate().exchange("http://localhost:" + this.port + "/error",
            HttpMethod.GET, new HttpEntity<Void>(headers), String.class);
    assertEquals(HttpStatus.OK, entity.getStatusCode());
    assertTrue("Wrong body:\n" + entity.getBody(), entity.getBody().contains("<html>"));
    assertTrue("Wrong body:\n" + entity.getBody(), entity.getBody().contains("<body>"));
    assertTrue("Wrong body:\n" + entity.getBody(),
            entity.getBody().contains("Please contact the operator with the above information"));
}

From source file:comsat.sample.actuator.ui.SampleActuatorUiApplicationTests.java

@Test
public void testError() throws Exception {
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
    ResponseEntity<String> entity = new TestRestTemplate().exchange("http://localhost:" + this.port + "/error",
            HttpMethod.GET, new HttpEntity<Void>(headers), String.class);
    assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
    assertThat(entity.getBody()).contains("<html>").contains("<body>")
            .contains("Please contact the operator with the above information");
}