diff options
author | Martin Schreier <martin_s@apache.org> | 2022-02-13 10:27:32 +0100 |
---|---|---|
committer | Martin Schreier <martin_s@apache.org> | 2022-02-13 10:27:32 +0100 |
commit | 0a2dab16770cce10bda6fa2cc2e2d9c7d64c5dbf (patch) | |
tree | 1e633e0ae7fc56a857b5467a12a67adfc017b9e2 /archiva-modules/archiva-web/archiva-rest/archiva-rest-services | |
parent | 99bd81ac000598105529473a53f62a5b53d885a5 (diff) | |
download | archiva-0a2dab16770cce10bda6fa2cc2e2d9c7d64c5dbf.tar.gz archiva-0a2dab16770cce10bda6fa2cc2e2d9c7d64c5dbf.zip |
Improving event API
Diffstat (limited to 'archiva-modules/archiva-web/archiva-rest/archiva-rest-services')
4 files changed, 79 insertions, 36 deletions
diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/pom.xml b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/pom.xml index 7dd8dad50..519fb1e5b 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/pom.xml +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/pom.xml @@ -49,6 +49,10 @@ <artifactId>archiva-storage-api</artifactId> </dependency> <dependency> + <groupId>org.apache.archiva.event</groupId> + <artifactId>archiva-event-api</artifactId> + </dependency> + <dependency> <groupId>org.apache.archiva</groupId> <artifactId>archiva-repository-admin-api</artifactId> </dependency> diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/v2/svc/AbstractService.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/v2/svc/AbstractService.java new file mode 100644 index 000000000..09e367e8a --- /dev/null +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/v2/svc/AbstractService.java @@ -0,0 +1,51 @@ +package org.apache.archiva.rest.v2.svc; +/* + * 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 org.apache.archiva.admin.model.AuditInformation; +import org.apache.archiva.redback.rest.services.RedbackAuthenticationThreadLocal; +import org.apache.archiva.redback.rest.services.RedbackRequestInformation; +import org.apache.archiva.redback.users.User; + +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.core.Context; + +/** + * @author Martin Schreier <martin_s@apache.org> + */ +public class AbstractService +{ + @Context + private HttpServletRequest httpServletRequest; + + protected AuditInformation getAuditInformation( ) + { + RedbackRequestInformation redbackRequestInformation = RedbackAuthenticationThreadLocal.get( ); + User user; + String remoteAddr; + if (redbackRequestInformation==null) { + user = null; + remoteAddr = httpServletRequest.getRemoteAddr( ); + } else + { + user = redbackRequestInformation.getUser( ); + remoteAddr = redbackRequestInformation.getRemoteAddr( ); + } + return new AuditInformation( user, remoteAddr ); + } +} diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/v2/svc/maven/DefaultMavenManagedRepositoryService.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/v2/svc/maven/DefaultMavenManagedRepositoryService.java index 5bec54dff..406acff59 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/v2/svc/maven/DefaultMavenManagedRepositoryService.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/v2/svc/maven/DefaultMavenManagedRepositoryService.java @@ -20,13 +20,12 @@ package org.apache.archiva.rest.v2.svc.maven; import org.apache.archiva.admin.model.AuditInformation; import org.apache.archiva.admin.model.RepositoryAdminException; import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin; +import org.apache.archiva.common.MultiModelMapper; import org.apache.archiva.components.rest.model.PagedResult; import org.apache.archiva.components.rest.util.QueryHelper; import org.apache.archiva.configuration.model.ManagedRepositoryConfiguration; import org.apache.archiva.redback.authentication.AuthenticationResult; import org.apache.archiva.redback.authorization.AuthorizationException; -import org.apache.archiva.redback.rest.services.RedbackAuthenticationThreadLocal; -import org.apache.archiva.redback.rest.services.RedbackRequestInformation; import org.apache.archiva.redback.system.DefaultSecuritySession; import org.apache.archiva.redback.system.SecuritySession; import org.apache.archiva.redback.system.SecuritySystem; @@ -36,6 +35,7 @@ import org.apache.archiva.redback.users.UserNotFoundException; import org.apache.archiva.repository.ManagedRepository; import org.apache.archiva.repository.ReleaseScheme; import org.apache.archiva.repository.Repository; +import org.apache.archiva.repository.RepositoryException; import org.apache.archiva.repository.RepositoryRegistry; import org.apache.archiva.repository.RepositoryType; import org.apache.archiva.repository.content.ContentItem; @@ -44,10 +44,12 @@ import org.apache.archiva.repository.storage.fs.FsStorageUtil; import org.apache.archiva.rest.api.v2.model.FileInfo; import org.apache.archiva.rest.api.v2.model.MavenManagedRepository; import org.apache.archiva.rest.api.v2.model.MavenManagedRepositoryUpdate; +import org.apache.archiva.rest.api.v2.model.map.ServiceMapperFactory; import org.apache.archiva.rest.api.v2.svc.ArchivaRestServiceException; import org.apache.archiva.rest.api.v2.svc.ErrorKeys; import org.apache.archiva.rest.api.v2.svc.ErrorMessage; import org.apache.archiva.rest.api.v2.svc.maven.MavenManagedRepositoryService; +import org.apache.archiva.rest.v2.svc.AbstractService; import org.apache.archiva.security.common.ArchivaRoleConstants; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; @@ -65,14 +67,14 @@ import java.util.List; import java.util.function.Predicate; import java.util.stream.Collectors; -import static org.apache.archiva.security.common.ArchivaRoleConstants.OPERATION_READ_REPOSITORY; import static org.apache.archiva.security.common.ArchivaRoleConstants.OPERATION_ADD_ARTIFACT; +import static org.apache.archiva.security.common.ArchivaRoleConstants.OPERATION_READ_REPOSITORY; /** * @author Martin Stockhammer <martin_s@apache.org> */ @Service("v2.managedMavenRepositoryService#rest") -public class DefaultMavenManagedRepositoryService implements MavenManagedRepositoryService +public class DefaultMavenManagedRepositoryService extends AbstractService implements MavenManagedRepositoryService { @Context HttpServletResponse httpServletResponse; @@ -80,6 +82,8 @@ public class DefaultMavenManagedRepositoryService implements MavenManagedReposit @Context UriInfo uriInfo; + + private static final Logger log = LoggerFactory.getLogger( DefaultMavenManagedRepositoryService.class ); private static final QueryHelper<ManagedRepository> QUERY_HELPER = new QueryHelper<>( new String[]{"id", "name"} ); static @@ -96,36 +100,20 @@ public class DefaultMavenManagedRepositoryService implements MavenManagedReposit private final ManagedRepositoryAdmin managedRepositoryAdmin; private final RepositoryRegistry repositoryRegistry; private final SecuritySystem securitySystem; + private final ServiceMapperFactory serviceMapperFactory; + private final MultiModelMapper<MavenManagedRepository, ManagedRepositoryConfiguration, ManagedRepository> mapper; + public DefaultMavenManagedRepositoryService( SecuritySystem securitySystem, RepositoryRegistry repositoryRegistry, - ManagedRepositoryAdmin managedRepositoryAdmin ) + ManagedRepositoryAdmin managedRepositoryAdmin, + ServiceMapperFactory serviceMapperFactory ) throws IllegalArgumentException { this.securitySystem = securitySystem; this.repositoryRegistry = repositoryRegistry; this.managedRepositoryAdmin = managedRepositoryAdmin; - } - - protected AuditInformation getAuditInformation( ) - { - RedbackRequestInformation redbackRequestInformation = RedbackAuthenticationThreadLocal.get( ); - User user; - String remoteAddr; - if (redbackRequestInformation==null) { - user = null; - remoteAddr = null; - } else - { - user = redbackRequestInformation.getUser( ); - remoteAddr = redbackRequestInformation.getRemoteAddr( ); - } - return new AuditInformation( user, remoteAddr ); - } - - public static ManagedRepositoryConfiguration toConfig(MavenManagedRepository repo) { - ManagedRepositoryConfiguration cfg = new ManagedRepositoryConfiguration( ); - return cfg; - + this.serviceMapperFactory = serviceMapperFactory; + this.mapper = serviceMapperFactory.getMapper( MavenManagedRepository.class, ManagedRepositoryConfiguration.class, ManagedRepository.class ); } @Override @@ -140,7 +128,7 @@ public class DefaultMavenManagedRepositoryService implements MavenManagedReposit final Comparator<ManagedRepository> comparator = QUERY_HELPER.getComparator( orderBy, order ); int totalCount = Math.toIntExact( repos.stream( ).filter( queryFilter ).count( ) ); return PagedResult.of( totalCount, offset, limit, repos.stream( ).filter( queryFilter ).sorted( comparator ) - .map( MavenManagedRepository::of ).skip( offset ).limit( limit ).collect( Collectors.toList( ) ) ); + .map( mapper::reverseMap ).skip( offset ).limit( limit ).collect( Collectors.toList( ) ) ); } catch (ArithmeticException e) { log.error( "Invalid number of repositories detected." ); @@ -158,7 +146,7 @@ public class DefaultMavenManagedRepositoryService implements MavenManagedReposit if (repo.getType()!=RepositoryType.MAVEN) { throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_WRONG_TYPE, repositoryId, repo.getType().name() ), 404 ); } - return MavenManagedRepository.of( repo ); + return mapper.reverseMap( repo ); } @Override @@ -220,13 +208,14 @@ public class DefaultMavenManagedRepositoryService implements MavenManagedReposit } try { - managedRepositoryAdmin.addManagedRepository( convert( managedRepository ), managedRepository.hasStagingRepository(), getAuditInformation() ); + repositoryRegistry.putRepository( mapper.map( managedRepository ) ); httpServletResponse.setStatus( 201 ); - return MavenManagedRepository.of( repositoryRegistry.getManagedRepository( repoId ) ); + return mapper.reverseMap( repositoryRegistry.getManagedRepository( repoId ) ); } - catch ( RepositoryAdminException e ) + catch ( RepositoryException e ) { - throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_ADMIN_ERROR, e.getMessage( ) ) ); + log.error( "Could not create repository: {}", e.getMessage( ), e ); + throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_ADD_FAILED, repoId ) ); } } @@ -241,7 +230,7 @@ public class DefaultMavenManagedRepositoryService implements MavenManagedReposit if (newRepo==null) { throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_UPDATE_FAILED, repositoryId ) ); } - return MavenManagedRepository.of( newRepo ); + return mapper.reverseMap( newRepo ); } catch ( RepositoryAdminException e ) { diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/v2/svc/maven/NativeMavenManagedRepositoryServiceTest.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/v2/svc/maven/NativeMavenManagedRepositoryServiceTest.java index 3dffabed1..d012d6175 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/v2/svc/maven/NativeMavenManagedRepositoryServiceTest.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/v2/svc/maven/NativeMavenManagedRepositoryServiceTest.java @@ -100,7 +100,6 @@ public class NativeMavenManagedRepositoryServiceTest extends AbstractNativeRestS } - @Disabled @Test @Order( 2 ) void testCreateRepository() { @@ -111,7 +110,7 @@ public class NativeMavenManagedRepositoryServiceTest extends AbstractNativeRestS assertNotNull( json ); assertEquals( "repo001", json.get( "id" ) ); assertEquals( "Repository 001", json.get( "name" ) ); - assertEquals( "maven", json.get( "type" ) ); + assertEquals( "MAVEN", json.get( "type" ) ); assertEquals( "This is repository 001", json.get( "description" ) ); } |