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.springframework.security.oauth2.provider.error.DefaultOAuth2ExceptionRenderer.java

public void handleHttpEntityResponse(HttpEntity<?> responseEntity, ServletWebRequest webRequest)
        throws Exception {
    if (responseEntity == null) {
        return;//from www .  j a va 2s.co m
    }
    HttpInputMessage inputMessage = createHttpInputMessage(webRequest);
    HttpOutputMessage outputMessage = createHttpOutputMessage(webRequest);
    if (responseEntity instanceof ResponseEntity && outputMessage instanceof ServerHttpResponse) {
        ((ServerHttpResponse) outputMessage)
                .setStatusCode(((ResponseEntity<?>) responseEntity).getStatusCode());
    }
    HttpHeaders entityHeaders = responseEntity.getHeaders();
    if (!entityHeaders.isEmpty()) {
        outputMessage.getHeaders().putAll(entityHeaders);
    }
    Object body = responseEntity.getBody();
    if (body != null) {
        writeWithMessageConverters(body, inputMessage, outputMessage);
    } else {
        // flush headers
        outputMessage.getBody();
    }
}

From source file:org.springframework.vault.authentication.AuthenticationStepsExecutor.java

private static HttpEntity<?> getEntity(HttpEntity<?> entity, @Nullable Object state) {

    if (entity == null) {
        return state == null ? HttpEntity.EMPTY : new HttpEntity<>(state);
    }//w  w w . j a va 2s  . c  om

    if (entity.getBody() == null && state != null) {
        return new HttpEntity<>(state, entity.getHeaders());
    }

    return entity;
}

From source file:org.springframework.vault.authentication.AuthenticationStepsOperator.java

private Mono<Object> doHttpRequest(HttpRequestNode<Object> step, Object state) {

    HttpRequest<Object> definition = step.getDefinition();
    HttpEntity<?> entity = getEntity(definition.getEntity(), state);

    RequestBodySpec spec;//from  ww w .j  a  va  2s. co m
    if (definition.getUri() == null) {

        spec = webClient.method(definition.getMethod()).uri(definition.getUriTemplate(),
                (Object[]) definition.getUrlVariables());
    } else {
        spec = webClient.method(definition.getMethod()).uri(definition.getUri());
    }

    for (Entry<String, List<String>> header : entity.getHeaders().entrySet()) {
        spec = spec.header(header.getKey(), header.getValue().get(0));
    }

    if (entity.getBody() != null && !entity.getBody().equals(Undefinded.INSTANCE)) {
        return spec.syncBody(entity.getBody()).retrieve().bodyToMono(definition.getResponseType());
    }

    return spec.retrieve().bodyToMono(definition.getResponseType());
}

From source file:org.springframework.vault.authentication.AuthenticationStepsOperator.java

private static HttpEntity<?> getEntity(HttpEntity<?> entity, Object state) {

    if (entity == null) {
        return state == null ? HttpEntity.EMPTY : new HttpEntity<>(state);
    }/*from  ww w .  j a va2s  .  c  o  m*/

    if (entity.getBody() == null && state != null) {
        return new HttpEntity<>(state, entity.getHeaders());
    }

    return entity;
}

From source file:org.venice.beachfront.bfapi.services.OAuthServiceTests.java

@Test
public void testRequestAccessTokenSuccess() throws UserException {
    String mockAuthCode = "mock-auth-code-123";
    String mockAccessToken = "mock-access-token-321";

    AccessTokenResponseBody mockResponse = Mockito.mock(AccessTokenResponseBody.class);
    Mockito.when(mockResponse.getAccessToken()).thenReturn(mockAccessToken);

    Mockito.when(this.restTemplate.exchange(Mockito.eq(this.oauthTokenUrl), Mockito.eq(HttpMethod.POST),
            Mockito.any(), Mockito.eq(AccessTokenResponseBody.class)))
            .then(new Answer<ResponseEntity<AccessTokenResponseBody>>() {
                @Override/*from  w w w.j  a v  a2  s  . c om*/
                public ResponseEntity<AccessTokenResponseBody> answer(InvocationOnMock invocation) {
                    HttpEntity<MultiValueMap<String, String>> entity = invocation.getArgumentAt(2,
                            HttpEntity.class);
                    MultiValueMap<String, String> body = entity.getBody();
                    assertEquals(mockAuthCode, body.get("code").get(0));
                    assertEquals("authorization_code", body.get("grant_type").get(0));
                    assertEquals(oauthService.getOauthRedirectUri(), body.get("redirect_uri").get(0));
                    return new ResponseEntity<AccessTokenResponseBody>(mockResponse, HttpStatus.OK);
                }
            });

    String receivedAccessToken = this.oauthService.requestAccessToken(mockAuthCode);
    assertEquals(mockAccessToken, receivedAccessToken);
}