public DNSLookupContinuation onDNSResponse()

in resolver/src/main/java/org/apache/james/jspf/terms/MXMechanism.java [93:158]


	public DNSLookupContinuation onDNSResponse(DNSResponse response, SPFSession spfSession)
        throws PermErrorException, TempErrorException, NoneException, NeutralException {
        try {
            
            List<String> records = (List<String>) spfSession.getAttribute(ATTRIBUTE_CHECK_RECORDS);
            List<String> mxR = (List<String>) spfSession.getAttribute(ATTRIBUTE_MX_RECORDS);

            if (records == null) {
            
                records = response.getResponse();

                if (records == null) {
                    // no mx record found
                    spfSession.setAttribute(Directive.ATTRIBUTE_MECHANISM_RESULT, Boolean.FALSE);
                    return null;
                }
                
                spfSession.setAttribute(ATTRIBUTE_CHECK_RECORDS, records);
                
            } else {
                
                List<String> res = response.getResponse();

                if (res != null) {
                    if (mxR == null) {
                        mxR = new ArrayList<String>();
                        spfSession.setAttribute(ATTRIBUTE_MX_RECORDS, mxR);
                    }
                    mxR.addAll(res);
                }
                
            }

            // if the remote IP is an ipv6 we check ipv6 addresses, otherwise ip4
            boolean isIPv6 = IPAddr.isIPV6(spfSession.getIpAddress());

            String mx;
            while (records.size() > 0 && (mx = records.remove(0)) != null && mx.length() > 0) {
                LOGGER.debug("Add MX-Record {} to list", mx);

                return new DNSLookupContinuation(new DNSRequest(mx, isIPv6 ? DNSRequest.AAAA : DNSRequest.A), MXMechanism.this);
                
            }
                
            // no mx record found
            if (mxR == null || mxR.size() == 0) {
                spfSession.setAttribute(Directive.ATTRIBUTE_MECHANISM_RESULT, Boolean.FALSE);
                return null;
            }

            // get the ipAddress
            IPAddr checkAddress;
            checkAddress = IPAddr.getAddress(spfSession.getIpAddress(), isIPv6 ? getIp6cidr() : getIp4cidr());
            
            // clean up attributes
            spfSession.removeAttribute(ATTRIBUTE_CHECK_RECORDS);
            spfSession.removeAttribute(ATTRIBUTE_MX_RECORDS);
            spfSession.setAttribute(Directive.ATTRIBUTE_MECHANISM_RESULT, Boolean.valueOf(checkAddressList(checkAddress, mxR, getIp4cidr())));
            return null;
            
        } catch (TimeoutException e) {
            spfSession.setAttribute(ATTRIBUTE_CHECK_RECORDS, null);
            spfSession.setAttribute(ATTRIBUTE_MX_RECORDS, null);
            throw new TempErrorException("Timeout querying the dns server");
        }
    }