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.metadata.repository.MetadataRepository;
25 import org.apache.archiva.metadata.repository.MetadataRepositoryException;
26 import org.apache.archiva.metadata.repository.RepositorySession;
27 import org.apache.archiva.metadata.repository.stats.model.RepositoryStatistics;
28 import org.apache.archiva.metadata.repository.stats.model.RepositoryStatisticsManager;
29 import org.apache.archiva.rest.api.model.ArchivaRepositoryStatistics;
30 import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
31 import org.apache.archiva.rest.api.services.ManagedRepositoriesService;
32 import org.apache.commons.lang.StringEscapeUtils;
33 import org.apache.commons.lang.StringUtils;
34 import org.springframework.stereotype.Service;
36 import javax.inject.Inject;
37 import javax.ws.rs.core.Response;
39 import java.text.SimpleDateFormat;
40 import java.util.Collections;
41 import java.util.List;
42 import java.util.Locale;
45 * @author Olivier Lamy
48 @Service( "managedRepositoriesService#rest" )
49 public class DefaultManagedRepositoriesService
50 extends AbstractRestService
51 implements ManagedRepositoriesService
55 private RepositoryCommonValidator repositoryCommonValidator;
58 private RepositoryStatisticsManager repositoryStatisticsManager;
61 public List<ManagedRepository> getManagedRepositories()
62 throws ArchivaRestServiceException
66 List<org.apache.archiva.admin.model.beans.ManagedRepository> repos =
67 managedRepositoryAdmin.getManagedRepositories();
68 return repos == null ? Collections.<ManagedRepository>emptyList() : repos;
70 catch ( RepositoryAdminException e )
72 throw new ArchivaRestServiceException( e.getMessage(), e );
77 public ManagedRepository getManagedRepository( String repositoryId )
78 throws ArchivaRestServiceException
80 List<ManagedRepository> repos = getManagedRepositories();
81 for ( ManagedRepository repo : repos )
83 if ( StringUtils.equals( repo.getId(), repositoryId ) )
93 public Boolean deleteManagedRepository( String repoId, boolean deleteContent )
94 throws ArchivaRestServiceException
99 return managedRepositoryAdmin.deleteManagedRepository( repoId, getAuditInformation(), deleteContent );
101 catch ( RepositoryAdminException e )
103 log.error( e.getMessage(), e );
104 throw new ArchivaRestServiceException( e.getMessage(), e.getFieldName(), e );
109 public ManagedRepository addManagedRepository( ManagedRepository managedRepository )
110 throws ArchivaRestServiceException
116 managedRepositoryAdmin.addManagedRepository( managedRepository, managedRepository.isStageRepoNeeded(),
117 getAuditInformation() );
120 return getManagedRepository( managedRepository.getId() );
122 throw new ArchivaRestServiceException( "fail to created managed Repository", null );
124 catch ( RepositoryAdminException e )
126 throw new ArchivaRestServiceException( e.getMessage(), e.getFieldName(), e );
132 public Boolean updateManagedRepository( ManagedRepository managedRepository )
133 throws ArchivaRestServiceException
138 return managedRepositoryAdmin.updateManagedRepository( managedRepository,
139 managedRepository.isStageRepoNeeded(),
140 getAuditInformation(),
141 managedRepository.isResetStats() );
143 catch ( RepositoryAdminException e )
145 throw new ArchivaRestServiceException( e.getMessage(), e.getFieldName(), e );
150 public Boolean fileLocationExists( String fileLocation )
151 throws ArchivaRestServiceException
153 String location = repositoryCommonValidator.removeExpressions( fileLocation );
154 return new File( location ).exists();
158 public ArchivaRepositoryStatistics getManagedRepositoryStatistics( String repositoryId, String lang )
159 throws ArchivaRestServiceException
161 RepositorySession repositorySession = repositorySessionFactory.createSession();
162 SimpleDateFormat sdf = new SimpleDateFormat( "EEE, d MMM yyyy HH:mm:ss Z", new Locale( lang ) );
165 MetadataRepository metadataRepository = repositorySession.getRepository();
167 RepositoryStatistics stats = null;
170 stats = repositoryStatisticsManager.getLastStatistics( metadataRepository, repositoryId );
172 catch ( MetadataRepositoryException e )
174 log.warn( "Error retrieving repository statistics: {}", e.getMessage(), e );
178 ArchivaRepositoryStatistics archivaRepositoryStatistics =
179 getModelMapper().map( stats, ArchivaRepositoryStatistics.class );
180 archivaRepositoryStatistics.setDuration( archivaRepositoryStatistics.getScanEndTime().getTime()
181 - archivaRepositoryStatistics.getScanStartTime().getTime() );
182 archivaRepositoryStatistics.setLastScanDate(
183 sdf.format( archivaRepositoryStatistics.getScanEndTime() ) );
184 return archivaRepositoryStatistics;
190 if ( repositorySession != null )
192 repositorySession.close();
199 public String getPomSnippet( String repositoryId )
200 throws ArchivaRestServiceException
202 return createSnippet( getManagedRepository( repositoryId ) );
205 private String createSnippet( ManagedRepository repo )
206 throws ArchivaRestServiceException
210 StringBuilder snippet = new StringBuilder();
211 snippet.append( "<project>\n" );
212 snippet.append( " ...\n" );
213 snippet.append( " <distributionManagement>\n" );
215 String distRepoName = "repository";
216 if ( repo.isSnapshots() )
218 distRepoName = "snapshotRepository";
221 snippet.append( " <" ).append( distRepoName ).append( ">\n" );
222 snippet.append( " <id>" ).append( repo.getId() ).append( "</id>\n" );
223 snippet.append( " <url>" );
224 snippet.append( getBaseUrl( ) + "/repository" );
225 snippet.append( "/" ).append( repo.getId() ).append( "/" ).append( "</url>\n" );
227 if ( !"default".equals( repo.getLayout() ) )
229 snippet.append( " <layout>" ).append( repo.getLayout() ).append( "</layout>" );
232 snippet.append( " </" ).append( distRepoName ).append( ">\n" );
233 snippet.append( " </distributionManagement>\n" );
234 snippet.append( "\n" );
236 snippet.append( " <repositories>\n" );
237 snippet.append( " <repository>\n" );
238 snippet.append( " <id>" ).append( repo.getId() ).append( "</id>\n" );
239 snippet.append( " <name>" ).append( repo.getName() ).append( "</name>\n" );
241 snippet.append( " <url>" );
242 snippet.append( getBaseUrl( ) + "/repository" );
243 snippet.append( "/" ).append( repo.getId() ).append( "/" );
245 snippet.append( "</url>\n" );
247 if ( !"default".equals( repo.getLayout() ) )
249 snippet.append( " <layout>" ).append( repo.getLayout() ).append( "</layout>\n" );
252 snippet.append( " <releases>\n" );
253 snippet.append( " <enabled>" ).append( Boolean.valueOf( repo.isReleases() ) ).append(
255 snippet.append( " </releases>\n" );
256 snippet.append( " <snapshots>\n" );
257 snippet.append( " <enabled>" ).append( Boolean.valueOf( repo.isSnapshots() ) ).append(
259 snippet.append( " </snapshots>\n" );
260 snippet.append( " </repository>\n" );
261 snippet.append( " </repositories>\n" );
262 snippet.append( " <pluginRepositories>\n" );
263 snippet.append( " <pluginRepository>\n" );
264 snippet.append( " <id>" ).append( repo.getId() ).append( "</id>\n" );
265 snippet.append( " <name>" ).append( repo.getName() ).append( "</name>\n" );
267 snippet.append( " <url>" );
268 snippet.append( getBaseUrl( ) + "/repository" );
269 snippet.append( "/" ).append( repo.getId() ).append( "/" );
271 snippet.append( "</url>\n" );
273 if ( !"default".equals( repo.getLayout() ) )
275 snippet.append( " <layout>" ).append( repo.getLayout() ).append( "</layout>\n" );
278 snippet.append( " <releases>\n" );
279 snippet.append( " <enabled>" ).append( Boolean.valueOf( repo.isReleases() ) ).append(
281 snippet.append( " </releases>\n" );
282 snippet.append( " <snapshots>\n" );
283 snippet.append( " <enabled>" ).append( Boolean.valueOf( repo.isSnapshots() ) ).append(
285 snippet.append( " </snapshots>\n" );
286 snippet.append( " </pluginRepository>\n" );
287 snippet.append( " </pluginRepositories>\n" );
289 snippet.append( " ...\n" );
290 snippet.append( "</project>\n" );
292 return StringEscapeUtils.escapeXml( snippet.toString() );
294 catch ( RepositoryAdminException e )
296 throw new ArchivaRestServiceException( e.getMessage(),
297 Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e );