Refactor form submission to use dynamic Discord URLs and emails
Updated form classes to return Optional URLs for Discord bot submissions. Refactored VerifyController to handle these Optionals and improved error handling when sending forms. Added receiver email method in form classes for more flexible form submissions.
This commit is contained in:
@@ -6,6 +6,7 @@ import org.hibernate.validator.constraints.Length;
|
||||
import org.hibernate.validator.constraints.Range;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.Optional;
|
||||
|
||||
public class StaffAppFormData extends Form {
|
||||
|
||||
@@ -109,8 +110,13 @@ public class StaffAppFormData extends Form {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDiscordBotUrl() {
|
||||
return "http://discordbot:8001/api/apply/staffApplication";
|
||||
public Optional<String> getDiscordBotUrl() {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getReceiver() {
|
||||
return "[email protected]";
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -6,6 +6,8 @@ import jakarta.validation.constraints.NotEmpty;
|
||||
import jakarta.validation.constraints.Pattern;
|
||||
import org.hibernate.validator.constraints.Length;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public class ContactFormData extends Form {
|
||||
|
||||
public ContactFormData(String username, String email, String question) {
|
||||
@@ -37,8 +39,13 @@ public class ContactFormData extends Form {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDiscordBotUrl() {
|
||||
return "http://discordbot:8001/api/contact/submitContactForm";
|
||||
public Optional<String> getDiscordBotUrl() {
|
||||
return Optional.of("http://discordbot:8001/api/contact/submitContactForm");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getReceiver() {
|
||||
return "[email protected]";
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -4,6 +4,8 @@ import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public abstract class Form {
|
||||
|
||||
public String toJsonString() throws JsonProcessingException {
|
||||
@@ -36,5 +38,7 @@ public abstract class Form {
|
||||
@Override
|
||||
public abstract String toString();
|
||||
|
||||
public abstract String getDiscordBotUrl();
|
||||
public abstract Optional<String> getDiscordBotUrl();
|
||||
|
||||
public abstract String getReceiver();
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.alttd.forms.verify_mail;
|
||||
|
||||
import com.alttd.forms.form.Form;
|
||||
import com.alttd.forms.mail.mail_forms.MailForm;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import jakarta.validation.Valid;
|
||||
@@ -18,6 +19,7 @@ import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
@RestController
|
||||
@@ -32,34 +34,14 @@ public class VerifyController {
|
||||
logger.trace("verificationData: {}", verificationData);
|
||||
return new FormQuery().getFormForCode(verificationData.code, verificationData.eMail).thenApply(result -> result.form()
|
||||
.map(form -> {
|
||||
try {
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
//Discord bot url
|
||||
.uri(new URI(form.getDiscordBotUrl()))//TODO get uri from form
|
||||
.header("Content-Type", "application/json;charset=UTF-8")
|
||||
.POST(HttpRequest.BodyPublishers.ofString(form.toJsonString(), StandardCharsets.UTF_8))
|
||||
.build();
|
||||
logger.trace("request: {}", request);
|
||||
HttpClient client = HttpClient.newHttpClient();
|
||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
logger.trace("response: {}", response);
|
||||
if (response.statusCode() < 200 || response.statusCode() > 200) {
|
||||
logger.error(String.format("Failed to send form to Discord. Got status code [%d], with body\n%s", response.statusCode(), response.body()));
|
||||
//TODO handle failure
|
||||
//TODO strings to config
|
||||
return ResponseEntity.ok("Failed to send form to Discord, please contact us at [email protected]");
|
||||
Optional<String> discordBotUrl = form.getDiscordBotUrl();
|
||||
if (discordBotUrl.isPresent()){
|
||||
Optional<ResponseEntity<String>> stringResponseEntity = sendDiscordMessage(discordBotUrl.get(), form);
|
||||
if (stringResponseEntity.isPresent()){
|
||||
return stringResponseEntity.get();
|
||||
}
|
||||
} catch (URISyntaxException e) {
|
||||
logger.error("Unable to create URI for posting form", e); //TODO more clear
|
||||
return ResponseEntity.ok("Unable to create URI for posting form, please contact us at [email protected]");
|
||||
} catch (JsonProcessingException e) {
|
||||
logger.error("Unable to send form to Discord, invalid JSON", e);
|
||||
return ResponseEntity.ok("Unable to send form to Discord, please contact us at [email protected]");
|
||||
} catch (IOException | InterruptedException e) {
|
||||
logger.error("Unable to send form to Discord", e); //TODO more clear
|
||||
return ResponseEntity.ok("Unable to send form to Discord, please contact us at [email protected]");
|
||||
}
|
||||
MailForm.sendForm("[email protected]", form);
|
||||
MailForm.sendForm(form.getReceiver(), form);
|
||||
try {
|
||||
return ResponseEntity.ok(form.toJsonString());
|
||||
} catch (JsonProcessingException e) {
|
||||
@@ -71,4 +53,34 @@ public class VerifyController {
|
||||
).exceptionally(throwable -> ResponseEntity.internalServerError()
|
||||
.body("The server was unable to process your request, if this issue persists please contact [email protected]"));
|
||||
}
|
||||
|
||||
private Optional<ResponseEntity<String>> sendDiscordMessage(String discordBotUrl, Form form) {
|
||||
try {
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(new URI(discordBotUrl))
|
||||
.header("Content-Type", "application/json;charset=UTF-8")
|
||||
.POST(HttpRequest.BodyPublishers.ofString(form.toJsonString(), StandardCharsets.UTF_8))
|
||||
.build();
|
||||
logger.trace("request: {}", request);
|
||||
HttpClient client = HttpClient.newHttpClient();
|
||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
logger.trace("response: {}", response);
|
||||
if (response.statusCode() < 200 || response.statusCode() > 200) {
|
||||
logger.error(String.format("Failed to send form to Discord. Got status code [%d], with body\n%s", response.statusCode(), response.body()));
|
||||
//TODO handle failure
|
||||
//TODO strings to config
|
||||
return Optional.of(ResponseEntity.ok("Failed to send form to Discord, please contact us at [email protected]"));
|
||||
}
|
||||
} catch (URISyntaxException e) {
|
||||
logger.error("Unable to create URI for posting form", e); //TODO more clear
|
||||
return Optional.of(ResponseEntity.ok("Unable to create URI for posting form, please contact us at [email protected]"));
|
||||
} catch (JsonProcessingException e) {
|
||||
logger.error("Unable to send form to Discord, invalid JSON", e);
|
||||
return Optional.of(ResponseEntity.ok("Unable to send form to Discord, please contact us at [email protected]"));
|
||||
} catch (IOException | InterruptedException e) {
|
||||
logger.error("Unable to send form to Discord", e); //TODO more clear
|
||||
return Optional.of(ResponseEntity.ok("Unable to send form to Discord, please contact us at [email protected]"));
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user