Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2008-2011 Sonatype, Inc. All rights reserved. Includes the third-party code listed at http://www.sonatype.com/products/nexus/attributions. This program is free software: you can redistribute it and/or modify it only under the terms of the GNU Affero General Public License Version 3 as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License Version 3 for more details. You should have received a copy of the GNU Affero General Public License Version 3 along with this program. If not, see http://www.gnu.org/licenses. Sonatype Nexus (TM) Open Source Version is available from Sonatype, Inc. Sonatype and Sonatype Nexus are trademarks of Sonatype, Inc. Apache Maven is a trademark of the Apache Foundation. M2Eclipse is a trademark of the Eclipse Foundation. All other trademarks are the property of their respective owners.
 
 package org.sonatype.nexus.rest.global;
 
 import java.util.List;
 
 import  javax.ws.rs.Consumes;
 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.Reference;
 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;
The GlobalConfiguration resource. It simply gets and builds the requested config REST model (DTO) and passes serializes it using underlying representation mechanism.

Author(s):
cstamas
tstevens
 
 @Component( role = PlexusResource.class, hint = "GlobalConfigurationPlexusResource" )
 @Produces( { "application/xml""application/json" } )
 @Consumes( { "application/xml""application/json" } )
 {
    
The config key used in URI and request attributes
 
     public static final String CONFIG_NAME_KEY = "configName";
 
     public static final String RESOURCE_URI = "/global_settings/{" +  + "}";

    
Name denoting current Nexus configuration
 
     public static final String CURRENT_CONFIG_NAME = "current";

    
Name denoting default Nexus configuration
 
     public static final String DEFAULT_CONFIG_NAME = "default";
 
     @Requirement
     private SecuritySystem securitySystem;
    
    // DEFAULT CONFIG
    // ==
    @Requirement( hint = "static" )
    @Requirement( hint = "static" )
    // ----------------------------------------------------------------------------
    // Default Configuration
    // ----------------------------------------------------------------------------
    public boolean isDefaultSecurityEnabled()
    {
    }
    public boolean isDefaultAnonymousAccessEnabled()
    {
    }
    {
    }
    {
    }
    public List<StringgetDefaultRealms()
    {
    }
    {
    }
    {
    }
    {
    }
    {
    }
    // ==
    {
        this.setModifiabletrue );
    }
    @Override
    public Object getPayloadInstance()
    {
        return new GlobalConfigurationResourceResponse();
    }
    @Override
    public String getResourceUri()
    {
        return ;
    }
    @Override
    {
        return new PathProtectionDescriptor"/global_settings/*""authcBasic,perms[nexus:settings]" );
    }

    
Get the specified global configuration (i.e. current or default)

Parameters:
configName The name of the config (as returned by the global configuration list resource) to get.
    @Override
    @GET
    @ResourceMethodSignature( pathParams = { @PathParam( . ) }, output = GlobalConfigurationResourceResponse.class )
    public Object get( Context context, Request request, Response response, Variant variant )
        throws ResourceException
    {
        String configurationName = request.getAttributes().get(  ).toString();
        if ( !.equalsconfigurationName ) && !.equalsconfigurationName ) )
        {
            throw new ResourceException( Status.CLIENT_ERROR_NOT_FOUND );
        }
        else
        {
            GlobalConfigurationResource resource = new GlobalConfigurationResource();
            if ( .equalsconfigurationName ) )
            {
                fillDefaultConfigurationrequestresource );
            }
            else
            {
                fillCurrentConfigurationrequestresource );
            }
            result.setDataresource );
            return result;
        }
    }

    
Update the global configuration.

Parameters:
configName The name of the config (as returned by the global configuration list resource) to update.
    @Override
    @PUT
    @ResourceMethodSignature( pathParams = { @PathParam( . ) }, input = GlobalConfigurationResourceResponse.class )
    public Object put( Context context, Request request, Response responseObject payload )
        throws ResourceException
    {
        String configurationName = request.getAttributes().get(  ).toString();
        if ( !.equalsconfigurationName ) && !.equalsconfigurationName ) )
        {
            throw new ResourceException( Status.CLIENT_ERROR_NOT_FOUND );
        }
        else if ( !.equalsconfigurationName ) )
        {
            throw new ResourceException( Status.CLIENT_ERROR_METHOD_NOT_ALLOWED );
        }
        else
        {
            GlobalConfigurationResourceResponse configRequest = (GlobalConfigurationResourceResponsepayload;
            if ( configRequest != null )
            {
                GlobalConfigurationResource resource = configRequest.getData();
                try
                {
                    if ( resource.getSmtpSettings() != null )
                    {
                        SmtpSettings settings = resource.getSmtpSettings();
                        getNexusEmailer().setSMTPHostnamesettings.getHost() );
                        // lookup old password
                        String oldPassword = getNexusEmailer().getSMTPPassword();
                        getNexusEmailer().setSMTPPasswordthis.getActualPasswordsettings.getPassword(), oldPassword ) );
                        getNexusEmailer().setSMTPPortsettings.getPort() );
                        getNexusEmailer().setSMTPSslEnabledsettings.isSslEnabled() );
                        getNexusEmailer().setSMTPTlsEnabledsettings.isTlsEnabled() );
                        getNexusEmailer().setSMTPUsernamesettings.getUsername() );
                        getNexusEmailer().setSMTPSystemEmailAddress(
                                                                     new Address(
                                                                                  settings.getSystemEmailAddress().trim() ) );
                    }
                    ErrorReportingSettings settings = resource.getErrorReportingSettings();
                    if ( settings != null )
                    {
                        getErrorReportingManager().setEnabledsettings.isReportErrorsAutomatically() );
                        getErrorReportingManager().setJIRAUsernamesettings.getJiraUsername() );
                        // look up old password
                        getErrorReportingManager().setJIRAPassword(
                                                                    this.getActualPassword(
                                                                                            settings.getJiraPassword(),
                                                                                            getErrorReportingManager().getJIRAPassword() ) );
                        getErrorReportingManager().setUseGlobalProxytrue );
                    }
                    else
                    {
                        getErrorReportingManager().setEnabledfalse );
                        getErrorReportingManager().setJIRAUsernamenull );
                        getErrorReportingManager().setJIRAPasswordnull );
                    }
                    if ( resource.getGlobalConnectionSettings() != null )
                    {
                        RemoteConnectionSettings s = resource.getGlobalConnectionSettings();
                        getGlobalRemoteConnectionSettings().setConnectionTimeouts.getConnectionTimeout() * 1000 );
                        getGlobalRemoteConnectionSettings().setRetrievalRetryCounts.getRetrievalRetryCount() );
                        getGlobalRemoteConnectionSettings().setQueryStrings.getQueryString() );
                        getGlobalRemoteConnectionSettings().setUserAgentCustomizationStrings.getUserAgentString() );
                    }
                    if ( resource.getGlobalHttpProxySettings() != null
                        && !StringUtils.isEmptyresource.getGlobalHttpProxySettings().getProxyHostname() ) )
                    {
                        RemoteHttpProxySettings s = resource.getGlobalHttpProxySettings();
                        getGlobalHttpProxySettings().setHostnames.getProxyHostname() );
                        getGlobalHttpProxySettings().setPorts.getProxyPort() );
                        List<StringnonProxyHosts = resource.getGlobalHttpProxySettings().getNonProxyHosts();
                        if ( nonProxyHosts != null && !nonProxyHosts.isEmpty() )
                        {
                            // removing nulls and empty strings
                            HashSet<StringcleanNonProxyHosts = new HashSet<String>();
                            for ( String host : nonProxyHosts )
                            {
                                if ( StringUtils.isNotEmptyhost ) )
                                {
                                    cleanNonProxyHosts.addhost );
                                }
                            }
                            getGlobalHttpProxySettings().setNonProxyHostscleanNonProxyHosts );
                        }
                        else
                        {
                            // clear it out
                            getGlobalHttpProxySettings().setNonProxyHostsnew HashSet<String>( 0 ) );
                        }
                        if ( s.getAuthentication() != null )
                        {
                            CRemoteAuthentication auth = new CRemoteAuthentication();
                            auth.setUsernames.getAuthentication().getUsername() );
                            String oldPassword = null;
                            if ( getGlobalHttpProxySettings().getProxyAuthentication() != null )
                            {
                                oldPassword =
                                    ( (UsernamePasswordRemoteAuthenticationSettingsgetGlobalHttpProxySettings().getProxyAuthentication() ).getPassword();
                            }
                            auth.setPasswordthis.getActualPasswords.getAuthentication().getPassword(), oldPassword ) );
                            auth.setNtlmDomains.getAuthentication().getNtlmDomain() );
                            auth.setNtlmHosts.getAuthentication().getNtlmHost() );
                            // auth.setPrivateKey( s.getAuthentication().getPrivateKey() );
                            // auth.setPassphrase( s.getAuthentication().getPassphrase() );
                            getGlobalHttpProxySettings().setProxyAuthentication(
                                                                                 getAuthenticationInfoConverter().convertAndValidateFromModel(
                                                                                                                                               auth ) );
                        }
                        else
                        {
                            getGlobalHttpProxySettings().setProxyAuthenticationnull );
                        }
                    }
                    else
                    {
                        getGlobalHttpProxySettings().disable();
                    }
                    getNexusConfiguration().setRealmsresource.getSecurityRealms() );
                    getNexusConfiguration().setSecurityEnabledresource.isSecurityEnabled() );
                    getNexusConfiguration().setAnonymousAccessEnabledresource.isSecurityAnonymousAccessEnabled() );
                    if ( resource.isSecurityAnonymousAccessEnabled()
                        && !StringUtils.isEmptyresource.getSecurityAnonymousUsername() )
                        && !StringUtils.isEmptyresource.getSecurityAnonymousPassword() ) )
                    {
                        // check if the user/pass changed
                        String oldPassword = getNexusConfiguration().getAnonymousPassword();
                        String newPassword =
                            this.getActualPasswordresource.getSecurityAnonymousPassword(), oldPassword );
                        if ( !StringUtils.equalsgetNexusConfiguration().getAnonymousUsername(),
                                                  resource.getSecurityAnonymousUsername() )
                            || !StringUtils.equalsnewPasswordoldPassword ) )
                        {
                            // test auth
                            try
                            {
                                // try to "log in" with supplied credentials
                                // the anon user a) should exists b) the pwd must work
                                .getUserresource.getSecurityAnonymousUsername() );
                                .authenticatenew UsernamePasswordToken(
                                                                                        resource.getSecurityAnonymousUsername(),
                                                                                        newPassword ) );
                            }
                            catch ( UserNotFoundException e )
                            {
                                getLogger().warn(
                                                  "Nexus refused to apply configuration, the supplied anonymous information is wrong.",
                                                  e );
                                String msg = "User '" + resource.getSecurityAnonymousUsername() + "' does not exist.";
                                throw new PlexusResourceException( Status.CLIENT_ERROR_BAD_REQUEST, msg,
                                                                   getNexusErrorResponse"securityAnonymousUsername",
                                                                                          msg ) );
                            }
                            catch ( AuthenticationException e )
                            {
                                // the supplied anon auth info is wrong
                                getLogger().warn(
                                                  "Nexus refused to apply configuration, the supplied anonymous information is wrong.",
                                                  e );
                                String msg =
                                    "The password of user '" + resource.getSecurityAnonymousUsername()
                                        + "' is incorrect.";
                                throw new PlexusResourceException( Status.CLIENT_ERROR_BAD_REQUEST, msg,
                                                                   getNexusErrorResponse"securityAnonymousPassword",
                                                                                          msg ) );
                            }
                        }
                        getNexusConfiguration().setAnonymousUsernameresource.getSecurityAnonymousUsername() );
                        getNexusConfiguration().setAnonymousPasswordnewPassword );
                    }
                    else if ( resource.isSecurityAnonymousAccessEnabled() )
                    {
                        // the supplied anon auth info is wrong
                        getLogger().warn(
                                          "Nexus refused to apply configuration, the supplied anonymous username/pwd information is empty." );
                        throw new PlexusResourceException(
                                                           Status.CLIENT_ERROR_BAD_REQUEST,
                                                           getNexusErrorResponse"securityAnonymousUsername",
                                                                                  "Cannot be empty when Anonynous access is enabled" ) );
                    }
                    if ( resource.getGlobalRestApiSettings() != null )
                    {
                        RestApiSettings restApiSettings = resource.getGlobalRestApiSettings();
                        getGlobalRestApiSettings().setForceBaseUrlrestApiSettings.isForceBaseUrl() );
                        if ( StringUtils.isEmptyresource.getGlobalRestApiSettings().getBaseUrl() ) )
                        {
                            getGlobalRestApiSettings().setBaseUrlnull );
                        }
                        else
                        {
                            getGlobalRestApiSettings().setBaseUrl(
                                                                   new Reference( restApiSettings.getBaseUrl() ).getTargetRef().toString() );
                        }
                        getGlobalRestApiSettings().setUITimeoutrestApiSettings.getUiTimeout() * 1000 );
                    }
                    else
                    {
                        getGlobalRestApiSettings().disable();
                    }
                    
                    if ( resource.getSystemNotificationSettings() != null )
                    {
                        .setEnabledresource.getSystemNotificationSettings().isEnabled() );
                        
                        
                        if ( target == null )
                        {
                            target = new NotificationTarget();
                            target.setTargetId. );
                        }
                        
                        target.getTargetRoles().clear();
                        target.getTargetRoles().addAllresource.getSystemNotificationSettings().getRoles() );
                        
                        target.getExternalTargets().clear();
                        
                        if ( StringUtils.isNotEmptyresource.getSystemNotificationSettings().getEmailAddresses() ) )
                        {
                            target.getExternalTargets().addAll( Arrays.asListresource.getSystemNotificationSettings().getEmailAddresses().split"," ) ) );
                        }
                        
                        target.getTargetUsers().clear();
                        
                        .updateNotificationTargettarget );
                    }
                    // NEXUS-3064: to "inform" global remote storage context (and hence, all affected proxy
                    // repositories) about the change, but only if config is saved okay
                    // TODO: this is wrong, the config framework should "tell" this changed, but we have some
                    // design flaw here: the globalRemoteStorageContext is NOT a component, while the settings are
                    boolean remoteConnectionSettingsIsDirty = getGlobalRemoteConnectionSettings().isDirty();
                    boolean remoteHttpProxySettingsIsDirty = getGlobalHttpProxySettings().isDirty();
                    getNexusConfiguration().saveConfiguration();
                    // NEXUS-3064: to "inform" global remote storage context (and hence, all affected proxy
                    // repositories) about the change, but only if config is saved okay
                    // TODO: this is wrong, the config framework should "tell" this changed, but we have some
                    // design flaw here: the globalRemoteStorageContext is NOT a component, while the settings are
                    if ( remoteConnectionSettingsIsDirty )
                    {
                        getNexusConfiguration().getGlobalRemoteStorageContext().setRemoteConnectionSettings(
                                                                                                             getGlobalRemoteConnectionSettings() );
                    }
                    if ( remoteHttpProxySettingsIsDirty )
                    {
                        getNexusConfiguration().getGlobalRemoteStorageContext().setRemoteProxySettings(
                                                                                                        getGlobalHttpProxySettings() );
                    }
                }
                catch ( IOException e )
                {
                    getLogger().warn"Got IO Exception during update of Nexus configuration."e );
                    throw new ResourceException( Status.SERVER_ERROR_INTERNAL );
                }
                catch ( InvalidConfigurationException e )
                {
                    // TODO: this should be removed from the Global config, as it is NO longer part of the nexus.xml
                    getLogger().debug"Configuraiton Exception while setting security values"e );
                    this.handleInvalidConfigurationExceptione );
                }
                catch ( ConfigurationException e )
                {
                    getLogger().warn"Nexus refused to apply configuration."e );
                    throw new PlexusResourceException( Status.CLIENT_ERROR_BAD_REQUEST, e.getMessage(),
                                                       getNexusErrorResponse"*"e.getMessage() ) );
                }
            }
        }
        // TODO: this method needs some serious cleaning up...
        response.setStatus( Status.SUCCESS_NO_CONTENT );
        return null;
    }

    
Externalized Nexus object to DTO's conversion, using default Nexus configuration.

Parameters:
resource
    protected void fillDefaultConfiguration( Request requestGlobalConfigurationResource resource )
    {
        resource.setSecurityEnabledisDefaultSecurityEnabled() );
        resource.setSecurityRealmsgetDefaultRealms() );
        RestApiSettings restApiSettings = convertreadDefaultRestApiSettings() );
        if ( restApiSettings != null )
        {
            restApiSettings.setBaseUrlgetContextRootrequest ).getTargetRef().toString() );
        }
        resource.setGlobalRestApiSettingsrestApiSettings );
        resource.setSmtpSettingsconvertreadDefaultSmtpConfiguration() ) );
    }

    
Externalized Nexus object to DTO's conversion, using current Nexus configuration.

Parameters:
resource
    protected void fillCurrentConfiguration( Request requestGlobalConfigurationResource resource )
    {
        resource.setSecurityRealmsgetNexusConfiguration().getRealms() );
        RestApiSettings restApiSettings = convertgetGlobalRestApiSettings() );
        if ( restApiSettings != null && StringUtils.isEmptyrestApiSettings.getBaseUrl() ) )
        {
            restApiSettings.setBaseUrlgetContextRootrequest ).getTargetRef().toString() );
        }
        resource.setGlobalRestApiSettingsrestApiSettings );
        resource.setSmtpSettingsconvertgetNexusEmailer() ) );
        resource.setErrorReportingSettingsconvertgetErrorReportingManager() ) );
        
    }
    protected String getSecurityConfigurationboolean enabledString authSourceType )
    {
        if ( !enabled )
        {
            return ;
        }
        else
        {
            if ( .equalsauthSourceType ) )
            {
                return ;
            }
            else
            {
                return ;
            }
        }
    }
New to GrepCode? Check out our FAQ X