diff options
Diffstat (limited to 'src/main/java/com/juick/service/EmailServiceImpl.java')
-rw-r--r-- | src/main/java/com/juick/service/EmailServiceImpl.java | 36 |
1 files changed, 35 insertions, 1 deletions
diff --git a/src/main/java/com/juick/service/EmailServiceImpl.java b/src/main/java/com/juick/service/EmailServiceImpl.java index 383dbdaf..08aaf1b9 100644 --- a/src/main/java/com/juick/service/EmailServiceImpl.java +++ b/src/main/java/com/juick/service/EmailServiceImpl.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2008-2020, Juick + * Copyright (C) 2008-2024, Juick * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as @@ -17,13 +17,21 @@ package com.juick.service; +import com.fasterxml.jackson.databind.ObjectMapper; import com.juick.model.User; +import jakarta.inject.Inject; +import okhttp3.OkHttpClient; +import okhttp3.Request; import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Value; import org.springframework.dao.EmptyResultDataAccessException; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; import org.springframework.stereotype.Repository; import org.springframework.transaction.annotation.Transactional; +import java.io.IOException; import java.time.Instant; import java.time.ZoneOffset; import java.time.temporal.ChronoUnit; @@ -36,6 +44,13 @@ import java.util.List; @Transactional public class EmailServiceImpl extends BaseJdbcService implements EmailService { + @Inject + private OkHttpClient httpClient; + @Inject + private ObjectMapper jsonMapper; + + @Value("${email_validation_service_url:http://localhost:8080}") + private String emailValidationServiceUrl; @Override public boolean verifyAddressByCode(Integer userId, String code) { try { @@ -120,4 +135,23 @@ public class EmailServiceImpl extends BaseJdbcService implements EmailService { new MapSqlParameterSource() .addValue("day", toDateTime(day.atOffset(ZoneOffset.UTC)), dateTimeType())); } + + @Override + public boolean isValidEmail(String email) { + var request = new Request.Builder() + .url(emailValidationServiceUrl + "/?email=" + email) + .addHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE) + .build(); + try { + try(var response = httpClient.newCall(request).execute()) { + if (response.isSuccessful() && response.body() != null) { + var account = jsonMapper.readTree(response.body().string()); + return account.has("disposable") && account.get("disposable").textValue().equals("false"); + } + } + } catch (IOException e) { + return false; + } + return false; + } } |