Example usage for javax.naming.directory Attribute get

List of usage examples for javax.naming.directory Attribute get

Introduction

In this page you can find the example usage for javax.naming.directory Attribute get.

Prototype

Object get(int ix) throws NamingException;

Source Link

Document

Retrieves the attribute value from the ordered list of attribute values.

Usage

From source file:net.spfbl.spf.SPF.java

/**
 * Consulta o registro SPF nos registros DNS do domnio. Se houver mais de
 * dois registros diferentes, realiza o merge do forma a retornar um nico
 * registro.//from   ww  w. j a  va2 s .  co  m
 *
 * @param hostname o nome do hostname para consulta do SPF.
 * @param bgWhenUnavailable usar best-guess quando houver erro temporrio
 * para alcanar o registro.
 * @return o registro SPF consertado, padronuzado e mergeado.
 * @throws ProcessException
 */
private static LinkedList<String> getRegistrySPF(String hostname, boolean bgWhenUnavailable)
        throws ProcessException {
    LinkedList<String> registryList = new LinkedList<String>();
    try {
        //            if (CacheGuess.containsExact(hostname)) {
        //                // Sempre que houver registro de
        //                // chute, sobrepor registro atual.
        //                registryList.add(CacheGuess.get(hostname));
        //            } else {
        //                // Caso contrrio procurar nos
        //                // registros oficiais do domnio.
        try {
            Attributes attributes = Server.getAttributesDNS(hostname, new String[] { "SPF" });
            Attribute attribute = attributes.get("SPF");
            if (attribute != null) {
                for (int index = 0; index < attribute.size(); index++) {
                    String registry = (String) attribute.get(index);
                    if (registry.contains("v=spf1 ")) {
                        registry = fixRegistry(registry);
                        if (!registryList.contains(registry)) {
                            registryList.add(registry);
                        }
                    }
                }
            }
        } catch (InvalidAttributeIdentifierException ex) {
            // No encontrou registro SPF.
        }
        if (registryList.isEmpty()) {
            try {
                Attributes attributes = Server.getAttributesDNS(hostname, new String[] { "TXT" });
                Attribute attribute = attributes.get("TXT");
                if (attribute != null) {
                    for (int index = 0; index < attribute.size(); index++) {
                        String registry = (String) attribute.get(index);
                        if (registry.contains("v=spf1 ")) {
                            registry = fixRegistry(registry);
                            if (!registryList.contains(registry)) {
                                registryList.add(registry);
                            }
                        }
                    }

                }
            } catch (InvalidAttributeIdentifierException ex2) {
                // No encontrou registro TXT.
            }
        }
        //            }
        if (registryList.isEmpty()) {
            //                hostname = "." + hostname;
            //                if (CacheGuess.containsExact(hostname)) {
            //                    // Significa que um palpite SPF
            //                    // foi registrado para este hostname.
            //                    // Neste caso utilizar o paltpite especfico.
            //                    registryList.add(CacheGuess.get(hostname));
            //                } else {
            //                    // Se no hoouver palpite especfico para o hostname,
            //                    // utilizar o palpite padro, porm adaptado para IPv6.
            //                    // http://www.openspf.org/FAQ/Best_guess_record
            //                    registryList.add(CacheGuess.BEST_GUESS);
            //                }

            // Como o domnio no tem registro SPF,
            // utilizar um registro SPF de chute do sistema.
            String guess = CacheGuess.get(hostname);
            registryList.add(guess);
        }
        return registryList;
    } catch (NameNotFoundException ex) {
        return null;
    } catch (NamingException ex) {
        if (bgWhenUnavailable) {
            // Na indisponibilidade do DNS
            // utilizar um registro SPF de chute do sistema.
            String guess = CacheGuess.get(hostname);
            registryList.add(guess);
            return registryList;
        } else if (ex instanceof CommunicationException) {
            throw new ProcessException("ERROR: DNS UNAVAILABLE");
        } else {
            throw new ProcessException("ERROR: DNS UNAVAILABLE", ex);
        }
    } catch (Exception ex) {
        throw new ProcessException("ERROR: FATAL", ex);
    }
}