]> source.dussan.org Git - archiva.git/blob
417c7b04802f30b9623b0c7f9600b7dd05a62aaa
[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 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;
37
38 import javax.inject.Inject;
39 import javax.ws.rs.core.Response;
40 import java.io.File;
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 ManagedRepositoryAdmin managedRepositoryAdmin;
58
59     @Inject
60     private RepositoryCommonValidator repositoryCommonValidator;
61
62     @Inject
63     private RepositoryStatisticsManager repositoryStatisticsManager;
64
65     public List<ManagedRepository> getManagedRepositories()
66         throws ArchivaRestServiceException
67     {
68         try
69         {
70             List<org.apache.archiva.admin.model.beans.ManagedRepository> repos =
71                 managedRepositoryAdmin.getManagedRepositories();
72             return repos == null ? Collections.<ManagedRepository>emptyList() : repos;
73         }
74         catch ( RepositoryAdminException e )
75         {
76             throw new ArchivaRestServiceException( e.getMessage(), e );
77         }
78     }
79
80     public ManagedRepository getManagedRepository( String repositoryId )
81         throws ArchivaRestServiceException
82     {
83         List<ManagedRepository> repos = getManagedRepositories();
84         for ( ManagedRepository repo : repos )
85         {
86             if ( StringUtils.equals( repo.getId(), repositoryId ) )
87             {
88                 return repo;
89             }
90         }
91         return null;
92     }
93
94
95     public Boolean deleteManagedRepository( String repoId, boolean deleteContent )
96         throws ArchivaRestServiceException
97     {
98
99         try
100         {
101             return managedRepositoryAdmin.deleteManagedRepository( repoId, getAuditInformation(), deleteContent );
102         }
103         catch ( RepositoryAdminException e )
104         {
105             log.error( e.getMessage(), e );
106             throw new ArchivaRestServiceException( e.getMessage(), e.getFieldName(), e );
107         }
108     }
109
110     public ManagedRepository addManagedRepository( ManagedRepository managedRepository )
111         throws ArchivaRestServiceException
112     {
113
114         try
115         {
116             boolean res =
117                 managedRepositoryAdmin.addManagedRepository( managedRepository, managedRepository.isStageRepoNeeded(),
118                                                              getAuditInformation() );
119             if ( res )
120             {
121                 return getManagedRepository( managedRepository.getId() );
122             }
123             throw new ArchivaRestServiceException( "fail to created managed Repository", null );
124         }
125         catch ( RepositoryAdminException e )
126         {
127             throw new ArchivaRestServiceException( e.getMessage(), e.getFieldName(), e );
128         }
129     }
130
131
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     public Boolean fileLocationExists( String fileLocation )
150         throws ArchivaRestServiceException
151     {
152         String location = repositoryCommonValidator.removeExpressions( fileLocation );
153         return new File( location ).exists();
154     }
155
156     public ArchivaRepositoryStatistics getManagedRepositoryStatistics( String repositoryId, String lang )
157         throws ArchivaRestServiceException
158     {
159         RepositorySession repositorySession = repositorySessionFactory.createSession();
160         SimpleDateFormat sdf = new SimpleDateFormat( "EEE, d MMM yyyy HH:mm:ss Z", new Locale( lang ) );
161         try
162         {
163             MetadataRepository metadataRepository = repositorySession.getRepository();
164
165             RepositoryStatistics stats = null;
166             try
167             {
168                 stats = repositoryStatisticsManager.getLastStatistics( metadataRepository, repositoryId );
169             }
170             catch ( MetadataRepositoryException e )
171             {
172                 log.warn( "Error retrieving repository statistics: " + e.getMessage(), e );
173             }
174             if ( stats != null )
175             {
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;
183             }
184
185         }
186         finally
187         {
188             if ( repositorySession != null )
189             {
190                 repositorySession.close();
191             }
192         }
193         return null;
194     }
195
196     public String getPomSnippet( String repositoryId )
197         throws ArchivaRestServiceException
198     {
199         return createSnippet( getManagedRepository( repositoryId ) );
200     }
201
202     private String createSnippet( ManagedRepository repo )
203         throws ArchivaRestServiceException
204     {
205         try
206         {
207             StringBuilder snippet = new StringBuilder();
208             snippet.append( "<project>\n" );
209             snippet.append( "  ...\n" );
210             snippet.append( "  <distributionManagement>\n" );
211
212             String distRepoName = "repository";
213             if ( repo.isSnapshots() )
214             {
215                 distRepoName = "snapshotRepository";
216             }
217
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" );
223
224             if ( !"default".equals( repo.getLayout() ) )
225             {
226                 snippet.append( "      <layout>" ).append( repo.getLayout() ).append( "</layout>" );
227             }
228
229             snippet.append( "    </" ).append( distRepoName ).append( ">\n" );
230             snippet.append( "  </distributionManagement>\n" );
231             snippet.append( "\n" );
232
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" );
237
238             snippet.append( "      <url>" );
239             snippet.append( getBaseUrl( httpServletRequest ) + "/repository" );
240             snippet.append( "/" ).append( repo.getId() ).append( "/" );
241
242             snippet.append( "</url>\n" );
243
244             if ( !"default".equals( repo.getLayout() ) )
245             {
246                 snippet.append( "      <layout>" ).append( repo.getLayout() ).append( "</layout>\n" );
247             }
248
249             snippet.append( "      <releases>\n" );
250             snippet.append( "        <enabled>" ).append( Boolean.valueOf( repo.isReleases() ) ).append(
251                 "</enabled>\n" );
252             snippet.append( "      </releases>\n" );
253             snippet.append( "      <snapshots>\n" );
254             snippet.append( "        <enabled>" ).append( Boolean.valueOf( repo.isSnapshots() ) ).append(
255                 "</enabled>\n" );
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" );
263
264             snippet.append( "      <url>" );
265             snippet.append( getBaseUrl( httpServletRequest ) + "/repository" );
266             snippet.append( "/" ).append( repo.getId() ).append( "/" );
267
268             snippet.append( "</url>\n" );
269
270             if ( !"default".equals( repo.getLayout() ) )
271             {
272                 snippet.append( "      <layout>" ).append( repo.getLayout() ).append( "</layout>\n" );
273             }
274
275             snippet.append( "      <releases>\n" );
276             snippet.append( "        <enabled>" ).append( Boolean.valueOf( repo.isReleases() ) ).append(
277                 "</enabled>\n" );
278             snippet.append( "      </releases>\n" );
279             snippet.append( "      <snapshots>\n" );
280             snippet.append( "        <enabled>" ).append( Boolean.valueOf( repo.isSnapshots() ) ).append(
281                 "</enabled>\n" );
282             snippet.append( "      </snapshots>\n" );
283             snippet.append( "    </pluginRepository>\n" );
284             snippet.append( "  </pluginRepositories>\n" );
285
286             snippet.append( "  ...\n" );
287             snippet.append( "</project>\n" );
288
289             return StringEscapeUtils.escapeXml( snippet.toString() );
290         }
291         catch ( RepositoryAdminException e )
292         {
293             throw new ArchivaRestServiceException( e.getMessage(),
294                                                    Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e );
295         }
296     }
297 }