This project has retired. For details please refer to its Attic page.
DefaultManagedRepositoriesService xref
View Javadoc
1   package org.apache.archiva.rest.services;
2   /*
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
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
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
18   * under the License.
19   */
20  
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.RepositoryStatistics;
28  import org.apache.archiva.metadata.repository.stats.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;
35  
36  import javax.inject.Inject;
37  import javax.ws.rs.core.Response;
38  import java.io.File;
39  import java.text.SimpleDateFormat;
40  import java.util.Collections;
41  import java.util.List;
42  import java.util.Locale;
43  
44  /**
45   * @author Olivier Lamy
46   * @since 1.4-M1
47   */
48  @Service( "managedRepositoriesService#rest" )
49  public class DefaultManagedRepositoriesService
50      extends AbstractRestService
51      implements ManagedRepositoriesService
52  {
53  
54      @Inject
55      private RepositoryCommonValidator repositoryCommonValidator;
56  
57      @Inject
58      private RepositoryStatisticsManager repositoryStatisticsManager;
59  
60      @Override
61      public List<ManagedRepository> getManagedRepositories()
62          throws ArchivaRestServiceException
63      {
64          try
65          {
66              List<org.apache.archiva.admin.model.beans.ManagedRepository> repos =
67                  managedRepositoryAdmin.getManagedRepositories();
68              return repos == null ? Collections.<ManagedRepository>emptyList() : repos;
69          }
70          catch ( RepositoryAdminException e )
71          {
72              throw new ArchivaRestServiceException( e.getMessage(), e );
73          }
74      }
75  
76      @Override
77      public ManagedRepository getManagedRepository( String repositoryId )
78          throws ArchivaRestServiceException
79      {
80          List<ManagedRepository> repos = getManagedRepositories();
81          for ( ManagedRepository repo : repos )
82          {
83              if ( StringUtils.equals( repo.getId(), repositoryId ) )
84              {
85                  return repo;
86              }
87          }
88          return null;
89      }
90  
91  
92      @Override
93      public Boolean deleteManagedRepository( String repoId, boolean deleteContent )
94          throws ArchivaRestServiceException
95      {
96  
97          try
98          {
99              return managedRepositoryAdmin.deleteManagedRepository( repoId, getAuditInformation(), deleteContent );
100         }
101         catch ( RepositoryAdminException e )
102         {
103             log.error( e.getMessage(), e );
104             throw new ArchivaRestServiceException( e.getMessage(), e.getFieldName(), e );
105         }
106     }
107 
108     @Override
109     public ManagedRepository addManagedRepository( ManagedRepository managedRepository )
110         throws ArchivaRestServiceException
111     {
112 
113         try
114         {
115             boolean res =
116                 managedRepositoryAdmin.addManagedRepository( managedRepository, managedRepository.isStageRepoNeeded(),
117                                                              getAuditInformation() );
118             if ( res )
119             {
120                 return getManagedRepository( managedRepository.getId() );
121             }
122             throw new ArchivaRestServiceException( "fail to created managed Repository", null );
123         }
124         catch ( RepositoryAdminException e )
125         {
126             throw new ArchivaRestServiceException( e.getMessage(), e.getFieldName(), e );
127         }
128     }
129 
130 
131     @Override
132     public Boolean updateManagedRepository( ManagedRepository managedRepository )
133         throws ArchivaRestServiceException
134     {
135 
136         try
137         {
138             return managedRepositoryAdmin.updateManagedRepository( managedRepository,
139                                                                    managedRepository.isStageRepoNeeded(),
140                                                                    getAuditInformation(),
141                                                                    managedRepository.isResetStats() );
142         }
143         catch ( RepositoryAdminException e )
144         {
145             throw new ArchivaRestServiceException( e.getMessage(), e.getFieldName(), e );
146         }
147     }
148 
149     @Override
150     public Boolean fileLocationExists( String fileLocation )
151         throws ArchivaRestServiceException
152     {
153         String location = repositoryCommonValidator.removeExpressions( fileLocation );
154         return new File( location ).exists();
155     }
156 
157     @Override
158     public ArchivaRepositoryStatistics getManagedRepositoryStatistics( String repositoryId, String lang )
159         throws ArchivaRestServiceException
160     {
161         RepositorySession repositorySession = repositorySessionFactory.createSession();
162         SimpleDateFormat sdf = new SimpleDateFormat( "EEE, d MMM yyyy HH:mm:ss Z", new Locale( lang ) );
163         try
164         {
165             MetadataRepository metadataRepository = repositorySession.getRepository();
166 
167             RepositoryStatistics stats = null;
168             try
169             {
170                 stats = repositoryStatisticsManager.getLastStatistics( metadataRepository, repositoryId );
171             }
172             catch ( MetadataRepositoryException e )
173             {
174                 log.warn( "Error retrieving repository statistics: {}", e.getMessage(), e );
175             }
176             if ( stats != null )
177             {
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;
185             }
186 
187         }
188         finally
189         {
190             if ( repositorySession != null )
191             {
192                 repositorySession.close();
193             }
194         }
195         return null;
196     }
197 
198     @Override
199     public String getPomSnippet( String repositoryId )
200         throws ArchivaRestServiceException
201     {
202         return createSnippet( getManagedRepository( repositoryId ) );
203     }
204 
205     private String createSnippet( ManagedRepository repo )
206         throws ArchivaRestServiceException
207     {
208         try
209         {
210             StringBuilder snippet = new StringBuilder();
211             snippet.append( "<project>\n" );
212             snippet.append( "  ...\n" );
213             snippet.append( "  <distributionManagement>\n" );
214 
215             String distRepoName = "repository";
216             if ( repo.isSnapshots() )
217             {
218                 distRepoName = "snapshotRepository";
219             }
220 
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" );
226 
227             if ( !"default".equals( repo.getLayout() ) )
228             {
229                 snippet.append( "      <layout>" ).append( repo.getLayout() ).append( "</layout>" );
230             }
231 
232             snippet.append( "    </" ).append( distRepoName ).append( ">\n" );
233             snippet.append( "  </distributionManagement>\n" );
234             snippet.append( "\n" );
235 
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" );
240 
241             snippet.append( "      <url>" );
242             snippet.append( getBaseUrl(  ) + "/repository" );
243             snippet.append( "/" ).append( repo.getId() ).append( "/" );
244 
245             snippet.append( "</url>\n" );
246 
247             if ( !"default".equals( repo.getLayout() ) )
248             {
249                 snippet.append( "      <layout>" ).append( repo.getLayout() ).append( "</layout>\n" );
250             }
251 
252             snippet.append( "      <releases>\n" );
253             snippet.append( "        <enabled>" ).append( Boolean.valueOf( repo.isReleases() ) ).append(
254                 "</enabled>\n" );
255             snippet.append( "      </releases>\n" );
256             snippet.append( "      <snapshots>\n" );
257             snippet.append( "        <enabled>" ).append( Boolean.valueOf( repo.isSnapshots() ) ).append(
258                 "</enabled>\n" );
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" );
266 
267             snippet.append( "      <url>" );
268             snippet.append( getBaseUrl(  ) + "/repository" );
269             snippet.append( "/" ).append( repo.getId() ).append( "/" );
270 
271             snippet.append( "</url>\n" );
272 
273             if ( !"default".equals( repo.getLayout() ) )
274             {
275                 snippet.append( "      <layout>" ).append( repo.getLayout() ).append( "</layout>\n" );
276             }
277 
278             snippet.append( "      <releases>\n" );
279             snippet.append( "        <enabled>" ).append( Boolean.valueOf( repo.isReleases() ) ).append(
280                 "</enabled>\n" );
281             snippet.append( "      </releases>\n" );
282             snippet.append( "      <snapshots>\n" );
283             snippet.append( "        <enabled>" ).append( Boolean.valueOf( repo.isSnapshots() ) ).append(
284                 "</enabled>\n" );
285             snippet.append( "      </snapshots>\n" );
286             snippet.append( "    </pluginRepository>\n" );
287             snippet.append( "  </pluginRepositories>\n" );
288 
289             snippet.append( "  ...\n" );
290             snippet.append( "</project>\n" );
291 
292             return StringEscapeUtils.escapeXml( snippet.toString() );
293         }
294         catch ( RepositoryAdminException e )
295         {
296             throw new ArchivaRestServiceException( e.getMessage(),
297                                                    Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e );
298         }
299     }
300 }