protected boolean matchedWhitelist()

in server/mailet/mailets/src/main/java/org/apache/james/transport/matchers/IsInWhiteList.java [81:178]


    protected boolean matchedWhitelist(MailAddress recipientMailAddress, Mail mail) throws MessagingException {
        if (!mail.hasSender()) {
            return true;
        }
        MailAddress senderMailAddress = mail.getMaybeSender().get();
        String senderUser = senderMailAddress.getLocalPart().toLowerCase(Locale.US);
        Domain senderHost = senderMailAddress.getDomain();

        Connection conn = null;
        PreparedStatement selectStmt = null;
        ResultSet selectRS = null;
        try {
            String recipientUser = recipientMailAddress.getLocalPart().toLowerCase(Locale.US);
            Domain recipientHost = recipientMailAddress.getDomain();

            if (conn == null) {
                conn = datasource.getConnection();
            }

            try {
                if (selectStmt == null) {
                    selectStmt = conn.prepareStatement(selectByPK);
                }
                selectStmt.setString(1, recipientUser);
                selectStmt.setString(2, recipientHost.asString());
                selectStmt.setString(3, senderUser);
                selectStmt.setString(4, senderHost.asString());
                selectRS = selectStmt.executeQuery();
                if (selectRS.next()) {
                    // This address was already in the list
                    return true;
                }
            } finally {
                jdbcUtil.closeJDBCResultSet(selectRS);
                jdbcUtil.closeJDBCStatement(selectStmt);
            }
            
            try {
                // check for wildcard domain entries
                selectStmt = conn.prepareStatement(selectByPK);
    
                selectStmt.setString(1, recipientUser);
                selectStmt.setString(2, recipientHost.asString());
                selectStmt.setString(3, "*");
                selectStmt.setString(4, senderHost.asString());
                selectRS = selectStmt.executeQuery();
                if (selectRS.next()) {
                    // This address was already in the list
                    return true;
                }
            } finally {
                jdbcUtil.closeJDBCResultSet(selectRS);
                jdbcUtil.closeJDBCStatement(selectStmt);
            }

            try {
                // check for wildcard recipient domain entries
                selectStmt = conn.prepareStatement(selectByPK);
    
                selectStmt.setString(1, "*");
                selectStmt.setString(2, recipientHost.asString());
                selectStmt.setString(3, senderUser);
                selectStmt.setString(4, senderHost.asString());
                selectRS = selectStmt.executeQuery();
                if (selectRS.next()) {
                    // This address was already in the list
                    return true;
                }
            } finally {
                jdbcUtil.closeJDBCResultSet(selectRS);
                jdbcUtil.closeJDBCStatement(selectStmt);
            }

            try {
                // check for wildcard domain entries on both
                selectStmt = conn.prepareStatement(selectByPK);
    
                selectStmt.setString(1, "*");
                selectStmt.setString(2, recipientHost.asString());
                selectStmt.setString(3, "*");
                selectStmt.setString(4, senderHost.asString());
                selectRS = selectStmt.executeQuery();
                if (selectRS.next()) {
                    // This address was already in the list
                    return true;
                }
            } finally {
                jdbcUtil.closeJDBCResultSet(selectRS);
                jdbcUtil.closeJDBCStatement(selectStmt);
            }
        } catch (SQLException sqle) {
            LOGGER.error("Error accessing database", sqle);
            throw new MessagingException("Exception thrown", sqle);
        } finally {
            theJDBCUtil.closeJDBCConnection(conn);
        }
        return false;
    }