001package com.hfg.ldap;
002
003import com.hfg.security.CredentialsMgr;
004import com.hfg.security.LoginCredentials;
005import com.hfg.util.BooleanUtil;
006import com.hfg.util.CompareUtil;
007import com.hfg.util.StringUtil;
008import com.hfg.xml.XMLNode;
009import com.hfg.xml.XMLTag;
010
011//------------------------------------------------------------------------------
012/**
013 Container for LDAP settings.
014 <div>
015 @author J. Alex Taylor, hairyfatguy.com
016 </div>
017 */
018//------------------------------------------------------------------------------
019// com.hfg XML/HTML Coding Library
020//
021// This library is free software; you can redistribute it and/or
022// modify it under the terms of the GNU Lesser General Public
023// License as published by the Free Software Foundation; either
024// version 2.1 of the License, or (at your option) any later version.
025//
026// This library is distributed in the hope that it will be useful,
027// but WITHOUT ANY WARRANTY; without even the implied warranty of
028// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
029// Lesser General Public License for more details.
030//
031// You should have received a copy of the GNU Lesser General Public
032// License along with this library; if not, write to the Free Software
033// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
034//
035// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
036// jataylor@hairyfatguy.com
037//------------------------------------------------------------------------------
038
039public class LDAP_Config
040{
041   private String mDomain;
042   private String mDomainCommonName;
043   private String mServerURL;
044   private String mCredentialsMgrKey;
045   private LoginCredentials mPrincipalCredentials;
046   private String mPrincipalFieldName;
047   private String mUserContext;
048   private Boolean mIsActiveDirectory;
049   private LDAPClient.ReferralHandling mReferralHandling;
050
051   private static final String LDAP_CONFIG_TAG          = "LDAP_Config";
052   private static final String DOMAIN_TAG               = "Domain";
053   private static final String CRED_MGR_KEY_TAG         = "CredMgrKey";
054   private static final String USER_CONTEXT_TAG         = "UserContext";
055   private static final String PRINCIPAL_FIELD_NAME_TAG = "PrincipalFieldName";
056   private static final String REFERRAL_HANDLING_TAG    = "ReferralHandling";
057   private static final String ACTIVE_DIRECTORY_TAG     = "ActiveDirectory";
058 
059   private static final String URL_ATT         = "url";
060   private static final String COMMON_NAME_ATT = "commonName";
061   
062   //###########################################################################
063   // CONSTRUCTORS
064   //###########################################################################
065
066   //------------------------------------------------------------------------
067   public LDAP_Config()
068   {
069      
070   }
071
072   //------------------------------------------------------------------------
073   public LDAP_Config(XMLTag inXMLTag)
074   {
075      inXMLTag.verifyTagName(LDAP_CONFIG_TAG);
076
077      setServerURL(inXMLTag.getAttributeValue(URL_ATT));
078      
079      XMLNode subtag = inXMLTag.getOptionalSubtagByName(DOMAIN_TAG);
080      if (subtag != null)
081      {
082         setDomain(subtag.getUnescapedContent());
083         setDomainCommonName(subtag.getAttributeValue(COMMON_NAME_ATT));
084      }
085
086      subtag = inXMLTag.getOptionalSubtagByName(USER_CONTEXT_TAG);
087      if (subtag != null)
088      {
089         setUserContext(subtag.getUnescapedContent());
090      }
091
092      subtag = inXMLTag.getOptionalSubtagByName(PRINCIPAL_FIELD_NAME_TAG);
093      if (subtag != null)
094      {
095         setPrincipalFieldName(subtag.getUnescapedContent());
096      }
097
098      subtag = inXMLTag.getOptionalSubtagByName(REFERRAL_HANDLING_TAG);
099      if (subtag != null)
100      {
101         setReferralHandling(LDAPClient.ReferralHandling.valueOf(subtag.getUnescapedContent()));
102      }
103
104      subtag = inXMLTag.getOptionalSubtagByName(CRED_MGR_KEY_TAG);
105      if (subtag != null)
106      {
107         setCredentialsMgrKey(subtag.getUnescapedContent());
108      }
109
110      subtag = inXMLTag.getOptionalSubtagByName(ACTIVE_DIRECTORY_TAG);
111      if (subtag != null)
112      {
113         setIsActiveDirectory(BooleanUtil.valueOf(subtag.getUnescapedContent()));
114      }
115   }
116   
117   //###########################################################################
118   // PUBLIC METHODS
119   //###########################################################################
120
121   //------------------------------------------------------------------------
122   public LDAP_Config setDomain(String inValue)
123   {
124      mDomain = inValue;
125      return this;
126   }
127
128   //------------------------------------------------------------------------
129   public String getDomain()
130   {
131      return mDomain;
132   }
133
134
135   //------------------------------------------------------------------------
136   public LDAP_Config setDomainCommonName(String inValue)
137   {
138      mDomainCommonName = inValue;
139      return this;
140   }
141
142   //------------------------------------------------------------------------
143   public String getDomainCommonName()
144   {
145      return mDomainCommonName;
146   }
147
148
149   //------------------------------------------------------------------------
150   public LDAP_Config setServerURL(String inValue)
151   {
152      mServerURL = inValue;
153      return this;
154   }
155
156   //------------------------------------------------------------------------
157   public String getServerURL()
158   {
159      return mServerURL;
160   }
161
162
163   //------------------------------------------------------------------------
164   public LDAP_Config setCredentialsMgrKey(String inValue)
165   {
166      mCredentialsMgrKey = inValue;
167      return this;
168   }
169
170   //------------------------------------------------------------------------
171   public String getCredentialsMgrKey()
172   {
173      return mCredentialsMgrKey;
174   }
175
176
177   //------------------------------------------------------------------------
178   public LDAP_Config setPrincipalCredentials(LoginCredentials inValue)
179   {
180      mPrincipalCredentials = inValue;
181      return this;
182   }
183
184   //------------------------------------------------------------------------
185   public LoginCredentials getPrincipalCredentials()
186   {
187      if (null == mPrincipalCredentials
188          && getCredentialsMgrKey() != null)
189      {
190         mPrincipalCredentials = new CredentialsMgr().get(getCredentialsMgrKey());
191      }
192      
193      return mPrincipalCredentials;
194   }
195
196
197   //------------------------------------------------------------------------
198   public LDAP_Config setPrincipalFieldName(String inValue)
199   {
200      mPrincipalFieldName = inValue;
201      return this;
202   }
203
204   //------------------------------------------------------------------------
205   public String getPrincipalFieldName()
206   {
207      return mPrincipalFieldName;
208   }
209
210
211   //------------------------------------------------------------------------
212   public LDAP_Config setUserContext(String inValue)
213   {
214      mUserContext = inValue;
215      return this;
216   }
217
218   //------------------------------------------------------------------------
219   public String getUserContext()
220   {
221      return mUserContext;
222   }
223
224
225   //------------------------------------------------------------------------
226   public LDAP_Config setIsActiveDirectory(Boolean inValue)
227   {
228      mIsActiveDirectory = inValue;
229      return this;
230   }
231
232   //------------------------------------------------------------------------
233   public Boolean isActiveDirectory()
234   {
235      return mIsActiveDirectory;
236   }
237
238
239   //------------------------------------------------------------------------
240   public LDAP_Config setReferralHandling(LDAPClient.ReferralHandling inValue)
241   {
242      mReferralHandling = inValue;
243      return this;
244   }
245
246   //------------------------------------------------------------------------
247   public LDAPClient.ReferralHandling getReferralHandling()
248   {
249      return mReferralHandling;
250   }
251
252
253   //--------------------------------------------------------------------------
254   @Override
255   public boolean equals(Object inObj2)
256   {
257      return (inObj2 != null 
258              && inObj2 instanceof LDAP_Config
259              && 0 == CompareUtil.compare(getServerURL(), ((LDAP_Config) inObj2).getServerURL()));      
260   }
261   
262   //--------------------------------------------------------------------------
263   public XMLNode toXMLTag()
264   {
265      XMLNode node = new XMLTag(LDAP_CONFIG_TAG);
266      
267      if (getServerURL() != null)
268      {
269         node.setAttribute(URL_ATT, getServerURL());
270      }
271      
272      if (StringUtil.isSet(getDomain()))
273      {
274         XMLNode domainTag = new XMLTag(DOMAIN_TAG);
275         domainTag.setContent(getDomain());
276         if (getDomainCommonName() != null)
277         {
278            domainTag.setAttribute(COMMON_NAME_ATT, getDomainCommonName());
279         }
280         
281         node.addSubtag(domainTag);
282      }
283      
284      if (StringUtil.isSet(getUserContext()))
285      {
286         XMLNode subtag = new XMLTag(USER_CONTEXT_TAG);
287         subtag.setContent(getUserContext());
288         
289         node.addSubtag(subtag);
290      }
291      
292      if (StringUtil.isSet(getPrincipalFieldName()))
293      {
294         XMLNode subtag = new XMLTag(PRINCIPAL_FIELD_NAME_TAG);
295         subtag.setContent(getPrincipalFieldName());
296         
297         node.addSubtag(subtag);
298      }
299  
300      if (getReferralHandling() != null)
301      {
302         XMLNode subtag = new XMLTag(REFERRAL_HANDLING_TAG);
303         subtag.setContent(getReferralHandling().name());
304         
305         node.addSubtag(subtag);
306      }
307
308      // Note that we don't serialize the credentials themselves
309      if (getCredentialsMgrKey() != null)
310      {
311         XMLNode subtag = new XMLTag(CRED_MGR_KEY_TAG);
312         subtag.setContent(getCredentialsMgrKey());
313         
314         node.addSubtag(subtag);
315      }
316
317      if (isActiveDirectory() != null)
318      {
319         XMLNode subtag = new XMLTag(ACTIVE_DIRECTORY_TAG);
320         subtag.setContent(isActiveDirectory() ? "true" : "false");
321
322         node.addSubtag(subtag);
323      }
324
325
326      return node;
327   }
328
329}