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.ArchivaRepositoryStatistics;
31 import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
32 import org.apache.archiva.rest.api.services.ManagedRepositoriesService;
33 import org.apache.commons.lang3.StringEscapeUtils;
34 import org.apache.commons.lang3.StringUtils;
35 import org.springframework.stereotype.Service;
37 import javax.inject.Inject;
38 import javax.ws.rs.core.Response;
39 import java.nio.file.Files;
40 import java.nio.file.Paths;
41 import java.text.SimpleDateFormat;
42 import java.util.Collections;
43 import java.util.List;
44 import java.util.Locale;
47 * @author Olivier Lamy
50 @Service( "managedRepositoriesService#rest" )
51 public class DefaultManagedRepositoriesService
52 extends AbstractRestService
53 implements ManagedRepositoriesService
57 private RepositoryCommonValidator repositoryCommonValidator;
60 private RepositoryStatisticsManager repositoryStatisticsManager;
63 private ManagedRepositoryAdmin managedRepositoryAdmin;
66 public List<ManagedRepository> getManagedRepositories()
67 throws ArchivaRestServiceException
71 List<org.apache.archiva.admin.model.beans.ManagedRepository> repos =
72 managedRepositoryAdmin.getManagedRepositories();
73 return repos == null ? Collections.<ManagedRepository>emptyList() : repos;
75 catch ( RepositoryAdminException e )
77 throw new ArchivaRestServiceException( e.getMessage(), e );
82 public ManagedRepository getManagedRepository( String repositoryId )
83 throws ArchivaRestServiceException
85 List<ManagedRepository> repos = getManagedRepositories();
86 for ( ManagedRepository repo : repos )
88 if ( StringUtils.equals( repo.getId(), repositoryId ) )
98 public Boolean deleteManagedRepository( String repoId, boolean deleteContent )
99 throws ArchivaRestServiceException
104 return managedRepositoryAdmin.deleteManagedRepository( repoId, getAuditInformation(), deleteContent );
106 catch ( RepositoryAdminException e )
108 log.error( e.getMessage(), e );
109 throw new ArchivaRestServiceException( e.getMessage(), e.getFieldName(), e );
114 public ManagedRepository addManagedRepository( ManagedRepository managedRepository )
115 throws ArchivaRestServiceException
121 managedRepositoryAdmin.addManagedRepository( managedRepository, managedRepository.isStageRepoNeeded(),
122 getAuditInformation() );
125 return getManagedRepository( managedRepository.getId() );
127 throw new ArchivaRestServiceException( "fail to created managed Repository", null );
129 catch ( RepositoryAdminException e )
131 throw new ArchivaRestServiceException( e.getMessage(), e.getFieldName(), e );
137 public Boolean updateManagedRepository( ManagedRepository managedRepository )
138 throws ArchivaRestServiceException
143 return managedRepositoryAdmin.updateManagedRepository( managedRepository,
144 managedRepository.isStageRepoNeeded(),
145 getAuditInformation(),
146 managedRepository.isResetStats() );
148 catch ( RepositoryAdminException e )
150 throw new ArchivaRestServiceException( e.getMessage(), e.getFieldName(), e );
155 public Boolean fileLocationExists( String fileLocation )
156 throws ArchivaRestServiceException
158 String location = repositoryCommonValidator.removeExpressions( fileLocation );
159 return Files.exists( Paths.get( location ));
163 public ArchivaRepositoryStatistics getManagedRepositoryStatistics( String repositoryId, String lang )
164 throws ArchivaRestServiceException
166 RepositorySession repositorySession = null;
169 repositorySession = repositorySessionFactory.createSession();
171 catch ( MetadataRepositoryException e )
173 e.printStackTrace( );
175 SimpleDateFormat sdf = new SimpleDateFormat( "EEE, d MMM yyyy HH:mm:ss Z", new Locale( lang ) );
178 MetadataRepository metadataRepository = repositorySession.getRepository();
180 RepositoryStatistics stats = null;
183 stats = repositoryStatisticsManager.getLastStatistics( repositoryId );
185 catch ( MetadataRepositoryException e )
187 log.warn( "Error retrieving repository statistics: {}", e.getMessage(), e );
191 ArchivaRepositoryStatistics archivaRepositoryStatistics =
192 getModelMapper().map( stats, ArchivaRepositoryStatistics.class );
193 archivaRepositoryStatistics.setDuration( archivaRepositoryStatistics.getScanEndTime().getTime()
194 - archivaRepositoryStatistics.getScanStartTime().getTime() );
195 archivaRepositoryStatistics.setLastScanDate(
196 sdf.format( archivaRepositoryStatistics.getScanEndTime() ) );
197 return archivaRepositoryStatistics;
203 if ( repositorySession != null )
205 repositorySession.close();
212 public String getPomSnippet( String repositoryId )
213 throws ArchivaRestServiceException
215 return createSnippet( getManagedRepository( repositoryId ) );
218 private String createSnippet( ManagedRepository repo )
219 throws ArchivaRestServiceException
223 StringBuilder snippet = new StringBuilder();
224 snippet.append( "<project>\n" );
225 snippet.append( " ...\n" );
226 snippet.append( " <distributionManagement>\n" );
228 String distRepoName = "repository";
229 if ( repo.isSnapshots() )
231 distRepoName = "snapshotRepository";
234 snippet.append( " <" ).append( distRepoName ).append( ">\n" );
235 snippet.append( " <id>" ).append( repo.getId() ).append( "</id>\n" );
236 snippet.append( " <url>" );
237 snippet.append( getBaseUrl( ) + "/repository" );
238 snippet.append( "/" ).append( repo.getId() ).append( "/" ).append( "</url>\n" );
240 if ( !"default".equals( repo.getLayout() ) )
242 snippet.append( " <layout>" ).append( repo.getLayout() ).append( "</layout>" );
245 snippet.append( " </" ).append( distRepoName ).append( ">\n" );
246 snippet.append( " </distributionManagement>\n" );
247 snippet.append( "\n" );
249 snippet.append( " <repositories>\n" );
250 snippet.append( " <repository>\n" );
251 snippet.append( " <id>" ).append( repo.getId() ).append( "</id>\n" );
252 snippet.append( " <name>" ).append( repo.getName() ).append( "</name>\n" );
254 snippet.append( " <url>" );
255 snippet.append( getBaseUrl( ) + "/repository" );
256 snippet.append( "/" ).append( repo.getId() ).append( "/" );
258 snippet.append( "</url>\n" );
260 if ( !"default".equals( repo.getLayout() ) )
262 snippet.append( " <layout>" ).append( repo.getLayout() ).append( "</layout>\n" );
265 snippet.append( " <releases>\n" );
266 snippet.append( " <enabled>" ).append( Boolean.valueOf( repo.isReleases() ) ).append(
268 snippet.append( " </releases>\n" );
269 snippet.append( " <snapshots>\n" );
270 snippet.append( " <enabled>" ).append( Boolean.valueOf( repo.isSnapshots() ) ).append(
272 snippet.append( " </snapshots>\n" );
273 snippet.append( " </repository>\n" );
274 snippet.append( " </repositories>\n" );
275 snippet.append( " <pluginRepositories>\n" );
276 snippet.append( " <pluginRepository>\n" );
277 snippet.append( " <id>" ).append( repo.getId() ).append( "</id>\n" );
278 snippet.append( " <name>" ).append( repo.getName() ).append( "</name>\n" );
280 snippet.append( " <url>" );
281 snippet.append( getBaseUrl( ) + "/repository" );
282 snippet.append( "/" ).append( repo.getId() ).append( "/" );
284 snippet.append( "</url>\n" );
286 if ( !"default".equals( repo.getLayout() ) )
288 snippet.append( " <layout>" ).append( repo.getLayout() ).append( "</layout>\n" );
291 snippet.append( " <releases>\n" );
292 snippet.append( " <enabled>" ).append( Boolean.valueOf( repo.isReleases() ) ).append(
294 snippet.append( " </releases>\n" );
295 snippet.append( " <snapshots>\n" );
296 snippet.append( " <enabled>" ).append( Boolean.valueOf( repo.isSnapshots() ) ).append(
298 snippet.append( " </snapshots>\n" );
299 snippet.append( " </pluginRepository>\n" );
300 snippet.append( " </pluginRepositories>\n" );
302 snippet.append( " ...\n" );
303 snippet.append( "</project>\n" );
305 return StringEscapeUtils.escapeXml( snippet.toString() );
307 catch ( RepositoryAdminException e )
309 throw new ArchivaRestServiceException( e.getMessage(),
310 Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e );