From 5186190ce963888ea46e705e691cdd7a17409937 Mon Sep 17 00:00:00 2001 From: Peter Hormanns Date: Tue, 3 Dec 2019 20:12:36 +0100 Subject: [PATCH] extract method --- .../AutoconfigAutodiscoverServlet.java | 97 +++++++++++-------- 1 file changed, 55 insertions(+), 42 deletions(-) diff --git a/hsarback/src/de/hsadmin/autoconfig/AutoconfigAutodiscoverServlet.java b/hsarback/src/de/hsadmin/autoconfig/AutoconfigAutodiscoverServlet.java index 4f0d3b0..1a4d1a1 100644 --- a/hsarback/src/de/hsadmin/autoconfig/AutoconfigAutodiscoverServlet.java +++ b/hsarback/src/de/hsadmin/autoconfig/AutoconfigAutodiscoverServlet.java @@ -22,10 +22,10 @@ import org.w3c.dom.DOMException; import org.w3c.dom.Document; import org.w3c.dom.Element; +import de.hsadmin.core.model.HSAdminException; import de.hsadmin.core.model.Transaction; import de.hsadmin.core.util.IPv6Trick; import de.hsadmin.mods.email.EMailAddress; -import de.hsadmin.mods.pac.Pac; import de.hsadmin.mods.user.UnixUser; public class AutoconfigAutodiscoverServlet extends HttpServlet { @@ -47,55 +47,31 @@ public class AutoconfigAutodiscoverServlet extends HttpServlet { resp.sendError(HttpStatus.SC_BAD_REQUEST); return; } - 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(); - 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(); + String uniqueMailbox = null; try { - DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); - DocumentBuilder builder = builderFactory.newDocumentBuilder(); - Document document = builder.newDocument(); - Element clientConfig = document.createElement("clientConfig"); + uniqueMailbox = getUniqueMailbox(emailAddr); + } catch (HSAdminException e) { + throw new ServletException(e); + } + 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"); 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"); - 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, "port", "993"); appendElementWithText(document, incomingServer, "socketType", "SSL"); appendElementWithText(document, incomingServer, "username", userName); 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, "port", "465"); appendElementWithText(document, outgoingServer, "socketType", "SSL"); @@ -109,7 +85,6 @@ public class AutoconfigAutodiscoverServlet extends HttpServlet { } catch (ParserConfigurationException e) { throw new ServletException(e); } - transaction.close(); } /** @@ -153,4 +128,42 @@ public class AutoconfigAutodiscoverServlet extends HttpServlet { 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; + } }