-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy pathMain.java
More file actions
127 lines (110 loc) · 4.68 KB
/
Main.java
File metadata and controls
127 lines (110 loc) · 4.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package io.openruntimes.java.src;
import io.openruntimes.java.RuntimeContext;
import io.openruntimes.java.RuntimeOutput;
import java.util.Map;
import java.util.List;
import java.util.HashMap;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Collections;
import java.net.URI;
import java.net.URISyntaxException;
import io.appwrite.Client;
import jakarta.mail.MessagingException;
public class Main {
public RuntimeOutput main(RuntimeContext context) throws Exception {
List<String> requiredEnvVariables = Arrays.asList(
"SUBMIT_EMAIL",
"SMTP_HOST",
"SMTP_USERNAME",
"SMTP_PASSWORD"
);
Utils.throwIfMissing(System.getenv(), requiredEnvVariables);
if (System.getenv("ALLOWED_ORIGINS").equals("*")) {
context.log("WARNING: Allowing requests from any origin - this is a security risk!");
}
if (context.getReq().getMethod().equals("GET")) {
return context.getRes().send(
Utils.getHtmlContent("index.html"),
200,
Map.of("content-type", "text/html")
);
}
if(!context.getReq().getHeaders().get("content-type").equals("application/x-www-form-urlencoded")){
context.error("Incorrect content type");
String referer = context.getReq().getHeaders().get("referer");
return context.getRes().redirect(
String.format("%s?code=%s", referer, ErrorCode.INVALID_REQUEST)
);
}
if(!Cors.isOriginPermitted(context)){
context.error("Origin not permitted");
String referer = context.getReq().getHeaders().get("referer");
return context.getRes().redirect(
String.format("%s?code=%s", referer, ErrorCode.INVALID_REQUEST)
);
}
Map<String, List<String>> formData = new HashMap<>();
String body = (String) context.getReq().getBody();
String[] params = body.split("&");
for (String param : params) {
String[] keyValue = param.split("=");
String key = keyValue[0];
String value = keyValue[1];
if (!formData.containsKey(key)) {
formData.put(key, new ArrayList<>());
}
formData.get(key).add(value);
}
Map<String, String> form = new HashMap<>();
for (Map.Entry<String, List<String>> entry : formData.entrySet()) {
form.put(entry.getKey(), entry.getValue().get(0));
}
try {
Utils.throwIfMissing(form, Collections.singletonList("email"));
} catch (IllegalArgumentException ex) {
String referer = context.getReq().getHeaders().get("referer");
return context.getRes().redirect(
String.format("%s?code=%s", referer, ErrorCode.MISSING_FORM_FIELDS),
301,
Cors.getCorsHeaders(context)
);
}
try {
Map<String, String> emailOptions = new HashMap<>();
emailOptions.put("from", System.getenv("SMTP_USERNAME"));
emailOptions.put("to", System.getenv("SUBMIT_EMAIL"));
emailOptions.put("subject", "New Contact Form Submission");
emailOptions.put("text", Utils.templateFormMessage(form));
Utils.sendEmail(emailOptions);
} catch (MessagingException ex) {
context.log("MessagingException: " + ex.getMessage());
String referer = context.getReq().getHeaders().get("referer");
return context.getRes().redirect(
String.format("%s?code=%s", referer, ErrorCode.SERVER_ERROR),
301,
Cors.getCorsHeaders(context)
);
} catch (Exception ex) {
context.log("Exception: " + ex.getMessage());
String referer = context.getReq().getHeaders().get("referer");
return context.getRes().redirect(
String.format("%s?code=%s", referer, ErrorCode.SERVER_ERROR),
301,
Cors.getCorsHeaders(context)
);
}
if (form.get("_next") == null || form.get("_next").isEmpty()) {
return context.getRes().send(
Utils.getHtmlContent("success.html"),
200,
Map.of("content-type", "text/html; charset=utf-8")
);
}
return context.getRes().redirect(
Utils.joinURL(context.getReq().getHeaders().get("referer"), form.get("_next").substring(0,1)),
301,
Cors.getCorsHeaders(context)
);
}
}