All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.rocketbase.commons.resource.ValidationResource Maven / Gradle / Ivy

There is a newer version: 4.4.1
Show newest version
package io.rocketbase.commons.resource;

import io.rocketbase.commons.dto.validation.EmailErrorCodes;
import io.rocketbase.commons.dto.validation.PasswordErrorCodes;
import io.rocketbase.commons.dto.validation.UsernameErrorCodes;
import io.rocketbase.commons.dto.validation.ValidationResponse;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.Assert;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

public class ValidationResource implements BaseRestResource {

    protected String baseAuthApiUrl;
    protected RestTemplate restTemplate;

    public ValidationResource(String baseAuthApiUrl) {
        this(baseAuthApiUrl, null);
    }

    public ValidationResource(String baseAuthApiUrl, RestTemplate restTemplate) {
        Assert.hasText(baseAuthApiUrl, "baseAuthApiUrl is required");
        this.baseAuthApiUrl = baseAuthApiUrl;
        this.restTemplate = restTemplate;
    }

    protected RestTemplate getRestTemplate() {
        if (restTemplate == null) {
            restTemplate = new RestTemplate();
            restTemplate.setErrorHandler(new BasicResponseErrorHandler());
        }
        return restTemplate;
    }

    public ValidationResponse validatePassword(String password) {
        ResponseEntity> response = getRestTemplate()
                .exchange(UriComponentsBuilder.fromUriString(ensureEndsWithSlash(baseAuthApiUrl))
                                .path("/auth/validate/password").toUriString(),
                        HttpMethod.POST,
                        new HttpEntity<>(password),
                        new ParameterizedTypeReference>() {
                        });
        return response.getBody();
    }

    public ValidationResponse validateUsername(String username) {
        ResponseEntity> response = getRestTemplate()
                .exchange(UriComponentsBuilder.fromUriString(ensureEndsWithSlash(baseAuthApiUrl))
                                .path("/auth/validate/username").toUriString(),
                        HttpMethod.POST,
                        new HttpEntity<>(username),
                        new ParameterizedTypeReference>() {
                        });
        return response.getBody();
    }

    public ValidationResponse validateEmail(String email) {
        ResponseEntity> response = getRestTemplate()
                .exchange(UriComponentsBuilder.fromUriString(ensureEndsWithSlash(baseAuthApiUrl))
                                .path("/auth/validate/email").toUriString(),
                        HttpMethod.POST,
                        new HttpEntity<>(email),
                        new ParameterizedTypeReference>() {
                        });
        return response.getBody();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy