73
73
import com .unboundid .ldap .sdk .ResultCode ;
74
74
import com .unboundid .ldap .sdk .SearchRequest ;
75
75
import com .unboundid .ldap .sdk .SearchResult ;
76
+ import com .unboundid .ldap .sdk .SearchResultEntry ;
76
77
import com .unboundid .ldap .sdk .SearchScope ;
77
78
import com .unboundid .ldap .sdk .controls .ServerSideSortRequestControl ;
78
79
import com .unboundid .ldap .sdk .controls .VirtualListViewRequestControl ;
@@ -104,21 +105,13 @@ public CRUDInsertionResponse insert(CRUDOperationContext ctx,
104
105
EntityMetadata md = ctx .getEntityMetadata (ctx .getEntityName ());
105
106
LdapDataStore store = getLdapDataStore (md );
106
107
107
- LDAPConnection connection = null ;
108
- try {
109
- connection = dbResolver .get (store );
110
- }
111
- catch (LDAPException e ) {
112
- //TODO: throw more relevant exception.
113
- throw new RuntimeException ("Unable to establish connection to LDAP" , e );
114
- }
115
-
116
108
FieldAccessRoleEvaluator roles = new FieldAccessRoleEvaluator (md , ctx .getCallerRoles ());
117
109
EntryBuilder entryBuilder = new EntryBuilder (md );
118
110
119
111
//Create Entry instances for each document.
120
112
List <com .unboundid .ldap .sdk .Entry > entries = new ArrayList <com .unboundid .ldap .sdk .Entry >();
121
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,10 +130,20 @@ public CRUDInsertionResponse insert(CRUDOperationContext ctx,
137
130
138
131
String dn = createDN (store , uniqueNode .asText ());
139
132
documentToDnMap .put (document , dn );
140
- entries .add (entryBuilder .build (dn , document ));
133
+ try {
134
+ entries .add (entryBuilder .build (dn , document ));
135
+ }
136
+ catch (Exception e ){
137
+ document .addError (Error .get (e ));
138
+ hasError = true ;
139
+ }
140
+ }
141
+ if (hasError ){
142
+ return response ;
141
143
}
142
144
143
145
//Persist each Entry.
146
+ LDAPConnection connection = getNewLdapConnection (store );
144
147
for (com .unboundid .ldap .sdk .Entry entry : entries ){
145
148
InsertCommand command = new InsertCommand (connection , entry );
146
149
@@ -198,9 +201,9 @@ public CRUDFindResponse find(CRUDOperationContext ctx,
198
201
CRUDFindResponse response = new CRUDFindResponse ();
199
202
response .setSize (0 );
200
203
201
- try {
202
- LDAPConnection connection = dbResolver .get (store );
204
+ LDAPConnection connection = getNewLdapConnection (store );
203
205
206
+ try {
204
207
//TODO: Support scopes other than SUB
205
208
SearchRequest request = new SearchRequest (
206
209
store .getBaseDN (),
@@ -218,7 +221,19 @@ public CRUDFindResponse find(CRUDOperationContext ctx,
218
221
SearchResult result = connection .search (request );
219
222
220
223
response .setSize (result .getEntryCount ());
221
- 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 );
222
237
223
238
Projector projector = Projector .getInstance (
224
239
Projection .add (
@@ -387,4 +402,22 @@ private void projectChanges(Projection projection, CRUDOperationContext ctx, Map
387
402
}
388
403
}
389
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
+
390
423
}
0 commit comments