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

org.fax4j.spi.email.AbstractMailFaxClientSpi Maven / Gradle / Ivy

package org.fax4j.spi.email;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.Transport;
import org.fax4j.FaxException;
import org.fax4j.FaxJob;
import org.fax4j.common.Logger;
import org.fax4j.spi.AbstractFax4JClientSpi;
import org.fax4j.util.Connection;

/**
 * This class implements the fax client service provider interface.
* This parial implementation will invoke the requests by sending emails to a mail server that supports * conversion between email messages and fax messages.
* The mail SPI supports persistent connection to enable to reuse the same connection for all fax * operation invocations or to create a new connection for each fax operation invocation.
* By default the SPI will create a new connection for each operation invocation however the * org.fax4j.spi.mail.persistent.connection set to true will enable to reuse the connection.
* To set the user/password values of the mail connection the following 2 properties must be defined: * org.fax4j.spi.mail.user.name and org.fax4j.spi.mail.password
* All properties defined in the fax4j configuration will be passed to the mail connection therefore it is * possible to define mail specific properties (see java mail for more info) in the fax4j properties.
* Implementing SPI class will have to implement the createXXXFaxJobMessage methods.
* These methods will return the message to be sent for that fax job operation. In case the method * returns null, this class will throw an UnsupportedOperationException exception. *
* The configuration of the fax4j framework is made up of 3 layers.
* The configuration is based on simple properties.
* Each layer overrides the lower layers by adding/changing the property values.
* The first layer is the internal fax4j.properties file located in the fax4j jar.
* This layer contains the preconfigured values for the fax4j framework and can be changed * by updating these properties in the higher layers.
* The second layer is the external fax4j.properties file that is located on the classpath.
* This file is optional and provides the ability to override the internal configuration for the * entire fax4j framework.
* The top most layer is the optional java.util.Properties object provided by the external classes * when creating a new fax client.
* These properties enable to override the configuration of the lower 2 layers.
*
* SPI Status (Draft, Beta, Stable): Stable
*
* Below table describes the configuration values relevant for this class.
* Configuration: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
NameDescriptionPreconfigured ValueDefault ValueMandatory
org.fax4j.spi.mail.persistent.connectionTrue to reuse the same mail connection for all fax activites, false to create a * new mail connection for each fax activity.falsefalsefalse
org.fax4j.spi.mail.connection.factory.class.nameThe connection factory class nameorg.fax4j.spi.email.MailConnectionFactoryImplorg.fax4j.spi.email.MailConnectionFactoryImplfalse
org.fax4j.spi.mail.user.nameThe mail account user name.nonenonefalse
org.fax4j.spi.mail.passwordThe mail account password.nonenonefalse
javax mail propertiesAny of the javax mail properties can be defined in the fax4j properties.
* These properties will be passed to the java mail framework.
mail.transport.protocol=smtp
* mail.smtp.port=25
nonefalse
*
* Limitations:
*
    *
  • This SPI is based on the java mail infrastructure, therefore this SPI cannot * connect to mail servers through a proxy server. *
  • This SPI provides only partial implementation (this is an abstract class). *
*
* Dependencies:
*
    *
  • Required jar files: mail-1.4.jar, activation-1.1.jar *
*
* * @author Sagie Gur-Ari * @version 1.07 * @since 0.1 */ public abstract class AbstractMailFaxClientSpi extends AbstractFax4JClientSpi { /**The mail configuration properties*/ private Properties mailConfiguration; /** * The use persistent connection flag which defines if the SPI will use a new * connection for each request or will reuse the same connection */ private boolean usePersistentConnection; /**The mail connection factory*/ private MailConnectionFactory connectionFactory; /**The mail connection*/ private Connection connection; /** * This class holds the SPI configuration constants. * * @author Sagie Gur-Ari * @version 1.01 * @since 0.1 */ public static class FaxClientSpiConfigurationConstants { /** * This is the class constructor. */ protected FaxClientSpiConfigurationConstants() { super(); } /** * The use persistent connection property key which defines if the SPI will use a new * connection for each request or will reuse the same connection */ public static final String USE_PERSISTENT_CONNECTION_PROPERTY_KEY="org.fax4j.spi.mail.persistent.connection"; /**The connection factory class name*/ public static final String CONNECTION_FACTORY_CLASS_NAME_PROPERTY_KEY="org.fax4j.spi.mail.connection.factory.class.name"; /**The user name used to connect to the mail server*/ public static final String USER_NAME_PROPERTY_KEY="org.fax4j.spi.mail.user.name"; /**The password used to connect to the mail server*/ public static final String PASSWORD_PROPERTY_KEY="org.fax4j.spi.mail.password"; } /** * This is the default constructor. */ public AbstractMailFaxClientSpi() { super(); } /** * This function initializes the fax client SPI. */ @Override protected void initializeImpl() { //get logger Logger logger=this.getLogger(); //create mail configuration properties this.mailConfiguration=new Properties(); Map configuration=this.getConfiguration(); Iterator> iterator=configuration.entrySet().iterator(); Entry entry=null; String key=null; String value=null; while(iterator.hasNext()) { //get next entry entry=iterator.next(); //get next key key=entry.getKey(); if((key!=null)&&(key.indexOf("org.fax4j.")==-1)) //filter out fax4j properties { //get next value value=entry.getValue(); //put in properties this.mailConfiguration.setProperty(key,value); } } //get use persistent connection value this.usePersistentConnection=Boolean.parseBoolean(this.getConfigurationValue(FaxClientSpiConfigurationConstants.USE_PERSISTENT_CONNECTION_PROPERTY_KEY)); logger.logDebug(new Object[]{"Using persistent connection: ",String.valueOf(this.usePersistentConnection)},null); //setup connection factory String className=this.getConfigurationValue(FaxClientSpiConfigurationConstants.CONNECTION_FACTORY_CLASS_NAME_PROPERTY_KEY); this.connectionFactory=this.createMailConnectionFactoryImpl(className); if(this.connectionFactory==null) { throw new FaxException("Mail connection factory is not available."); } } /** * Creates and returns the mail connection factory. * * @param className * The connection factory class name * @return The mail connection factory */ protected final MailConnectionFactory createMailConnectionFactoryImpl(String className) { String factoryClassName=className; if(factoryClassName==null) { factoryClassName=MailConnectionFactoryImpl.class.getName(); } MailConnectionFactory factory=null; try { //create new instance Class classDefinition=Class.forName(factoryClassName); factory=(MailConnectionFactory)classDefinition.newInstance(); //initialize factory.initialize(this); } catch(Exception exception) { throw new FaxException("Unable to create a new mail connection factory for class name: "+factoryClassName,exception); } return factory; } /** * Returns the configuration. * * @return The configuration */ public final Properties getMailConfiguration() { return this.mailConfiguration; } /** * Releases the connection if open. * * @throws Throwable * Any throwable */ @Override protected void finalize() throws Throwable { //get reference Connection mailConnection=this.connection; //release connection this.closeMailConnection(mailConnection); super.finalize(); } /** * Creates and returns the mail connection to be used to send the fax * via mail. * * @return The mail connection */ protected Connection createMailConnection() { //create new connection Connection mailConnection=this.connectionFactory.createConnection(); //log debug Logger logger=this.getLogger(); logger.logInfo(new Object[]{"Created mail connection."},null); return mailConnection; } /** * This function closes the provided mail connection. * * @param mailConnection * The mail connection to close * @throws IOException * Never thrown */ protected void closeMailConnection(Connection mailConnection) throws IOException { if(mailConnection!=null) { //get logger Logger logger=this.getLogger(); //release connection logger.logInfo(new Object[]{"Closing mail connection."},null); mailConnection.close(); } } /** * Returns the mail connection to be used to send the fax * via mail. * * @return The mail connection */ protected Connection getMailConnection() { Connection mailConnection=null; if(this.usePersistentConnection) { synchronized(this) { if(this.connection==null) { //create new connection this.connection=this.createMailConnection(); } } //get connection mailConnection=this.connection; } else { //create new connection mailConnection=this.createMailConnection(); } return mailConnection; } /** * This function will send the mail message. * * @param faxJob * The fax job object containing the needed information * @param mailConnection * The mail connection (will be released if not persistent) * @param message * The message to send */ protected void sendMail(FaxJob faxJob,Connection mailConnection,Message message) { if(message==null) { this.throwUnsupportedException(); } else { //get holder MailResourcesHolder mailResourcesHolder=mailConnection.getResource(); //get transport Transport transport=mailResourcesHolder.getTransport(); try { //send message message.saveChanges(); if(transport==null) { Transport.send(message,message.getAllRecipients()); } else { transport.sendMessage(message,message.getAllRecipients()); } } catch(Throwable throwable) { throw new FaxException("Unable to send message.",throwable); } finally { if(!this.usePersistentConnection) { try { //close connection this.closeMailConnection(mailConnection); } catch(Exception exception) { //log error Logger logger=this.getLogger(); logger.logInfo(new Object[]{"Error while releasing mail connection."},exception); } } } } } /** * This function will submit a new fax job.
* The fax job ID may be populated by this method in the provided * fax job object. * * @param faxJob * The fax job object containing the needed information */ @Override protected void submitFaxJobImpl(FaxJob faxJob) { //get connection Connection mailConnection=this.getMailConnection(); //get holder MailResourcesHolder mailResourcesHolder=mailConnection.getResource(); //create message Message message=this.createSubmitFaxJobMessage(faxJob,mailResourcesHolder); //send message this.sendMail(faxJob,mailConnection,message); } /** * This function will suspend an existing fax job. * * @param faxJob * The fax job object containing the needed information */ @Override protected void suspendFaxJobImpl(FaxJob faxJob) { //get connection Connection mailConnection=this.getMailConnection(); //get holder MailResourcesHolder mailResourcesHolder=mailConnection.getResource(); //create message Message message=this.createSuspendFaxJobMessage(faxJob,mailResourcesHolder); //send message this.sendMail(faxJob,mailConnection,message); } /** * This function will resume an existing fax job. * * @param faxJob * The fax job object containing the needed information */ @Override protected void resumeFaxJobImpl(FaxJob faxJob) { //get connection Connection mailConnection=this.getMailConnection(); //get holder MailResourcesHolder mailResourcesHolder=mailConnection.getResource(); //create message Message message=this.createResumeFaxJobMessage(faxJob,mailResourcesHolder); //send message this.sendMail(faxJob,mailConnection,message); } /** * This function will cancel an existing fax job. * * @param faxJob * The fax job object containing the needed information */ @Override protected void cancelFaxJobImpl(FaxJob faxJob) { //get connection Connection mailConnection=this.getMailConnection(); //get holder MailResourcesHolder mailResourcesHolder=mailConnection.getResource(); //create message Message message=this.createCancelFaxJobMessage(faxJob,mailResourcesHolder); //send message this.sendMail(faxJob,mailConnection,message); } /** * This function will create the message used to invoke the fax * job action.
* If this method returns null, the SPI will throw an UnsupportedOperationException. * * @param faxJob * The fax job object containing the needed information * @param mailResourcesHolder * The mail resources holder * @return The message to send (if null, the SPI will throw an UnsupportedOperationException) */ protected abstract Message createSubmitFaxJobMessage(FaxJob faxJob,MailResourcesHolder mailResourcesHolder); /** * This function will create the message used to invoke the fax * job action.
* If this method returns null, the SPI will throw an UnsupportedOperationException. * * @param faxJob * The fax job object containing the needed information * @param mailResourcesHolder * The mail resources holder * @return The message to send (if null, the SPI will throw an UnsupportedOperationException) */ protected abstract Message createSuspendFaxJobMessage(FaxJob faxJob,MailResourcesHolder mailResourcesHolder); /** * This function will create the message used to invoke the fax * job action.
* If this method returns null, the SPI will throw an UnsupportedOperationException. * * @param faxJob * The fax job object containing the needed information * @param mailResourcesHolder * The mail resources holder * @return The message to send (if null, the SPI will throw an UnsupportedOperationException) */ protected abstract Message createResumeFaxJobMessage(FaxJob faxJob,MailResourcesHolder mailResourcesHolder); /** * This function will create the message used to invoke the fax * job action.
* If this method returns null, the SPI will throw an UnsupportedOperationException. * * @param faxJob * The fax job object containing the needed information * @param mailResourcesHolder * The mail resources holder * @return The message to send (if null, the SPI will throw an UnsupportedOperationException) */ protected abstract Message createCancelFaxJobMessage(FaxJob faxJob,MailResourcesHolder mailResourcesHolder); }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy