From: Olivier Lamy Date: Thu, 5 Jul 2012 20:43:58 +0000 (+0000) Subject: oups missed some add files X-Git-Tag: archiva-1.4-M3~547 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=c7165afad7ccd3630e67d5be19035b4ac16cf1fe;p=archiva.git oups missed some add files git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1357890 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/services/ReportRepositoriesService.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/services/ReportRepositoriesService.java new file mode 100644 index 000000000..52365b9cd --- /dev/null +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/services/ReportRepositoriesService.java @@ -0,0 +1,62 @@ +package org.apache.archiva.rest.api.services; + +/* + * Copyright 2012 Zenika + * + * Licensed 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.metadata.repository.stats.RepositoryStatistics; +import org.apache.archiva.redback.authorization.RedbackAuthorization; +import org.apache.archiva.reports.RepositoryProblemFacet; +import org.apache.archiva.security.common.ArchivaRoleConstants; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; +import javax.ws.rs.core.MediaType; +import java.util.Date; +import java.util.List; + +/** + * ReportRepositoriesService + * + * @author Adrien Lecharpentier + * @since 1.4-M3 + */ +@Path( "/reportServices/" ) +public interface ReportRepositoriesService +{ + + @Path( "getStatisticsReport" ) + @GET + @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } ) + @RedbackAuthorization( permissions = ArchivaRoleConstants.OPERATION_MANAGE_CONFIGURATION ) + public List getStatisticsReport( @QueryParam( "repository" ) List repositoriesId, + @QueryParam( "rowCount" ) int rowCount, + @QueryParam( "startDate" ) Date startDate, + @QueryParam( "endDate" ) Date endDate ) + throws ArchivaRestServiceException; + + @Path( "getHealthReports/{repository}/{rowCount}" ) + @GET + @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } ) + @RedbackAuthorization( permissions = ArchivaRoleConstants.OPERATION_MANAGE_CONFIGURATION ) + public List getHealthReport( @PathParam( "repository" ) String repository, + @QueryParam( "groupId" ) String groupId, + @PathParam( "rowCount" ) int rowCount ) + throws ArchivaRestServiceException; + +} diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultReportRepositoriesService.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultReportRepositoriesService.java new file mode 100644 index 000000000..4618f54da --- /dev/null +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultReportRepositoriesService.java @@ -0,0 +1,172 @@ +package org.apache.archiva.rest.services; + +/* + * Copyright 2012 Zenika + * + * Licensed 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.metadata.repository.MetadataRepository; +import org.apache.archiva.metadata.repository.MetadataRepositoryException; +import org.apache.archiva.metadata.repository.RepositorySession; +import org.apache.archiva.metadata.repository.stats.RepositoryStatistics; +import org.apache.archiva.metadata.repository.stats.RepositoryStatisticsManager; +import org.apache.archiva.reports.RepositoryProblemFacet; +import org.apache.archiva.rest.api.services.ArchivaRestServiceException; +import org.apache.archiva.rest.api.services.ReportRepositoriesService; +import org.apache.commons.lang.StringUtils; +import org.springframework.stereotype.Service; + +import javax.inject.Inject; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Date; +import java.util.List; + +/** + * DefaultReportRepositoriesService + * + * @author Adrien Lecharpentier + * @since 1.4-M3 + */ +@Service( "reportRepositoriesService#rest" ) +public class DefaultReportRepositoriesService + extends AbstractRestService + implements ReportRepositoriesService +{ + + private static final String ALL_REPOSITORIES = "all"; + + @Inject + private RepositoryStatisticsManager repositoryStatisticsManager; + + public List getStatisticsReport( List repositoriesId, int rowCount, Date startDate, + Date endDate ) + throws ArchivaRestServiceException + { + switch ( repositoriesId.size() ) + { + case 0: + throw new ArchivaRestServiceException( "report.statistics.report.missing-repositories", null ); + case 1: + return getUniqueRepositoryReport( repositoriesId.get( 0 ), rowCount, startDate, endDate ); + default: + return getMultipleRepositoriesReport( repositoriesId, rowCount ); + } + } + + private List getMultipleRepositoriesReport( List repositoriesId, int rowCount ) + { + RepositorySession repositorySession = repositorySessionFactory.createSession(); + try + { + MetadataRepository metadataRepository = repositorySession.getRepository(); + List stats = new ArrayList(); + for ( String repo : repositoriesId ) + { + try + { + stats.add( repositoryStatisticsManager.getLastStatistics( metadataRepository, repo ) ); + } + catch ( MetadataRepositoryException e ) + { + log.warn( "Unable to retrieve stats, assuming is empty: " + e.getMessage(), e ); + } + } + + return stats.subList( 0, stats.size() > rowCount ? rowCount : stats.size() ); + } + finally + { + repositorySession.close(); + } + } + + private List getUniqueRepositoryReport( String repositoryId, int rowCount, Date startDate, + Date endDate ) + { + RepositorySession repositorySession = repositorySessionFactory.createSession(); + try + { + MetadataRepository metadataRepository = repositorySession.getRepository(); + List stats = null; + try + { + stats = repositoryStatisticsManager.getStatisticsInRange( metadataRepository, repositoryId, startDate, + endDate ); + } + catch ( MetadataRepositoryException e ) + { + log.warn( "Unable to retrieve stats, assuming is empty: " + e.getMessage(), e ); + } + if ( stats == null || stats.isEmpty() ) + { + return Collections.emptyList(); + } + + return stats.subList( 0, stats.size() > rowCount ? rowCount : stats.size() ); + } + finally + { + repositorySession.close(); + } + } + + public List getHealthReport( String repository, String groupId, int rowCount ) + throws ArchivaRestServiceException + { + RepositorySession repositorySession = repositorySessionFactory.createSession(); + try + { + List observableRepositories = getObservableRepos(); + if ( !ALL_REPOSITORIES.equals( repository ) && !observableRepositories.contains( repository ) ) + { + throw new ArchivaRestServiceException( + "${$.i18n.prop('report.repository.illegal-access', " + repository + ")}", "repositoryId", + new IllegalAccessException() ); + } + + if ( !ALL_REPOSITORIES.equals( repository ) ) + { + observableRepositories = Collections.singletonList( repository ); + } + + List problemArtifacts = new ArrayList(); + MetadataRepository metadataRepository = repositorySession.getRepository(); + for ( String repoId : observableRepositories ) + { + for ( String name : metadataRepository.getMetadataFacets( repoId, RepositoryProblemFacet.FACET_ID ) ) + { + RepositoryProblemFacet metadataFacet = + (RepositoryProblemFacet) metadataRepository.getMetadataFacet( repoId, + RepositoryProblemFacet.FACET_ID, + name ); + if ( StringUtils.isEmpty( groupId ) || groupId.equals( metadataFacet.getNamespace() ) ) + { + problemArtifacts.add( metadataFacet ); + } + } + } + + return problemArtifacts; + } + catch ( MetadataRepositoryException e ) + { + throw new ArchivaRestServiceException( e.getMessage(), e ); + } + finally + { + repositorySession.close(); + } + } +}