Example usage for org.springframework.http HttpEntity getBody

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

Introduction

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

Prototype

@Nullable
public T getBody() 

Source Link

Document

Returns the body of this entity.

Usage

From source file:org.mitreid.multiparty.web.ResourceController.java

private String registerResourceSet(Principal p, String issuer, MultipartyServerConfiguration server,
        String accessTokenValue) {
    JsonObject requestJson = new JsonObject();
    /*//from  ww w.j  a v  a  2s. co  m
     rs.setId(getAsLong(o, "_id"));
    rs.setName(getAsString(o, "name"));
    rs.setIconUri(getAsString(o, "icon_uri"));
    rs.setType(getAsString(o, "type"));
    rs.setScopes(getAsStringSet(o, "scopes"));
    rs.setUri(getAsString(o, "uri"));
            
     */
    requestJson.addProperty("name", p.getName() + "'s Resources");
    JsonArray scopes = new JsonArray();
    scopes.add(new JsonPrimitive("read"));
    scopes.add(new JsonPrimitive("write"));
    requestJson.add("resource_set_scopes", scopes);

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    headers.add("Authorization", "Bearer " + accessTokenValue);

    HttpEntity<String> request = new HttpEntity<String>(requestJson.toString(), headers);

    HttpEntity<String> responseEntity = restTemplate.postForEntity(server.getResourceSetRegistrationEndpoint(),
            request, String.class);

    JsonObject rso = parser.parse(responseEntity.getBody()).getAsJsonObject();
    String location = responseEntity.getHeaders().getLocation().toString();

    SharedResourceSet srs = new SharedResourceSet();
    srs.setIssuer(issuer);
    srs.setRsid(rso.get("_id").getAsString());
    srs.setUserAccessPolicyUri(rso.get("user_access_policy_uri").getAsString());
    srs.setLocation(location);

    resourceService.shareResourceForUser(srs, p);

    return "redirect:";
}

From source file:jp.go.aist.six.util.core.web.spring.SpringHttpClientImpl.java

public <T> T getObject(final String url, final Class<T> response_type, final Object... uri_variables) {
    _LOG_.debug("HTTP GET: URL=" + url + ", response type=" + response_type + ", variables="
            + Arrays.toString(uri_variables));

    HttpHeaders request_headers = new HttpHeaders();
    request_headers.setContentType(getObjectMediaType());
    HttpEntity<?> request_entity = new HttpEntity<Void>(request_headers);

    HttpEntity<T> response = null;
    try {//from  w w w . j  ava2s  .  c o  m
        response = _newRestTemplate().exchange(url, HttpMethod.GET, request_entity, response_type,
                uri_variables);
        //throws RestClientException
    } catch (Exception ex) {
        throw new HttpException(ex);
    }

    T body = response.getBody();

    return body;
}

From source file:com.jvoid.core.controller.HomeController.java

@RequestMapping("/jvoid-products")
public @ResponseBody String listAllJVoidProductsForOutView() {
    RestTemplate restTemplate = new RestTemplate();
    HttpHeaders headers = new HttpHeaders();
    headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);

    JSONObject jsonObj = new JSONObject();
    try {//from w  ww  . ja va 2 s . c  o  m
        jsonObj.put("id", -1);
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    UriComponentsBuilder builder = UriComponentsBuilder
            .fromHttpUrl(ServerUris.PRODUCT_SERVER_URI + URIConstants.GET_PRODUCT)
            .queryParam("params", jsonObj);
    HttpEntity<?> entity = new HttpEntity<>(headers);
    HttpEntity<String> returnString = restTemplate.exchange(builder.build().toUri(), HttpMethod.GET, entity,
            String.class);

    JSONObject returnJsonObj = null;
    try {
        returnJsonObj = new JSONObject(returnString.getBody());
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return returnJsonObj.toString();
}

From source file:com.jvoid.core.controller.HomeController.java

@RequestMapping("/jvoid-products-by-cat")
public @ResponseBody String listAllJVoidProductsByCategoryForOutView(@RequestParam("catId") String catId) {
    RestTemplate restTemplate = new RestTemplate();
    HttpHeaders headers = new HttpHeaders();
    headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);

    JSONObject jsonObj = new JSONObject();
    try {//from  ww  w . ja va 2  s . co m
        jsonObj.put("id", Integer.parseInt(catId));
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    UriComponentsBuilder builder = UriComponentsBuilder
            .fromHttpUrl(ServerUris.PRODUCT_SERVER_URI + URIConstants.GET_PRODUCTS_BY_CATEGORY)
            .queryParam("params", jsonObj);
    HttpEntity<?> entity = new HttpEntity<>(headers);
    HttpEntity<String> returnString = restTemplate.exchange(builder.build().toUri(), HttpMethod.GET, entity,
            String.class);

    JSONObject returnJsonObj = null;
    try {
        returnJsonObj = new JSONObject(returnString.getBody());
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return returnJsonObj.toString();
}

From source file:com.jvoid.core.controller.HomeController.java

@RequestMapping("/jvoid-categories")
public @ResponseBody String listAllJVoidCategoriesForOutView() {
    RestTemplate restTemplate = new RestTemplate();
    HttpHeaders headers = new HttpHeaders();
    headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);

    JSONObject jsonObj = new JSONObject();
    try {//from ww w.  j  av a 2 s . c  o  m
        jsonObj.put("id", -1);
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    UriComponentsBuilder builder = UriComponentsBuilder
            .fromHttpUrl(ServerUris.PRODUCT_SERVER_URI + URIConstants.GET_CATEGORY)
            .queryParam("params", jsonObj);
    HttpEntity<?> entity = new HttpEntity<>(headers);
    HttpEntity<String> returnString = restTemplate.exchange(builder.build().toUri(), HttpMethod.GET, entity,
            String.class);

    JSONObject returnJsonObj = null;
    try {
        returnJsonObj = new JSONObject(returnString.getBody());
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return returnJsonObj.toString();
}

From source file:com.jvoid.core.controller.HomeController.java

@RequestMapping(value = "/jvoid-checkout-cart", method = RequestMethod.POST)
public @ResponseBody String jvoidChrckoutCart(@RequestParam("params") String jsonParams) {
    RestTemplate restTemplate = new RestTemplate();
    HttpHeaders headers = new HttpHeaders();
    headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);

    System.out.println("jsonParams=>" + jsonParams);
    UriComponentsBuilder builder = UriComponentsBuilder
            .fromHttpUrl(ServerUris.QUOTE_SERVER_URI + URIConstants.CHECKOUT_CART)
            .queryParam("params", jsonParams);
    HttpEntity<?> entity = new HttpEntity<>(headers);
    HttpEntity<String> returnString = restTemplate.exchange(builder.build().toUri(), HttpMethod.GET, entity,
            String.class);

    JSONObject returnJsonObj = null;/*  ww w.j  av a 2 s.c o  m*/
    try {
        returnJsonObj = new JSONObject(returnString.getBody());
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    System.out.println("returnJsonObj=>" + returnJsonObj);

    String result = "";
    try {
        result = returnJsonObj.getString("result");
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    String response = "";
    if (result.equals("Success")) {
        UriComponentsBuilder builder1 = UriComponentsBuilder
                .fromHttpUrl(ServerUris.ORDER_SERVER_URI + URIConstants.ADD_ORDER)
                .queryParam("params", jsonParams);
        HttpEntity<?> entity1 = new HttpEntity<>(headers);
        HttpEntity<String> returnString1 = restTemplate.exchange(builder1.build().toUri(), HttpMethod.GET,
                entity1, String.class);
        response = returnString1.getBody();
    }

    return response;
}

From source file:net.slkdev.swagger.confluence.service.impl.XHtmlToConfluenceServiceImplTest.java

@Test
public void testCreatePageWithPaginationModeSingleWithOrphanFailSafe() {
    final SwaggerConfluenceConfig swaggerConfluenceConfig = getTestSwaggerConfluenceConfig();
    swaggerConfluenceConfig.setAncestorId(null);

    final String xhtml = IOUtils.readFull(
            AsciiDocToXHtmlServiceImplTest.class.getResourceAsStream("/swagger-petstore-xhtml-example.html"));

    when(restTemplate.exchange(any(URI.class), eq(HttpMethod.GET), any(RequestEntity.class), eq(String.class)))
            .thenReturn(responseEntity, responseEntity);
    when(restTemplate.exchange(any(URI.class), eq(HttpMethod.POST), any(HttpEntity.class), eq(String.class)))
            .thenReturn(responseEntity);
    when(responseEntity.getBody()).thenReturn(GET_RESPONSE_NOT_FOUND, GET_RESPONSE_FOUND, POST_RESPONSE);

    final ArgumentCaptor<HttpEntity> httpEntityCaptor = ArgumentCaptor.forClass(HttpEntity.class);

    xHtmlToConfluenceService.postXHtmlToConfluence(swaggerConfluenceConfig, xhtml);

    verify(restTemplate, times(2)).exchange(any(URI.class), eq(HttpMethod.GET), any(RequestEntity.class),
            eq(String.class));
    verify(restTemplate).exchange(any(URI.class), eq(HttpMethod.POST), httpEntityCaptor.capture(),
            eq(String.class));

    final HttpEntity<String> capturedHttpEntity = httpEntityCaptor.getValue();

    final String expectedPostBody = IOUtils.readFull(AsciiDocToXHtmlServiceImplTest.class
            .getResourceAsStream("/swagger-confluence-create-json-body-example.json"));

    assertNotNull("Failed to Capture RequestEntity for POST", capturedHttpEntity);
    assertEquals("Unexpected JSON Post Body", expectedPostBody, capturedHttpEntity.getBody());
}

From source file:net.slkdev.swagger.confluence.service.impl.XHtmlToConfluenceServiceImplTest.java

@Test
public void testUpdatePageWithPaginationModeSingleAndNoTableOfContents() {
    final SwaggerConfluenceConfig swaggerConfluenceConfig = getTestSwaggerConfluenceConfig();
    swaggerConfluenceConfig.setIncludeTableOfContentsOnSinglePage(false);

    final String xhtml = IOUtils.readFull(
            AsciiDocToXHtmlServiceImplTest.class.getResourceAsStream("/swagger-petstore-xhtml-example.html"));

    final ResponseEntity<String> postResponseEntity = new ResponseEntity<>(POST_RESPONSE, HttpStatus.OK);

    when(restTemplate.exchange(any(URI.class), eq(HttpMethod.GET), any(RequestEntity.class), eq(String.class)))
            .thenReturn(responseEntity);
    when(responseEntity.getBody()).thenReturn(GET_RESPONSE_FOUND);
    when(restTemplate.exchange(any(URI.class), eq(HttpMethod.PUT), any(RequestEntity.class), eq(String.class)))
            .thenReturn(postResponseEntity);

    final ArgumentCaptor<HttpEntity> httpEntityCaptor = ArgumentCaptor.forClass(HttpEntity.class);

    xHtmlToConfluenceService.postXHtmlToConfluence(swaggerConfluenceConfig, xhtml);

    verify(restTemplate).exchange(any(URI.class), eq(HttpMethod.GET), any(RequestEntity.class),
            eq(String.class));
    verify(restTemplate).exchange(any(URI.class), eq(HttpMethod.PUT), httpEntityCaptor.capture(),
            eq(String.class));

    final HttpEntity<String> capturedHttpEntity = httpEntityCaptor.getValue();

    final String expectedPostBody = IOUtils.readFull(AsciiDocToXHtmlServiceImplTest.class
            .getResourceAsStream("/swagger-confluence-update-json-body-example.json"));

    assertNotNull("Failed to Capture RequestEntity for POST", capturedHttpEntity);
    assertEquals("Unexpected JSON Post Body", expectedPostBody, capturedHttpEntity.getBody());
}

From source file:com.jvoid.quote.controller.JVoidQuoteController.java

public ProductsMaster getJVoidProduct(int productId) {

    RestTemplate restTemplate = new RestTemplate();
    HttpHeaders headers = new HttpHeaders();
    headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);

    JSONObject jsonObj = new JSONObject();
    try {//from   w  w w  . ja v  a  2s  .co m
        jsonObj.put("id", productId);
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    System.out.println("param jsonObj=>" + jsonObj.toString());

    UriComponentsBuilder builder = UriComponentsBuilder
            .fromHttpUrl(PRODUCT_SERVER_URI + URIConstants.GET_PRODUCT).queryParam("params", jsonObj);
    HttpEntity<?> entity = new HttpEntity<>(headers);
    HttpEntity<String> returnString = restTemplate.exchange(builder.build().toUri(), HttpMethod.GET, entity,
            String.class);
    System.out.println("returnString=>" + returnString);

    JSONObject returnJsonObj = null;
    try {
        returnJsonObj = new JSONObject(returnString.getBody());
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    JSONArray productsArr = null;
    ProductsMaster productsMaster = null;
    try {
        productsArr = returnJsonObj.getJSONArray("products");
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    productsMaster = new ProductsMaster();
    try {
        ObjectMapper mapper = new ObjectMapper();
        try {
            productsMaster = mapper.readValue(productsArr.getJSONObject(0).toString(), ProductsMaster.class);
        } catch (JsonParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JsonMappingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return productsMaster;
}

From source file:org.slc.sli.dashboard.client.RESTClient.java

/**
 * Makes a JSONRequest with the path. Times out, if boolean timeout property is set to true.
 * Timeout value is set in application-context.xml (dashboard.WSCall.timeout)
 * @param path/*  ww  w.  j a  va2 s  . com*/
 * @param timeout
 * @return
 */
public String getJsonRequest(String path, boolean timeout) {
    HttpHeaders headers = new HttpHeaders();
    HttpEntity<String> response = null;

    RestTemplate templateToUse;
    if (timeout) {
        templateToUse = templateWTimeout;
    } else {
        templateToUse = template;
    }

    try {
        response = exchange(templateToUse, path, HttpMethod.GET, new HttpEntity(headers), String.class);
    } catch (HttpClientErrorException e) {
        logger.debug("Catch HttpClientException: {}", e.getStatusCode());
    }

    if (response == null) {
        return null;
    }

    JsonParser parser = new JsonParser();
    String jsonText = parser.parse(response.getBody()).getAsString();
    return jsonText;

}