Skip to content

Commit 6d1fefd

Browse files
committed
changes based on feedback
1 parent 4097c7e commit 6d1fefd

File tree

15 files changed

+660
-137
lines changed

15 files changed

+660
-137
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
Copyright 2015 Red Hat, Inc. and/or its affiliates.
3+
4+
This file is part of lightblue.
5+
6+
This program is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License as published by
8+
the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
This program is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
package com.redhat.lightblue.common.ldap;
20+
21+
/**
22+
* Error codes specific to LDAP
23+
*
24+
* @author dcrissman
25+
*/
26+
public final class LdapErrorCode {
27+
28+
/** An unsupported feature was used. */
29+
public static final String ERR_UNSUPPORTED_FEATURE = "ldap:UnsupportedFeature:";
30+
31+
/** Lightblue-Ldap does not currently support object arrays. This error indicates that such a field was used. */
32+
public static final String ERR_UNSUPPORTED_FEATURE_OBJECT_ARRAY = ERR_UNSUPPORTED_FEATURE + "ObjectArray";
33+
34+
private LdapErrorCode(){}
35+
36+
}

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

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@
2424

2525
import com.fasterxml.jackson.databind.JsonNode;
2626
import com.redhat.lightblue.common.ldap.LdapConstant;
27+
import com.redhat.lightblue.common.ldap.LdapErrorCode;
2728
import com.redhat.lightblue.common.ldap.LdapFieldNameTranslator;
2829
import com.redhat.lightblue.common.ldap.LightblueUtil;
2930
import com.redhat.lightblue.metadata.ArrayElement;
3031
import com.redhat.lightblue.metadata.ArrayField;
3132
import com.redhat.lightblue.metadata.EntityMetadata;
33+
import com.redhat.lightblue.metadata.MetadataConstants;
3234
import com.redhat.lightblue.metadata.SimpleField;
3335
import com.redhat.lightblue.metadata.Type;
3436
import com.redhat.lightblue.metadata.types.BinaryType;
@@ -54,11 +56,17 @@ public EntryBuilder(EntityMetadata md, LdapFieldNameTranslator fieldNameTranslat
5456
}
5557

5658
public Entry build(String dn, JsonDoc document){
59+
Error.push("build entry");
5760
Error.push(LdapConstant.ATTRIBUTE_DN + "=" + dn);
58-
Entry entry = new Entry(dn);
59-
translate(document, entry);
60-
Error.pop();
61-
return entry;
61+
try{
62+
Entry entry = new Entry(dn);
63+
translate(document, entry);
64+
return entry;
65+
}
66+
finally{
67+
Error.pop();
68+
Error.pop();
69+
}
6270
}
6371

6472
@Override
@@ -79,10 +87,8 @@ protected void translate(SimpleField field, JsonNode node, Entry target) {
7987
String attributeName = fieldNameTranslator.translateFieldName(field.getFullPath());
8088

8189
if(LdapConstant.ATTRIBUTE_DN.equalsIgnoreCase(attributeName)){
82-
throw Error.get("Invalid Field Definition", "'" + LdapConstant.ATTRIBUTE_DN + "' should not be included as its value will be"
83-
+ " derived from the metadata.basedn and"
84-
+ " the metadata.uniqueattr. Including the '" + LdapConstant.ATTRIBUTE_DN
85-
+ "' as an insert attribute is confusing.");
90+
//DN is derived using metadata.uniqueattr, providing it is confusing.
91+
throw Error.get(MetadataConstants.ERR_INVALID_FIELD_REFERENCE, LdapConstant.ATTRIBUTE_DN);
8692
}
8793
else if(LightblueUtil.isFieldObjectType(attributeName)
8894
|| LightblueUtil.isFieldAnArrayCount(attributeName, getEntityMetadata().getFields())){
@@ -127,7 +133,7 @@ protected void translateSimpleArray(ArrayField field, List<Object> items, Entry
127133

128134
@Override
129135
protected void translateObjectArray(ArrayField field, JsonNodeCursor cursor, Entry target) {
130-
throw Error.get("Unsupported Feature: object array", field.getFullPath().toString());
136+
throw Error.get(LdapErrorCode.ERR_UNSUPPORTED_FEATURE_OBJECT_ARRAY, field.getFullPath().toString());
131137
}
132138

133139
}

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

Lines changed: 6 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -44,24 +44,17 @@
4444
import com.redhat.lightblue.crud.CRUDUpdateResponse;
4545
import com.redhat.lightblue.crud.CrudConstants;
4646
import com.redhat.lightblue.crud.DocCtx;
47-
import com.redhat.lightblue.crud.ldap.model.TrivialLdapFieldNameTranslator;
4847
import com.redhat.lightblue.crud.ldap.translator.FilterTranslator;
4948
import com.redhat.lightblue.crud.ldap.translator.ResultTranslator;
5049
import com.redhat.lightblue.crud.ldap.translator.SortTranslator;
5150
import com.redhat.lightblue.eval.FieldAccessRoleEvaluator;
5251
import com.redhat.lightblue.eval.Projector;
5352
import com.redhat.lightblue.hystrix.ldap.InsertCommand;
54-
import com.redhat.lightblue.metadata.ArrayField;
5553
import com.redhat.lightblue.metadata.DataStore;
56-
import com.redhat.lightblue.metadata.EntityInfo;
5754
import com.redhat.lightblue.metadata.EntityMetadata;
5855
import com.redhat.lightblue.metadata.FieldCursor;
59-
import com.redhat.lightblue.metadata.Fields;
60-
import com.redhat.lightblue.metadata.Metadata;
56+
import com.redhat.lightblue.metadata.MetadataConstants;
6157
import com.redhat.lightblue.metadata.MetadataListener;
62-
import com.redhat.lightblue.metadata.SimpleArrayElement;
63-
import com.redhat.lightblue.metadata.SimpleField;
64-
import com.redhat.lightblue.metadata.types.IntegerType;
6558
import com.redhat.lightblue.metadata.types.StringType;
6659
import com.redhat.lightblue.query.Projection;
6760
import com.redhat.lightblue.query.QueryExpression;
@@ -107,7 +100,7 @@ public CRUDInsertionResponse insert(CRUDOperationContext ctx,
107100

108101
EntityMetadata md = ctx.getEntityMetadata(ctx.getEntityName());
109102
LdapDataStore store = getLdapDataStore(md);
110-
LdapFieldNameTranslator fieldNameTranslator = getLdapFieldNameTranslator(md);
103+
LdapFieldNameTranslator fieldNameTranslator = LdapCrudUtil.getLdapFieldNameTranslator(md);
111104

112105
FieldAccessRoleEvaluator roles = new FieldAccessRoleEvaluator(md, ctx.getCallerRoles());
113106
EntryBuilder entryBuilder = new EntryBuilder(md, fieldNameTranslator);
@@ -128,7 +121,7 @@ public CRUDInsertionResponse insert(CRUDOperationContext ctx,
128121
Path uniqueFieldPath = fieldNameTranslator.translateAttributeName(store.getUniqueAttribute());
129122
JsonNode uniqueNode = document.get(uniqueFieldPath);
130123
if(uniqueNode == null){
131-
throw Error.get("Required Field", store.getUniqueAttribute());
124+
throw Error.get(MetadataConstants.ERR_PARSE_MISSING_ELEMENT, store.getUniqueAttribute());
132125
}
133126

134127
String dn = createDN(store, uniqueNode.asText());
@@ -206,7 +199,7 @@ public CRUDFindResponse find(CRUDOperationContext ctx,
206199

207200
LDAPConnection connection = getNewLdapConnection(store);
208201

209-
LdapFieldNameTranslator fieldNameTranslator = getLdapFieldNameTranslator(md);
202+
LdapFieldNameTranslator fieldNameTranslator = LdapCrudUtil.getLdapFieldNameTranslator(md);
210203

211204
try {
212205
//TODO: Support scopes other than SUB
@@ -265,43 +258,7 @@ public void updatePredefinedFields(CRUDOperationContext ctx, JsonDoc doc) {
265258

266259
@Override
267260
public MetadataListener getMetadataListener() {
268-
return new MetadataListener() {
269-
270-
@Override
271-
public void beforeUpdateEntityInfo(Metadata m, EntityInfo ei, boolean newEntity) {
272-
//Do Nothing!!
273-
}
274-
275-
/**
276-
* Ensure that dn and objectClass are on the entity.
277-
*/
278-
@Override
279-
public void beforeCreateNewSchema(Metadata m, EntityMetadata md) {
280-
LdapFieldNameTranslator ldapNameTranslator = getLdapFieldNameTranslator(md);
281-
282-
Fields fields = md.getEntitySchema().getFields();
283-
Path dnFieldPath = ldapNameTranslator.translateAttributeName(LdapConstant.ATTRIBUTE_DN);
284-
if(!fields.has(dnFieldPath.toString())){
285-
fields.addNew(new SimpleField(dnFieldPath.toString(), StringType.TYPE));
286-
}
287-
288-
Path objectClassFieldPath = ldapNameTranslator.translateAttributeName(LdapConstant.ATTRIBUTE_OBJECT_CLASS);
289-
if(!fields.has(objectClassFieldPath.toString())){
290-
fields.addNew(new ArrayField(objectClassFieldPath.toString(), new SimpleArrayElement(StringType.TYPE)));
291-
fields.addNew(new SimpleField(LightblueUtil.createArrayCountFieldName(objectClassFieldPath.toString()), IntegerType.TYPE));
292-
}
293-
}
294-
295-
@Override
296-
public void afterUpdateEntityInfo(Metadata m, EntityInfo ei, boolean newEntity) {
297-
//Do Nothing!!
298-
}
299-
300-
@Override
301-
public void afterCreateNewSchema(Metadata m, EntityMetadata md) {
302-
//Do Nothing!!
303-
}
304-
};
261+
return new LdapMetadataListener();
305262
}
306263

307264
/**
@@ -320,26 +277,6 @@ private LdapDataStore getLdapDataStore(EntityMetadata md){
320277
return (LdapDataStore) store;
321278
}
322279

323-
/**
324-
* Shortcut method to get and return the {@link LdapFieldNameTranslator} on the passed
325-
* in {@link EntityMetadata}.
326-
* @param md - {@link EntityMetadata}.
327-
* @return {@link LdapFieldNameTranslator}
328-
* @throws IllegalArgumentException if an invalid object is found.
329-
*/
330-
private LdapFieldNameTranslator getLdapFieldNameTranslator(EntityMetadata md){
331-
Object o = md.getEntityInfo().getProperties().get(LdapConstant.BACKEND);
332-
333-
if(o == null){
334-
return new TrivialLdapFieldNameTranslator();
335-
}
336-
337-
if(!(o instanceof LdapFieldNameTranslator)){
338-
throw new IllegalArgumentException("Object of type " + o.getClass() + " is not supported.");
339-
}
340-
return (LdapFieldNameTranslator) o;
341-
}
342-
343280
/**
344281
* Creates and returns a unique DN.
345282
* @param store - {@link LdapDataStore} to use as the BaseDN and field that
@@ -418,7 +355,7 @@ private void projectChanges(Projection projection, CRUDOperationContext ctx, Map
418355

419356
EntityMetadata md = ctx.getEntityMetadata(ctx.getEntityName());
420357
JsonNodeFactory factory = ctx.getFactory().getNodeFactory();
421-
LdapFieldNameTranslator fieldNameTranslator = getLdapFieldNameTranslator(md);
358+
LdapFieldNameTranslator fieldNameTranslator = LdapCrudUtil.getLdapFieldNameTranslator(md);
422359

423360
Set<String> requiredAttributeNames = translateFieldNames(fieldNameTranslator, gatherRequiredFields(md, projection, null, null));
424361
Projector projector = Projector.getInstance(
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
Copyright 2015 Red Hat, Inc. and/or its affiliates.
3+
4+
This file is part of lightblue.
5+
6+
This program is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License as published by
8+
the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
This program is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
package com.redhat.lightblue.crud.ldap;
20+
21+
import com.redhat.lightblue.common.ldap.LdapConstant;
22+
import com.redhat.lightblue.common.ldap.LdapFieldNameTranslator;
23+
import com.redhat.lightblue.crud.ldap.model.TrivialLdapFieldNameTranslator;
24+
import com.redhat.lightblue.metadata.EntityMetadata;
25+
26+
/**
27+
* Utility methods for LDAP CRUD operations.
28+
*
29+
* @author dcrissman
30+
*/
31+
public final class LdapCrudUtil {
32+
33+
/**
34+
* Shortcut method to get and return the {@link LdapFieldNameTranslator} on the passed
35+
* in {@link EntityMetadata}.
36+
* @param md - {@link EntityMetadata}.
37+
* @return {@link LdapFieldNameTranslator}
38+
* @throws IllegalArgumentException if an invalid object is found.
39+
*/
40+
public static LdapFieldNameTranslator getLdapFieldNameTranslator(EntityMetadata md){
41+
Object o = md.getEntityInfo().getProperties().get(LdapConstant.BACKEND);
42+
43+
if(o == null){
44+
return new TrivialLdapFieldNameTranslator();
45+
}
46+
47+
if(!(o instanceof LdapFieldNameTranslator)){
48+
throw new IllegalArgumentException("Object of type " + o.getClass() + " is not supported.");
49+
}
50+
return (LdapFieldNameTranslator) o;
51+
}
52+
53+
private LdapCrudUtil(){}
54+
55+
}

0 commit comments

Comments
 (0)