23
23
import java .util .List ;
24
24
import java .util .Map ;
25
25
26
- import org .slf4j .Logger ;
27
- import org .slf4j .LoggerFactory ;
28
-
29
26
import com .fasterxml .jackson .databind .JsonNode ;
30
27
import com .redhat .lightblue .common .ldap .DBResolver ;
28
+ import com .redhat .lightblue .common .ldap .LdapDataStore ;
31
29
import com .redhat .lightblue .crud .CRUDController ;
32
30
import com .redhat .lightblue .crud .CRUDDeleteResponse ;
33
31
import com .redhat .lightblue .crud .CRUDFindResponse ;
36
34
import com .redhat .lightblue .crud .CRUDSaveResponse ;
37
35
import com .redhat .lightblue .crud .CRUDUpdateResponse ;
38
36
import com .redhat .lightblue .crud .DocCtx ;
37
+ import com .redhat .lightblue .eval .FieldAccessRoleEvaluator ;
38
+ import com .redhat .lightblue .eval .Projector ;
39
39
import com .redhat .lightblue .hystrix .ldap .InsertCommand ;
40
+ import com .redhat .lightblue .metadata .DataStore ;
40
41
import com .redhat .lightblue .metadata .EntityMetadata ;
41
42
import com .redhat .lightblue .metadata .MetadataListener ;
42
43
import com .redhat .lightblue .query .Projection ;
46
47
import com .redhat .lightblue .util .JsonDoc ;
47
48
import com .unboundid .ldap .sdk .Attribute ;
48
49
import com .unboundid .ldap .sdk .Entry ;
50
+ import com .unboundid .ldap .sdk .Filter ;
49
51
import com .unboundid .ldap .sdk .LDAPConnection ;
50
52
import com .unboundid .ldap .sdk .LDAPException ;
51
53
import com .unboundid .ldap .sdk .LDAPResult ;
52
54
import com .unboundid .ldap .sdk .ResultCode ;
55
+ import com .unboundid .ldap .sdk .SearchRequest ;
56
+ import com .unboundid .ldap .sdk .SearchResult ;
57
+ import com .unboundid .ldap .sdk .SearchResultEntry ;
58
+ import com .unboundid .ldap .sdk .SearchScope ;
53
59
54
60
public class LdapCRUDController implements CRUDController {
55
61
56
- private static final Logger LOGGER = LoggerFactory .getLogger (LdapCRUDController .class );
57
-
58
- private static final String DN = "dn" ;
59
-
60
62
private final DBResolver dbResolver ;
61
63
62
64
public LdapCRUDController (DBResolver dbResolver ){
@@ -74,6 +76,7 @@ public CRUDInsertionResponse insert(CRUDOperationContext ctx,
74
76
}
75
77
76
78
EntityMetadata md = ctx .getEntityMetadata (ctx .getEntityName ());
79
+ LdapDataStore store = getLdapDataStore (md );
77
80
78
81
//TODO Revisit Projection
79
82
//FieldAccessRoleEvaluator roleEval = new FieldAccessRoleEvaluator(md, ctx.getCallerRoles());
@@ -87,23 +90,26 @@ public CRUDInsertionResponse insert(CRUDOperationContext ctx,
87
90
}*/
88
91
89
92
try {
90
- LDAPConnection connection = dbResolver .get (md . getDataStore () );
93
+ LDAPConnection connection = dbResolver .get (store );
91
94
92
95
for (DocCtx document : documents ){
93
96
//document.setOriginalDocument(document);
94
97
JsonNode rootNode = document .getRoot ();
95
- JsonNode dnNode = rootNode .get (DN );
96
- if (dnNode == null ){
97
- throw new IllegalArgumentException ("dn is a required field" );
98
+
99
+ JsonNode uniqueNode = rootNode .get (store .getUniqueField ());
100
+ if (uniqueNode == null ){
101
+ throw new IllegalArgumentException (store .getUniqueField () + " is a required field" );
98
102
}
99
103
100
- Entry entry = new Entry (dnNode .asText ());
104
+ Entry entry = new Entry (createDN ( store , uniqueNode .asText () ));
101
105
102
106
Iterator <Map .Entry <String , JsonNode >> nodeIterator = rootNode .fields ();
103
107
while (nodeIterator .hasNext ()){
104
108
Map .Entry <String , JsonNode > node = nodeIterator .next ();
105
- if (DN .equalsIgnoreCase (node .getKey ())){
106
- continue ;
109
+ if ("dn" .equalsIgnoreCase (node .getKey ())){
110
+ throw new IllegalArgumentException (
111
+ "DN should not be included as it's value will be derived from the metadata.basedn and" +
112
+ " the metadata.uniqueattr. Including the DN as an insert attribute is confusing." );
107
113
}
108
114
109
115
JsonNode valueNode = node .getValue ();
@@ -168,8 +174,50 @@ public CRUDDeleteResponse delete(CRUDOperationContext ctx,
168
174
public CRUDFindResponse find (CRUDOperationContext ctx ,
169
175
QueryExpression query , Projection projection , Sort sort , Long from ,
170
176
Long to ) {
171
- // TODO Auto-generated method stub
172
- return null ;
177
+
178
+ if (query == null ) {
179
+ throw new IllegalArgumentException ("No query was provided." );
180
+ }
181
+ if (projection == null ) {
182
+ throw new IllegalArgumentException ("No projection was provided" );
183
+ }
184
+
185
+ EntityMetadata md = ctx .getEntityMetadata (ctx .getEntityName ());
186
+ LdapDataStore store = getLdapDataStore (md );
187
+
188
+ CRUDFindResponse response = new CRUDFindResponse ();
189
+ response .setSize (0 );
190
+
191
+ try {
192
+ LDAPConnection connection = dbResolver .get (store );
193
+
194
+ Filter filter = new FilterTranslator ().translate (query );
195
+ SearchRequest request = new SearchRequest (store .getBaseDN (), SearchScope .SUB , filter , "*" );
196
+ SearchResult result = connection .search (request );
197
+
198
+ response .setSize (result .getEntryCount ());
199
+ for (SearchResultEntry resultEntry : result .getSearchEntries ()){
200
+ resultEntry .getDN ();
201
+ }
202
+
203
+ Projector projector = Projector .getInstance (
204
+ Projection .add (
205
+ projection ,
206
+ new FieldAccessRoleEvaluator (
207
+ md ,
208
+ ctx .getCallerRoles ()).getExcludedFields (FieldAccessRoleEvaluator .Operation .find )
209
+ ),
210
+ md );
211
+ for (DocCtx document : ctx .getDocuments ()) {
212
+ document .setOutputDocument (projector .project (document , ctx .getFactory ().getNodeFactory ()));
213
+ }
214
+ }
215
+ catch (LDAPException e ) {
216
+ // TODO Auto-generated catch block
217
+ e .printStackTrace ();
218
+ }
219
+
220
+ return response ;
173
221
}
174
222
175
223
public void updatePredefinedFields (CRUDOperationContext ctx , JsonDoc doc ) {
@@ -180,4 +228,16 @@ public MetadataListener getMetadataListener() {
180
228
return null ;
181
229
}
182
230
231
+ private LdapDataStore getLdapDataStore (EntityMetadata md ){
232
+ DataStore store = md .getDataStore ();
233
+ if (!(store instanceof LdapDataStore )){
234
+ throw new IllegalArgumentException ("DataStore of type " + store .getClass () + " is not supported." );
235
+ }
236
+ return (LdapDataStore ) store ;
237
+ }
238
+
239
+ private String createDN (LdapDataStore store , String uniqueValue ){
240
+ return store .getUniqueField () + "=" + uniqueValue + "," + store .getBaseDN ();
241
+ }
242
+
183
243
}
0 commit comments