/** * Retrieve the input attributes from the Active Directory for the given search query * * <p>Method getAttributes. * * @param searchBase String * @return List<User> * @throws NamingException */ private final List<User> getAttributes(String searchBase) throws NamingException { LOGGER.info(">> getAttributes()"); NamingEnumeration<SearchResult> results = localInitialLdapContext.search(searchBase, searchFilter, searchctls); List<User> users = new ArrayList<User>(); User user = null; while (results.hasMoreElements()) { user = new User(); SearchResult searchResult = results.next(); Attributes attrs = searchResult.getAttributes(); if (attrs != null && attrs.size() != 0) { Attribute attribute = null; String[] retrieveAttributes = parameters.getRetrieveAttributes(); String[] attributesValues = new String[retrieveAttributes.length]; for (int i = 0; i < retrieveAttributes.length; i++) { attribute = attrs.get(retrieveAttributes[i]); if (attribute != null && attribute.get() != null) { if (!isNullOrEmpty(attribute.get().toString())) { attributesValues[i] = attribute.get().toString(); } } } user.setAttributeValues(attributesValues); } users.add(user); } LOGGER.info("<< getAttributes()"); return users; }
/** * Constructs an Rdn from the given attribute set. See {@link javax.naming.directory.Attributes * Attributes}. * * <p>The string attribute values are not interpreted as <a * href="http://www.ietf.org/rfc/rfc2253.txt">RFC 2253</a> formatted RDN strings. That is, the * values are used literally (not parsed) and assumed to be unescaped. * * @param attrSet The non-null and non-empty attributes containing type/value mappings. * @throws InvalidNameException If contents of <tt>attrSet</tt> cannot be used to construct a * valid RDN. */ public Rdn(Attributes attrSet) throws InvalidNameException { if (attrSet.size() == 0) { throw new InvalidNameException("Attributes cannot be empty"); } entries = new ArrayList<>(attrSet.size()); NamingEnumeration<? extends Attribute> attrs = attrSet.getAll(); try { for (int nEntries = 0; attrs.hasMore(); nEntries++) { RdnEntry entry = new RdnEntry(); Attribute attr = attrs.next(); entry.type = attr.getID(); entry.value = attr.get(); entries.add(nEntries, entry); } } catch (NamingException e) { InvalidNameException e2 = new InvalidNameException(e.getMessage()); e2.initCause(e); throw e2; } sort(); // arrange entries for comparison }
/** * Send anonymous email. Note that although we could give any address as from address, (for * example: <b>'[email protected]' is valid</b>), the from of MailMessage should always be the correct format * of email address(for example the <b>'aaaa' is invalid</b>). Otherwise an exception would be * thrown say that username is invalid. * * @param mail The MailMessage object which contains at least all the required attributes to be * sent. */ public static void sendAnonymousEmail(MailMessage mail) { String dns = "dns://"; Hashtable<String, String> env = new Hashtable<String, String>(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.dns.DnsContextFactory"); env.put(Context.PROVIDER_URL, dns); String[] tos = mail.getTos(); try { DirContext ctx = new InitialDirContext(env); for (String to : tos) { String domain = to.substring(to.indexOf('@') + 1); // Get MX(Mail eXchange) records from DNS Attributes attrs = ctx.getAttributes(domain, new String[] {"MX"}); if (attrs == null || attrs.size() <= 0) { throw new java.lang.IllegalStateException( "Error: Your DNS server has no Mail eXchange records!"); } @SuppressWarnings("rawtypes") NamingEnumeration servers = attrs.getAll(); String smtpHost = null; boolean isSend = false; StringBuffer buf = new StringBuffer(); // try all the mail exchange server to send the email. while (servers.hasMore()) { Attribute hosts = (Attribute) servers.next(); for (int i = 0; i < hosts.size(); ++i) { // sample: 20 mx2.qq.com smtpHost = (String) hosts.get(i); // parse the string to get smtpHost. sample: mx2.qq.com smtpHost = smtpHost.substring(smtpHost.lastIndexOf(' ') + 1); try { sendEmail(smtpHost, mail, true); isSend = true; return; } catch (Exception e) { LOGGER.error("", e); buf.append(e.toString()).append("\r\n"); continue; } } } if (!isSend) { throw new java.lang.IllegalStateException("Error: Send email error." + buf.toString()); } } } catch (NamingException e) { LOGGER.error("", e); } }
/** Returns an unmodifiable Map of the header fields. */ @Override public Map<String, List<String>> getHeaderFields() { if (!connected) { // Try to connect (silently) try { connect(); } catch (IOException e) { // Ignore } } if (attributes == null) return (Collections.emptyMap()); HashMap<String, List<String>> headerFields = new HashMap<String, List<String>>(attributes.size()); NamingEnumeration<String> attributeEnum = attributes.getIDs(); try { while (attributeEnum.hasMore()) { String attributeID = attributeEnum.next(); Attribute attribute = attributes.get(attributeID); if (attribute == null) continue; ArrayList<String> attributeValueList = new ArrayList<String>(attribute.size()); NamingEnumeration<?> attributeValues = attribute.getAll(); while (attributeValues.hasMore()) { Object attrValue = attributeValues.next(); attributeValueList.add(getHeaderValueAsString(attrValue)); } attributeValueList.trimToSize(); // should be a no-op if attribute.size() didn't lie headerFields.put(attributeID, Collections.unmodifiableList(attributeValueList)); } } catch (NamingException ne) { // Shouldn't happen } return Collections.unmodifiableMap(headerFields); }
/** * This compares all but the named attributes allbut true => All must be equal except those on the * list * * @param that * @param attrIDs * @return boolean * @throws Throwable */ public boolean equalsAllBut(DirRecord that, String[] attrIDs) throws Throwable { if (attrIDs == null) throw new Exception("DirectoryRecord: null attrID list"); if (!dnEquals(that)) { return false; } int n = attrIDs.length; if (n == 0) return true; Attributes thisAttrs = getAttributes(); Attributes thatAttrs = that.getAttributes(); if (thisAttrs == null) { if (thatAttrs == null) return true; return false; } if (thatAttrs == null) { return false; } /** * We need to ensure that all attributes are checked. We init thatLeft to the number of * attributes in the source. We decrement for each checked attribute. We then decrement for each * ignored attribute present in that If the result is non-zero, then there are some extra * attributes in that so we return unequal. */ int sz = thisAttrs.size(); int thatLeft = sz; if ((sz == 0) && (thatAttrs.size() == 0)) { return true; } NamingEnumeration ne = thisAttrs.getAll(); if (ne == null) { return false; } while (ne.hasMore()) { Attribute attr = (Attribute) ne.next(); String id = attr.getID(); boolean present = false; for (int i = 0; i < attrIDs.length; i++) { if (id.equalsIgnoreCase(attrIDs[i])) { present = true; break; } } if (present) { // We don't compare if (thatAttrs.get(id) != null) thatLeft--; } else { Attribute thatAttr = thatAttrs.get(id); if (thatAttr == null) { return false; } if (!thatAttr.contains(attr)) { return false; } thatLeft--; } } return (thatLeft == 0); }