This is an automated email from the ASF dual-hosted git repository.
martin_s pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/archiva-redback-core.git
The following commit(s) were added to refs/heads/master by this push:
new ab1b2b7 Adding query search for groups
ab1b2b7 is described below
commit ab1b2b7bbb0b24d5e600dbaf94fb927172723c95
Author: Martin Stockhammer <[email protected]>
AuthorDate: Sun Jan 17 16:37:40 2021 +0100
Adding query search for groups
---
.../common/ldap/connection/DefaultLdapConnection.java | 5 +++++
.../apache/archiva/redback/rest/api/MessageKeys.java | 1 +
.../redback/rest/api/services/v2/GroupService.java | 2 +-
.../redback/rest/services/v2/DefaultGroupService.java | 18 +++++++++++++++---
4 files changed, 22 insertions(+), 4 deletions(-)
diff --git
a/redback-common/redback-common-ldap/src/main/java/org/apache/archiva/redback/common/ldap/connection/DefaultLdapConnection.java
b/redback-common/redback-common-ldap/src/main/java/org/apache/archiva/redback/common/ldap/connection/DefaultLdapConnection.java
index 7583a46..d86b5d2 100644
---
a/redback-common/redback-common-ldap/src/main/java/org/apache/archiva/redback/common/ldap/connection/DefaultLdapConnection.java
+++
b/redback-common/redback-common-ldap/src/main/java/org/apache/archiva/redback/common/ldap/connection/DefaultLdapConnection.java
@@ -19,6 +19,7 @@ package org.apache.archiva.redback.common.ldap.connection;
* under the License.
*/
+import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -60,6 +61,10 @@ public class DefaultLdapConnection
{
this.config = config;
+ if( config.getBaseDn() == null ) {
+ throw new LdapException( "Invalid BaseDn in the configuration." );
+ }
+
LdapName baseDn = new LdapName( config.getBaseDn().getRdns() );
if ( subRdn != null )
diff --git
a/redback-integrations/redback-rest/redback-rest-api/src/main/java/org/apache/archiva/redback/rest/api/MessageKeys.java
b/redback-integrations/redback-rest/redback-rest-api/src/main/java/org/apache/archiva/redback/rest/api/MessageKeys.java
index a5e018f..9076c00 100644
---
a/redback-integrations/redback-rest/redback-rest-api/src/main/java/org/apache/archiva/redback/rest/api/MessageKeys.java
+++
b/redback-integrations/redback-rest/redback-rest-api/src/main/java/org/apache/archiva/redback/rest/api/MessageKeys.java
@@ -79,4 +79,5 @@ public interface MessageKeys
String ERR_REGISTRATION_USER_VALIDATED = "rb.registration.user.validated";
String ERR_REGISTRATION_ROLE_ASSIGNMENT_FAILED =
"rb.registration.role.assignment.failed";
+ String ERR_INVALID_PAGING_RESULT_ERROR = "rb.paging_result.invalid";
}
diff --git
a/redback-integrations/redback-rest/redback-rest-api/src/main/java/org/apache/archiva/redback/rest/api/services/v2/GroupService.java
b/redback-integrations/redback-rest/redback-rest-api/src/main/java/org/apache/archiva/redback/rest/api/services/v2/GroupService.java
index 1f668fc..fe0fd13 100644
---
a/redback-integrations/redback-rest/redback-rest-api/src/main/java/org/apache/archiva/redback/rest/api/services/v2/GroupService.java
+++
b/redback-integrations/redback-rest/redback-rest-api/src/main/java/org/apache/archiva/redback/rest/api/services/v2/GroupService.java
@@ -83,7 +83,7 @@ public interface GroupService
PagedResult<Group> getGroups( @QueryParam("q") @DefaultValue( "" ) String
searchTerm,
@QueryParam( "offset" ) @DefaultValue( "0" )
Integer offset,
@QueryParam( "limit" ) @DefaultValue( value
= DEFAULT_PAGE_LIMIT ) Integer limit,
- @QueryParam( "orderBy") @DefaultValue( "id"
) List<String> orderBy,
+ @QueryParam( "orderBy") @DefaultValue(
"name" ) List<String> orderBy,
@QueryParam("order") @DefaultValue( "asc" )
String order)
throws RedbackServiceException;
diff --git
a/redback-integrations/redback-rest/redback-rest-services/src/main/java/org/apache/archiva/redback/rest/services/v2/DefaultGroupService.java
b/redback-integrations/redback-rest/redback-rest-services/src/main/java/org/apache/archiva/redback/rest/services/v2/DefaultGroupService.java
index 80ccb5e..eb437f7 100644
---
a/redback-integrations/redback-rest/redback-rest-services/src/main/java/org/apache/archiva/redback/rest/services/v2/DefaultGroupService.java
+++
b/redback-integrations/redback-rest/redback-rest-services/src/main/java/org/apache/archiva/redback/rest/services/v2/DefaultGroupService.java
@@ -56,7 +56,9 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.BiPredicate;
+import java.util.function.Predicate;
import java.util.stream.Collectors;
+import java.util.stream.Stream;
/**
*
@@ -73,7 +75,7 @@ public class DefaultGroupService
{
private static final Logger log = LoggerFactory.getLogger(
DefaultGroupService.class );
- private static final String[] DEFAULT_SEARCH_FIELDS = {"name",
"uniqueName"};
+ private static final String[] DEFAULT_SEARCH_FIELDS = {"name"};
private static final Map<String, BiPredicate<String, LdapGroup>>
FILTER_MAP = new HashMap<>( );
private static final Map<String, Comparator<LdapGroup>> ORDER_MAP = new
HashMap<>( );
private static final QueryHelper<LdapGroup> QUERY_HELPER;
@@ -130,9 +132,16 @@ public class DefaultGroupService
{
try(LdapConnection ldapConnection =
this.ldapConnectionFactory.getConnection())
{
+ boolean isAscending = QUERY_HELPER.isAscending( order );
DirContext context = ldapConnection.getDirContext();
+ Predicate<LdapGroup> filter = QUERY_HELPER.getQueryFilter(
searchTerm );
+ Comparator<LdapGroup> comparator = QUERY_HELPER.getComparator(
orderBy, isAscending );
List<LdapGroup> groups = ldapRoleMapper.getAllGroupObjects(
context );
- return PagedResult.of( groups.size( ), offset, limit,
groups.stream( ).skip( offset ).limit( limit ).map(
DefaultGroupService::getGroupFromLdap ).collect( Collectors.toList( ) ) );
+ int totalCount = Math.toIntExact( groups.stream( ).filter( filter
).count( ) );
+ List<Group> result = groups.stream( ).filter( filter ).sorted(
comparator ).skip( offset ).limit( limit )
+ .map(DefaultGroupService::getGroupFromLdap)
+ .collect( Collectors.toList());
+ return PagedResult.of( totalCount, offset, limit, result );
}
catch ( NamingException e )
{
@@ -141,6 +150,9 @@ public class DefaultGroupService
} catch (MappingException e) {
log.error( "Mapping Error {}", e.getMessage(), e );
throw new RedbackServiceException( ErrorMessage.of(
MessageKeys.ERR_ROLE_MAPPING, e.getMessage( ) ) );
+ } catch (ArithmeticException e) {
+ log.error( "Could not convert total count to integer: {}",
e.getMessage( ) );
+ throw new RedbackServiceException( ErrorMessage.of(
MessageKeys.ERR_INVALID_PAGING_RESULT_ERROR ) );
}
}
@@ -288,7 +300,7 @@ public class DefaultGroupService
{
try
{
- ldapRoleMapperConfiguration.addLdapMapping( groupName,
Arrays.asList( groupName) );
+ ldapRoleMapperConfiguration.addLdapMapping( groupName,
Arrays.asList( roleId) );
return Response.ok( ).build( );
}
catch ( MappingException mappingException )