Skip to content

Commit 28d207a

Browse files
committed
use Error.get
1 parent 0b68439 commit 28d207a

File tree

3 files changed

+132
-150
lines changed

3 files changed

+132
-150
lines changed

lightblue-ldap-crud/src/main/java/com/redhat/lightblue/crud/ldap/LdapCRUDController.java

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
import com.unboundid.ldap.sdk.ResultCode;
7474
import com.unboundid.ldap.sdk.SearchRequest;
7575
import com.unboundid.ldap.sdk.SearchResult;
76+
import com.unboundid.ldap.sdk.SearchResultEntry;
7677
import com.unboundid.ldap.sdk.SearchScope;
7778
import com.unboundid.ldap.sdk.controls.ServerSideSortRequestControl;
7879
import com.unboundid.ldap.sdk.controls.VirtualListViewRequestControl;
@@ -104,21 +105,13 @@ public CRUDInsertionResponse insert(CRUDOperationContext ctx,
104105
EntityMetadata md = ctx.getEntityMetadata(ctx.getEntityName());
105106
LdapDataStore store = getLdapDataStore(md);
106107

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-
116108
FieldAccessRoleEvaluator roles = new FieldAccessRoleEvaluator(md, ctx.getCallerRoles());
117109
EntryBuilder entryBuilder = new EntryBuilder(md);
118110

119111
//Create Entry instances for each document.
120112
List<com.unboundid.ldap.sdk.Entry> entries = new ArrayList<com.unboundid.ldap.sdk.Entry>();
121113
Map<DocCtx, String> documentToDnMap = new HashMap<DocCtx, String>();
114+
boolean hasError = false;
122115
for(DocCtx document : documents){
123116
List<Path> paths = roles.getInaccessibleFields_Insert(document);
124117
if((paths != null) && !paths.isEmpty()){
@@ -137,10 +130,20 @@ public CRUDInsertionResponse insert(CRUDOperationContext ctx,
137130

138131
String dn = createDN(store, uniqueNode.asText());
139132
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;
141143
}
142144

143145
//Persist each Entry.
146+
LDAPConnection connection = getNewLdapConnection(store);
144147
for(com.unboundid.ldap.sdk.Entry entry : entries){
145148
InsertCommand command = new InsertCommand(connection, entry);
146149

@@ -198,9 +201,9 @@ public CRUDFindResponse find(CRUDOperationContext ctx,
198201
CRUDFindResponse response = new CRUDFindResponse();
199202
response.setSize(0);
200203

201-
try {
202-
LDAPConnection connection = dbResolver.get(store);
204+
LDAPConnection connection = getNewLdapConnection(store);
203205

206+
try {
204207
//TODO: Support scopes other than SUB
205208
SearchRequest request = new SearchRequest(
206209
store.getBaseDN(),
@@ -218,7 +221,19 @@ public CRUDFindResponse find(CRUDOperationContext ctx,
218221
SearchResult result = connection.search(request);
219222

220223
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);
222237

223238
Projector projector = Projector.getInstance(
224239
Projection.add(
@@ -387,4 +402,22 @@ private void projectChanges(Projection projection, CRUDOperationContext ctx, Map
387402
}
388403
}
389404

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+
390423
}

lightblue-ldap-crud/src/main/java/com/redhat/lightblue/crud/ldap/translator/ResultTranslator.java

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@
1818
*/
1919
package com.redhat.lightblue.crud.ldap.translator;
2020

21-
import java.util.ArrayList;
22-
import java.util.List;
23-
2421
import com.fasterxml.jackson.databind.JsonNode;
2522
import com.fasterxml.jackson.databind.node.ArrayNode;
2623
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
@@ -44,7 +41,6 @@
4441
import com.redhat.lightblue.metadata.types.StringType;
4542
import com.redhat.lightblue.util.JsonDoc;
4643
import com.unboundid.ldap.sdk.Attribute;
47-
import com.unboundid.ldap.sdk.SearchResult;
4844
import com.unboundid.ldap.sdk.SearchResultEntry;
4945

5046
/**
@@ -60,14 +56,6 @@ public ResultTranslator(JsonNodeFactory factory){
6056
this.factory = factory;
6157
}
6258

63-
public List<DocCtx> translate(SearchResult result, EntityMetadata md){
64-
List<DocCtx> docs = new ArrayList<DocCtx>();
65-
for(SearchResultEntry entry : result.getSearchEntries()){
66-
docs.add(translate(entry, md));
67-
}
68-
return docs;
69-
}
70-
7159
public DocCtx translate(SearchResultEntry entry, EntityMetadata md){
7260
FieldCursor cursor = md.getFieldCursor();
7361
String entityName = md.getEntityInfo().getName();
@@ -143,7 +131,7 @@ else if(type instanceof BinaryType){
143131
}
144132

145133
if(value == null){
146-
throw new NullPointerException("Unable to convert LDAP attribute to json resulting in a null value: " + attr);
134+
throw new NullPointerException("Unable to convert LDAP attribute to json resulting in a null value: " + attr.getName());
147135
}
148136

149137
return field.getType().toJson(factory, value);

0 commit comments

Comments
 (0)