001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.security.ldap;
016    
017    import com.liferay.portal.kernel.ldap.DummyDirContext;
018    import com.liferay.portal.kernel.util.Validator;
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            @Override
049            public Attributes getAttributes(Name name) throws NamingException {
050                    return getAttributes(name.toString());
051            }
052    
053            @Override
054            public Attributes getAttributes(Name name, String[] ids)
055                    throws NamingException {
056    
057                    return getAttributes(name.toString(), ids);
058            }
059    
060            @Override
061            public Attributes getAttributes(String name) throws NamingException {
062                    if (Validator.isNotNull(name)) {
063                            throw new NameNotFoundException();
064                    }
065    
066                    return (Attributes)_attributes.clone();
067            }
068    
069            @Override
070            public Attributes getAttributes(String name, String[] ids)
071                    throws NamingException {
072    
073                    if (Validator.isNotNull(name)) {
074                            throw new NameNotFoundException();
075                    }
076    
077                    Attributes attributes = new BasicAttributes(true);
078    
079                    for (int i = 0; i < ids.length; i++) {
080                            Attribute attribute = _attributes.get(ids[i]);
081    
082                            if (attribute != null) {
083                                    attributes.put(attribute);
084                            }
085                    }
086    
087                    return attributes;
088            }
089    
090            public void setAttributes(Attributes attributes) {
091                    _attributes = attributes;
092            }
093    
094            private Attributes _attributes;
095    
096    }