Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Sonatype Nexus (TM) Open Source Version Copyright (c) 2007-2012 Sonatype, Inc. All rights reserved. Includes the third-party code listed at http://links.sonatype.com/products/nexus/oss/attributions. This program and the accompanying materials are made available under the terms of the Eclipse Public License Version 1.0, which accompanies this distribution and is available at http://www.eclipse.org/legal/epl-v10.html. Sonatype Nexus (TM) Professional Version is available from Sonatype, Inc. "Sonatype" and "Sonatype Nexus" are trademarks of Sonatype, Inc. Apache Maven is a trademark of the Apache Software Foundation. M2eclipse is a trademark of the Eclipse Foundation. All other trademarks are the property of their respective owners.
 
 package org.sonatype.nexus.rest.repositories;
 
 
 import  javax.ws.rs.Consumes;
 import  javax.ws.rs.DELETE;
 import  javax.ws.rs.GET;
 import  javax.ws.rs.PUT;
 import  javax.ws.rs.Path;
 import  javax.ws.rs.PathParam;
 import  javax.ws.rs.Produces;
 
 import  org.codehaus.enunciate.contract.jaxrs.ResourceMethodSignature;
 import  org.restlet.Context;
 import  org.restlet.data.Request;
 import  org.restlet.data.Response;
 import  org.restlet.data.Status;
 import  org.restlet.resource.ResourceException;
 import  org.restlet.resource.Variant;
Resource handler for Repository resource.

Author(s):
cstamas
 
 @Component( role = PlexusResource.class, hint = "RepositoryPlexusResource" )
 @Produces( { "application/xml""application/json" } )
 @Consumes( { "application/xml""application/json" } )
 {
     public static final String RESOURCE_URI = "/repositories/{" +  + "}";
 
     public RepositoryPlexusResource()
     {
         this.setModifiabletrue );
     }
 
     @Override
     public Object getPayloadInstance()
     {
         return new RepositoryResourceResponse();
     }
 
     @Override
     public String getResourceUri()
     {
         return ;
     }
 
     @Override
     {
         return new PathProtectionDescriptor"/repositories/*""authcBasic,perms[nexus:repositories]" );
     }

    
Get the configuration of an existing repository.

Parameters:
repositoryId The repository to access.
    @Override
    @GET
    @ResourceMethodSignature( pathParams = { @PathParam( . ) }, output = RepositoryResourceResponse.class )
    public Object get( Context context, Request request, Response response, Variant variant )
        throws ResourceException
    {
        return this.getRepositoryResourceResponserequestgetRepositoryIdrequest ) );
    }

    
Update an existing repository in nexus with new configuration.

Parameters:
repositoryId The repository to access.
    @Override
    @PUT
    @ResourceMethodSignature( pathParams = { @PathParam( . ) }, input = RepositoryResourceResponse.class, output = RepositoryResourceResponse.class )
    public Object put( Context context, Request request, Response responseObject payload )
        throws ResourceException
    {
        RepositoryResourceResponse repoRequest = (RepositoryResourceResponsepayload;
        String repoId = this.getRepositoryIdrequest );
        if ( repoRequest != null )
        {
            try
            {
                RepositoryBaseResource resource = repoRequest.getData();
                if ( ..equalsresource.getRepoType() ) )
                {
                    RepositoryShadowResource model = (RepositoryShadowResourceresource;
                    try
                    {
                        ShadowRepository shadow =
                            getRepositoryRegistry().getRepositoryWithFacetrepoIdShadowRepository.class );
                        shadow.setNamemodel.getName() );
                        shadow.setExposedresource.isExposed() );
                        
                        shadow.setMasterRepositorygetRepositoryRegistry().getRepositorymodel.getShadowOf() ) );
                        shadow.setSynchronizeAtStartupmodel.isSyncAtStartup() );
                        getNexusConfiguration().saveConfiguration();
                    }
                    catch ( NoSuchRepositoryAccessException e )
                    {
                        getLogger().warn"Repository access denied, id=" + repoId );
                        throw new ResourceException( Status.CLIENT_ERROR_FORBIDDEN, "Access Denied to Repository" );
                    }
                    catch ( NoSuchRepositoryException e )
                    {
                        getLogger().warn"Virtual repository not found, id=" + repoId );
                        throw new ResourceException( Status.CLIENT_ERROR_NOT_FOUND, "Virtual repository Not Found" );
                    }
                }
                else
                {
                    RepositoryResource model = (RepositoryResourceresource;
                    try
                    {
                        Repository repository = getRepositoryRegistry().getRepositoryrepoId );
                        repository.setNamemodel.getName() );
                        repository.setExposedresource.isExposed() );
                        // set null to read only
                        RepositoryWritePolicy writePolicy =
                            ( model.getWritePolicy() != null ) ? RepositoryWritePolicy.valueOfmodel.getWritePolicy() )
                                            : .;
                        repository.setWritePolicywritePolicy );
                        repository.setBrowseablemodel.isBrowseable() );
                        repository.setIndexablemodel.isIndexable() );
                        repository.setSearchablemodel.isIndexable() );
                        repository.setNotFoundCacheTimeToLivemodel.getNotFoundCacheTTL() );
                        if ( repository.getRepositoryKind().isFacetAvailableProxyRepository.class ) )
                        {
                            ProxyRepository proxyRepo = repository.adaptToFacetProxyRepository.class );
                            proxyRepo.setRemoteUrlmodel.getRemoteStorage().getRemoteStorageUrl() );
                            String oldPasswordForRemoteStorage = null;
                            if ( proxyRepo.getRemoteAuthenticationSettings() != null
                                && UsernamePasswordRemoteAuthenticationSettings.class.isInstanceproxyRepo
                                    .getRemoteAuthenticationSettings() ) )
                            {
                                oldPasswordForRemoteStorage =
                                    ( (UsernamePasswordRemoteAuthenticationSettingsproxyRepo
                                        .getRemoteAuthenticationSettings() ).getPassword();
                            }
                            String oldPasswordForProxy = null;
                            if ( proxyRepo.getRemoteProxySettings() != null
                                && proxyRepo.getRemoteProxySettings().isEnabled()
                                && proxyRepo.getRemoteProxySettings().getProxyAuthentication() != null
                                && UsernamePasswordRemoteAuthenticationSettings.class.isInstanceproxyRepo
                                    .getRemoteAuthenticationSettings() ) )
                            {
                                oldPasswordForProxy =
                                    ( (UsernamePasswordRemoteAuthenticationSettingsproxyRepo.getRemoteProxySettings()
                                        .getProxyAuthentication() ).getPassword();
                            }
                            RemoteAuthenticationSettings remoteAuth =
                                getAuthenticationInfoConverter().convertAndValidateFromModel(
                                    this.convertAuthenticationmodel.getRemoteStorage().getAuthentication(),
                                        oldPasswordForRemoteStorage ) );
                            RemoteConnectionSettings remoteConnSettings =
                                getGlobalRemoteConnectionSettings().convertAndValidateFromModel(
                                    this.convertRemoteConnectionSettingsmodel.getRemoteStorage()
                                        .getConnectionSettings() ) );
                            RemoteProxySettings httpProxySettings =
                                getGlobalHttpProxySettings().convertAndValidateFromModel(
                                    this.convertHttpProxySettingsmodel.getRemoteStorage().getHttpProxySettings(),
                                        oldPasswordForProxy ) );
                            if ( remoteAuth != null )
                            {
                                proxyRepo.setRemoteAuthenticationSettingsremoteAuth );
                            }
                            else
                            {
                                proxyRepo.getRemoteStorageContext().removeRemoteAuthenticationSettings();
                            }
                            if ( remoteConnSettings != null )
                            {
                                proxyRepo.setRemoteConnectionSettingsremoteConnSettings );
                            }
                            else
                            {
                                proxyRepo.getRemoteStorageContext().removeRemoteConnectionSettings();
                            }
                            if ( httpProxySettings != null )
                            {
                                proxyRepo.setRemoteProxySettingshttpProxySettings );
                            }
                            else
                            {
                                proxyRepo.getRemoteStorageContext().removeRemoteProxySettings();
                            }
                            // set auto block
                            proxyRepo.setAutoBlockActive( ( (RepositoryProxyResourcemodel ).isAutoBlockActive() );
                            
                            // set type validation
                            proxyRepo.setFileTypeValidation( ( (RepositoryProxyResourcemodel ).isFileTypeValidation() );
                            
                        }
                        
                        if ( repository.getRepositoryKind().isFacetAvailableMavenRepository.class ) )
                        {
                            RepositoryPolicy repoPolicy =
                                EnumUtil.valueOfmodel.getRepoPolicy(), RepositoryPolicy.class );
                            repository.adaptToFacetMavenRepository.class ).setRepositoryPolicyrepoPolicy );
                            if ( repository.getRepositoryKind().isFacetAvailableMavenProxyRepository.class ) )
                            {
                                ChecksumPolicy checksum =
                                    EnumUtil.valueOfmodel.getChecksumPolicy(), ChecksumPolicy.class );
                                MavenProxyRepository pRepository = repository.adaptToFacetMavenProxyRepository.class );
                                pRepository.setChecksumPolicychecksum );
                                pRepository.setDownloadRemoteIndexesmodel.isDownloadRemoteIndexes() );
                                pRepository.setChecksumPolicy( EnumUtil.valueOfmodel.getChecksumPolicy(),
                                    ChecksumPolicy.class ) );
                                pRepository.setDownloadRemoteIndexesmodel.isDownloadRemoteIndexes() );
                                RepositoryProxyResource proxyModel = (RepositoryProxyResourcemodel;
                                pRepository.setArtifactMaxAgeproxyModel.getArtifactMaxAge() );
                                pRepository.setMetadataMaxAgeproxyModel.getMetadataMaxAge() );
                            }
                        }
                        else
                        {
                            // This is a total hack to be able to retrieve this data from a non core repo if available
                            try
                            {
                                Method artifactMethod =
                                    repository.getClass().getMethod"setArtifactMaxAge"int.class );
                                Method metadataMethod =
                                    repository.getClass().getMethod"setMetadataMaxAge"int.class );
                                RepositoryProxyResource proxyModel = (RepositoryProxyResourcemodel;
                                if ( artifactMethod != null )
                                {
                                    artifactMethod.invokerepositoryproxyModel.getArtifactMaxAge() );
                                }
                                if ( metadataMethod != null )
                                {
                                    metadataMethod.invokerepositoryproxyModel.getMetadataMaxAge() );
                                }
                            }
                            catch ( Exception e )
                            {
                                // nothing to do here, doesn't support artifactmax age
                            }
                        }
                        repository.setLocalUrlmodel.getOverrideLocalStorageUrl() );
                        getNexusConfiguration().saveConfiguration();
                    }
                    catch ( NoSuchRepositoryAccessException e )
                    {
                        getLogger().warn"Repository access denied, id=" + repoId );
                        throw new ResourceException( Status.CLIENT_ERROR_FORBIDDEN, "Access Denied to Repository" );
                    }
                    catch ( NoSuchRepositoryException e )
                    {
                        getLogger().warn"Repository not found, id=" + repoId );
                        throw new ResourceException( Status.CLIENT_ERROR_NOT_FOUND, "Repository Not Found" );
                    }
                }
            }
            catch ( ConfigurationException e )
            {
                handleConfigurationExceptione );
            }
            catch ( StorageException e )
            {
                ErrorResponse nexusErrorResponse = getNexusErrorResponse"*"e.getMessage() );
                throw new PlexusResourceException( Status.CLIENT_ERROR_BAD_REQUEST, "Configuration error.",
                                                   nexusErrorResponse );
            }
            catch ( IOException e )
            {
                getLogger().warn"Got IO Exception!"e );
                throw new ResourceException( Status.SERVER_ERROR_INTERNAL );
            }
        }
        // return current repo
        return this.getRepositoryResourceResponserequestgetRepositoryIdrequest ) );
    }

    
Delete an existing repository from nexus.

Parameters:
repositoryId The repository to access.
    @Override
    @DELETE
    @ResourceMethodSignature( pathParams = { @PathParam( . ) } )
    public void delete( Context context, Request request, Response response )
        throws ResourceException
    {
        String repoId = this.getRepositoryIdrequest );
        try
        {
            getNexus().deleteRepositoryrepoId );
            response.setStatus( Status.SUCCESS_NO_CONTENT );
        }
        catch ( ConfigurationException e )
        {
            getLogger().warn"Repository not deletable, it has dependants, id=" + repoId );
            throw new ResourceException( Status.CLIENT_ERROR_BAD_REQUEST,
                                         "Repository is not deletable, it has dependants." );
        }
        catch ( NoSuchRepositoryAccessException e )
        {
            getLogger().warn"Repository access denied, id=" + repoId );
            throw new ResourceException( Status.CLIENT_ERROR_FORBIDDEN, "Access Denied to Repository" );
        }
        catch ( NoSuchRepositoryException e )
        {
            getLogger().warn"Repository not found, id=" + repoId );
            throw new ResourceException( Status.CLIENT_ERROR_NOT_FOUND, "Repository Not Found" );
        }
        catch ( IOException e )
        {
            getLogger().warn"Got IO Exception!"e );
            throw new ResourceException( Status.SERVER_ERROR_INTERNAL );
        }
        catch ( AccessDeniedException e )
        {
            getLogger().warn"Not allowed to delete repository '" + repoId + "'"e );
            throw new ResourceException( Status.CLIENT_ERROR_BAD_REQUEST, "Not allowed to delete repository '" + repoId
                + "'" );
        }
    }
New to GrepCode? Check out our FAQ X