001: /***
002: * jwma Java WebMail
003: * Copyright (c) 2000-2003 jwma team
004: *
005: * jwma is free software; you can distribute and use this source
006: * under the terms of the BSD-style license received along with
007: * the distribution.
008: ***/package dtw.webmail.directory;
009:
010: import java.util.*;
011: import java.text.*;
012: import javax.naming.*;
013: import javax.naming.directory.*;
014: import javax.mail.internet.InternetAddress;
015:
016: import dtw.webmail.model.JwmaException;
017: import dtw.webmail.util.*;
018:
019: /**
020: * Class implementing utility methods for using
021: * the directory framework.
022: * <p>
023: * <b>This class is under construction.</b>
024: *
025: * @author Dieter Wimberger
026: * @version 0.9.7 07/02/2003
027: */
028: public class DirectoryUtil {
029:
030: //instance attributes
031: DirectoryManager m_DirectoryManager;
032: SearchControls m_ValidateControls;
033:
034: /**
035: * Constructs a new <tt>DirectoryUtil</tt> instance.
036: */
037: public DirectoryUtil(DirectoryManager mgr) {
038: m_DirectoryManager = mgr;
039: m_ValidateControls = new SearchControls();
040: }//constructor
041:
042: public boolean validateRecipient(String address)
043: throws JwmaException {
044:
045: boolean result = false;
046:
047: try {
048: InternetAddress iadd = new InternetAddress(address);
049: String[] addrparts = StringUtil.split(iadd.getAddress(),
050: "@");
051: ContextPool ctxp = m_DirectoryManager
052: .getValidationContextPool(addrparts[1]);
053: DirContext ctx = null;
054:
055: try {
056: ctx = ctxp.leaseContext();
057: //prepare fill in
058: Object[] obj = { addrparts[0], addrparts[1], address };
059: String filter = "("
060: + MessageFormat.format(ctxp.getBaseFilter(),
061: obj) + ")";
062: //search entry
063: NamingEnumeration ne = ctx.search(ctxp.getSearchName(),
064: filter, m_ValidateControls);
065: //return if an entry was found
066: result = ne.hasMore();
067: } catch (NamingException nex) {
068: throw new JwmaException(nex.getMessage(), true)
069: .setException(nex);
070: } finally {
071: ctxp.releaseContext(ctx);
072: }
073:
074: } catch (Exception ex) {
075:
076: }
077: return result;
078: }//validateRecipient
079:
080: public List lookupEntry(String lookupname, String filter) {
081: List entries = new ArrayList(25);
082: try {
083: ContextPool ctxp = m_DirectoryManager
084: .getLookupContextPool(lookupname);
085: DirContext ctx = null;
086:
087: try {
088: ctx = ctxp.leaseContext();
089: //set search controls, including returning attributes
090: SearchControls searchControls = new SearchControls();
091: searchControls.setReturningAttributes(ctxp
092: .getResultAttributes());
093: //makeup filter logically & with the base filter if given
094: String baseFilter = ctxp.getBaseFilter();
095: if (baseFilter == null || baseFilter.length() == 0) {
096: filter = "(" + filter + ")";
097: } else {
098: filter = "(& (" + baseFilter + ") (" + filter
099: + "))";
100: }
101:
102: //search entries
103: NamingEnumeration results = ctx.search(ctxp
104: .getSearchName(), filter, searchControls);
105:
106: //create directory entries from results
107: while (results.hasMore()) {
108:
109: //fetch result
110: SearchResult sr = (SearchResult) results.next();
111: //DEBUG:show name
112: //System.out.println(">>>"+sr.getName());
113: //fetch attributes
114: entries.add(new DirectoryEntryImpl(sr
115: .getAttributes()));
116: }
117:
118: } catch (NamingException nex) {
119: throw new JwmaException(nex.getMessage(), true)
120: .setException(nex);
121: } finally {
122: ctxp.releaseContext(ctx);
123: }
124:
125: } catch (Exception ex) {
126: ex.printStackTrace();
127: }
128: return entries;
129: }//lookupEntry
130:
131: public static Attributes createAttributes(HashMap attrs,
132: boolean ignoreCase) {
133: Attributes attributes = new BasicAttributes(ignoreCase);
134:
135: for (Iterator iter = attrs.keySet().iterator(); iter.hasNext();) {
136: String name = (String) iter.next();
137: //fill in the attribute
138: attributes.put(new BasicAttribute(name, attrs.get(name)));
139: }
140: System.out.println(attributes.toString());
141:
142: return attributes;
143: }//createAttributes
144:
145: }//class DirectoryUtil
|