001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.security.ldap;
016    
017    import com.liferay.portal.kernel.util.Validator;
018    import com.liferay.util.ldap.DummyDirContext;
019    
020    import javax.naming.Name;
021    import javax.naming.NameNotFoundException;
022    import javax.naming.NamingException;
023    import javax.naming.directory.Attribute;
024    import javax.naming.directory.Attributes;
025    import javax.naming.directory.BasicAttributes;
026    
027    /**
028     * @author Edward Han
029     */
030    public class PortalLDAPContext extends DummyDirContext {
031    
032            public PortalLDAPContext(Attributes attributes) {
033                    _attributes = attributes;
034            }
035    
036            public PortalLDAPContext(boolean ignoreCase) {
037                    _attributes = new BasicAttributes(ignoreCase);
038            }
039    
040            public void addAttribute(String name, Object object) {
041                    _attributes.put(name, object);
042            }
043    
044            public Attributes getAttributes() {
045                    return _attributes;
046            }
047    
048            public Attributes getAttributes(Name name) throws NamingException {
049                    return getAttributes(name.toString());
050            }
051    
052            public Attributes getAttributes(Name name, String[] ids)
053                    throws NamingException {
054    
055                    return getAttributes(name.toString(), ids);
056            }
057    
058            public Attributes getAttributes(String name) throws NamingException {
059                    if (Validator.isNotNull(name)) {
060                            throw new NameNotFoundException();
061                    }
062    
063                    return (Attributes)_attributes.clone();
064            }
065    
066            public Attributes getAttributes(String name, String[] ids)
067                    throws NamingException {
068    
069                    if (Validator.isNotNull(name)) {
070                            throw new NameNotFoundException();
071                    }
072    
073                    Attributes attributes = new BasicAttributes(true);
074    
075                    for (int i = 0; i < ids.length; i++) {
076                            Attribute attribute = _attributes.get(ids[i]);
077    
078                            if (attribute != null) {
079                                    attributes.put(attribute);
080                            }
081                    }
082    
083                    return attributes;
084            }
085    
086            public void setAttributes(Attributes attributes) {
087                    _attributes = attributes;
088            }
089    
090            private Attributes _attributes;
091    
092    }