21
21
import java .util .ArrayList ;
22
22
import java .util .HashMap ;
23
23
import java .util .HashSet ;
24
- import java .util .Iterator ;
25
24
import java .util .List ;
26
25
import java .util .Map ;
27
26
import java .util .Map .Entry ;
68
67
import com .redhat .lightblue .util .Error ;
69
68
import com .redhat .lightblue .util .JsonDoc ;
70
69
import com .redhat .lightblue .util .Path ;
71
- import com .unboundid .ldap .sdk .Attribute ;
72
70
import com .unboundid .ldap .sdk .LDAPConnection ;
73
71
import com .unboundid .ldap .sdk .LDAPException ;
74
72
import com .unboundid .ldap .sdk .LDAPResult ;
75
73
import com .unboundid .ldap .sdk .ResultCode ;
76
74
import com .unboundid .ldap .sdk .SearchRequest ;
77
75
import com .unboundid .ldap .sdk .SearchResult ;
76
+ import com .unboundid .ldap .sdk .SearchResultEntry ;
78
77
import com .unboundid .ldap .sdk .SearchScope ;
79
78
import com .unboundid .ldap .sdk .controls .ServerSideSortRequestControl ;
80
79
import com .unboundid .ldap .sdk .controls .VirtualListViewRequestControl ;
@@ -106,19 +105,13 @@ public CRUDInsertionResponse insert(CRUDOperationContext ctx,
106
105
EntityMetadata md = ctx .getEntityMetadata (ctx .getEntityName ());
107
106
LdapDataStore store = getLdapDataStore (md );
108
107
109
- Map <DocCtx , String > documentToDnMap = new HashMap <DocCtx , String >();
110
-
111
- LDAPConnection connection = null ;
112
- try {
113
- connection = dbResolver .get (store );
114
- }
115
- catch (LDAPException e ) {
116
- //TODO: throw more relevant exception.
117
- throw new RuntimeException ("Unable to establish connection to LDAP" , e );
118
- }
119
-
120
108
FieldAccessRoleEvaluator roles = new FieldAccessRoleEvaluator (md , ctx .getCallerRoles ());
109
+ EntryBuilder entryBuilder = new EntryBuilder (md );
121
110
111
+ //Create Entry instances for each document.
112
+ List <com .unboundid .ldap .sdk .Entry > entries = new ArrayList <com .unboundid .ldap .sdk .Entry >();
113
+ Map <DocCtx , String > documentToDnMap = new HashMap <DocCtx , String >();
114
+ boolean hasError = false ;
122
115
for (DocCtx document : documents ){
123
116
List <Path > paths = roles .getInaccessibleFields_Insert (document );
124
117
if ((paths != null ) && !paths .isEmpty ()){
@@ -137,40 +130,21 @@ public CRUDInsertionResponse insert(CRUDOperationContext ctx,
137
130
138
131
String dn = createDN (store , uniqueNode .asText ());
139
132
documentToDnMap .put (document , dn );
140
- com .unboundid .ldap .sdk .Entry entry = new com .unboundid .ldap .sdk .Entry (dn );
141
-
142
- Iterator <Map .Entry <String , JsonNode >> nodeIterator = rootNode .fields ();
143
- while (nodeIterator .hasNext ()){
144
- Map .Entry <String , JsonNode > node = nodeIterator .next ();
145
- if (LdapConstant .FIELD_DN .equalsIgnoreCase (node .getKey ())){
146
- throw new IllegalArgumentException (
147
- "'dn' should not be included as it's value will be derived from the metadata.basedn and" +
148
- " the metadata.uniqueattr. Including the 'dn' as an insert attribute is confusing." );
149
- }
150
-
151
- JsonNode valueNode = node .getValue ();
152
- if (valueNode .isArray ()){
153
- List <String > values = new ArrayList <String >();
154
- for (JsonNode string : valueNode ){
155
- values .add (string .asText ());
156
- }
157
- entry .addAttribute (new Attribute (node .getKey (), values ));
158
- }
159
- else {
160
- String fieldName = node .getKey ();
161
- if (LightblueUtil .isFieldObjectType (fieldName )
162
- || LightblueUtil .isFieldAnArrayCount (fieldName , md .getFields ())){
163
- /*
164
- * Indicates the field is auto-generated for lightblue purposes. These fields
165
- * should not be inserted into LDAP.
166
- */
167
- continue ;
168
- }
169
-
170
- entry .addAttribute (new Attribute (node .getKey (), node .getValue ().asText ()));
171
- }
133
+ try {
134
+ entries .add (entryBuilder .build (dn , document ));
172
135
}
136
+ catch (Exception e ){
137
+ document .addError (Error .get (e ));
138
+ hasError = true ;
139
+ }
140
+ }
141
+ if (hasError ){
142
+ return response ;
143
+ }
173
144
145
+ //Persist each Entry.
146
+ LDAPConnection connection = getNewLdapConnection (store );
147
+ for (com .unboundid .ldap .sdk .Entry entry : entries ){
174
148
InsertCommand command = new InsertCommand (connection , entry );
175
149
176
150
LDAPResult result = command .execute ();
@@ -227,9 +201,9 @@ public CRUDFindResponse find(CRUDOperationContext ctx,
227
201
CRUDFindResponse response = new CRUDFindResponse ();
228
202
response .setSize (0 );
229
203
230
- try {
231
- LDAPConnection connection = dbResolver .get (store );
204
+ LDAPConnection connection = getNewLdapConnection (store );
232
205
206
+ try {
233
207
//TODO: Support scopes other than SUB
234
208
SearchRequest request = new SearchRequest (
235
209
store .getBaseDN (),
@@ -247,7 +221,19 @@ public CRUDFindResponse find(CRUDOperationContext ctx,
247
221
SearchResult result = connection .search (request );
248
222
249
223
response .setSize (result .getEntryCount ());
250
- ctx .setDocuments (new ResultTranslator (ctx .getFactory ().getNodeFactory ()).translate (result , md ));
224
+ ResultTranslator resultTranslator = new ResultTranslator (ctx .getFactory ().getNodeFactory ());
225
+ List <DocCtx > translatedDocs = new ArrayList <DocCtx >();
226
+ for (SearchResultEntry entry : result .getSearchEntries ()){
227
+ try {
228
+ translatedDocs .add (resultTranslator .translate (entry , md ));
229
+ }
230
+ catch (Exception e ){
231
+ DocCtx erroredDoc = new DocCtx (null );
232
+ erroredDoc .addError (Error .get (e ));
233
+ translatedDocs .add (erroredDoc );
234
+ }
235
+ }
236
+ ctx .setDocuments (translatedDocs );
251
237
252
238
Projector projector = Projector .getInstance (
253
239
Projection .add (
@@ -416,4 +402,22 @@ private void projectChanges(Projection projection, CRUDOperationContext ctx, Map
416
402
}
417
403
}
418
404
405
+ /**
406
+ * Returns a new connection to ldap.
407
+ * @param store - {@link LdapDataStore} to connect too.
408
+ * @return a new connection to ldap
409
+ * @throws RuntimeException when unable to connect to ldap.
410
+ */
411
+ private LDAPConnection getNewLdapConnection (LdapDataStore store ) {
412
+ LDAPConnection connection = null ;
413
+ try {
414
+ connection = dbResolver .get (store );
415
+ }
416
+ catch (LDAPException e ) {
417
+ //TODO: throw more relevant exception.
418
+ throw new RuntimeException ("Unable to establish connection to LDAP" , e );
419
+ }
420
+ return connection ;
421
+ }
422
+
419
423
}
0 commit comments