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 e9ea418  Improving LDAP group handling and REST service
e9ea418 is described below

commit e9ea4189119c6933e18232e65b8af124c1caed33
Author: Martin Stockhammer <[email protected]>
AuthorDate: Tue Jul 7 08:46:22 2020 +0200

    Improving LDAP group handling and REST service
---
 .../redback/common/ldap/role/LdapGroup.java        |  8 ++-
 .../redback/common/ldap/role/LdapRoleMapper.java   |  1 +
 .../common/ldap/role/TestLdapRoleMapper.java       | 16 +++++
 .../redback/rest/api/model/GroupMapping.java       |  7 +-
 .../redback/rest/api/model/PagedResult.java        | 69 ++++++++++++++++++++
 .../redback/rest/api/model/PaginationInfo.java     | 75 ++++++++++++++++++++++
 .../rest/api/services/LdapGroupMappingService.java | 15 +++--
 .../redback/rest/api/services/v2/GroupService.java |  5 +-
 .../rest/services/v2/DefaultGroupService.java      |  9 +--
 .../src/main/resources/META-INF/spring-context.xml | 11 +++-
 .../redback/rest/services/v2/GroupServiceTest.java |  2 +-
 11 files changed, 200 insertions(+), 18 deletions(-)

diff --git 
a/redback-common/redback-common-ldap/src/main/java/org/apache/archiva/redback/common/ldap/role/LdapGroup.java
 
b/redback-common/redback-common-ldap/src/main/java/org/apache/archiva/redback/common/ldap/role/LdapGroup.java
index 86e48e2..6e5a937 100644
--- 
a/redback-common/redback-common-ldap/src/main/java/org/apache/archiva/redback/common/ldap/role/LdapGroup.java
+++ 
b/redback-common/redback-common-ldap/src/main/java/org/apache/archiva/redback/common/ldap/role/LdapGroup.java
@@ -20,6 +20,7 @@ package org.apache.archiva.redback.common.ldap.role;
 
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.List;
 
 /**
@@ -30,8 +31,8 @@ import java.util.List;
 public class LdapGroup
 {
     String dn = "";
-    String name;
-    String description;
+    String name = "";
+    String description = "";
     List<String> memberList;
 
     public LdapGroup( )
@@ -92,6 +93,9 @@ public class LdapGroup
     }
 
     public List<String> getMemberList() {
+        if (memberList==null) {
+            return Collections.EMPTY_LIST;
+        }
         return memberList;
     }
 
diff --git 
a/redback-common/redback-common-ldap/src/main/java/org/apache/archiva/redback/common/ldap/role/LdapRoleMapper.java
 
b/redback-common/redback-common-ldap/src/main/java/org/apache/archiva/redback/common/ldap/role/LdapRoleMapper.java
index 638f8ce..016d5f0 100644
--- 
a/redback-common/redback-common-ldap/src/main/java/org/apache/archiva/redback/common/ldap/role/LdapRoleMapper.java
+++ 
b/redback-common/redback-common-ldap/src/main/java/org/apache/archiva/redback/common/ldap/role/LdapRoleMapper.java
@@ -24,6 +24,7 @@ import javax.naming.directory.DirContext;
 import java.util.Collection;
 import java.util.List;
 import java.util.Map;
+import java.util.stream.Stream;
 
 /**
  * will map ldap group to redback role
diff --git 
a/redback-common/redback-common-ldap/src/test/java/org/apache/archiva/redback/common/ldap/role/TestLdapRoleMapper.java
 
b/redback-common/redback-common-ldap/src/test/java/org/apache/archiva/redback/common/ldap/role/TestLdapRoleMapper.java
index 3aabfa3..55e6018 100644
--- 
a/redback-common/redback-common-ldap/src/test/java/org/apache/archiva/redback/common/ldap/role/TestLdapRoleMapper.java
+++ 
b/redback-common/redback-common-ldap/src/test/java/org/apache/archiva/redback/common/ldap/role/TestLdapRoleMapper.java
@@ -53,6 +53,7 @@ import java.util.Arrays;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.stream.Collectors;
 
 /**
  * @author Olivier Lamy
@@ -309,6 +310,21 @@ public class TestLdapRoleMapper
     }
 
     @Test
+    public void getAllGroupObjects()
+        throws Exception
+    {
+        List<LdapGroup> allGroups = ldapRoleMapper.getAllGroupObjects( 
getDirContext() );
+
+        log.info( "allGroups: {}", allGroups );
+
+        assertThat( allGroups ).isNotNull( ).isNotEmpty( );
+        assertThat( allGroups.stream().map(group -> group.getName()).collect( 
Collectors.toList()) ).contains( "archiva/group-with-slash", "archiva-admin",
+            "internal-repo-manager" );
+        assertThat( allGroups.stream().map(group -> group.getDn()).collect( 
Collectors.toList()) ).contains( "cn=archiva/group-with-slash,"+groupSuffix, 
"cn=archiva-admin,"+groupSuffix,
+            "cn=internal-repo-manager,"+groupSuffix );
+    }
+
+    @Test
     public void getGroupsMember()
         throws Exception
     {
diff --git 
a/redback-integrations/redback-rest/redback-rest-api/src/main/java/org/apache/archiva/redback/rest/api/model/GroupMapping.java
 
b/redback-integrations/redback-rest/redback-rest-api/src/main/java/org/apache/archiva/redback/rest/api/model/GroupMapping.java
index 4dd1fdc..c59d2e9 100644
--- 
a/redback-integrations/redback-rest/redback-rest-api/src/main/java/org/apache/archiva/redback/rest/api/model/GroupMapping.java
+++ 
b/redback-integrations/redback-rest/redback-rest-api/src/main/java/org/apache/archiva/redback/rest/api/model/GroupMapping.java
@@ -21,6 +21,7 @@ package org.apache.archiva.redback.rest.api.model;
 import javax.xml.bind.annotation.XmlRootElement;
 import java.io.Serializable;
 import java.util.Collection;
+import java.util.List;
 
 /**
  * @author Olivier Lamy
@@ -32,14 +33,14 @@ public class GroupMapping
 {
     private String group;
 
-    private Collection<String> roleNames;
+    private List<String> roleNames;
 
     public GroupMapping()
     {
         // no op
     }
 
-    public GroupMapping( String group, Collection<String> roleNames )
+    public GroupMapping( String group, List<String> roleNames )
     {
         this.group = group;
         this.roleNames = roleNames;
@@ -60,7 +61,7 @@ public class GroupMapping
         return roleNames;
     }
 
-    public void setRoleNames( Collection<String> roleNames )
+    public void setRoleNames( List<String> roleNames )
     {
         this.roleNames = roleNames;
     }
diff --git 
a/redback-integrations/redback-rest/redback-rest-api/src/main/java/org/apache/archiva/redback/rest/api/model/PagedResult.java
 
b/redback-integrations/redback-rest/redback-rest-api/src/main/java/org/apache/archiva/redback/rest/api/model/PagedResult.java
new file mode 100644
index 0000000..c0af729
--- /dev/null
+++ 
b/redback-integrations/redback-rest/redback-rest-api/src/main/java/org/apache/archiva/redback/rest/api/model/PagedResult.java
@@ -0,0 +1,69 @@
+package org.apache.archiva.redback.rest.api.model;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import javax.xml.bind.annotation.XmlRootElement;
+import java.util.List;
+
+/**
+ * @author Martin Stockhammer <[email protected]>
+ */
+@XmlRootElement(name="pagedResult")
+public class PagedResult<T>
+{
+    PaginationInfo pagination;
+    List<T> data;
+
+    public PagedResult() {
+
+    }
+
+    public PagedResult( long totalCount, long offset, long limit, List<T> data 
) {
+        this.data = data;
+        this.pagination = new PaginationInfo( totalCount, offset, limit );
+    }
+
+    public static final <T> PagedResult<T> ofAllElements(long offset, long 
limit, List<T> elements) {
+        return new PagedResult( elements.size( ), offset, limit, 
elements.subList( (int)offset, (int)offset + (int)limit ) );
+    }
+
+    public static final <T> PagedResult<T> ofSegment(long totalSize, long 
offset, long limit, List<T> elements) {
+        return new PagedResult( totalSize, offset, limit, elements);
+    }
+
+    public List<T> getData( )
+    {
+        return data;
+    }
+
+    public void setData( List<T> data )
+    {
+        this.data = data;
+    }
+
+    public PaginationInfo getPagination( )
+    {
+        return pagination;
+    }
+
+    public void setPagination( PaginationInfo pagination )
+    {
+        this.pagination = pagination;
+    }
+}
diff --git 
a/redback-integrations/redback-rest/redback-rest-api/src/main/java/org/apache/archiva/redback/rest/api/model/PaginationInfo.java
 
b/redback-integrations/redback-rest/redback-rest-api/src/main/java/org/apache/archiva/redback/rest/api/model/PaginationInfo.java
new file mode 100644
index 0000000..073ea18
--- /dev/null
+++ 
b/redback-integrations/redback-rest/redback-rest-api/src/main/java/org/apache/archiva/redback/rest/api/model/PaginationInfo.java
@@ -0,0 +1,75 @@
+package org.apache.archiva.redback.rest.api.model;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import javax.xml.bind.annotation.XmlElement;
+import javax.xml.bind.annotation.XmlRootElement;
+
+/**
+ * @author Martin Stockhammer <[email protected]>
+ */
+@XmlRootElement(name="pagination")
+public class PaginationInfo
+{
+    long totalCount;
+    long offset;
+    long limit;
+
+    public PaginationInfo() {
+
+    }
+
+    public PaginationInfo( long totalCount, long offset, long limit )
+    {
+        this.totalCount = totalCount;
+        this.offset = offset;
+        this.limit = limit;
+    }
+
+    @XmlElement(name="total_count")
+    public long getTotalCount( )
+    {
+        return totalCount;
+    }
+
+    public void setTotalCount( long totalCount )
+    {
+        this.totalCount = totalCount;
+    }
+
+    public long getOffset( )
+    {
+        return offset;
+    }
+
+    public void setOffset( long offset )
+    {
+        this.offset = offset;
+    }
+
+    public long getLimit( )
+    {
+        return limit;
+    }
+
+    public void setLimit( long limit )
+    {
+        this.limit = limit;
+    }
+}
diff --git 
a/redback-integrations/redback-rest/redback-rest-api/src/main/java/org/apache/archiva/redback/rest/api/services/LdapGroupMappingService.java
 
b/redback-integrations/redback-rest/redback-rest-api/src/main/java/org/apache/archiva/redback/rest/api/services/LdapGroupMappingService.java
index a0ea6b5..718a39f 100644
--- 
a/redback-integrations/redback-rest/redback-rest-api/src/main/java/org/apache/archiva/redback/rest/api/services/LdapGroupMappingService.java
+++ 
b/redback-integrations/redback-rest/redback-rest-api/src/main/java/org/apache/archiva/redback/rest/api/services/LdapGroupMappingService.java
@@ -18,6 +18,7 @@ package org.apache.archiva.redback.rest.api.services;
  * under the License.
  */
 
+import io.swagger.v3.oas.annotations.Operation;
 import io.swagger.v3.oas.annotations.tags.Tag;
 import org.apache.archiva.redback.authorization.RedbackAuthorization;
 import 
org.apache.archiva.redback.integration.security.role.RedbackRoleConstants;
@@ -46,14 +47,17 @@ import java.util.List;
 @Deprecated
 public interface LdapGroupMappingService
 {
-    @Path("ldapGroups")
+
+    @Operation( deprecated = true )
+    @Path( "ldapGroups" )
     @GET
-    @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
-    @RedbackAuthorization(permissions = 
RedbackRoleConstants.CONFIGURATION_EDIT_OPERATION)
-    StringList getLdapGroups()
+    @Produces( {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML} )
+    @RedbackAuthorization( permissions = 
RedbackRoleConstants.CONFIGURATION_EDIT_OPERATION )
+    StringList getLdapGroups( )
         throws RedbackServiceException;
 
 
+    @Operation( deprecated = true )
     @GET
     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
     @RedbackAuthorization(permissions = 
RedbackRoleConstants.CONFIGURATION_EDIT_OPERATION)
@@ -61,6 +65,7 @@ public interface LdapGroupMappingService
         throws RedbackServiceException;
 
 
+    @Operation( deprecated = true )
     @PUT
     @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@@ -68,6 +73,7 @@ public interface LdapGroupMappingService
     ActionStatus addLdapGroupMapping( LdapGroupMapping ldapGroupMapping )
         throws RedbackServiceException;
 
+    @Operation( deprecated = true )
     @DELETE
     @Path("{group}")
     @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@@ -76,6 +82,7 @@ public interface LdapGroupMappingService
     ActionStatus removeLdapGroupMapping( @PathParam("group") String group )
         throws RedbackServiceException;
 
+    @Operation( deprecated = true )
     @POST
     @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
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 7770791..5274986 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
@@ -28,6 +28,7 @@ import org.apache.archiva.redback.rest.api.model.ActionStatus;
 import org.apache.archiva.redback.rest.api.model.Group;
 import org.apache.archiva.redback.rest.api.model.GroupMapping;
 import org.apache.archiva.redback.rest.api.model.GroupMappingUpdateRequest;
+import org.apache.archiva.redback.rest.api.model.PagedResult;
 import org.apache.archiva.redback.rest.api.services.RedbackServiceException;
 
 import javax.ws.rs.Consumes;
@@ -61,8 +62,8 @@ public interface GroupService
             @ApiResponse( description = "List of group objects. The number of 
returned results depend on the pagination parameters offset and limit." )
         }
     )
-    List<Group> getGroups( @QueryParam( "offset" ) @DefaultValue( "0" ) Long 
offset,
-                           @QueryParam( "limit" ) @DefaultValue( value = 
Long.MAX_VALUE+"" ) Long limit)
+    PagedResult<Group> getGroups( @QueryParam( "offset" ) @DefaultValue( "0" ) 
Long offset,
+                                  @QueryParam( "limit" ) @DefaultValue( value 
= Long.MAX_VALUE+"" ) Long limit)
         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 f6428df..ae1bce0 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
@@ -29,7 +29,7 @@ import org.apache.archiva.redback.rest.api.model.ActionStatus;
 import org.apache.archiva.redback.rest.api.model.Group;
 import org.apache.archiva.redback.rest.api.model.GroupMapping;
 import org.apache.archiva.redback.rest.api.model.GroupMappingUpdateRequest;
-import org.apache.archiva.redback.rest.api.model.StringList;
+import org.apache.archiva.redback.rest.api.model.PagedResult;
 import org.apache.archiva.redback.rest.api.services.RedbackServiceException;
 import org.apache.archiva.redback.rest.api.services.v2.GroupService;
 import org.slf4j.Logger;
@@ -82,7 +82,7 @@ public class DefaultGroupService
     }
 
     @Override
-    public List<Group> getGroups( Long offset, Long limit ) throws 
RedbackServiceException
+    public PagedResult<Group> getGroups( Long offset, Long limit ) throws 
RedbackServiceException
     {
         LdapConnection ldapConnection = null;
 
@@ -92,7 +92,8 @@ public class DefaultGroupService
         {
             ldapConnection = ldapConnectionFactory.getConnection();
             context = ldapConnection.getDirContext();
-            return ldapRoleMapper.getAllGroupObjects( context ).stream( 
).skip( offset ).limit( limit ).map( DefaultGroupService::getGroupFromLdap 
).collect( Collectors.toList( ) );
+            List<LdapGroup> groups = ldapRoleMapper.getAllGroupObjects( 
context );
+            return PagedResult.ofSegment( groups.size( ), offset, limit, 
groups.stream( ).skip( offset ).limit( limit ).map( 
DefaultGroupService::getGroupFromLdap ).collect( Collectors.toList( ) ) );
         }
         catch ( LdapException | MappingException e )
         {
@@ -116,7 +117,7 @@ public class DefaultGroupService
             List<GroupMapping> ldapGroupMappings = new ArrayList<>( map.size( 
) );
             for ( Map.Entry<String, Collection<String>> entry : map.entrySet() 
)
             {
-                GroupMapping ldapGroupMapping = new GroupMapping( 
entry.getKey(), entry.getValue() );
+                GroupMapping ldapGroupMapping = new GroupMapping( 
entry.getKey( ), new ArrayList<>( entry.getValue( ) ) );
                 ldapGroupMappings.add( ldapGroupMapping );
             }
 
diff --git 
a/redback-integrations/redback-rest/redback-rest-services/src/main/resources/META-INF/spring-context.xml
 
b/redback-integrations/redback-rest/redback-rest-services/src/main/resources/META-INF/spring-context.xml
index 8850ee3..356f704 100644
--- 
a/redback-integrations/redback-rest/redback-rest-services/src/main/resources/META-INF/spring-context.xml
+++ 
b/redback-integrations/redback-rest/redback-rest-services/src/main/resources/META-INF/spring-context.xml
@@ -47,8 +47,15 @@
     <property name="mapper" ref="redbackJacksonXMLMapper"/>
   </bean>
 
-  <bean id="redbackJacksonJsonMapper" 
class="com.fasterxml.jackson.databind.ObjectMapper" />
-  <bean id="redbackJacksonXMLMapper" 
class="com.fasterxml.jackson.dataformat.xml.XmlMapper" />
+  <bean id="redbackJacksonJsonMapper" 
class="com.fasterxml.jackson.databind.ObjectMapper" >
+    <property name="annotationIntrospector" 
ref="jacksonAnnotationIntrospector"/>
+  </bean>
+  <bean id="redbackJacksonXMLMapper" 
class="com.fasterxml.jackson.dataformat.xml.XmlMapper" >
+    <property name="annotationIntrospector" 
ref="jacksonAnnotationIntrospector"/>
+  </bean>
+
+  <bean id="jacksonAnnotationIntrospector" 
class="com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector" />
+
 
 
   <jaxrs:server id="redbackServices" address="/redbackServices">
diff --git 
a/redback-integrations/redback-rest/redback-rest-services/src/test/java/org/apache/archiva/redback/rest/services/v2/GroupServiceTest.java
 
b/redback-integrations/redback-rest/redback-rest-services/src/test/java/org/apache/archiva/redback/rest/services/v2/GroupServiceTest.java
index 88c25de..a54144f 100644
--- 
a/redback-integrations/redback-rest/redback-rest-services/src/test/java/org/apache/archiva/redback/rest/services/v2/GroupServiceTest.java
+++ 
b/redback-integrations/redback-rest/redback-rest-services/src/test/java/org/apache/archiva/redback/rest/services/v2/GroupServiceTest.java
@@ -193,7 +193,7 @@ public class GroupServiceTest
         {
             GroupService service = getGroupService( authorizationHeader );
 
-            List<String> allGroups = service.getGroups( Long.valueOf( 0 ), 
Long.valueOf( Long.MAX_VALUE ) ).stream( ).map( group -> group.getName( ) 
).collect( Collectors.toList( ) );
+            List<String> allGroups = service.getGroups( Long.valueOf( 0 ), 
Long.valueOf( Long.MAX_VALUE ) ).getData().stream( ).map( group -> 
group.getName( ) ).collect( Collectors.toList( ) );
 
             assertThat( allGroups ).isNotNull().isNotEmpty().hasSize( 3 
).containsAll( groups );
         }

Reply via email to