001package org.apache.archiva.rest.services; 002/* 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, 014 * software distributed under the License is distributed on an 015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 016 * KIND, either express or implied. See the License for the 017 * specific language governing permissions and limitations 018 * under the License. 019 */ 020 021import org.apache.archiva.admin.model.RepositoryAdminException; 022import org.apache.archiva.admin.model.RepositoryCommonValidator; 023import org.apache.archiva.admin.model.beans.ManagedRepository; 024import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin; 025import org.apache.archiva.metadata.repository.MetadataRepository; 026import org.apache.archiva.metadata.repository.MetadataRepositoryException; 027import org.apache.archiva.metadata.repository.RepositorySession; 028import org.apache.archiva.metadata.repository.stats.model.RepositoryStatistics; 029import org.apache.archiva.metadata.repository.stats.model.RepositoryStatisticsManager; 030import org.apache.archiva.rest.api.model.ArchivaRepositoryStatistics; 031import org.apache.archiva.rest.api.services.ArchivaRestServiceException; 032import org.apache.archiva.rest.api.services.ManagedRepositoriesService; 033import org.apache.commons.lang3.StringEscapeUtils; 034import org.apache.commons.lang3.StringUtils; 035import org.springframework.stereotype.Service; 036 037import javax.inject.Inject; 038import javax.ws.rs.core.Response; 039import java.nio.file.Files; 040import java.nio.file.Paths; 041import java.text.SimpleDateFormat; 042import java.util.Collections; 043import java.util.List; 044import java.util.Locale; 045 046/** 047 * @author Olivier Lamy 048 * @since 1.4-M1 049 */ 050@Service( "managedRepositoriesService#rest" ) 051public class DefaultManagedRepositoriesService 052 extends AbstractRestService 053 implements ManagedRepositoriesService 054{ 055 056 @Inject 057 private RepositoryCommonValidator repositoryCommonValidator; 058 059 @Inject 060 private RepositoryStatisticsManager repositoryStatisticsManager; 061 062 @Inject 063 private ManagedRepositoryAdmin managedRepositoryAdmin; 064 065 @Override 066 public List<ManagedRepository> getManagedRepositories() 067 throws ArchivaRestServiceException 068 { 069 try 070 { 071 List<org.apache.archiva.admin.model.beans.ManagedRepository> repos = 072 managedRepositoryAdmin.getManagedRepositories(); 073 return repos == null ? Collections.<ManagedRepository>emptyList() : repos; 074 } 075 catch ( RepositoryAdminException e ) 076 { 077 throw new ArchivaRestServiceException( e.getMessage(), e ); 078 } 079 } 080 081 @Override 082 public ManagedRepository getManagedRepository( String repositoryId ) 083 throws ArchivaRestServiceException 084 { 085 List<ManagedRepository> repos = getManagedRepositories(); 086 for ( ManagedRepository repo : repos ) 087 { 088 if ( StringUtils.equals( repo.getId(), repositoryId ) ) 089 { 090 return repo; 091 } 092 } 093 return null; 094 } 095 096 097 @Override 098 public Boolean deleteManagedRepository( String repoId, boolean deleteContent ) 099 throws ArchivaRestServiceException 100 { 101 102 try 103 { 104 return managedRepositoryAdmin.deleteManagedRepository( repoId, getAuditInformation(), deleteContent ); 105 } 106 catch ( RepositoryAdminException e ) 107 { 108 log.error( e.getMessage(), e ); 109 throw new ArchivaRestServiceException( e.getMessage(), e.getFieldName(), e ); 110 } 111 } 112 113 @Override 114 public ManagedRepository addManagedRepository( ManagedRepository managedRepository ) 115 throws ArchivaRestServiceException 116 { 117 118 try 119 { 120 boolean res = 121 managedRepositoryAdmin.addManagedRepository( managedRepository, managedRepository.isStageRepoNeeded(), 122 getAuditInformation() ); 123 if ( res ) 124 { 125 return getManagedRepository( managedRepository.getId() ); 126 } 127 throw new ArchivaRestServiceException( "fail to created managed Repository", null ); 128 } 129 catch ( RepositoryAdminException e ) 130 { 131 throw new ArchivaRestServiceException( e.getMessage(), e.getFieldName(), e ); 132 } 133 } 134 135 136 @Override 137 public Boolean updateManagedRepository( ManagedRepository managedRepository ) 138 throws ArchivaRestServiceException 139 { 140 141 try 142 { 143 return managedRepositoryAdmin.updateManagedRepository( managedRepository, 144 managedRepository.isStageRepoNeeded(), 145 getAuditInformation(), 146 managedRepository.isResetStats() ); 147 } 148 catch ( RepositoryAdminException e ) 149 { 150 throw new ArchivaRestServiceException( e.getMessage(), e.getFieldName(), e ); 151 } 152 } 153 154 @Override 155 public Boolean fileLocationExists( String fileLocation ) 156 throws ArchivaRestServiceException 157 { 158 String location = repositoryCommonValidator.removeExpressions( fileLocation ); 159 return Files.exists( Paths.get( location )); 160 } 161 162 @Override 163 public ArchivaRepositoryStatistics getManagedRepositoryStatistics( String repositoryId, String lang ) 164 throws ArchivaRestServiceException 165 { 166 RepositorySession repositorySession = null; 167 try 168 { 169 repositorySession = repositorySessionFactory.createSession(); 170 } 171 catch ( MetadataRepositoryException e ) 172 { 173 e.printStackTrace( ); 174 } 175 SimpleDateFormat sdf = new SimpleDateFormat( "EEE, d MMM yyyy HH:mm:ss Z", new Locale( lang ) ); 176 try 177 { 178 MetadataRepository metadataRepository = repositorySession.getRepository(); 179 180 RepositoryStatistics stats = null; 181 try 182 { 183 stats = repositoryStatisticsManager.getLastStatistics( repositoryId ); 184 } 185 catch ( MetadataRepositoryException e ) 186 { 187 log.warn( "Error retrieving repository statistics: {}", e.getMessage(), e ); 188 } 189 if ( stats != null ) 190 { 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; 198 } 199 200 } 201 finally 202 { 203 if ( repositorySession != null ) 204 { 205 repositorySession.close(); 206 } 207 } 208 return null; 209 } 210 211 @Override 212 public String getPomSnippet( String repositoryId ) 213 throws ArchivaRestServiceException 214 { 215 return createSnippet( getManagedRepository( repositoryId ) ); 216 } 217 218 private String createSnippet( ManagedRepository repo ) 219 throws ArchivaRestServiceException 220 { 221 try 222 { 223 StringBuilder snippet = new StringBuilder(); 224 snippet.append( "<project>\n" ); 225 snippet.append( " ...\n" ); 226 snippet.append( " <distributionManagement>\n" ); 227 228 String distRepoName = "repository"; 229 if ( repo.isSnapshots() ) 230 { 231 distRepoName = "snapshotRepository"; 232 } 233 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" ); 239 240 if ( !"default".equals( repo.getLayout() ) ) 241 { 242 snippet.append( " <layout>" ).append( repo.getLayout() ).append( "</layout>" ); 243 } 244 245 snippet.append( " </" ).append( distRepoName ).append( ">\n" ); 246 snippet.append( " </distributionManagement>\n" ); 247 snippet.append( "\n" ); 248 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" ); 253 254 snippet.append( " <url>" ); 255 snippet.append( getBaseUrl( ) + "/repository" ); 256 snippet.append( "/" ).append( repo.getId() ).append( "/" ); 257 258 snippet.append( "</url>\n" ); 259 260 if ( !"default".equals( repo.getLayout() ) ) 261 { 262 snippet.append( " <layout>" ).append( repo.getLayout() ).append( "</layout>\n" ); 263 } 264 265 snippet.append( " <releases>\n" ); 266 snippet.append( " <enabled>" ).append( Boolean.valueOf( repo.isReleases() ) ).append( 267 "</enabled>\n" ); 268 snippet.append( " </releases>\n" ); 269 snippet.append( " <snapshots>\n" ); 270 snippet.append( " <enabled>" ).append( Boolean.valueOf( repo.isSnapshots() ) ).append( 271 "</enabled>\n" ); 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" ); 279 280 snippet.append( " <url>" ); 281 snippet.append( getBaseUrl( ) + "/repository" ); 282 snippet.append( "/" ).append( repo.getId() ).append( "/" ); 283 284 snippet.append( "</url>\n" ); 285 286 if ( !"default".equals( repo.getLayout() ) ) 287 { 288 snippet.append( " <layout>" ).append( repo.getLayout() ).append( "</layout>\n" ); 289 } 290 291 snippet.append( " <releases>\n" ); 292 snippet.append( " <enabled>" ).append( Boolean.valueOf( repo.isReleases() ) ).append( 293 "</enabled>\n" ); 294 snippet.append( " </releases>\n" ); 295 snippet.append( " <snapshots>\n" ); 296 snippet.append( " <enabled>" ).append( Boolean.valueOf( repo.isSnapshots() ) ).append( 297 "</enabled>\n" ); 298 snippet.append( " </snapshots>\n" ); 299 snippet.append( " </pluginRepository>\n" ); 300 snippet.append( " </pluginRepositories>\n" ); 301 302 snippet.append( " ...\n" ); 303 snippet.append( "</project>\n" ); 304 305 return StringEscapeUtils.escapeXml( snippet.toString() ); 306 } 307 catch ( RepositoryAdminException e ) 308 { 309 throw new ArchivaRestServiceException( e.getMessage(), 310 Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e ); 311 } 312 } 313}