The purpose of this example is to give you an overview of the various mechanisms the Connectors Framework brings to you for creating connections.
Developing a connection is quite straightforward. You just need to create the following elements:
This file should be available from the relative path "WEB-INF\storage<custom><connections>\education-mail.xml" within the final connectors package.
<?xml version="1.0" encoding="UTF-8"?>
<connections>
<connection-settings name="smtp.connection" category="education">
<class qualifiedName="com.axemble.education.connectors.smtp.SampleSMTPConnection" />
<configuration>
<class qualifiedName="com.axemble.education.connectors.smtp.SampleSMTPConnectionEditor" />
<section name="settings" label="LG_SETTINGS">
<fields>
<field name="fldMailBaseUrl" label="LG_MAIL_BASE_URL" ctrl="com.axemble.vdp.ui.core.document.fields.TextBoxField" />
<field name="fldSmtpServer" label="LG_SMTP_SERVER" ctrl="com.axemble.vdp.ui.core.document.fields.TextBoxField" mandatory="true" />
<field name="fldSmtpPort" label="LG_SMTP_PORT" ctrl="com.axemble.vdp.ui.core.document.fields.TextBoxField" mandatory="true" />
<field name="fldMailAdministrator" label="LG_SMTP_MAIL_ADMINISTRATOR" ctrl="com.axemble.vdp.ui.core.document.fields.TextBoxField" />
<field name="fldMailSender" label="LG_SMTP_MAIL_SENDER" ctrl="com.axemble.vdp.ui.core.document.fields.TextBoxField" />
</fields>
</section>
</configuration>
</connection-settings>
</connections>package com.axemble.education.connectors.smtp; import java.io.Serializable; import java.util.Properties; import javax.mail.Message; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import com.axemble.vdoc.sdk.connectors.BaseConnection; import com.axemble.vdoc.sdk.utils.Logger; public class SampleSMTPConnection extends BaseConnection
public class InnerConnection implements Serializable
{
private static final long serialVersionUID = 8564201166004587558L;
private Session session;
private Transport transport;
/**
* @param session
* @param transport
*/
public InnerConnection( Session session, Transport transport )
{
super();
this.session = session;
this.transport = transport;
}
public Session getSession()
{
return session;
}
public Transport getTransport()
{
return transport;
}
}@Override
public Object getConnection()
{
try
{
String serverName = (String)getConnectionDefinition().getValue( SampleSMTPConnectionEditor.FIELD_SMTP_SERVER );
String portNumber = (String)getConnectionDefinition().getValue( SampleSMTPConnectionEditor.FIELD_SMTP_PORT );
String smtpLogin = getPortalModule().getConfiguration().getStringProperty( "mail.smtp.login" );
String smtpPassword = getPortalModule().getConfiguration().getStringProperty( "mail.smtp.password" );
Properties prop = System.getProperties();
prop.put( "mail.smtp.host", serverName );
prop.put( "mail.smtp.port", portNumber );
Session session = Session.getDefaultInstance( prop, null );
Transport transport = session.getTransport( "smtp" );
transport.connect( serverName, smtpLogin, smtpPassword );
return new InnerConnection( session, transport );
}
catch( Exception e )
{
Logger.getLogger( this.getClass() ).error( getPortalModule().getStaticString( "LG_TEST_SEND_MAIL_ERROR" ) + e.toString() );
}
return null;
}@Override
public boolean validate() throws Exception
{
Transport transport = null;
try
{
// get a connection
InnerConnection innerConnection = (InnerConnection)getConnection();
if ( innerConnection == null )
return false;
String senderEmail = (String)getConnectionDefinition().getValue( SampleSMTPConnectionEditor.FIELD_MAIL_SENDER );
String administratorEmail = (String)getConnectionDefinition().getValue( SampleSMTPConnectionEditor.FIELD_MAIL_ADMINISTRATOR );
Session session = innerConnection.getSession();
MimeMessage mimeMessage = new MimeMessage( session );
mimeMessage.setSubject( "E-mail de test" );
mimeMessage.setText( "Ceci est un test d'envoi d'e-mail" );
mimeMessage.setFrom( new InternetAddress( senderEmail ) );
InternetAddress[] internetAddresses = new InternetAddress[1];
internetAddresses[0] = new InternetAddress( administratorEmail );
mimeMessage.setRecipients( Message.RecipientType.TO, internetAddresses );
transport = innerConnection.getTransport();
transport.sendMessage( mimeMessage, mimeMessage.getAllRecipients() );
return true;
}
finally
{
safeClose( transport );
}
}