Search in sources :

Example 51 with RestTemplate

use of org.springframework.web.client.RestTemplate in project openmrs-module-pihcore by PIH.

the class RestBiometricEngine method lookup.

@Override
public BiometricSubject lookup(String subjectId) {
    RestTemplate restTemplate = new RestTemplate();
    String url = getSubjectUrl() + "/" + subjectId;
    ResponseEntity<BiometricSubject> response = restTemplate.getForEntity(url, BiometricSubject.class);
    if (HttpStatus.OK.equals(response.getStatusCode())) {
        return response.getBody();
    } else if (HttpStatus.NOT_FOUND.equals(response.getStatusCode())) {
        return null;
    } else {
        throw new IllegalStateException("Error looking up biometric subject at URL <" + url + ">.  Response status code: " + response.getStatusCode());
    }
}
Also used : BiometricSubject(org.openmrs.module.registrationcore.api.biometrics.model.BiometricSubject) RestTemplate(org.springframework.web.client.RestTemplate)

Example 52 with RestTemplate

use of org.springframework.web.client.RestTemplate in project incubator-servicecomb-java-chassis by apache.

the class DynamicConfigurationIT method releaseConfiguration.

public int releaseConfiguration() {
    String release = url + "/openapi/v1/envs/DEV/apps/SampleApp/clusters/default/namespaces/application/releases";
    RestTemplate rest = new RestTemplate();
    Map<String, String> body = new HashMap<>();
    body.put("releaseTitle", "release-configuration");
    body.put("releasedBy", "apollo");
    HttpEntity<?> entity = new HttpEntity<Object>(body, headers);
    ResponseEntity<String> exchange = rest.exchange(release, HttpMethod.POST, entity, String.class);
    return exchange.getStatusCodeValue();
}
Also used : HttpEntity(org.springframework.http.HttpEntity) HashMap(java.util.HashMap) RestTemplate(org.springframework.web.client.RestTemplate)

Example 53 with RestTemplate

use of org.springframework.web.client.RestTemplate in project incubator-servicecomb-java-chassis by apache.

the class ServiceCenterExample method main.

public static void main(String[] args) throws Exception {
    RestTemplate template = new RestTemplate();
    template.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
    MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
    headers.add("X-Tenant-Name", "default");
    RequestEntity<String> requestEntity = new RequestEntity<String>(headers, HttpMethod.GET, new URI("http://127.0.0.1:9980/registry/v3/microservices"));
    ResponseEntity<String> stringResponseEntity = template.exchange(requestEntity, String.class);
    System.out.println(stringResponseEntity.getBody());
    ResponseEntity<MicroserviceArray> microseriveResponseEntity = template.exchange(requestEntity, MicroserviceArray.class);
    MicroserviceArray microserives = microseriveResponseEntity.getBody();
    System.out.println(microserives.getServices().get(1).getServiceId());
    // instance
    headers.add("X-ConsumerId", microserives.getServices().get(1).getServiceId());
    requestEntity = new RequestEntity<String>(headers, HttpMethod.GET, new URI("http://127.0.0.1:9980/registry/v3/microservices/" + microserives.getServices().get(1).getServiceId() + "/instances"));
    ResponseEntity<String> microserviceInstanceResponseEntity = template.exchange(requestEntity, String.class);
    System.out.println(microserviceInstanceResponseEntity.getBody());
}
Also used : MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) RestTemplate(org.springframework.web.client.RestTemplate) RequestEntity(org.springframework.http.RequestEntity) URI(java.net.URI)

Example 54 with RestTemplate

use of org.springframework.web.client.RestTemplate in project spring-security-oauth by spring-projects.

the class RemoteTokenServicesTest method loadAuthenticationWhenIntrospectionResponseContainsActiveTrueThenReturnAuthentication.

// gh-838
@Test
public void loadAuthenticationWhenIntrospectionResponseContainsActiveTrueThenReturnAuthentication() throws Exception {
    Map responseAttrs = new HashMap();
    // "active" is the only required attribute as per RFC 7662 (https://tools.ietf.org/search/rfc7662#section-2.2)
    responseAttrs.put("active", true);
    ResponseEntity<Map> response = new ResponseEntity<Map>(responseAttrs, HttpStatus.OK);
    RestTemplate restTemplate = mock(RestTemplate.class);
    when(restTemplate.exchange(anyString(), any(HttpMethod.class), any(HttpEntity.class), any(Class.class))).thenReturn(response);
    this.remoteTokenServices.setRestTemplate(restTemplate);
    OAuth2Authentication authentication = this.remoteTokenServices.loadAuthentication("access-token-1234");
    assertNotNull(authentication);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) HttpEntity(org.springframework.http.HttpEntity) HashMap(java.util.HashMap) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) RestTemplate(org.springframework.web.client.RestTemplate) HashMap(java.util.HashMap) Map(java.util.Map) HttpMethod(org.springframework.http.HttpMethod) Test(org.junit.Test)

Example 55 with RestTemplate

use of org.springframework.web.client.RestTemplate in project spring-security-oauth by spring-projects.

the class RemoteTokenServicesTest method loadAuthenticationWhenIntrospectionResponseMissingActiveAttributeThenThrowInvalidTokenException.

// gh-838
@Test(expected = InvalidTokenException.class)
public void loadAuthenticationWhenIntrospectionResponseMissingActiveAttributeThenThrowInvalidTokenException() throws Exception {
    Map responseAttrs = new HashMap();
    ResponseEntity<Map> response = new ResponseEntity<Map>(responseAttrs, HttpStatus.OK);
    RestTemplate restTemplate = mock(RestTemplate.class);
    when(restTemplate.exchange(anyString(), any(HttpMethod.class), any(HttpEntity.class), any(Class.class))).thenReturn(response);
    this.remoteTokenServices.setRestTemplate(restTemplate);
    this.remoteTokenServices.loadAuthentication("access-token-1234");
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) HttpEntity(org.springframework.http.HttpEntity) HashMap(java.util.HashMap) RestTemplate(org.springframework.web.client.RestTemplate) HashMap(java.util.HashMap) Map(java.util.Map) HttpMethod(org.springframework.http.HttpMethod) Test(org.junit.Test)

Aggregations

RestTemplate (org.springframework.web.client.RestTemplate)519 Test (org.junit.Test)135 Test (org.junit.jupiter.api.Test)78 HttpHeaders (org.springframework.http.HttpHeaders)77 HttpEntity (org.springframework.http.HttpEntity)76 URI (java.net.URI)73 HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)45 HttpComponentsClientHttpRequestFactory (org.springframework.http.client.HttpComponentsClientHttpRequestFactory)44 HashMap (java.util.HashMap)42 ArrayList (java.util.ArrayList)40 IOException (java.io.IOException)36 Bean (org.springframework.context.annotation.Bean)35 MappingJackson2HttpMessageConverter (org.springframework.http.converter.json.MappingJackson2HttpMessageConverter)32 HttpClientErrorException (org.springframework.web.client.HttpClientErrorException)29 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)27 StringHttpMessageConverter (org.springframework.http.converter.StringHttpMessageConverter)27 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)24 RestTemplateBuilder (org.springframework.boot.web.client.RestTemplateBuilder)22 ClientHttpRequestFactory (org.springframework.http.client.ClientHttpRequestFactory)22 SimpleClientHttpRequestFactory (org.springframework.http.client.SimpleClientHttpRequestFactory)22