extract method

This commit is contained in:
Peter Hormanns 2019-12-03 20:12:36 +01:00
parent a759d312fc
commit 5186190ce9

View File

@ -22,10 +22,10 @@ import org.w3c.dom.DOMException;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import org.w3c.dom.Element; import org.w3c.dom.Element;
import de.hsadmin.core.model.HSAdminException;
import de.hsadmin.core.model.Transaction; import de.hsadmin.core.model.Transaction;
import de.hsadmin.core.util.IPv6Trick; import de.hsadmin.core.util.IPv6Trick;
import de.hsadmin.mods.email.EMailAddress; import de.hsadmin.mods.email.EMailAddress;
import de.hsadmin.mods.pac.Pac;
import de.hsadmin.mods.user.UnixUser; import de.hsadmin.mods.user.UnixUser;
public class AutoconfigAutodiscoverServlet extends HttpServlet { public class AutoconfigAutodiscoverServlet extends HttpServlet {
@ -47,55 +47,31 @@ public class AutoconfigAutodiscoverServlet extends HttpServlet {
resp.sendError(HttpStatus.SC_BAD_REQUEST); resp.sendError(HttpStatus.SC_BAD_REQUEST);
return; return;
} }
final String localpart = addrParts[0]; String uniqueMailbox = null;
final String domain = addrParts[1];
final Transaction transaction = new Transaction("autoconfig");
final EntityManager em = transaction.getEntityManager();
final Query emailQuery = em.createQuery("SELECT addr FROM EMailAddresses addr WHERE addr.localpart = :localpart AND addr.domain.name = :domain AND ( addr.subdomain IS NULL OR addr.subdomain = :subdomain)");
emailQuery.setParameter("subdomain", "");
emailQuery.setParameter("domain", domain);
emailQuery.setParameter("localpart", localpart);
final Object emailResult = emailQuery.getSingleResult();
if (emailResult == null) {
transaction.close();
resp.sendError(HttpStatus.SC_BAD_REQUEST);
return;
}
final EMailAddress emailAddress = (EMailAddress) emailResult;
final Query mboxQuery = em.createQuery("SELECT mbox FROM UnixUsers mbox WHERE mbox.name = :target");
final String target = emailAddress.getTarget();
if (target == null || target.length() < 5 || (target.length() > 6 && target.charAt(5) != '-')) {
transaction.close();
resp.sendError(HttpStatus.SC_BAD_REQUEST);
return;
}
mboxQuery.setParameter("target", target);
final Object mboxResult = mboxQuery.getSingleResult();
if (mboxResult == null) {
transaction.close();
resp.sendError(HttpStatus.SC_BAD_REQUEST);
return;
}
final UnixUser mbox = (UnixUser) mboxResult;
final Pac pac = mbox.getPac();
final String pacDomain = pac.getName() + ".hostsharing.net";
final String userName = mbox.getName();
try { try {
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); uniqueMailbox = getUniqueMailbox(emailAddr);
DocumentBuilder builder = builderFactory.newDocumentBuilder(); } catch (HSAdminException e) {
Document document = builder.newDocument(); throw new ServletException(e);
Element clientConfig = document.createElement("clientConfig"); }
final String pacName = uniqueMailbox.substring(0, 5);
final String pacDomain = pacName + ".hostsharing.net";
final String userName = uniqueMailbox;
try {
final DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
final DocumentBuilder builder = builderFactory.newDocumentBuilder();
final Document document = builder.newDocument();
final Element clientConfig = document.createElement("clientConfig");
clientConfig.setAttribute("version", "1.1"); clientConfig.setAttribute("version", "1.1");
document.appendChild(clientConfig); document.appendChild(clientConfig);
Element emailProvider = appendElementWithAttribute(document, clientConfig, "emailProvider", "id", "hostsharing.net"); final Element emailProvider = appendElementWithAttribute(document, clientConfig, "emailProvider", "id", "hostsharing.net");
appendElementWithText(document, emailProvider, "domain", "hostsharing.net"); appendElementWithText(document, emailProvider, "domain", "hostsharing.net");
Element incomingServer = appendElementWithAttribute(document, emailProvider, "incomingServer", "type", "imap"); final Element incomingServer = appendElementWithAttribute(document, emailProvider, "incomingServer", "type", "imap");
appendElementWithText(document, incomingServer, "hostname", pacDomain); appendElementWithText(document, incomingServer, "hostname", pacDomain);
appendElementWithText(document, incomingServer, "port", "993"); appendElementWithText(document, incomingServer, "port", "993");
appendElementWithText(document, incomingServer, "socketType", "SSL"); appendElementWithText(document, incomingServer, "socketType", "SSL");
appendElementWithText(document, incomingServer, "username", userName); appendElementWithText(document, incomingServer, "username", userName);
appendElementWithText(document, incomingServer, "authentication", "password-cleartext"); appendElementWithText(document, incomingServer, "authentication", "password-cleartext");
Element outgoingServer = appendElementWithAttribute(document, emailProvider, "outgoingServer", "type", "smtp"); final Element outgoingServer = appendElementWithAttribute(document, emailProvider, "outgoingServer", "type", "smtp");
appendElementWithText(document, outgoingServer, "hostname", pacDomain); appendElementWithText(document, outgoingServer, "hostname", pacDomain);
appendElementWithText(document, outgoingServer, "port", "465"); appendElementWithText(document, outgoingServer, "port", "465");
appendElementWithText(document, outgoingServer, "socketType", "SSL"); appendElementWithText(document, outgoingServer, "socketType", "SSL");
@ -109,7 +85,6 @@ public class AutoconfigAutodiscoverServlet extends HttpServlet {
} catch (ParserConfigurationException e) { } catch (ParserConfigurationException e) {
throw new ServletException(e); throw new ServletException(e);
} }
transaction.close();
} }
/** /**
@ -153,4 +128,42 @@ public class AutoconfigAutodiscoverServlet extends HttpServlet {
throw new ServletException(e); throw new ServletException(e);
} }
} }
private String getUniqueMailbox(final String email) throws HSAdminException {
final String[] addrParts = email.split("@");
if (addrParts.length != 2) {
throw new HSAdminException("email address not valid");
}
final String localpart = addrParts[0];
final String domain = addrParts[1];
final Transaction transaction = new Transaction("autoconfig");
final EntityManager em = transaction.getEntityManager();
final Query emailQuery = em.createQuery("SELECT addr FROM EMailAddresses addr "
+ "WHERE addr.localpart = :localpart AND addr.domain.name = :domain AND ( addr.subdomain IS NULL OR addr.subdomain = :subdomain)");
emailQuery.setParameter("subdomain", "");
emailQuery.setParameter("domain", domain);
emailQuery.setParameter("localpart", localpart);
final Object emailResult = emailQuery.getSingleResult();
if (emailResult == null) {
transaction.close();
throw new HSAdminException("email address unknown");
}
final EMailAddress emailAddress = (EMailAddress) emailResult;
final String target = emailAddress.getTarget();
if (target == null || target.length() < 5 || (target.length() > 6 && target.charAt(5) != '-')) {
transaction.close();
throw new HSAdminException("no unique mailbox found");
}
final Query mboxQuery = em.createQuery("SELECT mbox FROM UnixUsers mbox WHERE mbox.name = :target");
mboxQuery.setParameter("target", target);
final Object mboxResult = mboxQuery.getSingleResult();
if (mboxResult == null || !(mboxQuery instanceof UnixUser)) {
transaction.close();
throw new HSAdminException("no unique mailbox found");
}
final UnixUser mbox = (UnixUser) mboxResult;
final String uniqueUsername = mbox.getName();
transaction.close();
return uniqueUsername;
}
} }