1 package org.apache.archiva.rest.services;
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
21 import org.apache.archiva.admin.model.RepositoryAdminException;
22 import org.apache.archiva.admin.model.RepositoryCommonValidator;
23 import org.apache.archiva.admin.model.beans.ManagedRepository;
24 import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin;
25 import org.apache.archiva.metadata.repository.MetadataRepository;
26 import org.apache.archiva.metadata.repository.MetadataRepositoryException;
27 import org.apache.archiva.metadata.repository.RepositorySession;
28 import org.apache.archiva.metadata.repository.stats.model.RepositoryStatistics;
29 import org.apache.archiva.metadata.repository.stats.model.RepositoryStatisticsManager;
30 import org.apache.archiva.rest.api.model.ActionStatus;
31 import org.apache.archiva.rest.api.model.ArchivaRepositoryStatistics;
32 import org.apache.archiva.rest.api.model.FileStatus;
33 import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
34 import org.apache.archiva.rest.api.services.ManagedRepositoriesService;
35 import org.apache.commons.lang3.StringEscapeUtils;
36 import org.apache.commons.lang3.StringUtils;
37 import org.springframework.stereotype.Service;
39 import javax.inject.Inject;
40 import javax.ws.rs.core.Response;
41 import java.nio.file.Files;
42 import java.nio.file.Paths;
43 import java.text.SimpleDateFormat;
44 import java.util.Collections;
45 import java.util.List;
46 import java.util.Locale;
49 * @author Olivier Lamy
52 @Service( "managedRepositoriesService#rest" )
53 public class DefaultManagedRepositoriesService
54 extends AbstractRestService
55 implements ManagedRepositoriesService
59 private RepositoryCommonValidator repositoryCommonValidator;
62 private RepositoryStatisticsManager repositoryStatisticsManager;
65 private ManagedRepositoryAdmin managedRepositoryAdmin;
68 public List<ManagedRepository> getManagedRepositories()
69 throws ArchivaRestServiceException
73 List<org.apache.archiva.admin.model.beans.ManagedRepository> repos =
74 managedRepositoryAdmin.getManagedRepositories();
75 return repos == null ? Collections.<ManagedRepository>emptyList() : repos;
77 catch ( RepositoryAdminException e )
79 throw new ArchivaRestServiceException( e.getMessage(), e );
84 public ManagedRepository getManagedRepository( String repositoryId )
85 throws ArchivaRestServiceException
87 List<ManagedRepository> repos = getManagedRepositories();
88 for ( ManagedRepository repo : repos )
90 if ( StringUtils.equals( repo.getId(), repositoryId ) )
100 public ActionStatus deleteManagedRepository( String repoId, boolean deleteContent )
101 throws ArchivaRestServiceException
106 return new ActionStatus( managedRepositoryAdmin.deleteManagedRepository( repoId, getAuditInformation( ), deleteContent ) );
108 catch ( RepositoryAdminException e )
110 log.error( e.getMessage(), e );
111 throw new ArchivaRestServiceException( e.getMessage(), e.getFieldName(), e );
116 public ManagedRepository addManagedRepository( ManagedRepository managedRepository )
117 throws ArchivaRestServiceException
123 managedRepositoryAdmin.addManagedRepository( managedRepository, managedRepository.isStageRepoNeeded(),
124 getAuditInformation() );
127 return getManagedRepository( managedRepository.getId() );
129 throw new ArchivaRestServiceException( "fail to created managed Repository", null );
131 catch ( RepositoryAdminException e )
133 throw new ArchivaRestServiceException( e.getMessage(), e.getFieldName(), e );
139 public ActionStatus updateManagedRepository( ManagedRepository managedRepository )
140 throws ArchivaRestServiceException
145 return new ActionStatus( managedRepositoryAdmin.updateManagedRepository( managedRepository,
146 managedRepository.isStageRepoNeeded( ),
147 getAuditInformation( ),
148 managedRepository.isResetStats( ) ) );
150 catch ( RepositoryAdminException e )
152 throw new ArchivaRestServiceException( e.getMessage(), e.getFieldName(), e );
157 public FileStatus getFileStatus( String fileLocation )
158 throws ArchivaRestServiceException
160 String location = repositoryCommonValidator.removeExpressions( fileLocation );
161 return new FileStatus( Files.exists( Paths.get( location ) ) );
165 public ArchivaRepositoryStatistics getManagedRepositoryStatistics( String repositoryId, String lang )
166 throws ArchivaRestServiceException
168 RepositorySession repositorySession = null;
171 repositorySession = repositorySessionFactory.createSession();
173 catch ( MetadataRepositoryException e )
175 e.printStackTrace( );
177 SimpleDateFormat sdf = new SimpleDateFormat( "EEE, d MMM yyyy HH:mm:ss Z", new Locale( lang ) );
180 MetadataRepository metadataRepository = repositorySession.getRepository();
182 RepositoryStatistics stats = null;
185 stats = repositoryStatisticsManager.getLastStatistics( repositoryId );
187 catch ( MetadataRepositoryException e )
189 log.warn( "Error retrieving repository statistics: {}", e.getMessage(), e );
193 ArchivaRepositoryStatistics archivaRepositoryStatistics =
194 getModelMapper().map( stats, ArchivaRepositoryStatistics.class );
195 archivaRepositoryStatistics.setDuration( archivaRepositoryStatistics.getScanEndTime().getTime()
196 - archivaRepositoryStatistics.getScanStartTime().getTime() );
197 archivaRepositoryStatistics.setLastScanDate(
198 sdf.format( archivaRepositoryStatistics.getScanEndTime() ) );
199 return archivaRepositoryStatistics;
205 if ( repositorySession != null )
207 repositorySession.close();
214 public String getPomSnippet( String repositoryId )
215 throws ArchivaRestServiceException
217 return createSnippet( getManagedRepository( repositoryId ) );
220 private String createSnippet( ManagedRepository repo )
221 throws ArchivaRestServiceException
225 StringBuilder snippet = new StringBuilder();
226 snippet.append( "<project>\n" );
227 snippet.append( " ...\n" );
228 snippet.append( " <distributionManagement>\n" );
230 String distRepoName = "repository";
231 if ( repo.isSnapshots() )
233 distRepoName = "snapshotRepository";
236 snippet.append( " <" ).append( distRepoName ).append( ">\n" );
237 snippet.append( " <id>" ).append( repo.getId() ).append( "</id>\n" );
238 snippet.append( " <url>" );
239 snippet.append( getBaseUrl( ) + "/repository" );
240 snippet.append( "/" ).append( repo.getId() ).append( "/" ).append( "</url>\n" );
242 if ( !"default".equals( repo.getLayout() ) )
244 snippet.append( " <layout>" ).append( repo.getLayout() ).append( "</layout>" );
247 snippet.append( " </" ).append( distRepoName ).append( ">\n" );
248 snippet.append( " </distributionManagement>\n" );
249 snippet.append( "\n" );
251 snippet.append( " <repositories>\n" );
252 snippet.append( " <repository>\n" );
253 snippet.append( " <id>" ).append( repo.getId() ).append( "</id>\n" );
254 snippet.append( " <name>" ).append( repo.getName() ).append( "</name>\n" );
256 snippet.append( " <url>" );
257 snippet.append( getBaseUrl( ) + "/repository" );
258 snippet.append( "/" ).append( repo.getId() ).append( "/" );
260 snippet.append( "</url>\n" );
262 if ( !"default".equals( repo.getLayout() ) )
264 snippet.append( " <layout>" ).append( repo.getLayout() ).append( "</layout>\n" );
267 snippet.append( " <releases>\n" );
268 snippet.append( " <enabled>" ).append( Boolean.valueOf( repo.isReleases() ) ).append(
270 snippet.append( " </releases>\n" );
271 snippet.append( " <snapshots>\n" );
272 snippet.append( " <enabled>" ).append( Boolean.valueOf( repo.isSnapshots() ) ).append(
274 snippet.append( " </snapshots>\n" );
275 snippet.append( " </repository>\n" );
276 snippet.append( " </repositories>\n" );
277 snippet.append( " <pluginRepositories>\n" );
278 snippet.append( " <pluginRepository>\n" );
279 snippet.append( " <id>" ).append( repo.getId() ).append( "</id>\n" );
280 snippet.append( " <name>" ).append( repo.getName() ).append( "</name>\n" );
282 snippet.append( " <url>" );
283 snippet.append( getBaseUrl( ) + "/repository" );
284 snippet.append( "/" ).append( repo.getId() ).append( "/" );
286 snippet.append( "</url>\n" );
288 if ( !"default".equals( repo.getLayout() ) )
290 snippet.append( " <layout>" ).append( repo.getLayout() ).append( "</layout>\n" );
293 snippet.append( " <releases>\n" );
294 snippet.append( " <enabled>" ).append( Boolean.valueOf( repo.isReleases() ) ).append(
296 snippet.append( " </releases>\n" );
297 snippet.append( " <snapshots>\n" );
298 snippet.append( " <enabled>" ).append( Boolean.valueOf( repo.isSnapshots() ) ).append(
300 snippet.append( " </snapshots>\n" );
301 snippet.append( " </pluginRepository>\n" );
302 snippet.append( " </pluginRepositories>\n" );
304 snippet.append( " ...\n" );
305 snippet.append( "</project>\n" );
307 return StringEscapeUtils.escapeXml( snippet.toString() );
309 catch ( RepositoryAdminException e )
311 throw new ArchivaRestServiceException( e.getMessage(),
312 Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e );