|
16 | 16 |
|
17 | 17 | import static google.registry.request.Action.Method.GET; |
18 | 18 | import static google.registry.request.Action.Method.POST; |
19 | | -import static java.nio.charset.StandardCharsets.UTF_8; |
20 | 19 |
|
| 20 | +import com.google.api.services.gmail.Gmail; |
21 | 21 | import com.google.common.flogger.FluentLogger; |
| 22 | +import dagger.Lazy; |
| 23 | +import google.registry.config.RegistryConfig.Config; |
| 24 | +import google.registry.groups.GmailClient; |
22 | 25 | import google.registry.request.Action; |
23 | 26 | import google.registry.request.Parameter; |
24 | 27 | import google.registry.request.Response; |
25 | | -import google.registry.request.UrlConnectionService; |
26 | | -import google.registry.request.UrlConnectionUtils; |
27 | 28 | import google.registry.request.auth.Auth; |
| 29 | +import google.registry.util.EmailMessage; |
| 30 | +import google.registry.util.Retrier; |
28 | 31 | import jakarta.inject.Inject; |
29 | | -import java.net.URL; |
30 | | -import javax.net.ssl.HttpsURLConnection; |
| 32 | +import jakarta.mail.internet.AddressException; |
| 33 | +import jakarta.mail.internet.InternetAddress; |
31 | 34 |
|
32 | 35 | /** |
33 | 36 | * Action that executes a canned script specified by the caller. |
34 | 37 | * |
35 | 38 | * <p>This class provides a hook for invoking hard-coded methods. The main use case is to verify in |
36 | 39 | * Sandbox and Production environments new features that depend on environment-specific |
37 | | - * configurations. For example, the {@code DelegatedCredential}, which requires correct GWorkspace |
38 | | - * configuration, has been tested this way. Since it is a hassle to add or remove endpoints, we keep |
39 | | - * this class all the time. |
| 40 | + * configurations. |
40 | 41 | * |
41 | 42 | * <p>This action can be invoked using the Nomulus CLI command: {@code nomulus -e ${env} curl |
42 | | - * --service BACKEND -X POST -u '/_dr/task/executeCannedScript}'} |
| 43 | + * --service BACKEND -X POST -d 'sender=sender@example.com' -d 'receiver=receiver@example.com' -u |
| 44 | + * '/_dr/task/executeCannedScript'} |
43 | 45 | */ |
44 | 46 | @Action( |
45 | 47 | service = Action.Service.BACKEND, |
|
50 | 52 | public class CannedScriptExecutionAction implements Runnable { |
51 | 53 | private static final FluentLogger logger = FluentLogger.forEnclosingClass(); |
52 | 54 |
|
53 | | - @Inject UrlConnectionService urlConnectionService; |
| 55 | + @Inject Lazy<Gmail> gmail; |
| 56 | + @Inject Retrier retrier; |
| 57 | + |
| 58 | + @Inject |
| 59 | + @Config("isEmailSendingEnabled") |
| 60 | + boolean isEmailSendingEnabled; |
| 61 | + |
54 | 62 | @Inject Response response; |
55 | 63 |
|
56 | 64 | @Inject |
57 | | - @Parameter("url") |
58 | | - String url; |
| 65 | + @Parameter("sender") |
| 66 | + String sender; |
| 67 | + |
| 68 | + @Inject |
| 69 | + @Parameter("receiver") |
| 70 | + String receiver; |
59 | 71 |
|
60 | 72 | @Inject |
61 | 73 | CannedScriptExecutionAction() {} |
62 | 74 |
|
63 | 75 | @Override |
64 | 76 | public void run() { |
65 | | - Integer responseCode = null; |
66 | | - String responseContent = null; |
| 77 | + // For b/510340944, validating a new G Workspace user can send email. Code below can be |
| 78 | + // removed or changed afterward. |
67 | 79 | try { |
68 | | - logger.atInfo().log("Connecting to: %s", url); |
69 | | - HttpsURLConnection connection = |
70 | | - (HttpsURLConnection) urlConnectionService.createConnection(new URL(url)); |
71 | | - responseCode = connection.getResponseCode(); |
72 | | - logger.atInfo().log("Code: %d", responseCode); |
73 | | - logger.atInfo().log("Headers: %s", connection.getHeaderFields()); |
74 | | - responseContent = new String(UrlConnectionUtils.getResponseBytes(connection), UTF_8); |
75 | | - logger.atInfo().log("Response: %s", responseContent); |
| 80 | + logger.atInfo().log("Sending email from %s to %s", sender, receiver); |
| 81 | + GmailClient gmailClient = |
| 82 | + new GmailClient( |
| 83 | + gmail, retrier, isEmailSendingEnabled, sender, sender, new InternetAddress(sender)); |
| 84 | + gmailClient.sendEmail( |
| 85 | + EmailMessage.newBuilder() |
| 86 | + .addRecipient(new InternetAddress(receiver)) |
| 87 | + .setSubject(String.format("Email send test from %s", sender)) |
| 88 | + .setBody(String.format("This is a test email sent from %s to %s.", sender, receiver)) |
| 89 | + .build()); |
| 90 | + response.setPayload("Email sent successfully."); |
| 91 | + } catch (AddressException e) { |
| 92 | + logger.atWarning().withCause(e).log( |
| 93 | + "Invalid email address: sender=%s, receiver=%s", sender, receiver); |
| 94 | + response.setStatus(400); |
| 95 | + response.setPayload("Invalid email address provided."); |
76 | 96 | } catch (Exception e) { |
77 | | - logger.atWarning().withCause(e).log("Connection to %s failed", url); |
| 97 | + logger.atSevere().withCause(e).log("Failed to send email"); |
78 | 98 | throw new RuntimeException(e); |
79 | | - } finally { |
80 | | - if (responseCode != null) { |
81 | | - response.setStatus(responseCode); |
82 | | - } |
83 | | - if (responseContent != null) { |
84 | | - response.setPayload(responseContent); |
85 | | - } |
86 | 99 | } |
87 | 100 | } |
88 | 101 | } |
0 commit comments