All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.archiva.rest.services.DefaultManagedRepositoriesService Maven / Gradle / Ivy

package org.apache.archiva.rest.services;
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import org.apache.archiva.admin.model.RepositoryAdminException;
import org.apache.archiva.admin.model.RepositoryCommonValidator;
import org.apache.archiva.admin.model.beans.ManagedRepository;
import org.apache.archiva.metadata.repository.MetadataRepository;
import org.apache.archiva.metadata.repository.MetadataRepositoryException;
import org.apache.archiva.metadata.repository.RepositorySession;
import org.apache.archiva.metadata.repository.stats.RepositoryStatistics;
import org.apache.archiva.metadata.repository.stats.RepositoryStatisticsManager;
import org.apache.archiva.rest.api.model.ArchivaRepositoryStatistics;
import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
import org.apache.archiva.rest.api.services.ManagedRepositoriesService;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.inject.Inject;
import javax.ws.rs.core.Response;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.List;
import java.util.Locale;

/**
 * @author Olivier Lamy
 * @since 1.4-M1
 */
@Service( "managedRepositoriesService#rest" )
public class DefaultManagedRepositoriesService
    extends AbstractRestService
    implements ManagedRepositoriesService
{

    @Inject
    private RepositoryCommonValidator repositoryCommonValidator;

    @Inject
    private RepositoryStatisticsManager repositoryStatisticsManager;

    @Override
    public List getManagedRepositories()
        throws ArchivaRestServiceException
    {
        try
        {
            List repos =
                managedRepositoryAdmin.getManagedRepositories();
            return repos == null ? Collections.emptyList() : repos;
        }
        catch ( RepositoryAdminException e )
        {
            throw new ArchivaRestServiceException( e.getMessage(), e );
        }
    }

    @Override
    public ManagedRepository getManagedRepository( String repositoryId )
        throws ArchivaRestServiceException
    {
        List repos = getManagedRepositories();
        for ( ManagedRepository repo : repos )
        {
            if ( StringUtils.equals( repo.getId(), repositoryId ) )
            {
                return repo;
            }
        }
        return null;
    }


    @Override
    public Boolean deleteManagedRepository( String repoId, boolean deleteContent )
        throws ArchivaRestServiceException
    {

        try
        {
            return managedRepositoryAdmin.deleteManagedRepository( repoId, getAuditInformation(), deleteContent );
        }
        catch ( RepositoryAdminException e )
        {
            log.error( e.getMessage(), e );
            throw new ArchivaRestServiceException( e.getMessage(), e.getFieldName(), e );
        }
    }

    @Override
    public ManagedRepository addManagedRepository( ManagedRepository managedRepository )
        throws ArchivaRestServiceException
    {

        try
        {
            boolean res =
                managedRepositoryAdmin.addManagedRepository( managedRepository, managedRepository.isStageRepoNeeded(),
                                                             getAuditInformation() );
            if ( res )
            {
                return getManagedRepository( managedRepository.getId() );
            }
            throw new ArchivaRestServiceException( "fail to created managed Repository", null );
        }
        catch ( RepositoryAdminException e )
        {
            throw new ArchivaRestServiceException( e.getMessage(), e.getFieldName(), e );
        }
    }


    @Override
    public Boolean updateManagedRepository( ManagedRepository managedRepository )
        throws ArchivaRestServiceException
    {

        try
        {
            return managedRepositoryAdmin.updateManagedRepository( managedRepository,
                                                                   managedRepository.isStageRepoNeeded(),
                                                                   getAuditInformation(),
                                                                   managedRepository.isResetStats() );
        }
        catch ( RepositoryAdminException e )
        {
            throw new ArchivaRestServiceException( e.getMessage(), e.getFieldName(), e );
        }
    }

    @Override
    public Boolean fileLocationExists( String fileLocation )
        throws ArchivaRestServiceException
    {
        String location = repositoryCommonValidator.removeExpressions( fileLocation );
        return new File( location ).exists();
    }

    @Override
    public ArchivaRepositoryStatistics getManagedRepositoryStatistics( String repositoryId, String lang )
        throws ArchivaRestServiceException
    {
        RepositorySession repositorySession = repositorySessionFactory.createSession();
        SimpleDateFormat sdf = new SimpleDateFormat( "EEE, d MMM yyyy HH:mm:ss Z", new Locale( lang ) );
        try
        {
            MetadataRepository metadataRepository = repositorySession.getRepository();

            RepositoryStatistics stats = null;
            try
            {
                stats = repositoryStatisticsManager.getLastStatistics( metadataRepository, repositoryId );
            }
            catch ( MetadataRepositoryException e )
            {
                log.warn( "Error retrieving repository statistics: {}", e.getMessage(), e );
            }
            if ( stats != null )
            {
                ArchivaRepositoryStatistics archivaRepositoryStatistics =
                    getModelMapper().map( stats, ArchivaRepositoryStatistics.class );
                archivaRepositoryStatistics.setDuration( archivaRepositoryStatistics.getScanEndTime().getTime()
                                                             - archivaRepositoryStatistics.getScanStartTime().getTime() );
                archivaRepositoryStatistics.setLastScanDate(
                    sdf.format( archivaRepositoryStatistics.getScanEndTime() ) );
                return archivaRepositoryStatistics;
            }

        }
        finally
        {
            if ( repositorySession != null )
            {
                repositorySession.close();
            }
        }
        return null;
    }

    @Override
    public String getPomSnippet( String repositoryId )
        throws ArchivaRestServiceException
    {
        return createSnippet( getManagedRepository( repositoryId ) );
    }

    private String createSnippet( ManagedRepository repo )
        throws ArchivaRestServiceException
    {
        try
        {
            StringBuilder snippet = new StringBuilder();
            snippet.append( "\n" );
            snippet.append( "  ...\n" );
            snippet.append( "  \n" );

            String distRepoName = "repository";
            if ( repo.isSnapshots() )
            {
                distRepoName = "snapshotRepository";
            }

            snippet.append( "    <" ).append( distRepoName ).append( ">\n" );
            snippet.append( "      " ).append( repo.getId() ).append( "\n" );
            snippet.append( "      " );
            snippet.append( getBaseUrl(  ) + "/repository" );
            snippet.append( "/" ).append( repo.getId() ).append( "/" ).append( "\n" );

            if ( !"default".equals( repo.getLayout() ) )
            {
                snippet.append( "      " ).append( repo.getLayout() ).append( "" );
            }

            snippet.append( "    \n" );
            snippet.append( "  \n" );
            snippet.append( "\n" );

            snippet.append( "  \n" );
            snippet.append( "    \n" );
            snippet.append( "      " ).append( repo.getId() ).append( "\n" );
            snippet.append( "      " ).append( repo.getName() ).append( "\n" );

            snippet.append( "      " );
            snippet.append( getBaseUrl(  ) + "/repository" );
            snippet.append( "/" ).append( repo.getId() ).append( "/" );

            snippet.append( "\n" );

            if ( !"default".equals( repo.getLayout() ) )
            {
                snippet.append( "      " ).append( repo.getLayout() ).append( "\n" );
            }

            snippet.append( "      \n" );
            snippet.append( "        " ).append( Boolean.valueOf( repo.isReleases() ) ).append(
                "\n" );
            snippet.append( "      \n" );
            snippet.append( "      \n" );
            snippet.append( "        " ).append( Boolean.valueOf( repo.isSnapshots() ) ).append(
                "\n" );
            snippet.append( "      \n" );
            snippet.append( "    \n" );
            snippet.append( "  \n" );
            snippet.append( "  \n" );
            snippet.append( "    \n" );
            snippet.append( "      " ).append( repo.getId() ).append( "\n" );
            snippet.append( "      " ).append( repo.getName() ).append( "\n" );

            snippet.append( "      " );
            snippet.append( getBaseUrl(  ) + "/repository" );
            snippet.append( "/" ).append( repo.getId() ).append( "/" );

            snippet.append( "\n" );

            if ( !"default".equals( repo.getLayout() ) )
            {
                snippet.append( "      " ).append( repo.getLayout() ).append( "\n" );
            }

            snippet.append( "      \n" );
            snippet.append( "        " ).append( Boolean.valueOf( repo.isReleases() ) ).append(
                "\n" );
            snippet.append( "      \n" );
            snippet.append( "      \n" );
            snippet.append( "        " ).append( Boolean.valueOf( repo.isSnapshots() ) ).append(
                "\n" );
            snippet.append( "      \n" );
            snippet.append( "    \n" );
            snippet.append( "  \n" );

            snippet.append( "  ...\n" );
            snippet.append( "\n" );

            return StringEscapeUtils.escapeXml( snippet.toString() );
        }
        catch ( RepositoryAdminException e )
        {
            throw new ArchivaRestServiceException( e.getMessage(),
                                                   Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e );
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy