]> source.dussan.org Git - archiva.git/blob
ca55135450e78d553d6eb532ecb7102acd5ef879
[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.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;
36
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;
45
46 /**
47  * @author Olivier Lamy
48  * @since 1.4-M1
49  */
50 @Service( "managedRepositoriesService#rest" )
51 public class DefaultManagedRepositoriesService
52     extends AbstractRestService
53     implements ManagedRepositoriesService
54 {
55
56     @Inject
57     private RepositoryCommonValidator repositoryCommonValidator;
58
59     @Inject
60     private RepositoryStatisticsManager repositoryStatisticsManager;
61
62     @Inject
63     private ManagedRepositoryAdmin managedRepositoryAdmin;
64
65     @Override
66     public List<ManagedRepository> getManagedRepositories()
67         throws ArchivaRestServiceException
68     {
69         try
70         {
71             List<org.apache.archiva.admin.model.beans.ManagedRepository> repos =
72                 managedRepositoryAdmin.getManagedRepositories();
73             return repos == null ? Collections.<ManagedRepository>emptyList() : repos;
74         }
75         catch ( RepositoryAdminException e )
76         {
77             throw new ArchivaRestServiceException( e.getMessage(), e );
78         }
79     }
80
81     @Override
82     public ManagedRepository getManagedRepository( String repositoryId )
83         throws ArchivaRestServiceException
84     {
85         List<ManagedRepository> repos = getManagedRepositories();
86         for ( ManagedRepository repo : repos )
87         {
88             if ( StringUtils.equals( repo.getId(), repositoryId ) )
89             {
90                 return repo;
91             }
92         }
93         return null;
94     }
95
96
97     @Override
98     public Boolean deleteManagedRepository( String repoId, boolean deleteContent )
99         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 }