working on subscription

This commit is contained in:
2022-04-16 18:09:13 +02:00
parent 853926e0f7
commit 1282064565
5 changed files with 58 additions and 9 deletions

View File

@@ -17,6 +17,7 @@ import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.security.InvalidKeyException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
@@ -40,6 +41,7 @@ public class Web extends HttpServlet {
private static final String SMTP_PORT = "smtp_port";
private static final String SMTP_USER = "smtp_user";
private static final String SMTP_PASS = "smtp_pass";
private static final int PRIMARY_KEY_CONSTRAINT = 19;
private final String baseDir;
private STGroup templates;
private static final String WEB_ROOT = "/web";
@@ -312,7 +314,6 @@ public class Web extends HttpServlet {
var data = new HashMap<String,String>();
data.put(NAME,name);
data.put(EMAIL,email);
data.put(PASSWORD,pass);
data.put(LIST,list);
if (list == null || list.isBlank()){
data.put(ERROR,"No list provided by form data!");
@@ -323,6 +324,39 @@ public class Web extends HttpServlet {
data.put(ERROR,"Name and email are required fields for list subscription!");
return loadTemplate(SUBSCRIBE,data,resp);
}
return "not implemented";
if (pass != null && pass.isBlank()) pass = null;
try {
User.create(email,name,pass);
} catch (SQLException sqle) {
var cause = getCausingException(sqle);
int code = cause.getErrorCode();
if (code == PRIMARY_KEY_CONSTRAINT) try {// user already exists
User.load(email,pass);
// success → subscribe
} catch (InvalidKeyException | SQLException e) {
// invalid credentials
data.put(ERROR,t("'{}' already in database, but with different password!",email));
return loadTemplate(SUBSCRIBE,data,resp);
}
}
try {
ListMember.create(list,email,ListMember.STATE_SUBSCRIBER);
data.put(NOTES,t("Successfully subscribed '{}' to '{}'.",email,list));
return loadTemplate(INDEX,data,resp);
} catch (SQLException e) {
LOG.debug("List subscription failed: ",e);
data.put(ERROR,t("Subscription failed: {}",e.getMessage()));
return loadTemplate(SUBSCRIBE,data,resp);
}
}
private SQLException getCausingException(SQLException sqle) {
Throwable cause = sqle.getCause();
while (cause instanceof SQLException){
sqle = (SQLException) cause;
cause = sqle.getCause();
}
return sqle;
}
}