Skip to content

Commit

Permalink
Merge pull request #3 from devatherock/map-to-list
Browse files Browse the repository at this point in the history
Changed the response to a List of maps from a Map
  • Loading branch information
devatherock authored Dec 20, 2020
2 parents 0edbb45 + 3ff99e3 commit bc46624
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 9 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@

## [Unreleased]
### Added
- Initial version. REST endpoint to query a LDAP server
- Initial version. REST endpoint to query a LDAP server

### Changed
- The response to a `List` of maps from a `Map`
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,32 @@ docker run --rm \
### Endpoints
- `/search?filter=<an ldap query>` - Searches LDAP under the given base DN with the supplied filter criteria

### Sample response
```json
[
{
"loginShell": "/bin/bash",
"uid": "testdummy",
"homeDirectory": "/home/test",
"mail": "[email protected]",
"uidNumber": "1234",
"givenName": "Test",
"objectClass": [
"top",
"person",
"organizationalPerson",
"inetOrgPerson",
"shadowAccount",
"posixAccount",
"jumpcloudUser"
],
"sn": "User",
"gidNumber": "1234",
"cn": "Test User"
}
]
```

## Troubleshooting
### Enabling debug logs
- Set the environment variable `LOGGING_LEVEL_ROOT` to `DEBUG` to enable all debug logs - custom and framework
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.devatherock.ldapsearch.controllers;

import java.util.List;
import java.util.Map;

import javax.annotation.Nullable;
Expand Down Expand Up @@ -31,15 +32,15 @@ public class LdapSearchController {
*
* @param baseDn
* @param filter
* @return the search result
* @return the search results
* @throws NamingException
*/
@Get(value = "/search")
public HttpResponse<Map<String, Object>> search(@Nullable @QueryValue(value = "base_dn") String baseDn,
public HttpResponse<List<Map<String, Object>>> search(@Nullable @QueryValue(value = "base_dn") String baseDn,
@QueryValue String filter) throws NamingException {
HttpResponse<Map<String, Object>> response = null;
HttpResponse<List<Map<String, Object>>> response = null;

Map<String, Object> responseBody = searchService.search(baseDn, filter);
List<Map<String, Object>> responseBody = searchService.search(baseDn, filter);
if (responseBody.size() == 0) {
response = HttpResponse.notFound(responseBody);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ public class LdapSearchService {
*
* @param inputBaseDn
* @param filter
* @return the search result
* @return the search results
* @throws NamingException
*/
public Map<String, Object> search(String inputBaseDn, String filter) throws NamingException {
public List<Map<String, Object>> search(String inputBaseDn, String filter) throws NamingException {
// If the credentials are not valid, the constructor
// will throw an exception
LdapContext ldapContext = new InitialLdapContext(initializeLdapEnvironment(), null);
Expand All @@ -62,14 +62,17 @@ public Map<String, Object> search(String inputBaseDn, String filter) throws Nami
ldapContext.setRequestControls(null);

NamingEnumeration<SearchResult> results = null;
Map<String, Object> finalResult = new HashMap<>();
Map<String, Object> transformedResult = null;
List<Map<String, Object>> finalResult = new ArrayList<>();

try {
// Execute the search
results = ldapContext.search(baseDn, filter, searchControls);

while (results.hasMoreElements()) {
readAttributes(finalResult, results.next().getAttributes());
transformedResult = new HashMap<>();
readAttributes(transformedResult, results.next().getAttributes());
finalResult.add(transformedResult);
}
results.close();
} catch (NameNotFoundException e) {
Expand Down

0 comments on commit bc46624

Please sign in to comment.