1/*
2 * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package com.sun.jndi.ldap;
27
28import java.security.AccessControlContext;
29import java.security.AccessController;
30import java.security.PrivilegedActionException;
31import java.security.PrivilegedExceptionAction;
32import java.util.Vector;
33import javax.naming.*;
34import javax.naming.directory.*;
35import javax.naming.ldap.Control;
36import javax.naming.spi.*;
37
38import com.sun.jndi.toolkit.ctx.Continuation;
39
40final class LdapBindingEnumeration
41        extends AbstractLdapNamingEnumeration<Binding> {
42
43    private final AccessControlContext acc = AccessController.getContext();
44
45    LdapBindingEnumeration(LdapCtx homeCtx, LdapResult answer, Name remain,
46        Continuation cont) throws NamingException
47    {
48        super(homeCtx, answer, remain, cont);
49    }
50
51    @Override
52    protected Binding
53      createItem(String dn, Attributes attrs, Vector<Control> respCtls)
54        throws NamingException {
55
56        Object obj = null;
57        String atom = getAtom(dn);
58
59        if (attrs.get(Obj.JAVA_ATTRIBUTES[Obj.CLASSNAME]) != null) {
60            // serialized object or object reference
61            try {
62                obj = AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
63                    @Override
64                    public Object run() throws NamingException {
65                        return Obj.decodeObject(attrs);
66                    }
67                }, acc);
68            } catch (PrivilegedActionException e) {
69                throw (NamingException)e.getException();
70            }
71        }
72        if (obj == null) {
73            // DirContext object
74            obj = new LdapCtx(homeCtx, dn);
75        }
76
77        CompositeName cn = new CompositeName();
78        cn.add(atom);
79
80        try {
81            obj = DirectoryManager.getObjectInstance(obj, cn, homeCtx,
82                homeCtx.envprops, attrs);
83
84        } catch (NamingException e) {
85            throw e;
86
87        } catch (Exception e) {
88            NamingException ne =
89                new NamingException(
90                        "problem generating object using object factory");
91            ne.setRootCause(e);
92            throw ne;
93        }
94
95        Binding binding;
96        if (respCtls != null) {
97           binding = new BindingWithControls(cn.toString(), obj,
98                                homeCtx.convertControls(respCtls));
99        } else {
100            binding = new Binding(cn.toString(), obj);
101        }
102        binding.setNameInNamespace(dn);
103        return binding;
104    }
105
106    @Override
107    protected AbstractLdapNamingEnumeration<? extends NameClassPair> getReferredResults(
108            LdapReferralContext refCtx) throws NamingException{
109        // repeat the original operation at the new context
110        return (AbstractLdapNamingEnumeration<? extends NameClassPair>)refCtx.listBindings(listArg);
111    }
112}
113