Skip to content

Commit 6d7be8b

Browse files
committed
continue fix for #31
1 parent ca6e7bb commit 6d7be8b

File tree

20 files changed

+301
-129
lines changed

20 files changed

+301
-129
lines changed

lightblue-ldap-common/src/main/java/com/redhat/lightblue/common/ldap/LdapConstant.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626
public final class LdapConstant {
2727

2828
public static final String BACKEND = "ldap";
29-
public static final String FIELD_DN = "dn";
30-
public static final String FIELD_OBJECT_CLASS = "objectClass";
29+
public static final String ATTRIBUTE_DN = "dn";
30+
public static final String ATTRIBUTE_OBJECT_CLASS = "objectClass";
3131

32-
private LdapConstant(){}
32+
private LdapConstant(){} //NOSONAR
3333

3434
}

lightblue-ldap-common/src/main/java/com/redhat/lightblue/common/ldap/LdapMetadataProperty.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,25 @@
1818
*/
1919
package com.redhat.lightblue.common.ldap;
2020

21+
/**
22+
* Represents a class that can provide information about the LDAP metadata property.
23+
*
24+
* @author dcrissman
25+
*/
2126
public interface LdapMetadataProperty {
2227

2328
/**
2429
* Returns the attributeName with the given fieldName.
2530
* @param fieldName - metadata field name
26-
* @return ldap attribute name or <code>null</code> if a match is not present.
31+
* @return ldap attributeName or the fieldName back at you if no mapping is present.
2732
*/
28-
public String getAttributeNameForFieldName(String fieldName);
33+
public String translateFieldName(String fieldName);
2934

3035
/**
3136
* Returns the fieldName with the given attributeName.
3237
* @param attributeName - ldap attribute name
33-
* @return metadata field name or <code>null</code> if a match is not present.
38+
* @return metadata fieldName or the attributeName back at you if no mapping is present.
3439
*/
35-
public String getFieldNameForAttributeName(String attributeName);
40+
public String translateAttributeName(String attributeName);
3641

3742
}

lightblue-ldap-common/src/main/java/com/redhat/lightblue/common/ldap/LightblueUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,6 @@ public static String createArrayFieldNameFromCountField(String countField){
104104
return countField.substring(0, countField.length() - 1);
105105
}
106106

107-
private LightblueUtil(){}
107+
private LightblueUtil(){} //NOSONAR
108108

109109
}

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

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ else if(type instanceof BinaryType){
7575

7676
@Override
7777
protected void translate(SimpleField field, Path path, JsonNode node, Entry target) {
78-
String fieldName = findAttributeName(field.getName());
78+
String fieldName = property.translateFieldName(field.getName());
7979

80-
if(LdapConstant.FIELD_DN.equalsIgnoreCase(fieldName)){
80+
if(LdapConstant.ATTRIBUTE_DN.equalsIgnoreCase(fieldName)){
8181
throw new IllegalArgumentException(
8282
"'dn' should not be included as it's value will be derived from the metadata.basedn and" +
8383
" the metadata.uniqueattr. Including the 'dn' as an insert attribute is confusing.");
@@ -110,7 +110,7 @@ protected void translate(ObjectField field, Path path, JsonNode node, Entry targ
110110
protected void translateSimpleArray(ArrayField field, Path path, List<Object> items, Entry target) {
111111
ArrayElement arrayElement = field.getElement();
112112
Type arrayElementType = arrayElement.getType();
113-
String fieldName = findAttributeName(field.getName());
113+
String fieldName = property.translateFieldName(field.getName());
114114

115115
if(arrayElementType instanceof BinaryType){
116116
List<byte[]> bytes = new ArrayList<byte[]>();
@@ -133,17 +133,4 @@ protected void translateObjectArray(ArrayField field, JsonNodeCursor cursor, Ent
133133
throw new UnsupportedOperationException("Object ArrayField type is not currently supported.");
134134
}
135135

136-
private String findAttributeName(String fieldName){
137-
if(property == null){
138-
return fieldName;
139-
}
140-
141-
String attributeName = property.getAttributeNameForFieldName(fieldName);
142-
if(attributeName == null){
143-
return fieldName;
144-
}
145-
146-
return attributeName;
147-
}
148-
149136
}

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

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package com.redhat.lightblue.crud.ldap;
2020

2121
import java.util.ArrayList;
22+
import java.util.Collection;
2223
import java.util.HashMap;
2324
import java.util.HashSet;
2425
import java.util.List;
@@ -43,6 +44,7 @@
4344
import com.redhat.lightblue.crud.CRUDUpdateResponse;
4445
import com.redhat.lightblue.crud.CrudConstants;
4546
import com.redhat.lightblue.crud.DocCtx;
47+
import com.redhat.lightblue.crud.ldap.model.NullLdapMetadataPropertyImpl;
4648
import com.redhat.lightblue.crud.ldap.translator.FilterTranslator;
4749
import com.redhat.lightblue.crud.ldap.translator.ResultTranslator;
4850
import com.redhat.lightblue.crud.ldap.translator.SortTranslator;
@@ -205,15 +207,17 @@ public CRUDFindResponse find(CRUDOperationContext ctx,
205207

206208
LDAPConnection connection = getNewLdapConnection(store);
207209

210+
LdapMetadataProperty property = getLdapMetadataProperty(md);
211+
208212
try {
209213
//TODO: Support scopes other than SUB
210214
SearchRequest request = new SearchRequest(
211215
store.getBaseDN(),
212216
SearchScope.SUB,
213-
new FilterTranslator().translate(query),
214-
collectRequiredFields(md, projection, query, sort).toArray(new String[0]));
217+
new FilterTranslator(property).translate(query),
218+
translateFieldNames(property, gatherRequiredFields(md, projection, query, sort)).toArray(new String[0]));
215219
if(sort != null){
216-
request.addControl(new ServerSideSortRequestControl(false, new SortTranslator().translate(sort)));
220+
request.addControl(new ServerSideSortRequestControl(false, new SortTranslator(property).translate(sort)));
217221
}
218222
if((from != null) && (from > 0)){
219223
int endPos = to.intValue() - from.intValue();
@@ -223,11 +227,11 @@ public CRUDFindResponse find(CRUDOperationContext ctx,
223227
SearchResult result = connection.search(request);
224228

225229
response.setSize(result.getEntryCount());
226-
ResultTranslator resultTranslator = new ResultTranslator(ctx.getFactory().getNodeFactory());
230+
ResultTranslator resultTranslator = new ResultTranslator(ctx.getFactory().getNodeFactory(), md, property);
227231
List<DocCtx> translatedDocs = new ArrayList<DocCtx>();
228232
for(SearchResultEntry entry : result.getSearchEntries()){
229233
try{
230-
translatedDocs.add(resultTranslator.translate(entry, md));
234+
translatedDocs.add(resultTranslator.translate(entry));
231235
}
232236
catch(Exception e){
233237
DocCtx erroredDoc = new DocCtx(null);
@@ -276,13 +280,18 @@ public void beforeUpdateEntityInfo(Metadata m, EntityInfo ei, boolean newEntity)
276280
*/
277281
@Override
278282
public void beforeCreateNewSchema(Metadata m, EntityMetadata md) {
283+
LdapMetadataProperty property = getLdapMetadataProperty(md);
284+
279285
Fields fields = md.getEntitySchema().getFields();
280-
if(!fields.has(LdapConstant.FIELD_DN)){
281-
fields.addNew(new SimpleField(LdapConstant.FIELD_DN, StringType.TYPE));
286+
String dnFieldName = property.translateAttributeName(LdapConstant.ATTRIBUTE_DN);
287+
if(!fields.has(dnFieldName)){
288+
fields.addNew(new SimpleField(dnFieldName, StringType.TYPE));
282289
}
283-
if(!fields.has(LdapConstant.FIELD_OBJECT_CLASS)){
284-
fields.addNew(new ArrayField(LdapConstant.FIELD_OBJECT_CLASS, new SimpleArrayElement(StringType.TYPE)));
285-
fields.addNew(new SimpleField(LightblueUtil.createArrayCountFieldName(LdapConstant.FIELD_OBJECT_CLASS), IntegerType.TYPE));
290+
291+
String objectClassFieldName = property.translateAttributeName(LdapConstant.ATTRIBUTE_OBJECT_CLASS);
292+
if(!fields.has(objectClassFieldName)){
293+
fields.addNew(new ArrayField(objectClassFieldName, new SimpleArrayElement(StringType.TYPE)));
294+
fields.addNew(new SimpleField(LightblueUtil.createArrayCountFieldName(objectClassFieldName), IntegerType.TYPE));
286295
}
287296
}
288297

@@ -325,7 +334,7 @@ private LdapMetadataProperty getLdapMetadataProperty(EntityMetadata md){
325334
Object o = md.getEntityInfo().getProperties().get(LdapConstant.BACKEND);
326335

327336
if(o == null){
328-
return null;
337+
return new NullLdapMetadataPropertyImpl();
329338
}
330339

331340
if(!(o instanceof LdapMetadataProperty)){
@@ -354,7 +363,7 @@ private String createDN(LdapDataStore store, String uniqueValue){
354363
* @param sort - (optional) {@link Sort}.
355364
* @return list of field names.
356365
*/
357-
private Set<String> collectRequiredFields(EntityMetadata md,
366+
private Set<String> gatherRequiredFields(EntityMetadata md,
358367
Projection projection, QueryExpression query, Sort sort){
359368
Set<String> fields = new HashSet<String>();
360369

@@ -382,6 +391,22 @@ private Set<String> collectRequiredFields(EntityMetadata md,
382391
return fields;
383392
}
384393

394+
/**
395+
* Translates a <code>Collection</code> of fieldNames into a <code>Set</code> of
396+
* attributeNames
397+
* @param property - {@link LdapMetadataProperty}.
398+
* @param fieldNames - <code>Collection</code> of fieldNames to translated
399+
* @return <code>Set</code> of translated attributeNames.
400+
*/
401+
private Set<String> translateFieldNames(LdapMetadataProperty property, Collection<String> fieldNames){
402+
Set<String> attributes = new HashSet<String>();
403+
for(String fieldName : fieldNames){
404+
attributes.add(property.translateFieldName(fieldName));
405+
}
406+
407+
return attributes;
408+
}
409+
385410
/**
386411
* For Insert and Save (and possibly Update), this method will project the results back
387412
* onto the documents.
@@ -396,7 +421,9 @@ private void projectChanges(Projection projection, CRUDOperationContext ctx, Map
396421

397422
EntityMetadata md = ctx.getEntityMetadata(ctx.getEntityName());
398423
JsonNodeFactory factory = ctx.getFactory().getNodeFactory();
399-
Set<String> requiredFields = collectRequiredFields(md, projection, null, null);
424+
LdapMetadataProperty property = getLdapMetadataProperty(md);
425+
426+
Set<String> requiredAttributes = translateFieldNames(property, gatherRequiredFields(md, projection, null, null));
400427
Projector projector = Projector.getInstance(
401428
Projection.add(
402429
projection,
@@ -406,15 +433,17 @@ private void projectChanges(Projection projection, CRUDOperationContext ctx, Map
406433
),
407434
md);
408435

436+
String dnFieldName = property.translateAttributeName(LdapConstant.ATTRIBUTE_DN);
437+
409438
for(Entry<DocCtx, String> insertedDn : documentToDnMap.entrySet()){
410439
DocCtx document = insertedDn.getKey();
411440
String dn = insertedDn.getValue();
412441
DocCtx projectionResponseJson = null;
413442

414443
// If only dn is in the projection, then no need to query LDAP.
415-
if((requiredFields.size() == 1) && requiredFields.contains(LdapConstant.FIELD_DN)){
444+
if((requiredAttributes.size() == 1) && requiredAttributes.contains(LdapConstant.ATTRIBUTE_DN)){
416445
ObjectNode node = factory.objectNode();
417-
node.set(LdapConstant.FIELD_DN, StringType.TYPE.toJson(factory, dn));
446+
node.set(dnFieldName, StringType.TYPE.toJson(factory, dn));
418447
projectionResponseJson = new DocCtx(new JsonDoc(node));
419448
}
420449
//TODO: else fetch entity from LDAP and project results.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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.model;
20+
21+
import com.redhat.lightblue.common.ldap.LdapMetadataProperty;
22+
23+
/**
24+
* An implementation of {@link LdapMetadataProperty} used by crud when
25+
* no other implementation can be found.
26+
*
27+
* @author dcrissman
28+
*/
29+
public class NullLdapMetadataPropertyImpl implements LdapMetadataProperty{
30+
31+
@Override
32+
public String translateFieldName(String fieldName) {
33+
return fieldName;
34+
}
35+
36+
@Override
37+
public String translateAttributeName(String attributeName) {
38+
return attributeName;
39+
}
40+
41+
}

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

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.ArrayList;
2222
import java.util.List;
2323

24+
import com.redhat.lightblue.common.ldap.LdapMetadataProperty;
2425
import com.redhat.lightblue.query.ArrayContainsExpression;
2526
import com.redhat.lightblue.query.ArrayMatchExpression;
2627
import com.redhat.lightblue.query.FieldComparisonExpression;
@@ -40,6 +41,12 @@
4041
*/
4142
public class FilterTranslator {
4243

44+
private final LdapMetadataProperty property;
45+
46+
public FilterTranslator(LdapMetadataProperty property){
47+
this.property = property;
48+
}
49+
4350
/**
4451
* <p>Translates a Lightblue {@link QueryExpression} into a UnboundID {@link Filter}.</p>
4552
* @param query - {@link QueryExpression}
@@ -79,11 +86,11 @@ else if (query instanceof ValueComparisonExpression){
7986
}
8087

8188
private Filter translate(ArrayContainsExpression query){
82-
String field = query.getArray().toString();
89+
String fieldName = property.translateFieldName(query.getArray().toString());
8390

8491
List<Filter> filters = new ArrayList<Filter>();
8592
for(Value value : query.getValues()){
86-
filters.add(Filter.createEqualityFilter(field, value.getValue().toString()));
93+
filters.add(Filter.createEqualityFilter(fieldName, value.getValue().toString()));
8794
}
8895

8996
switch(query.getOp()){
@@ -124,10 +131,10 @@ private Filter translate(NaryLogicalExpression query){
124131
}
125132

126133
private Filter translate(NaryRelationalExpression query){
127-
String field = query.getField().toString();
134+
String fieldName = property.translateFieldName(query.getField().toString());
128135
List<Filter> filters = new ArrayList<Filter>();
129136
for(Value value : query.getValues()){
130-
filters.add(Filter.createEqualityFilter(field, value.getValue().toString()));
137+
filters.add(Filter.createEqualityFilter(fieldName, value.getValue().toString()));
131138
}
132139

133140
switch (query.getOp()){
@@ -155,22 +162,22 @@ private Filter translate(UnaryLogicalExpression query){
155162
}
156163

157164
private Filter translate(ValueComparisonExpression query){
158-
String field = query.getField().toString();
165+
String fieldName = property.translateFieldName(query.getField().toString());
159166
String rValue = query.getRvalue().getValue().toString();
160167

161168
switch(query.getOp()){
162169
case _eq:
163-
return Filter.createEqualityFilter(field, rValue);
170+
return Filter.createEqualityFilter(fieldName, rValue);
164171
case _neq:
165-
return Filter.createNOTFilter(Filter.createEqualityFilter(field, rValue));
172+
return Filter.createNOTFilter(Filter.createEqualityFilter(fieldName, rValue));
166173
case _gte:
167-
return Filter.createGreaterOrEqualFilter(field, rValue);
174+
return Filter.createGreaterOrEqualFilter(fieldName, rValue);
168175
case _lte:
169-
return Filter.createLessOrEqualFilter(field, rValue);
176+
return Filter.createLessOrEqualFilter(fieldName, rValue);
170177
case _gt: //aka. !lte
171-
return Filter.createNOTFilter(Filter.createLessOrEqualFilter(field, rValue));
178+
return Filter.createNOTFilter(Filter.createLessOrEqualFilter(fieldName, rValue));
172179
case _lt: //aka. !gte
173-
return Filter.createNOTFilter(Filter.createGreaterOrEqualFilter(field, rValue));
180+
return Filter.createNOTFilter(Filter.createGreaterOrEqualFilter(fieldName, rValue));
174181
default:
175182
throw new UnsupportedOperationException("Unsupported operation: " + query.getOp());
176183
}

0 commit comments

Comments
 (0)