Skip to content

Commit 6e37b6e

Browse files
committed
start working on crud and hystrix
1 parent 107f259 commit 6e37b6e

File tree

13 files changed

+327
-21
lines changed

13 files changed

+327
-21
lines changed
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
package com.redhat.lightblue.common.ldap;
22

3+
import com.redhat.lightblue.metadata.DataStore;
34
import com.unboundid.ldap.sdk.LDAPConnection;
45
import com.unboundid.ldap.sdk.LDAPException;
56

67

78
public interface DBResolver {
89

910
/**
10-
* Returns a {@link LDAPConnection} based on the backend definition
11+
* Returns a {@link LDAPConnection} based on the backend definition.<br>
12+
* <b>NOTE:</b> A connection pool may be being used behind the scenes, so if
13+
* this method is called multiple times you might get a different connection instance
14+
* to the same database.
1115
*/
12-
LDAPConnection get(LdapDataStore store) throws LDAPException;
16+
LDAPConnection get(DataStore store) throws LDAPException;
1317

1418
}

lightblue-ldap-config/src/main/java/com/redhat/lightblue/config/ldap/LdapDBResolver.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import com.redhat.lightblue.common.ldap.DBResolver;
66
import com.redhat.lightblue.common.ldap.LdapDataStore;
7+
import com.redhat.lightblue.metadata.DataStore;
78
import com.unboundid.ldap.sdk.LDAPConnection;
89
import com.unboundid.ldap.sdk.LDAPException;
910

@@ -15,8 +16,12 @@ public LdapDBResolver(Map<String, LdapDataSourceConfiguration> ldapDataSources){
1516
this.ldapDataSources = ldapDataSources;
1617
}
1718

18-
public LDAPConnection get(LdapDataStore store) throws LDAPException {
19-
return get(store.getDatabase());
19+
public LDAPConnection get(DataStore store) throws LDAPException {
20+
if(!(store instanceof LdapDataStore)){
21+
throw new IllegalArgumentException("DataStore of type " + store.getClass() + " is not supported.");
22+
}
23+
24+
return get(((LdapDataStore)store).getDatabase());
2025
}
2126

2227
public LDAPConnection get(String database) throws LDAPException{

lightblue-ldap-config/src/main/java/com/redhat/lightblue/config/ldap/LdapDataSourceConfiguration.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ public class LdapDataSourceConfiguration implements DataSourceConfiguration{
4242
private static final long serialVersionUID = 3276072662352275664L;
4343
private static final Logger LOGGER = LoggerFactory.getLogger(LdapDataSourceConfiguration.class);
4444

45+
private static final String LDAP_CONFIG_DATABASE = "database";
46+
private static final String LDAP_CONFIG_BINDABLE_DB = "bindableDn";
47+
private static final String LDAP_CONFIG_PASSWORD = "password";
48+
private static final String LDAP_CONFIG_NUMBER_OF_INITIAL_CONNECTIONS = "numberOfInitialConnections";
49+
private static final String LDAP_CONFIG_MAX_NUMBER_OF_CONNECTIONS = "maxNumberOfConnections";
50+
private static final String LDAP_SERVER_CONFIG_HOST = "host";
51+
private static final String LDAP_SERVER_CONFIG_PORT = "port";
52+
4553
private static final int DEFAULT_NUMBER_OF_INITIAL_CONNECTIONS = 5;
4654
private static final int DEFAULT_MAX_NUMBER_OF_CONNECTIONS = 10;
4755

@@ -63,21 +71,21 @@ public void initializeFromJson(JsonNode node) {
6371
return;
6472
}
6573

66-
databaseName = parseJsonNode(node, "database", true).asText();
74+
databaseName = parseJsonNode(node, LDAP_CONFIG_DATABASE, true).asText();
6775

6876
//TODO Add functionality for other BindRequest Types
6977
BindRequest bindRequest = new SimpleBindRequest(
70-
parseJsonNode(node, "bindableDn", true).asText(),
71-
parseJsonNode(node, "password", true).asText());
78+
parseJsonNode(node, LDAP_CONFIG_BINDABLE_DB, true).asText(),
79+
parseJsonNode(node, LDAP_CONFIG_PASSWORD, true).asText());
7280

7381
int initialConnections = DEFAULT_NUMBER_OF_INITIAL_CONNECTIONS;
74-
JsonNode initialConnectionsNode = parseJsonNode(node, "numberOfInitialConnections", false);
82+
JsonNode initialConnectionsNode = parseJsonNode(node, LDAP_CONFIG_NUMBER_OF_INITIAL_CONNECTIONS, false);
7583
if(initialConnectionsNode != null){
7684
initialConnections = initialConnectionsNode.asInt(DEFAULT_NUMBER_OF_INITIAL_CONNECTIONS);
7785
}
7886

7987
int maxConnections = DEFAULT_MAX_NUMBER_OF_CONNECTIONS;
80-
JsonNode maxConnectionsNode = parseJsonNode(node, "maxNumberOfConnections", false);
88+
JsonNode maxConnectionsNode = parseJsonNode(node, LDAP_CONFIG_MAX_NUMBER_OF_CONNECTIONS, false);
8189
if(maxConnectionsNode != null){
8290
maxConnections = maxConnectionsNode.asInt(DEFAULT_MAX_NUMBER_OF_CONNECTIONS);
8391
}
@@ -89,8 +97,8 @@ public void initializeFromJson(JsonNode node) {
8997
while(serversIterator.hasNext()){
9098
JsonNode serverNode = serversIterator.next();
9199
hostPortMap.put(
92-
parseJsonNode(serverNode, "host", true).asText(),
93-
parseJsonNode(serverNode, "port", true).asInt());
100+
parseJsonNode(serverNode, LDAP_SERVER_CONFIG_HOST, true).asText(),
101+
parseJsonNode(serverNode, LDAP_SERVER_CONFIG_PORT, true).asInt());
94102
}
95103
}
96104
else{

lightblue-ldap-crud/pom.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,21 @@
3535
<groupId>com.redhat.lightblue</groupId>
3636
<artifactId>crud</artifactId>
3737
</dependency>
38+
3839
<dependency>
3940
<groupId>com.redhat.lightblue.ldap</groupId>
4041
<artifactId>lightblue-ldap-common</artifactId>
4142
</dependency>
43+
<dependency>
44+
<groupId>com.redhat.lightblue.ldap</groupId>
45+
<artifactId>lightblue-ldap-hystrix</artifactId>
46+
</dependency>
47+
48+
<dependency>
49+
<groupId>com.redhat.lightblue.ldap</groupId>
50+
<artifactId>lightblue-ldap-test</artifactId>
51+
<scope>test</scope>
52+
</dependency>
4253
</dependencies>
4354

4455
</project>

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

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
*/
1919
package com.redhat.lightblue.crud.ldap;
2020

21+
import java.util.List;
22+
23+
import org.slf4j.Logger;
24+
import org.slf4j.LoggerFactory;
25+
2126
import com.redhat.lightblue.common.ldap.DBResolver;
2227
import com.redhat.lightblue.crud.CRUDController;
2328
import com.redhat.lightblue.crud.CRUDDeleteResponse;
@@ -26,15 +31,27 @@
2631
import com.redhat.lightblue.crud.CRUDOperationContext;
2732
import com.redhat.lightblue.crud.CRUDSaveResponse;
2833
import com.redhat.lightblue.crud.CRUDUpdateResponse;
34+
import com.redhat.lightblue.crud.DocCtx;
35+
import com.redhat.lightblue.eval.FieldAccessRoleEvaluator;
36+
import com.redhat.lightblue.eval.Projector;
37+
import com.redhat.lightblue.hystrix.ldap.InsertCommand;
38+
import com.redhat.lightblue.metadata.EntityMetadata;
2939
import com.redhat.lightblue.metadata.MetadataListener;
3040
import com.redhat.lightblue.query.Projection;
3141
import com.redhat.lightblue.query.QueryExpression;
3242
import com.redhat.lightblue.query.Sort;
3343
import com.redhat.lightblue.query.UpdateExpression;
3444
import com.redhat.lightblue.util.JsonDoc;
45+
import com.unboundid.ldap.sdk.Entry;
46+
import com.unboundid.ldap.sdk.LDAPConnection;
47+
import com.unboundid.ldap.sdk.LDAPException;
48+
import com.unboundid.ldap.sdk.LDAPResult;
49+
import com.unboundid.ldap.sdk.ResultCode;
3550

3651
public class LdapCRUDController implements CRUDController{
3752

53+
private static final Logger LOGGER = LoggerFactory.getLogger(LdapCRUDController.class);
54+
3855
private final DBResolver dbResolver;
3956

4057
public LdapCRUDController(DBResolver dbResolver){
@@ -43,8 +60,46 @@ public LdapCRUDController(DBResolver dbResolver){
4360

4461
public CRUDInsertionResponse insert(CRUDOperationContext ctx,
4562
Projection projection) {
46-
// TODO Auto-generated method stub
47-
return null;
63+
CRUDInsertionResponse response = new CRUDInsertionResponse();
64+
List<DocCtx> documents = ctx.getDocumentsWithoutErrors();
65+
if (documents == null || documents.isEmpty()) {
66+
response.setNumInserted(0);
67+
return response;
68+
}
69+
70+
EntityMetadata md = ctx.getEntityMetadata(ctx.getEntityName());
71+
72+
FieldAccessRoleEvaluator roleEval = new FieldAccessRoleEvaluator(md, ctx.getCallerRoles());
73+
Projection combinedProjection = Projection.add(
74+
projection,
75+
roleEval.getExcludedFields(FieldAccessRoleEvaluator.Operation.insert));
76+
77+
Projector projector = null;
78+
if(combinedProjection != null){
79+
projector = Projector.getInstance(combinedProjection, md);
80+
}
81+
82+
try {
83+
LDAPConnection connection = dbResolver.get(md.getDataStore());
84+
85+
for(DocCtx document : documents){
86+
Entry entry = new Entry(""); //TODO populate Entry
87+
88+
InsertCommand command = new InsertCommand(connection, entry);
89+
90+
LDAPResult result = command.execute();
91+
if(result.getResultCode() != ResultCode.SUCCESS){
92+
//TODO: Do something to indicate unsuccessful status
93+
}
94+
95+
}
96+
}
97+
catch (LDAPException e) {
98+
// TODO Auto-generated catch block
99+
e.printStackTrace();
100+
}
101+
102+
return response;
48103
}
49104

50105
public CRUDSaveResponse save(CRUDOperationContext ctx, boolean upsert,
@@ -75,11 +130,9 @@ public CRUDFindResponse find(CRUDOperationContext ctx,
75130

76131
public void updatePredefinedFields(CRUDOperationContext ctx, JsonDoc doc) {
77132
// TODO Auto-generated method stub
78-
79133
}
80134

81135
public MetadataListener getMetadataListener() {
82-
// TODO Auto-generated method stub
83136
return null;
84137
}
85138

lightblue-ldap-hystrix/pom.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,16 @@
3535
<groupId>com.netflix.hystrix</groupId>
3636
<artifactId>hystrix-core</artifactId>
3737
</dependency>
38+
39+
<dependency>
40+
<groupId>com.unboundid</groupId>
41+
<artifactId>unboundid-ldapsdk</artifactId>
42+
</dependency>
43+
44+
<dependency>
45+
<groupId>com.redhat.lightblue.ldap</groupId>
46+
<artifactId>lightblue-ldap-test</artifactId>
47+
<scope>test</scope>
48+
</dependency>
3849
</dependencies>
3950
</project>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.redhat.lightblue.hystrix.ldap;
2+
3+
import com.netflix.hystrix.HystrixCommand;
4+
import com.netflix.hystrix.HystrixCommandGroupKey;
5+
import com.netflix.hystrix.HystrixCommandKey;
6+
import com.unboundid.ldap.sdk.LDAPConnection;
7+
8+
public abstract class AbstractLdapHystrixCommand<T> extends HystrixCommand<T>{
9+
10+
public static final String GROUPKEY = "ldap";
11+
12+
private final LDAPConnection connection;
13+
14+
public LDAPConnection getConnection(){
15+
return connection;
16+
}
17+
18+
public AbstractLdapHystrixCommand(LDAPConnection connection, String commandKey){
19+
super(HystrixCommand.Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey(GROUPKEY)).
20+
andCommandKey(HystrixCommandKey.Factory.asKey(GROUPKEY + ":" + commandKey)));
21+
22+
this.connection = connection;
23+
}
24+
25+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.redhat.lightblue.hystrix.ldap;
2+
3+
import com.unboundid.ldap.sdk.Entry;
4+
import com.unboundid.ldap.sdk.LDAPConnection;
5+
import com.unboundid.ldap.sdk.LDAPResult;
6+
7+
public class InsertCommand extends AbstractLdapHystrixCommand<LDAPResult>{
8+
9+
private final Entry entry;
10+
11+
public InsertCommand(LDAPConnection connection, Entry entry) {
12+
super(connection, InsertCommand.class.getSimpleName());
13+
14+
this.entry = entry;
15+
}
16+
17+
@Override
18+
protected LDAPResult run() throws Exception {
19+
return getConnection().add(entry);
20+
}
21+
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.redhat.lightblue.hystrix.ldap;
2+
3+
import com.unboundid.ldap.sdk.LDAPConnection;
4+
import com.unboundid.ldap.sdk.SearchRequest;
5+
import com.unboundid.ldap.sdk.SearchResult;
6+
7+
public class SearchCommand extends AbstractLdapHystrixCommand<SearchResult>{
8+
9+
private final SearchRequest searchRequest;
10+
11+
public SearchCommand(LDAPConnection connection, SearchRequest searchRequest) {
12+
super(connection, SearchCommand.class.getSimpleName());
13+
14+
this.searchRequest = searchRequest;
15+
}
16+
17+
@Override
18+
protected SearchResult run() throws Exception {
19+
return getConnection().search(searchRequest);
20+
}
21+
22+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.redhat.lightblue.hystrix.ldap;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertNotNull;
5+
6+
import java.util.LinkedHashMap;
7+
8+
import org.junit.Rule;
9+
import org.junit.Test;
10+
11+
import com.redhat.lightblue.ldap.test.LdapServerExternalResource;
12+
import com.redhat.lightblue.ldap.test.LdapServerExternalResource.InMemoryLdapServer;
13+
import com.unboundid.ldap.sdk.Attribute;
14+
import com.unboundid.ldap.sdk.Entry;
15+
import com.unboundid.ldap.sdk.LDAPConnection;
16+
import com.unboundid.ldap.sdk.LDAPException;
17+
import com.unboundid.ldap.sdk.LDAPResult;
18+
import com.unboundid.ldap.sdk.ResultCode;
19+
20+
@InMemoryLdapServer
21+
public class InsertCommandTest {
22+
23+
@SuppressWarnings("serial")
24+
@Rule
25+
public LdapServerExternalResource ldapServer = new LdapServerExternalResource(new LinkedHashMap<String, Attribute[]>(){{
26+
put("dc=com", new Attribute[]{
27+
new Attribute("objectClass", "top"),
28+
new Attribute("objectClass", "domain"),
29+
new Attribute("dc", "com")});
30+
put("dc=example,dc=com", new Attribute[]{
31+
new Attribute("objectClass", "top"),
32+
new Attribute("objectClass", "domain"),
33+
new Attribute("dc", "example")});
34+
}});
35+
36+
@Test
37+
public void testExecute() throws LDAPException{
38+
LDAPConnection connection = new LDAPConnection("localhost", LdapServerExternalResource.DEFAULT_PORT);
39+
40+
Entry entry = new Entry("uid=john.doe,dc=example,dc=com",
41+
new Attribute("objectClass", "top", "person", "organizationalPerson", "inetOrgPerson"),
42+
new Attribute("uid", "john.doe"),
43+
new Attribute("givenName", "John"),
44+
new Attribute("sn", "Doe"),
45+
new Attribute("cn", "John Doe")
46+
);
47+
48+
InsertCommand command = new InsertCommand(connection, entry);
49+
50+
LDAPResult result = command.execute();
51+
52+
assertNotNull(result);
53+
assertEquals(ResultCode.SUCCESS, result.getResultCode());
54+
}
55+
56+
}

0 commit comments

Comments
 (0)