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  javax.ws.rs.Consumes;
 import  javax.ws.rs.PUT;
 import  javax.ws.rs.Path;
 
 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;
The Smtp settings validation resource.

Author(s):
velo
 
 @Component( role = PlexusResource.class, hint = "SmtpSettingsValidation" )
 @Consumes( { "application/xml""application/json" } )
 {
     public static final String RESOURCE_URI = "/check_smtp_settings";
         
     private static final Pattern EMAIL_PATTERN = Pattern.compile".+@.+\\.[a-z]+" );
 
     @Requirement
     private SmtpSettingsValidator emailer;
 
     {
         this.setModifiabletrue );
     }
 
     @Override
     public Object getPayloadInstance()
     {
         return new SmtpSettingsResourceRequest();
     }
 
     @Override
     public String getResourceUri()
     {
         return ;
     }
 
     @Override
     {
         return new PathProtectionDescriptor"/global_settings/*""authcBasic,perms[nexus:settings]" );
     }

    
Validate smtp settings, send a test email using the configuration.
 
     @Override
     @PUT
     @ResourceMethodSignature( input = SmtpSettingsResourceRequest.class )
     public Object put( Context context, Request request, Response responseObject payload )
         throws ResourceException
     {
         SmtpSettingsResourceRequest configRequest = (SmtpSettingsResourceRequestpayload;
 
         SmtpSettingsResource settings = configRequest.getData();
 
         String email = settings.getTestEmail();
 
         if ( !.matcheremail ).matches() )
        {
            throw new ResourceException( Status.CLIENT_ERROR_BAD_REQUEST, "Invalid e-mail address: " + email );
        }
        CSmtpConfiguration config = new CSmtpConfiguration();
        config.setHostnamesettings.getHost() );
        String oldPassword = getNexusEmailer().getSMTPPassword();
        config.setPasswordthis.getActualPasswordsettings.getPassword(), oldPassword ) );
        config.setPortsettings.getPort() );
        config.setSslEnabledsettings.isSslEnabled() );
        config.setTlsEnabledsettings.isTlsEnabled() );
        config.setUsernamesettings.getUsername() );
        config.setSystemEmailAddresssettings.getSystemEmailAddress().trim() );
        boolean status;
        try
        {
            status = .sendSmtpConfigurationTestconfigemail );
        }
        catch ( EmailerException e )
        {
            throw new ResourceException( Status.CLIENT_ERROR_BAD_REQUEST, "Failed to send validation e-mail: "
                + e.getMessage(), e );
        }
        if ( status )
        {
            response.setStatus( Status.SUCCESS_OK, "Email was sent. Check your inbox!" );
        }
        else
        {
            response.setStatus( Status.SUCCESS_OK, "Unable to determine if e-mail was sent or not.  Check your inbox!" );
        }
        return null;
    }
New to GrepCode? Check out our FAQ X