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 net.sf.beanlib.provider.replicator.BeanReplicator;
22 import org.apache.archiva.admin.model.RepositoryAdminException;
23 import org.apache.archiva.admin.model.RepositoryCommonValidator;
24 import org.apache.archiva.admin.model.beans.ManagedRepository;
25 import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin;
26 import org.apache.archiva.metadata.repository.MetadataRepository;
27 import org.apache.archiva.metadata.repository.MetadataRepositoryException;
28 import org.apache.archiva.metadata.repository.RepositorySession;
29 import org.apache.archiva.metadata.repository.stats.RepositoryStatistics;
30 import org.apache.archiva.metadata.repository.stats.RepositoryStatisticsManager;
31 import org.apache.archiva.rest.api.model.ArchivaRepositoryStatistics;
32 import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
33 import org.apache.archiva.rest.api.services.ManagedRepositoriesService;
34 import org.apache.commons.lang.StringEscapeUtils;
35 import org.apache.commons.lang.StringUtils;
36 import org.springframework.stereotype.Service;
38 import javax.inject.Inject;
39 import javax.ws.rs.core.Response;
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 ManagedRepositoryAdmin managedRepositoryAdmin;
60 private RepositoryCommonValidator repositoryCommonValidator;
63 private RepositoryStatisticsManager repositoryStatisticsManager;
65 public List<ManagedRepository> getManagedRepositories()
66 throws ArchivaRestServiceException
70 List<org.apache.archiva.admin.model.beans.ManagedRepository> repos =
71 managedRepositoryAdmin.getManagedRepositories();
72 return repos == null ? Collections.<ManagedRepository>emptyList() : repos;
74 catch ( RepositoryAdminException e )
76 throw new ArchivaRestServiceException( e.getMessage(), e );
80 public ManagedRepository getManagedRepository( String repositoryId )
81 throws ArchivaRestServiceException
83 List<ManagedRepository> repos = getManagedRepositories();
84 for ( ManagedRepository repo : repos )
86 if ( StringUtils.equals( repo.getId(), repositoryId ) )
95 public Boolean deleteManagedRepository( String repoId, boolean deleteContent )
96 throws ArchivaRestServiceException
101 return managedRepositoryAdmin.deleteManagedRepository( repoId, getAuditInformation(), deleteContent );
103 catch ( RepositoryAdminException e )
105 log.error( e.getMessage(), e );
106 throw new ArchivaRestServiceException( e.getMessage(), e.getFieldName(), e );
110 public ManagedRepository addManagedRepository( ManagedRepository managedRepository )
111 throws ArchivaRestServiceException
117 managedRepositoryAdmin.addManagedRepository( managedRepository, managedRepository.isStageRepoNeeded(),
118 getAuditInformation() );
121 return getManagedRepository( managedRepository.getId() );
123 throw new ArchivaRestServiceException( "fail to created managed Repository", null );
125 catch ( RepositoryAdminException e )
127 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 );
149 public Boolean fileLocationExists( String fileLocation )
150 throws ArchivaRestServiceException
152 String location = repositoryCommonValidator.removeExpressions( fileLocation );
153 return new File( location ).exists();
156 public ArchivaRepositoryStatistics getManagedRepositoryStatistics( String repositoryId, String lang )
157 throws ArchivaRestServiceException
159 RepositorySession repositorySession = repositorySessionFactory.createSession();
160 SimpleDateFormat sdf = new SimpleDateFormat( "EEE, d MMM yyyy HH:mm:ss Z", new Locale( lang ) );
163 MetadataRepository metadataRepository = repositorySession.getRepository();
165 RepositoryStatistics stats = null;
168 stats = repositoryStatisticsManager.getLastStatistics( metadataRepository, repositoryId );
170 catch ( MetadataRepositoryException e )
172 log.warn( "Error retrieving repository statistics: " + e.getMessage(), e );
176 ArchivaRepositoryStatistics archivaRepositoryStatistics =
177 new BeanReplicator().replicateBean( stats, ArchivaRepositoryStatistics.class );
178 archivaRepositoryStatistics.setDuration( archivaRepositoryStatistics.getScanEndTime().getTime()
179 - archivaRepositoryStatistics.getScanStartTime().getTime() );
180 archivaRepositoryStatistics.setLastScanDate(
181 sdf.format( archivaRepositoryStatistics.getScanEndTime() ) );
182 return archivaRepositoryStatistics;
188 if ( repositorySession != null )
190 repositorySession.close();
196 public String getPomSnippet( String repositoryId )
197 throws ArchivaRestServiceException
199 return createSnippet( getManagedRepository( repositoryId ) );
202 private String createSnippet( ManagedRepository repo )
203 throws ArchivaRestServiceException
207 StringBuilder snippet = new StringBuilder();
208 snippet.append( "<project>\n" );
209 snippet.append( " ...\n" );
210 snippet.append( " <distributionManagement>\n" );
212 String distRepoName = "repository";
213 if ( repo.isSnapshots() )
215 distRepoName = "snapshotRepository";
218 snippet.append( " <" ).append( distRepoName ).append( ">\n" );
219 snippet.append( " <id>" ).append( repo.getId() ).append( "</id>\n" );
220 snippet.append( " <url>" );
221 snippet.append( getBaseUrl( httpServletRequest ) + "/repository" );
222 snippet.append( "/" ).append( repo.getId() ).append( "/" ).append( "</url>\n" );
224 if ( !"default".equals( repo.getLayout() ) )
226 snippet.append( " <layout>" ).append( repo.getLayout() ).append( "</layout>" );
229 snippet.append( " </" ).append( distRepoName ).append( ">\n" );
230 snippet.append( " </distributionManagement>\n" );
231 snippet.append( "\n" );
233 snippet.append( " <repositories>\n" );
234 snippet.append( " <repository>\n" );
235 snippet.append( " <id>" ).append( repo.getId() ).append( "</id>\n" );
236 snippet.append( " <name>" ).append( repo.getName() ).append( "</name>\n" );
238 snippet.append( " <url>" );
239 snippet.append( getBaseUrl( httpServletRequest ) + "/repository" );
240 snippet.append( "/" ).append( repo.getId() ).append( "/" );
242 snippet.append( "</url>\n" );
244 if ( !"default".equals( repo.getLayout() ) )
246 snippet.append( " <layout>" ).append( repo.getLayout() ).append( "</layout>\n" );
249 snippet.append( " <releases>\n" );
250 snippet.append( " <enabled>" ).append( Boolean.valueOf( repo.isReleases() ) ).append(
252 snippet.append( " </releases>\n" );
253 snippet.append( " <snapshots>\n" );
254 snippet.append( " <enabled>" ).append( Boolean.valueOf( repo.isSnapshots() ) ).append(
256 snippet.append( " </snapshots>\n" );
257 snippet.append( " </repository>\n" );
258 snippet.append( " </repositories>\n" );
259 snippet.append( " <pluginRepositories>\n" );
260 snippet.append( " <pluginRepository>\n" );
261 snippet.append( " <id>" ).append( repo.getId() ).append( "</id>\n" );
262 snippet.append( " <name>" ).append( repo.getName() ).append( "</name>\n" );
264 snippet.append( " <url>" );
265 snippet.append( getBaseUrl( httpServletRequest ) + "/repository" );
266 snippet.append( "/" ).append( repo.getId() ).append( "/" );
268 snippet.append( "</url>\n" );
270 if ( !"default".equals( repo.getLayout() ) )
272 snippet.append( " <layout>" ).append( repo.getLayout() ).append( "</layout>\n" );
275 snippet.append( " <releases>\n" );
276 snippet.append( " <enabled>" ).append( Boolean.valueOf( repo.isReleases() ) ).append(
278 snippet.append( " </releases>\n" );
279 snippet.append( " <snapshots>\n" );
280 snippet.append( " <enabled>" ).append( Boolean.valueOf( repo.isSnapshots() ) ).append(
282 snippet.append( " </snapshots>\n" );
283 snippet.append( " </pluginRepository>\n" );
284 snippet.append( " </pluginRepositories>\n" );
286 snippet.append( " ...\n" );
287 snippet.append( "</project>\n" );
289 return StringEscapeUtils.escapeXml( snippet.toString() );
291 catch ( RepositoryAdminException e )
293 throw new ArchivaRestServiceException( e.getMessage(),
294 Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e );