This project has retired. For details please refer to its Attic page.
Source code
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.metadata.repository.MetadataRepository;
025import org.apache.archiva.metadata.repository.MetadataRepositoryException;
026import org.apache.archiva.metadata.repository.RepositorySession;
027import org.apache.archiva.metadata.repository.stats.RepositoryStatistics;
028import org.apache.archiva.metadata.repository.stats.RepositoryStatisticsManager;
029import org.apache.archiva.rest.api.model.ArchivaRepositoryStatistics;
030import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
031import org.apache.archiva.rest.api.services.ManagedRepositoriesService;
032import org.apache.commons.lang.StringEscapeUtils;
033import org.apache.commons.lang.StringUtils;
034import org.springframework.stereotype.Service;
035
036import javax.inject.Inject;
037import javax.ws.rs.core.Response;
038import java.io.File;
039import java.text.SimpleDateFormat;
040import java.util.Collections;
041import java.util.List;
042import java.util.Locale;
043
044/**
045 * @author Olivier Lamy
046 * @since 1.4-M1
047 */
048@Service( "managedRepositoriesService#rest" )
049public class DefaultManagedRepositoriesService
050    extends AbstractRestService
051    implements ManagedRepositoriesService
052{
053
054    @Inject
055    private RepositoryCommonValidator repositoryCommonValidator;
056
057    @Inject
058    private RepositoryStatisticsManager repositoryStatisticsManager;
059
060    @Override
061    public List<ManagedRepository> getManagedRepositories()
062        throws ArchivaRestServiceException
063    {
064        try
065        {
066            List<org.apache.archiva.admin.model.beans.ManagedRepository> repos =
067                managedRepositoryAdmin.getManagedRepositories();
068            return repos == null ? Collections.<ManagedRepository>emptyList() : repos;
069        }
070        catch ( RepositoryAdminException e )
071        {
072            throw new ArchivaRestServiceException( e.getMessage(), e );
073        }
074    }
075
076    @Override
077    public ManagedRepository getManagedRepository( String repositoryId )
078        throws ArchivaRestServiceException
079    {
080        List<ManagedRepository> repos = getManagedRepositories();
081        for ( ManagedRepository repo : repos )
082        {
083            if ( StringUtils.equals( repo.getId(), repositoryId ) )
084            {
085                return repo;
086            }
087        }
088        return null;
089    }
090
091
092    @Override
093    public Boolean deleteManagedRepository( String repoId, boolean deleteContent )
094        throws ArchivaRestServiceException
095    {
096
097        try
098        {
099            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}