Search in sources :

Example 56 with RestTemplate

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

the class DefaultOAuth2ExceptionRenderer method geDefaultMessageConverters.

private List<HttpMessageConverter<?>> geDefaultMessageConverters() {
    List<HttpMessageConverter<?>> result = new ArrayList<HttpMessageConverter<?>>();
    result.addAll(new RestTemplate().getMessageConverters());
    result.add(new JaxbOAuth2ExceptionMessageConverter());
    return result;
}
Also used : JaxbOAuth2ExceptionMessageConverter(org.springframework.security.oauth2.http.converter.jaxb.JaxbOAuth2ExceptionMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) ArrayList(java.util.ArrayList) RestTemplate(org.springframework.web.client.RestTemplate)

Example 57 with RestTemplate

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

the class ClientCredentialsProviderTests method testHardCodedAuthenticationFineClient.

/**
 * No Basic authentication provided, only the hard coded client_id.
 */
@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void testHardCodedAuthenticationFineClient() {
    RestTemplate restTemplate = new RestTemplate();
    MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>();
    params.add("grant_type", "client_credentials");
    params.add("client_id", "my-client-with-secret");
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
    RequestEntity<MultiValueMap<String, String>> req = new RequestEntity<MultiValueMap<String, String>>(params, headers, HttpMethod.POST, tokenUri);
    ResponseEntity<Map> response = restTemplate.exchange(req, Map.class);
    assertEquals(HttpStatus.OK, response.getStatusCode());
    Map<String, String> body = response.getBody();
    String accessToken = body.get("access_token");
    assertNotNull(accessToken);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) RestTemplate(org.springframework.web.client.RestTemplate) RequestEntity(org.springframework.http.RequestEntity) MultiValueMap(org.springframework.util.MultiValueMap) Map(java.util.Map) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) MultiValueMap(org.springframework.util.MultiValueMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) Test(org.junit.Test)

Example 58 with RestTemplate

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

the class ApolloClientTest method refreshApolloConfig.

@Test
public void refreshApolloConfig() {
    ApolloConfig apolloConfig = ApolloConfig.INSTANCE;
    RestTemplate rest = Mockito.mock(RestTemplate.class);
    ResponseEntity<String> responseEntity = new ResponseEntity<>("{\"apollo\":\"mocked\", \"configurations\":{\"timeout\":1000}}", HttpStatus.OK);
    Mockito.when(rest.exchange(Matchers.anyString(), Matchers.any(HttpMethod.class), Matchers.<HttpEntity<String>>any(), Matchers.<Class<String>>any())).thenReturn(responseEntity);
    ApolloConfigurationSourceImpl impl = new ApolloConfigurationSourceImpl();
    UpdateHandler updateHandler = impl.new UpdateHandler();
    ApolloClient apolloClient = new ApolloClient(updateHandler);
    Deencapsulation.setField(apolloClient, "rest", rest);
    ConfigRefresh cr = apolloClient.new ConfigRefresh(apolloConfig.getServerUri());
    cr.run();
    Map<String, Object> originMap = Deencapsulation.getField(apolloClient, "originalConfigMap");
    Assert.assertEquals(1, originMap.size());
}
Also used : UpdateHandler(org.apache.servicecomb.config.archaius.sources.ApolloConfigurationSourceImpl.UpdateHandler) ResponseEntity(org.springframework.http.ResponseEntity) ConfigRefresh(org.apache.servicecomb.config.client.ApolloClient.ConfigRefresh) RestTemplate(org.springframework.web.client.RestTemplate) ApolloConfigurationSourceImpl(org.apache.servicecomb.config.archaius.sources.ApolloConfigurationSourceImpl) HttpMethod(org.springframework.http.HttpMethod) Test(org.junit.Test)

Example 59 with RestTemplate

use of org.springframework.web.client.RestTemplate in project spring-cloud-config by spring-cloud.

the class VaultEnvironmentRepositoryTests method testFindOneDefaultKeySetAndDifferentToApplication.

@Test
public void testFindOneDefaultKeySetAndDifferentToApplication() throws IOException {
    MockHttpServletRequest configRequest = new MockHttpServletRequest();
    configRequest.addHeader("X-CONFIG-TOKEN", "mytoken");
    RestTemplate rest = Mockito.mock(RestTemplate.class);
    ResponseEntity<VaultEnvironmentRepository.VaultResponse> myAppResp = Mockito.mock(ResponseEntity.class);
    Mockito.when(myAppResp.getStatusCode()).thenReturn(HttpStatus.OK);
    VaultEnvironmentRepository.VaultResponse myAppVaultResp = Mockito.mock(VaultEnvironmentRepository.VaultResponse.class);
    Mockito.when(myAppVaultResp.getData()).thenReturn("{\"foo\":\"bar\"}");
    Mockito.when(myAppResp.getBody()).thenReturn(myAppVaultResp);
    Mockito.when(rest.exchange(Mockito.eq("http://127.0.0.1:8200/v1/{backend}/{key}"), Mockito.eq(HttpMethod.GET), Mockito.any(HttpEntity.class), Mockito.eq(VaultEnvironmentRepository.VaultResponse.class), Mockito.eq("secret"), Mockito.eq("myapp"))).thenReturn(myAppResp);
    ResponseEntity<VaultEnvironmentRepository.VaultResponse> myDefaultKeyResp = Mockito.mock(ResponseEntity.class);
    Mockito.when(myDefaultKeyResp.getStatusCode()).thenReturn(HttpStatus.OK);
    VaultEnvironmentRepository.VaultResponse myDefaultKeyVaultResp = Mockito.mock(VaultEnvironmentRepository.VaultResponse.class);
    Mockito.when(myDefaultKeyVaultResp.getData()).thenReturn("{\"def-foo\":\"def-bar\"}");
    Mockito.when(myDefaultKeyResp.getBody()).thenReturn(myDefaultKeyVaultResp);
    Mockito.when(rest.exchange(Mockito.eq("http://127.0.0.1:8200/v1/{backend}/{key}"), Mockito.eq(HttpMethod.GET), Mockito.any(HttpEntity.class), Mockito.eq(VaultEnvironmentRepository.VaultResponse.class), Mockito.eq("secret"), Mockito.eq("mydefaultkey"))).thenReturn(myDefaultKeyResp);
    VaultEnvironmentRepository repo = new VaultEnvironmentRepository(configRequest, new EnvironmentWatch.Default(), rest, new VaultEnvironmentProperties());
    repo.setDefaultKey("mydefaultkey");
    Environment e = repo.findOne("myapp", null, null);
    assertEquals("Name should be the same as the application argument", "myapp", e.getName());
    assertEquals("Properties for specified application and default application with key 'mydefaultkey' should be returned", 2, e.getPropertySources().size());
    Map<String, String> firstResult = new HashMap<String, String>();
    firstResult.put("foo", "bar");
    assertEquals("Properties for specified application should be returned in priority position", firstResult, e.getPropertySources().get(0).getSource());
    Map<String, String> secondResult = new HashMap<String, String>();
    secondResult.put("def-foo", "def-bar");
    assertEquals("Properties for default application with key 'mydefaultkey' should be returned in second position", secondResult, e.getPropertySources().get(1).getSource());
}
Also used : HttpEntity(org.springframework.http.HttpEntity) HashMap(java.util.HashMap) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) RestTemplate(org.springframework.web.client.RestTemplate) Environment(org.springframework.cloud.config.environment.Environment) Test(org.junit.Test)

Example 60 with RestTemplate

use of org.springframework.web.client.RestTemplate in project NoraUi by NoraUi.

the class MailSteps method createRestTemplate.

private static RestTemplate createRestTemplate() {
    String proxyUser = "";
    String proxyPassword = "";
    String proxyUrl = "";
    int proxyPort = 0;
    HttpClientBuilder clientBuilder = HttpClientBuilder.create();
    if (!"".equals(proxyUrl)) {
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(new AuthScope(proxyUrl, proxyPort), new UsernamePasswordCredentials(proxyUser, proxyPassword));
        HttpHost myProxy = new HttpHost(proxyUrl, proxyPort);
        clientBuilder.setProxy(myProxy).setDefaultCredentialsProvider(credsProvider).disableCookieManagement();
    }
    HttpClient httpClient = clientBuilder.build();
    HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
    factory.setHttpClient(httpClient);
    RestTemplate restTemplate = new RestTemplate(factory);
    restTemplate.getMessageConverters().add(new GsonHttpMessageConverter());
    return restTemplate;
}
Also used : BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpHost(org.apache.http.HttpHost) HttpClient(org.apache.http.client.HttpClient) AuthScope(org.apache.http.auth.AuthScope) RestTemplate(org.springframework.web.client.RestTemplate) GsonHttpMessageConverter(org.springframework.http.converter.json.GsonHttpMessageConverter) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) HttpComponentsClientHttpRequestFactory(org.springframework.http.client.HttpComponentsClientHttpRequestFactory) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

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