KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejbca > core > model > ca > publisher > LdapSearchPublisher


1 package org.ejbca.core.model.ca.publisher;
2
3 import java.io.UnsupportedEncodingException JavaDoc;
4 import java.util.HashMap JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.regex.Pattern JavaDoc;
7
8 import org.apache.log4j.Logger;
9 import org.ejbca.core.model.InternalResources;
10 import org.ejbca.util.CertTools;
11
12 import com.novell.ldap.LDAPConnection;
13 import com.novell.ldap.LDAPEntry;
14 import com.novell.ldap.LDAPException;
15 import com.novell.ldap.LDAPSearchResults;
16
17 public class LdapSearchPublisher extends LdapPublisher {
18     
19     private static final Logger log = Logger.getLogger(LdapSearchPublisher.class);
20     /** Internal localization of logs and errors */
21     private static final InternalResources intres = InternalResources.getInstance();
22     
23     public static final int TYPE_LDAPSEARCHPUBLISHER = 4;
24         
25     // Default Values
26
protected static final String JavaDoc SEARCHBASEDN = "searchbasedn";
27     protected static final String JavaDoc SEARCHFILTER = "searchfilter";
28     
29     public LdapSearchPublisher() {
30         super();
31         data.put(TYPE, new Integer JavaDoc(TYPE_LDAPSEARCHPUBLISHER));
32         
33         setSearchBaseDN("");
34         setSearchFilter("");
35     }
36     
37     // Public Methods
38

39
40     /** SearchOldEntity is the only method differing between regular ldap and ldap search publishers.
41      * Aprat from how they find existing users, the publishing works the same.
42      */

43     protected LDAPEntry searchOldEntity(String JavaDoc username, int ldapVersion, LDAPConnection lc, String JavaDoc dn) throws PublisherException {
44         LDAPEntry oldEntry = null; // return value
45
int searchScope;
46         String JavaDoc searchbasedn;
47         boolean attributeOnly;
48
49         // PARTE 1: Search for an existing entry in the LDAP directory
50
// If it exists, s�lo se a�adir� al DN la parte del certificado (PARTE 2)
51
// if not exist, se a�adir� toda una entrada LDAP nueva (PARTE 2)
52
try {
53             // connect to the server
54
log.debug("Connecting to " + getHostname());
55             lc.connect(getHostname(), Integer.parseInt(getPort()));
56             // authenticate to the server
57
log.debug("Logging in with BIND DN " + getLoginDN());
58             lc.bind(ldapVersion, getLoginDN(), getLoginPassword().getBytes("UTF8"));
59             // Filtro est�tico:
60
//searchFilter = "(&(objectclass=person)(uid=" + username + "))";
61
String JavaDoc searchFilter = getSearchFilter();
62             log.debug("Compiling search filter: " +searchFilter);
63             if (username != null) {
64                 Pattern JavaDoc USER = Pattern.compile("\\$USERNAME", Pattern.CASE_INSENSITIVE);
65                 searchFilter = USER.matcher(searchFilter).replaceAll(username);
66             }
67             if (CertTools.getPartFromDN(dn, "CN") != null) {
68                 Pattern JavaDoc CN = Pattern.compile("\\$CN", Pattern.CASE_INSENSITIVE);
69                 searchFilter = CN.matcher(searchFilter).replaceAll(CertTools.getPartFromDN(dn, "CN"));
70             }
71             if (CertTools.getPartFromDN(dn, "O") != null) {
72                 Pattern JavaDoc O = Pattern.compile("\\$O", Pattern.CASE_INSENSITIVE);
73                 searchFilter = O.matcher(searchFilter).replaceAll(CertTools.getPartFromDN(dn, "O"));
74             }
75             if (CertTools.getPartFromDN(dn, "OU") != null) {
76                 Pattern JavaDoc OU = Pattern.compile("\\$OU", Pattern.CASE_INSENSITIVE);
77                 searchFilter = OU.matcher(searchFilter).replaceAll(CertTools.getPartFromDN(dn, "OU"));
78             }
79             if (CertTools.getPartFromDN(dn, "C") != null) {
80                 Pattern JavaDoc C = Pattern.compile("\\$C", Pattern.CASE_INSENSITIVE);
81                 searchFilter = C.matcher(searchFilter).replaceAll(CertTools.getPartFromDN(dn, "C"));
82             }
83             log.debug("Resulting search filter '" + searchFilter+"'.");
84             searchScope = LDAPConnection.SCOPE_SUB;
85             log.debug("Making SRCH with BaseDN '" + getSearchBaseDN() + "' and filter '" + searchFilter+"'.");
86             searchbasedn = getSearchBaseDN();
87             String JavaDoc attrs[] = { LDAPConnection.NO_ATTRS };
88             attributeOnly = true;
89             LDAPSearchResults searchResults = lc.search(searchbasedn, // container to search
90
searchScope, // search scope
91
searchFilter, // search filter
92
attrs, // "1.1" returns entry name only
93
attributeOnly); // no attributes are returned
94
// try to read the old object
95
if (log.isDebugEnabled()) {
96                 log.debug("serachResults contains entries: "+searchResults.hasMore());
97             }
98             if (searchResults.hasMore()) {
99                 oldEntry = searchResults.next();
100                 dn = oldEntry.getDN();
101                 if (searchResults.hasMore()) {
102                     log.debug("Found more than one matches with filter '" + searchFilter +
103                             "'. Using the first match with LDAP entry with DN: " +oldEntry.getDN());
104                 } else {
105                     log.debug("Found one match with filter: '"+searchFilter+"', match with DN: " + oldEntry.getDN());
106                 }
107             } else {
108                 log.debug("No matches found using filter: '" +searchFilter + "'. Using DN: " + dn);
109             }
110             // try to read the old object
111
try {
112                 oldEntry = lc.read(dn);
113             } catch (LDAPException e) {
114                 if (e.getResultCode() == LDAPException.NO_SUCH_OBJECT) {
115                     String JavaDoc msg = intres.getLocalizedMessage("publisher.noentry", dn);
116                     log.info(msg);
117                 } else {
118                     String JavaDoc msg = intres.getLocalizedMessage("publisher.infoexists", dn);
119                     log.info(msg);
120                 }
121             }
122         } catch (LDAPException e) {
123             if (e.getResultCode() == LDAPException.NO_SUCH_OBJECT) {
124                 String JavaDoc msg = intres.getLocalizedMessage("publisher.noentry", dn);
125                 log.info(msg);
126             } else {
127                 String JavaDoc msg = intres.getLocalizedMessage("publisher.errorldapbind", e.getMessage());
128                 log.error(msg, e);
129                 throw new PublisherException(msg);
130             }
131         } catch (UnsupportedEncodingException JavaDoc e) {
132             String JavaDoc msg = intres.getLocalizedMessage("publisher.errorpassword", getLoginPassword());
133             throw new PublisherException(msg);
134         } finally {
135             // disconnect with the server
136
try {
137                 lc.disconnect();
138             } catch (LDAPException e) {
139                 String JavaDoc msg = intres.getLocalizedMessage("publisher.errordisconnect");
140                 log.error(msg, e);
141             }
142         }
143         return oldEntry;
144     }
145     
146     /**
147      * Retorna el base de la b�squeda
148      */

149     public String JavaDoc getSearchBaseDN() {
150         return (String JavaDoc) data.get(SEARCHBASEDN);
151     }
152     
153     /**
154      * Establece la base de la b�squeda.
155      */

156     public void setSearchBaseDN(String JavaDoc searchbasedn) {
157         data.put(SEARCHBASEDN, searchbasedn);
158     }
159     
160     /**
161      * Retorna el filtro de b�squeda
162      */

163     public String JavaDoc getSearchFilter() {
164         return (String JavaDoc) data.get(SEARCHFILTER);
165     }
166     
167     /**
168      * Establece el filtro de b�squeda
169      */

170     public void setSearchFilter(String JavaDoc searchfilter) {
171         data.put(SEARCHFILTER, searchfilter);
172     }
173     
174     
175     // Private methods
176

177     
178     /**
179      * @see org.ejbca.core.model.ca.publisher.BasePublisher#clone()
180      */

181     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
182         LdapSearchPublisher clone = new LdapSearchPublisher();
183         HashMap JavaDoc clonedata = (HashMap JavaDoc) clone.saveData();
184         
185         Iterator JavaDoc i = (data.keySet()).iterator();
186         while (i.hasNext()) {
187             Object JavaDoc key = i.next();
188             clonedata.put(key, data.get(key));
189         }
190         
191         clone.loadData(clonedata);
192         return clone;
193     }
194         
195 }
196
Popular Tags