mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Matthew Swift
22.28.2012 b362d9da285a8dc1b86db29b945feb3bc245b03b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
 * The contents of this file are subject to the terms of the Common Development and
 * Distribution License (the License). You may not use this file except in compliance with the
 * License.
 *
 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
 * specific language governing permission and limitations under the License.
 *
 * When distributing Covered Software, include this CDDL Header Notice in each file and include
 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
 * Header, with the fields enclosed by brackets [] replaced by your own identifying
 * information: "Portions Copyrighted [year] [name of copyright owner]".
 *
 * Copyright 2012 ForgeRock AS. All rights reserved.
 */
 
package org.forgerock.opendj.rest2ldap;
 
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
 
import org.forgerock.json.fluent.JsonPointer;
import org.forgerock.json.fluent.JsonValue;
import org.forgerock.opendj.ldap.AssertionFailureException;
import org.forgerock.opendj.ldap.AuthenticationException;
import org.forgerock.opendj.ldap.AuthorizationException;
import org.forgerock.opendj.ldap.ConnectionException;
import org.forgerock.opendj.ldap.EntryNotFoundException;
import org.forgerock.opendj.ldap.ErrorResultException;
import org.forgerock.opendj.ldap.MultipleEntriesFoundException;
import org.forgerock.opendj.ldap.ResultHandler;
import org.forgerock.opendj.ldap.SearchResultHandler;
import org.forgerock.opendj.ldap.TimeoutResultException;
import org.forgerock.opendj.ldap.responses.Result;
import org.forgerock.opendj.ldap.responses.SearchResultEntry;
import org.forgerock.opendj.ldap.responses.SearchResultReference;
import org.forgerock.resource.exception.NotSupportedException;
import org.forgerock.resource.exception.ResourceException;
import org.forgerock.resource.provider.ActionRequest;
import org.forgerock.resource.provider.ActionResultHandler;
import org.forgerock.resource.provider.Context;
import org.forgerock.resource.provider.CreateRequest;
import org.forgerock.resource.provider.CreateResultHandler;
import org.forgerock.resource.provider.DeleteRequest;
import org.forgerock.resource.provider.DeleteResultHandler;
import org.forgerock.resource.provider.PatchRequest;
import org.forgerock.resource.provider.PatchResultHandler;
import org.forgerock.resource.provider.QueryRequest;
import org.forgerock.resource.provider.QueryResultHandler;
import org.forgerock.resource.provider.ReadRequest;
import org.forgerock.resource.provider.ReadResultHandler;
import org.forgerock.resource.provider.Resource;
import org.forgerock.resource.provider.UpdateRequest;
import org.forgerock.resource.provider.UpdateResultHandler;
 
/**
 *
 */
public class LDAPResource implements Resource {
    private final EntryContainer entryContainer;
    private final AttributeMapper attributeMapper;
 
    /**
     * Creates a new LDAP resource.
     */
    public LDAPResource(final EntryContainer container, final AttributeMapper mapper) {
        this.entryContainer = container;
        this.attributeMapper = mapper;
    }
 
    /**
     * {@inheritDoc}
     */
    public void action(final ActionRequest request, final Context context,
            final ActionResultHandler out) {
        out.setFailure(new NotSupportedException("Not yet implemented"));
    }
 
    /**
     * {@inheritDoc}
     */
    public void create(final CreateRequest request, final Context context,
            final CreateResultHandler out) {
        out.setFailure(new NotSupportedException("Not yet implemented"));
    }
 
    /**
     * {@inheritDoc}
     */
    public void delete(final DeleteRequest request, final Context context,
            final DeleteResultHandler out) {
        out.setFailure(new NotSupportedException("Not yet implemented"));
    }
 
    /**
     * {@inheritDoc}
     */
    public void patch(final PatchRequest request, final Context context,
            final PatchResultHandler out) {
        out.setFailure(new NotSupportedException("Not yet implemented"));
    }
 
    /**
     * {@inheritDoc}
     */
    public void query(final QueryRequest request, final Context context,
            final QueryResultHandler out) {
        out.setFailure(new NotSupportedException("Not yet implemented"));
    }
 
    /**
     * {@inheritDoc}
     */
    public void read(final ReadRequest request, final Context context, final ReadResultHandler out) {
        final String id = request.getId();
        if (id == null) {
            // List the entries.
            final SearchResultHandler handler = new SearchResultHandler() {
                private final List<String> resourceIDs = new ArrayList<String>();
 
                public boolean handleEntry(final SearchResultEntry entry) {
                    // TODO: should the resource or the container define the ID
                    // mapping?
                    resourceIDs.add(entryContainer.getIDFromEntry(entry));
                    return true;
                }
 
                public void handleErrorResult(final ErrorResultException error) {
                    out.setFailure(adaptErrorResult(error));
                }
 
                public boolean handleReference(final SearchResultReference reference) {
                    // TODO: should this be classed as an error since rest2ldap
                    // assumes entries are all colocated?
                    return true;
                }
 
                public void handleResult(final Result result) {
                    out.setResult(id, null, new JsonValue(resourceIDs));
                }
            };
            entryContainer.listEntries(context, handler);
        } else {
            // Read a single entry.
 
            // TODO: Determine the set of LDAP attributes that need to be read.
            final Set<JsonPointer> requestedAttributes = new LinkedHashSet<JsonPointer>();
            final Collection<String> requestedLDAPAttributes =
                    getRequestedLDAPAttributes(requestedAttributes);
 
            final ResultHandler<SearchResultEntry> handler =
                    new ResultHandler<SearchResultEntry>() {
                        public void handleErrorResult(final ErrorResultException error) {
                            out.setFailure(adaptErrorResult(error));
                        }
 
                        public void handleResult(final SearchResultEntry entry) {
                            final String revision = entryContainer.getEtagFromEntry(entry);
                            final ResultHandler<Map<String, Object>> mapHandler =
                                    new ResultHandler<Map<String, Object>>() {
                                        public void handleErrorResult(
                                                final ErrorResultException error) {
                                            out.setFailure(adaptErrorResult(error));
                                        }
 
                                        public void handleResult(final Map<String, Object> result) {
                                            out.setResult(id, revision, new JsonValue(result));
                                        }
                                    };
                            attributeMapper.toJson(context, entry, mapHandler);
                        }
                    };
            entryContainer.readEntry(context, id, requestedLDAPAttributes, handler);
        }
    }
 
    /**
     * {@inheritDoc}
     */
    public void update(final UpdateRequest request, final Context context,
            final UpdateResultHandler out) {
        out.setFailure(new NotSupportedException("Not yet implemented"));
    }
 
    /**
     * Adapts an LDAP result code to a resource exception.
     *
     * @param error
     *            The LDAP error that should be adapted.
     * @return The equivalent resource exception.
     */
    private ResourceException adaptErrorResult(final ErrorResultException error) {
        int resourceResultCode;
        try {
            throw error;
        } catch (AssertionFailureException e) {
            resourceResultCode = ResourceException.VERSION_MISMATCH;
        } catch (AuthenticationException e) {
            resourceResultCode = 401;
        } catch (AuthorizationException e) {
            resourceResultCode = ResourceException.FORBIDDEN;
        } catch (ConnectionException e) {
            resourceResultCode = ResourceException.UNAVAILABLE;
        } catch (EntryNotFoundException e) {
            resourceResultCode = ResourceException.NOT_FOUND;
        } catch (MultipleEntriesFoundException e) {
            resourceResultCode = ResourceException.INTERNAL_ERROR;
        } catch (TimeoutResultException e) {
            resourceResultCode = 408;
        } catch (ErrorResultException e) {
            resourceResultCode = ResourceException.INTERNAL_ERROR;
        }
        return ResourceException.getException(resourceResultCode, null, error.getMessage(), error);
    }
 
    /**
     * Determines the set of LDAP attributes to request in an LDAP read (search,
     * post-read), based on the provided set of JSON pointers.
     *
     * @param requestedAttributes
     *            The set of resource attributes to be read.
     * @return The set of LDAP attributes associated with the resource
     *         attributes.
     */
    private Collection<String> getRequestedLDAPAttributes(final Set<JsonPointer> requestedAttributes) {
        if (requestedAttributes.isEmpty()) {
            // Full read.
            return attributeMapper.getAllLDAPAttributes();
        } else {
            // Partial read.
            final Set<String> requestedLDAPAttributes =
                    new LinkedHashSet<String>(requestedAttributes.size());
            for (final JsonPointer requestedAttribute : requestedAttributes) {
                attributeMapper.getLDAPAttributesFor(requestedAttribute, requestedLDAPAttributes);
            }
            return requestedLDAPAttributes;
        }
    }
 
}