]> source.dussan.org Git - archiva.git/blob
6aec318dae204bf68ada9e0fc3b8dbdbfc0d692b
[archiva.git] /
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.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.ActionStatus;
31 import org.apache.archiva.rest.api.model.ArchivaRepositoryStatistics;
32 import org.apache.archiva.rest.api.model.FileStatus;
33 import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
34 import org.apache.archiva.rest.api.services.ManagedRepositoriesService;
35 import org.apache.commons.lang3.StringEscapeUtils;
36 import org.apache.commons.lang3.StringUtils;
37 import org.springframework.stereotype.Service;
38
39 import javax.inject.Inject;
40 import javax.ws.rs.core.Response;
41 import java.nio.file.Files;
42 import java.nio.file.Paths;
43 import java.text.SimpleDateFormat;
44 import java.util.Collections;
45 import java.util.List;
46 import java.util.Locale;
47
48 /**
49  * @author Olivier Lamy
50  * @since 1.4-M1
51  */
52 @Service( "managedRepositoriesService#rest" )
53 public class DefaultManagedRepositoriesService
54     extends AbstractRestService
55     implements ManagedRepositoriesService
56 {
57
58     @Inject
59     private RepositoryCommonValidator repositoryCommonValidator;
60
61     @Inject
62     private RepositoryStatisticsManager repositoryStatisticsManager;
63
64     @Inject
65     private ManagedRepositoryAdmin managedRepositoryAdmin;
66
67     @Override
68     public List<ManagedRepository> getManagedRepositories()
69         throws ArchivaRestServiceException
70     {
71         try
72         {
73             List<org.apache.archiva.admin.model.beans.ManagedRepository> repos =
74                 managedRepositoryAdmin.getManagedRepositories();
75             return repos == null ? Collections.<ManagedRepository>emptyList() : repos;
76         }
77         catch ( RepositoryAdminException e )
78         {
79             throw new ArchivaRestServiceException( e.getMessage(), e );
80         }
81     }
82
83     @Override
84     public ManagedRepository getManagedRepository( String repositoryId )
85         throws ArchivaRestServiceException
86     {
87         List<ManagedRepository> repos = getManagedRepositories();
88         for ( ManagedRepository repo : repos )
89         {
90             if ( StringUtils.equals( repo.getId(), repositoryId ) )
91             {
92                 return repo;
93             }
94         }
95         return null;
96     }
97
98
99     @Override
100     public ActionStatus deleteManagedRepository( String repoId, boolean deleteContent )
101         throws ArchivaRestServiceException
102     {
103
104         try
105         {
106             return new ActionStatus( managedRepositoryAdmin.deleteManagedRepository( repoId, getAuditInformation( ), deleteContent ) );
107         }
108         catch ( RepositoryAdminException e )
109         {
110             log.error( e.getMessage(), e );
111             throw new ArchivaRestServiceException( e.getMessage(), e.getFieldName(), e );
112         }
113     }
114
115     @Override
116     public ManagedRepository addManagedRepository( ManagedRepository managedRepository )
117         throws ArchivaRestServiceException
118     {
119
120         try
121         {
122             boolean res =
123                 managedRepositoryAdmin.addManagedRepository( managedRepository, managedRepository.isStageRepoNeeded(),
124                                                              getAuditInformation() );
125             if ( res )
126             {
127                 return getManagedRepository( managedRepository.getId() );
128             }
129             throw new ArchivaRestServiceException( "fail to created managed Repository", null );
130         }
131         catch ( RepositoryAdminException e )
132         {
133             throw new ArchivaRestServiceException( e.getMessage(), e.getFieldName(), e );
134         }
135     }
136
137
138     @Override
139     public ActionStatus updateManagedRepository( ManagedRepository managedRepository )
140         throws ArchivaRestServiceException
141     {
142
143         try
144         {
145             return new ActionStatus( managedRepositoryAdmin.updateManagedRepository( managedRepository,
146                 managedRepository.isStageRepoNeeded( ),
147                 getAuditInformation( ),
148                 managedRepository.isResetStats( ) ) );
149         }
150         catch ( RepositoryAdminException e )
151         {
152             throw new ArchivaRestServiceException( e.getMessage(), e.getFieldName(), e );
153         }
154     }
155
156     @Override
157     public FileStatus getFileStatus( String fileLocation )
158         throws ArchivaRestServiceException
159     {
160         String location = repositoryCommonValidator.removeExpressions( fileLocation );
161         return new FileStatus( Files.exists( Paths.get( location ) ) );
162     }
163
164     @Override
165     public ArchivaRepositoryStatistics getManagedRepositoryStatistics( String repositoryId, String lang )
166         throws ArchivaRestServiceException
167     {
168         RepositorySession repositorySession = null;
169         try
170         {
171             repositorySession = repositorySessionFactory.createSession();
172         }
173         catch ( MetadataRepositoryException e )
174         {
175             e.printStackTrace( );
176         }
177         SimpleDateFormat sdf = new SimpleDateFormat( "EEE, d MMM yyyy HH:mm:ss Z", new Locale( lang ) );
178         try
179         {
180             MetadataRepository metadataRepository = repositorySession.getRepository();
181
182             RepositoryStatistics stats = null;
183             try
184             {
185                 stats = repositoryStatisticsManager.getLastStatistics( repositoryId );
186             }
187             catch ( MetadataRepositoryException e )
188             {
189                 log.warn( "Error retrieving repository statistics: {}", e.getMessage(), e );
190             }
191             if ( stats != null )
192             {
193                 ArchivaRepositoryStatistics archivaRepositoryStatistics =
194                     getModelMapper().map( stats, ArchivaRepositoryStatistics.class );
195                 archivaRepositoryStatistics.setDuration( archivaRepositoryStatistics.getScanEndTime().getTime()
196                                                              - archivaRepositoryStatistics.getScanStartTime().getTime() );
197                 archivaRepositoryStatistics.setLastScanDate(
198                     sdf.format( archivaRepositoryStatistics.getScanEndTime() ) );
199                 return archivaRepositoryStatistics;
200             }
201
202         }
203         finally
204         {
205             if ( repositorySession != null )
206             {
207                 repositorySession.close();
208             }
209         }
210         return null;
211     }
212
213     @Override
214     public String getPomSnippet( String repositoryId )
215         throws ArchivaRestServiceException
216     {
217         return createSnippet( getManagedRepository( repositoryId ) );
218     }
219
220     private String createSnippet( ManagedRepository repo )
221         throws ArchivaRestServiceException
222     {
223         try
224         {
225             StringBuilder snippet = new StringBuilder();
226             snippet.append( "<project>\n" );
227             snippet.append( "  ...\n" );
228             snippet.append( "  <distributionManagement>\n" );
229
230             String distRepoName = "repository";
231             if ( repo.isSnapshots() )
232             {
233                 distRepoName = "snapshotRepository";
234             }
235
236             snippet.append( "    <" ).append( distRepoName ).append( ">\n" );
237             snippet.append( "      <id>" ).append( repo.getId() ).append( "</id>\n" );
238             snippet.append( "      <url>" );
239             snippet.append( getBaseUrl(  ) + "/repository" );
240             snippet.append( "/" ).append( repo.getId() ).append( "/" ).append( "</url>\n" );
241
242             if ( !"default".equals( repo.getLayout() ) )
243             {
244                 snippet.append( "      <layout>" ).append( repo.getLayout() ).append( "</layout>" );
245             }
246
247             snippet.append( "    </" ).append( distRepoName ).append( ">\n" );
248             snippet.append( "  </distributionManagement>\n" );
249             snippet.append( "\n" );
250
251             snippet.append( "  <repositories>\n" );
252             snippet.append( "    <repository>\n" );
253             snippet.append( "      <id>" ).append( repo.getId() ).append( "</id>\n" );
254             snippet.append( "      <name>" ).append( repo.getName() ).append( "</name>\n" );
255
256             snippet.append( "      <url>" );
257             snippet.append( getBaseUrl(  ) + "/repository" );
258             snippet.append( "/" ).append( repo.getId() ).append( "/" );
259
260             snippet.append( "</url>\n" );
261
262             if ( !"default".equals( repo.getLayout() ) )
263             {
264                 snippet.append( "      <layout>" ).append( repo.getLayout() ).append( "</layout>\n" );
265             }
266
267             snippet.append( "      <releases>\n" );
268             snippet.append( "        <enabled>" ).append( Boolean.valueOf( repo.isReleases() ) ).append(
269                 "</enabled>\n" );
270             snippet.append( "      </releases>\n" );
271             snippet.append( "      <snapshots>\n" );
272             snippet.append( "        <enabled>" ).append( Boolean.valueOf( repo.isSnapshots() ) ).append(
273                 "</enabled>\n" );
274             snippet.append( "      </snapshots>\n" );
275             snippet.append( "    </repository>\n" );
276             snippet.append( "  </repositories>\n" );
277             snippet.append( "  <pluginRepositories>\n" );
278             snippet.append( "    <pluginRepository>\n" );
279             snippet.append( "      <id>" ).append( repo.getId() ).append( "</id>\n" );
280             snippet.append( "      <name>" ).append( repo.getName() ).append( "</name>\n" );
281
282             snippet.append( "      <url>" );
283             snippet.append( getBaseUrl(  ) + "/repository" );
284             snippet.append( "/" ).append( repo.getId() ).append( "/" );
285
286             snippet.append( "</url>\n" );
287
288             if ( !"default".equals( repo.getLayout() ) )
289             {
290                 snippet.append( "      <layout>" ).append( repo.getLayout() ).append( "</layout>\n" );
291             }
292
293             snippet.append( "      <releases>\n" );
294             snippet.append( "        <enabled>" ).append( Boolean.valueOf( repo.isReleases() ) ).append(
295                 "</enabled>\n" );
296             snippet.append( "      </releases>\n" );
297             snippet.append( "      <snapshots>\n" );
298             snippet.append( "        <enabled>" ).append( Boolean.valueOf( repo.isSnapshots() ) ).append(
299                 "</enabled>\n" );
300             snippet.append( "      </snapshots>\n" );
301             snippet.append( "    </pluginRepository>\n" );
302             snippet.append( "  </pluginRepositories>\n" );
303
304             snippet.append( "  ...\n" );
305             snippet.append( "</project>\n" );
306
307             return StringEscapeUtils.escapeXml( snippet.toString() );
308         }
309         catch ( RepositoryAdminException e )
310         {
311             throw new ArchivaRestServiceException( e.getMessage(),
312                                                    Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e );
313         }
314     }
315 }