Search in sources :

Example 1 with RestTemplateBuilder

use of org.springframework.boot.web.client.RestTemplateBuilder in project sw360portal by sw360.

the class Sw360AuthenticationProvider method isAuthorized.

private boolean isAuthorized(String email, String password) {
    // Solution 1:
    // UserLocalServiceUtil.authenticateForBasic
    // userId = UserLocalServiceUtil.authenticateForBasic(companyId, authType, login, current);
    // this need a dependency to liferay
    // Solution 2:
    // Liferay json webservice call to verify username and password
    String liferayParameterURL = "/api/jsonws/user/get-user-id-by-email-address?companyId=%s&emailAddress=%s";
    String url = sw360PortalServerURL + String.format(liferayParameterURL, sw360LiferayCompanyId, email);
    RestTemplateBuilder restTemplateBuilder = new RestTemplateBuilder();
    String encodedPassword;
    try {
        encodedPassword = URLDecoder.decode(password, "US-ASCII");
    } catch (UnsupportedEncodingException e) {
        return false;
    }
    RestTemplate restTemplate = restTemplateBuilder.basicAuthorization(email, encodedPassword).build();
    ResponseEntity<String> response = restTemplate.postForEntity(url, null, String.class);
    try {
        // The user exits in liferay if the body contains a number
        Integer.parseInt(response.getBody());
    } catch (NumberFormatException e) {
        return false;
    }
    return true;
}
Also used : RestTemplateBuilder(org.springframework.boot.web.client.RestTemplateBuilder) RestTemplate(org.springframework.web.client.RestTemplate) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 2 with RestTemplateBuilder

use of org.springframework.boot.web.client.RestTemplateBuilder in project spring-boot by spring-projects.

the class RestTemplateMetricsConfigurationTests method restTemplateCanBeCustomizedManually.

@Test
void restTemplateCanBeCustomizedManually() {
    this.contextRunner.run((context) -> {
        assertThat(context).hasSingleBean(MetricsRestTemplateCustomizer.class);
        RestTemplateBuilder customBuilder = new RestTemplateBuilder().customizers(context.getBean(MetricsRestTemplateCustomizer.class));
        MeterRegistry registry = context.getBean(MeterRegistry.class);
        validateRestTemplate(customBuilder, registry);
    });
}
Also used : RestTemplateBuilder(org.springframework.boot.web.client.RestTemplateBuilder) MetricsRestTemplateCustomizer(org.springframework.boot.actuate.metrics.web.client.MetricsRestTemplateCustomizer) MeterRegistry(io.micrometer.core.instrument.MeterRegistry) Test(org.junit.jupiter.api.Test)

Example 3 with RestTemplateBuilder

use of org.springframework.boot.web.client.RestTemplateBuilder in project spring-boot by spring-projects.

the class RestTemplateMetricsConfigurationTests method restTemplateWithRootUriIsInstrumented.

@Test
void restTemplateWithRootUriIsInstrumented() {
    this.contextRunner.run((context) -> {
        MeterRegistry registry = context.getBean(MeterRegistry.class);
        RestTemplateBuilder builder = context.getBean(RestTemplateBuilder.class);
        builder = builder.rootUri("/root");
        validateRestTemplate(builder, registry, "/root");
    });
}
Also used : RestTemplateBuilder(org.springframework.boot.web.client.RestTemplateBuilder) MeterRegistry(io.micrometer.core.instrument.MeterRegistry) Test(org.junit.jupiter.api.Test)

Example 4 with RestTemplateBuilder

use of org.springframework.boot.web.client.RestTemplateBuilder in project spring-boot by spring-projects.

the class CloudFoundrySecurityServiceTests method skipSslValidationWhenTrue.

@Test
void skipSslValidationWhenTrue() {
    RestTemplateBuilder builder = new RestTemplateBuilder();
    this.securityService = new CloudFoundrySecurityService(builder, CLOUD_CONTROLLER, true);
    RestTemplate restTemplate = (RestTemplate) ReflectionTestUtils.getField(this.securityService, "restTemplate");
    assertThat(restTemplate.getRequestFactory()).isInstanceOf(SkipSslVerificationHttpRequestFactory.class);
}
Also used : RestTemplateBuilder(org.springframework.boot.web.client.RestTemplateBuilder) RestTemplate(org.springframework.web.client.RestTemplate) Test(org.junit.jupiter.api.Test)

Example 5 with RestTemplateBuilder

use of org.springframework.boot.web.client.RestTemplateBuilder in project spring-boot by spring-projects.

the class CloudFoundrySecurityServiceTests method setup.

@BeforeEach
void setup() {
    MockServerRestTemplateCustomizer mockServerCustomizer = new MockServerRestTemplateCustomizer();
    RestTemplateBuilder builder = new RestTemplateBuilder(mockServerCustomizer);
    this.securityService = new CloudFoundrySecurityService(builder, CLOUD_CONTROLLER, false);
    this.server = mockServerCustomizer.getServer();
}
Also used : RestTemplateBuilder(org.springframework.boot.web.client.RestTemplateBuilder) MockServerRestTemplateCustomizer(org.springframework.boot.test.web.client.MockServerRestTemplateCustomizer) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

RestTemplateBuilder (org.springframework.boot.web.client.RestTemplateBuilder)36 Test (org.junit.jupiter.api.Test)22 RestTemplate (org.springframework.web.client.RestTemplate)21 MockRestServiceServer (org.springframework.test.web.client.MockRestServiceServer)5 TestRestTemplate (org.springframework.boot.test.web.client.TestRestTemplate)4 MeterRegistry (io.micrometer.core.instrument.MeterRegistry)3 Test (org.junit.Test)3 BeforeEach (org.junit.jupiter.api.BeforeEach)3 LocalHostUriTemplateHandler (org.springframework.boot.test.web.client.LocalHostUriTemplateHandler)3 Bean (org.springframework.context.annotation.Bean)3 RestTemplateResponseErrorHandler (edu.hawaii.its.api.controller.RestTemplateResponseErrorHandler)2 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)2 MockServerRestTemplateCustomizer (org.springframework.boot.test.web.client.MockServerRestTemplateCustomizer)2 HttpEntity (org.springframework.http.HttpEntity)2 HttpHeaders (org.springframework.http.HttpHeaders)2 RequestExpectationManager (org.springframework.test.web.client.RequestExpectationManager)2 Builder (io.micrometer.core.instrument.Timer.Builder)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Method (java.lang.reflect.Method)1